Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-05 09:18:25

0001 /*
0002  *  Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 #pragma once
0010 
0011 #include "../H5Easy.hpp"
0012 #include "H5Easy_misc.hpp"
0013 #include "H5Easy_scalar.hpp"
0014 
0015 #ifdef H5_USE_EIGEN
0016 
0017 #include "../eigen.hpp"
0018 
0019 namespace H5Easy {
0020 
0021 namespace detail {
0022 
0023 template <typename T>
0024 struct io_impl<T, typename std::enable_if<std::is_base_of<Eigen::DenseBase<T>, T>::value>::type> {
0025     using EigenIndex = Eigen::DenseIndex;
0026 
0027     // When creating a dataset for an Eigen object, the shape of the dataset is
0028     // 1D for vectors. (legacy reasons)
0029     inline static std::vector<size_t> file_shape(const T& data) {
0030         if (std::decay<T>::type::RowsAtCompileTime == 1) {
0031             return {static_cast<size_t>(data.cols())};
0032         }
0033         if (std::decay<T>::type::ColsAtCompileTime == 1) {
0034             return {static_cast<size_t>(data.rows())};
0035         }
0036         return inspector<T>::getDimensions(data);
0037     }
0038 
0039     // The shape of an Eigen object as used in HighFive core.
0040     inline static std::vector<size_t> mem_shape(const T& data) {
0041         return inspector<T>::getDimensions(data);
0042     }
0043 
0044     // The shape of an Eigen object as used in HighFive core.
0045     template <class D>
0046     inline static std::vector<size_t> mem_shape(const File& file,
0047                                                 const std::string& path,
0048                                                 const D& dataset) {
0049         std::vector<size_t> dims = dataset.getDimensions();
0050 
0051         if (dims.size() == 1 && T::RowsAtCompileTime == 1) {
0052             return std::vector<size_t>{1, dims[0]};
0053         }
0054         if (dims.size() == 1 && T::ColsAtCompileTime == 1) {
0055             return std::vector<size_t>{dims[0], 1};
0056         }
0057         if (dims.size() == 2) {
0058             return dims;
0059         }
0060 
0061         throw detail::error(file, path, "H5Easy::load: Inconsistent rank");
0062     }
0063 
0064     inline static DataSet dump(File& file,
0065                                const std::string& path,
0066                                const T& data,
0067                                const DumpOptions& options) {
0068         using value_type = typename std::decay<T>::type::Scalar;
0069 
0070         std::vector<size_t> file_dims = file_shape(data);
0071         std::vector<size_t> mem_dims = mem_shape(data);
0072         DataSet dataset = initDataset<value_type>(file, path, file_dims, options);
0073         dataset.reshapeMemSpace(mem_dims).write(data);
0074         if (options.flush()) {
0075             file.flush();
0076         }
0077         return dataset;
0078     }
0079 
0080     inline static T load(const File& file, const std::string& path) {
0081         DataSet dataset = file.getDataSet(path);
0082         std::vector<size_t> dims = mem_shape(file, path, dataset);
0083         return dataset.reshapeMemSpace(dims).template read<T>();
0084     }
0085 
0086     inline static Attribute dumpAttribute(File& file,
0087                                           const std::string& path,
0088                                           const std::string& key,
0089                                           const T& data,
0090                                           const DumpOptions& options) {
0091         using value_type = typename std::decay<T>::type::Scalar;
0092 
0093         std::vector<size_t> file_dims = file_shape(data);
0094         std::vector<size_t> mem_dims = mem_shape(data);
0095         Attribute attribute = initAttribute<value_type>(file, path, key, file_dims, options);
0096         attribute.reshapeMemSpace(mem_dims).write(data);
0097         if (options.flush()) {
0098             file.flush();
0099         }
0100         return attribute;
0101     }
0102 
0103     inline static T loadAttribute(const File& file,
0104                                   const std::string& path,
0105                                   const std::string& key) {
0106         DataSet dataset = file.getDataSet(path);
0107         Attribute attribute = dataset.getAttribute(key);
0108         DataSpace dataspace = attribute.getSpace();
0109         std::vector<size_t> dims = mem_shape(file, path, dataspace);
0110         return attribute.reshapeMemSpace(dims).template read<T>();
0111     }
0112 };
0113 
0114 }  // namespace detail
0115 }  // namespace H5Easy
0116 
0117 #endif  // H5_USE_EIGEN