SpECTRE Documentation Coverage Report
Current view: top level - DataStructures - CompressedMatrix.hpp Hit Total Coverage
Commit: fb144cfca92c3773ae2a942619781e0e583aa113 Lines: 1 3 33.3 %
Date: 2023-06-02 00:56:01
Legend: Lines: hit not hit

          Line data    Source code
       1           1 : // Distributed under the MIT License.
       2             : // See LICENSE.txt for details.
       3             : 
       4             : /// \file
       5             : /// `blaze::CompressedMatrix` is a general-purpose sparse matrix type. This file
       6             : /// implements interoperability of `blaze::CompressedMatrix` with our data
       7             : /// structures.
       8             : 
       9             : #pragma once
      10             : 
      11             : #include <blaze/math/CompressedMatrix.h>
      12             : #include <cstddef>
      13             : #include <pup.h>
      14             : #include <vector>
      15             : 
      16             : #include "Options/Options.hpp"
      17             : #include "Utilities/Gsl.hpp"
      18             : 
      19             : namespace PUP {
      20             : /// @{
      21             : /// Serialization of blaze::CompressedMatrix
      22             : template <typename Type, bool SO, typename Tag>
      23             : void pup(er& p, blaze::CompressedMatrix<Type, SO, Tag>& t) {
      24             :   size_t rows = t.rows();
      25             :   size_t columns = t.columns();
      26             :   p | rows;
      27             :   p | columns;
      28             :   const size_t first_dimension = (SO == blaze::rowMajor) ? rows : columns;
      29             :   size_t num_non_zeros = t.nonZeros();
      30             :   p | num_non_zeros;
      31             :   // blaze::CompressedMatrix has no `.data()` access, so we use the low-level
      32             :   // `append` mechanism for serialization instead of `PUParray`. Maybe there's
      33             :   // an even faster way using PUPbytes.
      34             :   size_t index;
      35             :   if (p.isUnpacking()) {
      36             :     t.resize(rows, columns);
      37             :     t.reserve(num_non_zeros);
      38             :     Type value;
      39             :     for (size_t i = 0; i < first_dimension; ++i) {
      40             :       p | num_non_zeros;
      41             :       for (size_t j = 0; j < num_non_zeros; ++j) {
      42             :         p | index;
      43             :         p | value;
      44             :         if constexpr (SO == blaze::rowMajor) {
      45             :           t.append(i, index, value);
      46             :         } else {
      47             :           t.append(index, i, value);
      48             :         }
      49             :       }
      50             :       t.finalize(i);
      51             :     }
      52             :   } else {
      53             :     for (size_t i = 0; i < first_dimension; ++i) {
      54             :       num_non_zeros = t.nonZeros(i);
      55             :       p | num_non_zeros;
      56             :       for (auto it = t.begin(i); it != t.end(i); ++it) {
      57             :         index = it->index();
      58             :         p | index;
      59             :         p | it->value();
      60             :       }
      61             :     }
      62             :   }
      63             : }
      64             : template <typename Type, bool SO, typename Tag>
      65             : void operator|(er& p, blaze::CompressedMatrix<Type, SO, Tag>& t) {
      66             :   pup(p, t);
      67             : }
      68             : /// @}
      69             : }  // namespace PUP
      70             : 
      71             : namespace CompressedMatrix_detail {
      72             : template <typename Type>
      73             : std::vector<std::vector<Type>> parse_to_vectors(const Options::Option& options);
      74             : }  // namespace CompressedMatrix_detail
      75             : 
      76             : template <typename Type, bool SO, typename Tag>
      77           0 : struct Options::create_from_yaml<blaze::CompressedMatrix<Type, SO, Tag>> {
      78             :   template <typename Metavariables>
      79           0 :   static blaze::CompressedMatrix<Type, SO, Tag> create(
      80             :       const Options::Option& options) {
      81             :     const auto data = CompressedMatrix_detail::parse_to_vectors<Type>(options);
      82             :     const size_t num_rows = data.size();
      83             :     size_t num_cols = 0;
      84             :     if (num_rows > 0) {
      85             :       num_cols = data[0].size();
      86             :     }
      87             :     blaze::CompressedMatrix<Type, SO, Tag> result(num_rows, num_cols);
      88             :     for (size_t i = 0; i < num_rows; i++) {
      89             :       const auto& row = gsl::at(data, i);
      90             :       if (row.size() != num_cols) {
      91             :         PARSE_ERROR(options.context(),
      92             :                     "All matrix rows must have the same size.");
      93             :       }
      94             :       for (size_t j = 0; j < num_cols; j++) {
      95             :         if (gsl::at(row, j) != 0.) {
      96             :           result(i, j) = gsl::at(row, j);
      97             :         }
      98             :       }
      99             :     }
     100             :     return result;
     101             :   }
     102             : };

Generated by: LCOV version 1.14