LCOV - code coverage report
Current view: top level - pairinteraction - serialization_eigen.hpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 19 23 82.6 %
Date: 2024-04-29 00:41:50 Functions: 6 20 30.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 Sebastian Weber, Henri Menke. All rights reserved.
       3             :  *
       4             :  * This file is part of the pairinteraction library.
       5             :  *
       6             :  * The pairinteraction library is free software: you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as published by
       8             :  * the Free Software Foundation, either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * The pairinteraction library is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public License
      17             :  * along with the pairinteraction library. If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #ifndef SERIALIZATION_EIGEN_H
      21             : #define SERIALIZATION_EIGEN_H
      22             : 
      23             : #include <cereal/archives/binary.hpp>
      24             : #include <cereal/archives/json.hpp>
      25             : #include <cereal/cereal.hpp>
      26             : 
      27             : #include "EigenCompat.hpp"
      28             : #include <Eigen/Core>
      29             : #include <Eigen/SparseCore>
      30             : 
      31             : template <typename T, typename S>
      32             : struct ArrayData {
      33             :     T *data;
      34             :     S size;
      35             : 
      36             :     template <typename Archive>
      37         312 :     void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &ar) {
      38             :         using namespace cereal;
      39         312 :         ar(make_size_tag(static_cast<size_type>(size)));
      40             :         if constexpr (cereal::traits::is_text_archive<Archive>::value) { // NOLINT
      41       16668 :             for (S i = 0; i < size; ++i) {
      42       16656 :                 ar(data[i]);
      43             :             }
      44             :         } else { // NOLINT
      45         300 :             ar(binary_data(data, size * sizeof(*data)));
      46             :         }
      47         312 :     }
      48             : };
      49             : 
      50             : template <typename T, typename S>
      51             : ArrayData(T, S) -> ArrayData<std::remove_pointer_t<T>, std::decay_t<S>>;
      52             : 
      53             : namespace cereal {
      54             : 
      55             : template <typename Archive, typename Scalar, int Options, typename StorageIndex>
      56         104 : void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &ar,
      57             :                                     Eigen::SparseMatrix<Scalar, Options, StorageIndex> &m) {
      58             :     StorageIndex innerSize;
      59             :     StorageIndex outerSize;
      60             :     StorageIndex valuesSize;
      61             : 
      62             :     if constexpr (Archive::is_saving::value) { // NOLINT
      63           0 :         m.makeCompressed();
      64           0 :         innerSize = m.innerSize();
      65           0 :         outerSize = m.outerSize();
      66           0 :         valuesSize = m.nonZeros();
      67             :     }
      68             : 
      69         104 :     ar(CEREAL_NVP(innerSize));
      70         104 :     ar(CEREAL_NVP(outerSize));
      71         104 :     ar(CEREAL_NVP(valuesSize));
      72             : 
      73             :     if constexpr (Archive::is_loading::value) { // NOLINT
      74         104 :         StorageIndex const rows = (m.IsRowMajor) ? outerSize : innerSize;
      75         104 :         StorageIndex const cols = (m.IsRowMajor) ? innerSize : outerSize;
      76         104 :         m.resize(rows, cols);
      77         104 :         m.resizeNonZeros(valuesSize);
      78             :     }
      79             : 
      80         104 :     ar(make_nvp("innerIndexPtr", ArrayData{m.innerIndexPtr(), valuesSize}));
      81         104 :     ar(make_nvp("outerIndexPtr", ArrayData{m.outerIndexPtr(), outerSize + 1}));
      82         104 :     ar(make_nvp("valuePtr", ArrayData{m.valuePtr(), valuesSize}));
      83             : 
      84             :     if constexpr (Archive::is_loading::value) { // NOLINT
      85         104 :         m.finalize();
      86             :     }
      87         104 : }
      88             : 
      89             : template <class Archive, typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
      90             : void CEREAL_SERIALIZE_FUNCTION_NAME(
      91             :     Archive &ar, Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> &m) {
      92             :     Eigen::Index rows;
      93             :     Eigen::Index cols;
      94             : 
      95             :     if constexpr (Archive::is_saving::value) { // NOLINT
      96             :         rows = m.rows();
      97             :         cols = m.cols();
      98             :     }
      99             : 
     100             :     ar(CEREAL_NVP(rows));
     101             :     ar(CEREAL_NVP(cols));
     102             : 
     103             :     if constexpr (Archive::is_loading::value) { // NOLINT
     104             :         m.resize(rows, cols);
     105             :     }
     106             : 
     107             :     ar(make_nvp("data", ArrayData{m.data(), rows * cols}));
     108             : }
     109             : 
     110             : } // namespace cereal
     111             : 
     112             : #endif // SERIALIZATION_EIGEN_H

Generated by: LCOV version 1.14