DenseMatrix.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 /// \file
5 /// Defines class DenseMatrix.
6 
7 #pragma once
8 
9 #include <pup.h> // IWYU pragma: keep
10 
11 #include "Options/Options.hpp"
12 #include "Utilities/Blaze.hpp"
13 #include "Utilities/Gsl.hpp"
14 #include "Utilities/TypeTraits.hpp"
15 
16 #include <blaze/math/DynamicMatrix.h>
17 #include <blaze/system/Version.h>
18 
19 /*!
20  * \ingroup DataStructuresGroup
21  * \brief A dynamically sized matrix of arbitrary type.
22  *
23  * \note This is a thin wrapper around `blaze::DynamicMatrix`. Please refer to
24  * the [Blaze
25  * documentation](https://bitbucket.org/blaze-lib/blaze/wiki/Matrices) for
26  * information on how to use it.
27  */
28 template <typename T, bool SO = blaze::defaultStorageOrder>
29 class DenseMatrix : public blaze::DynamicMatrix<T, SO> {
30  public:
31  // Inherit constructors
32  using blaze::DynamicMatrix<T, SO>::DynamicMatrix;
33 
34  /// Charm++ serialization
35  // clang-tidy: runtime-references
36  void pup(PUP::er& p) noexcept { // NOLINT
37  auto rows = blaze::DynamicMatrix<T, SO>::rows();
38  auto columns = blaze::DynamicMatrix<T, SO>::columns();
39  p | rows;
40  p | columns;
41  if (p.isUnpacking()) {
42  blaze::DynamicMatrix<T, SO>::resize(rows, columns);
43  }
44  if (cpp17::is_fundamental_v<T>) {
45  PUParray(p, blaze::DynamicMatrix<T, SO>::data(), rows * columns);
46  } else {
47  for (size_t i = 0; i < rows * columns; i++) {
48  p | blaze::DynamicMatrix<T, SO>::data()[i];
49  }
50  }
51  }
52 };
53 
54 template <typename T, bool SO>
55 struct create_from_yaml<DenseMatrix<T, SO>> {
56  static DenseMatrix<T, SO> create(const Option& options) {
57  const auto data = options.parse_as<std::vector<std::vector<T>>>();
58  const size_t num_rows = data.size();
59  size_t num_cols = 0;
60  if (num_rows > 0) {
61  num_cols = data[0].size();
62  }
63  DenseMatrix<T, SO> result(num_rows, num_cols);
64  // We don't use an iterator over the matrix here to make the code
65  // independent of the matrix storage order (see the [Blaze
66  // documentation](https://bitbucket.org/
67  // blaze-lib/blaze/wiki/Matrix%20Operations#!iterators)).
68  for (size_t i = 0; i < num_rows; i++) {
69  const auto& row = gsl::at(data, i);
70  if (row.size() != num_cols) {
71  PARSE_ERROR(options.context(),
72  "All matrix columns must have the same size.");
73  }
74  for (size_t j = 0; j < num_cols; j++) {
75  result(i, j) = gsl::at(row, j);
76  }
77  }
78  return result;
79  }
80 };
The type that options are passed around as. Contains YAML node data and an OptionContext.
Definition: Options.hpp:103
T parse_as() const
Convert to an object of type T.
Definition: ParseOptions.hpp:69
Includes Blaze library with specific configs.
Used by the parser to create an object. The default action is to parse options using T::options...
Definition: Options.hpp:143
Defines classes and functions for making classes creatable from input files.
constexpr auto create(Args &&... args)
Create a new DataBox.
Definition: DataBox.hpp:1259
#define PARSE_ERROR(context, m)
Like ERROR("\n" << (context) << m), but instead throws an exception that will be caught in a higher l...
Definition: Options.hpp:66
A dynamically sized matrix of arbitrary type.
Definition: DenseMatrix.hpp:29
void pup(PUP::er &p) noexcept
Charm++ serialization.
Definition: DenseMatrix.hpp:36
Defines functions and classes from the GSL.
Defines type traits, some of which are future STL type_traits header.
constexpr T & at(std::array< T, N > &arr, Size index)
Retrieve a entry from a container, with checks in Debug mode that the index being retrieved is valid...
Definition: Gsl.hpp:124