openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
SF_abstract_matrix.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #ifndef _SF_ABSTRACT_MATRIX_H
5 #define _SF_ABSTRACT_MATRIX_H
6 
7 #include <mpi.h>
8 
9 #include "SF_abstract_vector.h"
10 #include "SF_container.h"
11 #include "SF_globals.h"
12 #include "SF_parallel_layout.h"
13 
14 namespace SF {
15 
16 
28 template <class T, class S>
30 {
31  protected:
33  abstract_matrix() : mesh(NULL),
34  NRows(0),
35  NCols(0),
36  row_dpn(0),
37  col_dpn(0),
38  lsize(0),
39  start(0),
40  stop(0)
41  {
42  }
43 
46 
47  T NRows;
48  T NCols;
49  int row_dpn;
50  int col_dpn;
51 
52  T lsize;
53  T start;
54  T stop;
55 
56  public:
57  mutable std::set<T> boundary_idxs;
58 
60  virtual ~abstract_matrix() = default;
61 
72  virtual inline void init(T iNRows, T iNCols, T ilrows, T ilcols,
73  T loc_offset, T mxent)
74  {
75  this->NRows = iNRows;
76  this->NCols = iNCols;
77  this->lsize = ilrows;
78  this->start = loc_offset;
79  this->stop = this->start + this->lsize;
80  }
81 
91  inline void init(const meshdata<mesh_int_t, mesh_real_t> & imesh,
92  const T irow_dpn,
93  const T icol_dpn,
94  const T max_edges)
95  {
96  mesh = &imesh;
97  this->row_dpn = irow_dpn;
98  this->col_dpn = icol_dpn;
99 
100  int rank;
101  MPI_Comm_rank(mesh->comm, &rank);
102 
103  T M = mesh->pl.num_global_idx() * this->row_dpn;
104  T N = mesh->pl.num_global_idx() * this->col_dpn;
105  T m = mesh->pl.num_algebraic_idx() * this->row_dpn;
106  T n = mesh->pl.num_algebraic_idx() * this->col_dpn;
107 
108  T nmax = max_edges;
109  if(max_edges < 0) nmax = max_nodal_edgecount(*mesh);
110 
111  T loc_offset = mesh->pl.algebraic_layout()[rank]*this->row_dpn;
112 
113  init(M, N, m, n, loc_offset, nmax*this->col_dpn);
114  zero();
115  }
116 
123  {
124  return this->mesh;
125  }
126 
132  inline int dpn_row() const { return this->row_dpn; }
133 
139  inline int dpn_col() const { return this->col_dpn; }
140 
144  virtual void zero() = 0;
145 
154  {
155  return false;
156  }
157 
162  {
163  }
164 
171  virtual void mult(const abstract_vector<T,S> & x, abstract_vector<T,S> & b) const = 0;
172 
180  virtual void mult_LR(const abstract_vector<T, S>& L, const abstract_vector<T, S>& R) = 0;
181 
187  virtual void diag_add(const abstract_vector<T, S>& diag) = 0;
188 
194  virtual void get_diagonal(abstract_vector<T, S>& vec) const = 0;
195 
199  virtual void finish_assembly() = 0;
200 
206  virtual void scale(S s) = 0;
207 
215  virtual void add_scaled_matrix(const abstract_matrix<T, S>& A, const S s, const bool same_nnz) = 0;
216 
222  virtual void duplicate(const abstract_matrix<T,S>& M) = 0;
223 
234  virtual void set_values(const vector<T>& row_idx, const vector<T>& col_idx, const vector<S>& vals, bool add) = 0;
235 
246  virtual void set_values(const vector<T> & row_idx, const vector<T> & col_idx,
247  const S* vals, bool add) = 0;
248 
257  virtual void set_value(T row_idx, T col_idx, S val, bool add) = 0;
258 
269  inline void get_values(const vector<T> & row_idx, const vector<T> & col_idx,
270  vector<S> & values) const {
271  assert(row_idx.size() == col_idx.size() == values.size());
272 
273  for (int i = 0; i < row_idx.size(); i++)
274  values[i] = get_value(row_idx[i], col_idx[i]);
275  }
276 
285  virtual S get_value(T row_idx, T col_idx) const = 0;
286 
292  virtual void write(const char* filename) const = 0;
293 
294 
305  inline bool equals(const abstract_matrix<T,S> & rhs) const
306  {
307  if (NRows != rhs.NRows or NCols != rhs.NCols)
308  return false;
309 
310  bool equal = true;
311  for (size_t row = 0; row < NRows; row++)
312  for (size_t col = 0; col < NCols; col++)
313  if (fabs(get_value(row, col) - rhs.get_value(row, col)) > 0.0001)
314  equal = false;
315 
316  return equal;
317  }
318 
324  inline std::string to_string() const
325  {
326  std::ostringstream stream;
327  stream << "matrix (" << this->NRows << " x " << this->NCols << ") [\n";
328 
329  for (auto row = 0; row < this->NRows; row++) {
330  stream << " [";
331  for (auto col = 0; col < this->NCols; col++) {
332  if (col > 0) stream << ", ";
333  stream << get_value(row, col);
334  }
335  stream << "]\n";
336  }
337  stream << "]";
338 
339  return stream.str();
340  }
341 };
342 
343 } // namespace SF
344 
345 
346 
347 #endif // _SF_ABSTRACT_MATRIX_H
Basic containers.
Classes and algorithms related to the layout of distributed meshes.
virtual void set_values(const vector< T > &row_idx, const vector< T > &col_idx, const S *vals, bool add)=0
std::string to_string() const
const meshdata< mesh_int_t, mesh_real_t > * mesh_ptr() const
virtual void mult(const abstract_vector< T, S > &x, abstract_vector< T, S > &b) const =0
virtual void scale(S s)=0
void get_values(const vector< T > &row_idx, const vector< T > &col_idx, vector< S > &values) const
virtual void finish_assembly()=0
T NRows
global number of rows
virtual void zero()=0
std::set< T > boundary_idxs
virtual void get_diagonal(abstract_vector< T, S > &vec) const =0
T start
start row index of local matrix portion
T NCols
global number of cols
virtual void finalize_exact_preallocation()
virtual bool begin_exact_preallocation()
virtual void mult_LR(const abstract_vector< T, S > &L, const abstract_vector< T, S > &R)=0
T stop
stop row index of local matrix portion
virtual void init(T iNRows, T iNCols, T ilrows, T ilcols, T loc_offset, T mxent)
virtual void set_values(const vector< T > &row_idx, const vector< T > &col_idx, const vector< S > &vals, bool add)=0
void init(const meshdata< mesh_int_t, mesh_real_t > &imesh, const T irow_dpn, const T icol_dpn, const T max_edges)
virtual void duplicate(const abstract_matrix< T, S > &M)=0
virtual void set_value(T row_idx, T col_idx, S val, bool add)=0
virtual void add_scaled_matrix(const abstract_matrix< T, S > &A, const S s, const bool same_nnz)=0
virtual void write(const char *filename) const =0
const meshdata< mesh_int_t, mesh_real_t > * mesh
pointer to the associated mesh
virtual ~abstract_matrix()=default
virtual void diag_add(const abstract_vector< T, S > &diag)=0
T lsize
size of local matrix (#locally stored rows)
bool equals(const abstract_matrix< T, S > &rhs) const
virtual S get_value(T row_idx, T col_idx) const =0
overlapping_layout< T > pl
nodal parallel layout
Definition: SF_container.h:414
MPI_Comm comm
the parallel mesh is defined on a MPI world
Definition: SF_container.h:389
A vector storing arbitrary data.
Definition: SF_vector.h:28
size_t size() const
The current size of the vector.
Definition: SF_vector.h:89
Definition: dense_mat.hpp:19
int max_nodal_edgecount(const meshdata< T, S > &mesh)
Compute the maximum number of node-to-node edges for a mesh.
Definition: SF_container.h:593