8#ifndef OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_PETSC_HPP_
9#define OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_PETSC_HPP_
11#include "util/petsc_util.hpp"
12#include "Vector/map_vector.hpp"
13#include <boost/mpl/int.hpp>
15#include "VTKWriter/VTKWriter.hpp"
16#include "CSVWriter/CSVWriter.hpp"
86 :row_(0),col_(0),val_(0)
97template<
typename T,
typename id_t>
124 bool m_created =
false;
160 while (i < trpl.
size())
162 PetscInt row = trpl.get(i).row();
164 while(i < trpl.
size() && row == trpl.get(i).row())
166 if ((
size_t)trpl.get(i).col() >= start_row && (
size_t)trpl.get(i).col() < start_row + l_row)
167 d_nnz.get(row - start_row)++;
169 o_nnz.get(row - start_row)++;
174 PETSC_SAFE_CALL(MatMPIAIJSetPreallocation(mat,0,
static_cast<const PetscInt*
>(d_nnz.getPointer()),0,
175 static_cast<const PetscInt*
>(o_nnz.getPointer())));
181 while (i < trpl.
size())
186 PetscInt row = trpl.get(i).row();
188 while(i < trpl.
size() && row == trpl.get(i).row())
190 vals.add(trpl.get(i).value());
191 cols.add(trpl.get(i).col());
194 PETSC_SAFE_CALL(MatSetValues(mat,1,&row,cols.
size(),
static_cast<const PetscInt*
>(cols.getPointer()),
195 static_cast<const PetscScalar *
>(vals.getPointer()),
199 PETSC_SAFE_CALL(MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY));
200 PETSC_SAFE_CALL(MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY));
222 :g_row(N1),g_col(N2),l_row(n_row_local),l_col(n_row_local)
224 PETSC_SAFE_CALL(MatCreate(PETSC_COMM_WORLD,&mat));
225 PETSC_SAFE_CALL(MatSetType(mat,MATMPIAIJ));
226 PETSC_SAFE_CALL(MatSetFromOptions(mat));
227 PETSC_SAFE_CALL(MatSetSizes(mat,n_row_local,n_row_local,N1,N2));
239 start_row += vn_row_local.get(i);
246 :g_row(0),g_col(0),l_row(0l),l_col(0),start_row(0)
248 PETSC_SAFE_CALL(MatCreate(PETSC_COMM_WORLD,&mat));
249 PETSC_SAFE_CALL(MatSetType(mat,MATMPIAIJ));
250 PETSC_SAFE_CALL(MatSetFromOptions(mat));
257 if (is_openfpm_init() ==
true)
258 {PETSC_SAFE_CALL(MatDestroy(&mat));}
282 if (m_created ==
false)
295 if (m_created ==
false)
309 void resize(
size_t row,
size_t col,
size_t l_row,
size_t l_col)
311 if ((g_row != 0 && g_row != row) || (g_col != 0 && g_col != col) ||
312 (this->l_row != 0 && this->l_row != l_row) || (this->l_col != 0 && this->l_col != l_col))
314 std::cout << __FILE__ <<
":" << __LINE__ <<
" error you are resizing a PETSC matrix " << std::endl;
317 if (g_row != 0 && g_col != 0) {
return;}
325 PETSC_SAFE_CALL(MatSetSizes(mat,l_row,l_col,g_row,g_col));
337 start_row += vn_row_local.get(i);
351 MatGetValues(mat,1,&i,1,&j,&v);
366 for (
size_t i = 0 ; i < trpl.
size() ; i++)
368 if (r == (
size_t)trpl.get(i).row() && c == (
size_t)trpl.get(i).col())
369 return trpl.get(i).value();
391 bool write(std::string out,
size_t opt = VTK_WRITER)
398 row_col.resize(trpl.
size());
399 values.resize(trpl.
size());
401 for (
int i = 0 ; i < trpl.
size() ; i++)
403 row_col.template get<0>(i)[1] = trpl.get(i).row();
404 row_col.template get<0>(i)[0] = trpl.get(i).col();
406 values.template get<0>(i) = trpl.get(i).value();
409 if (opt == VTK_WRITER)
411 auto ft = file_type::BINARY;
416 VECTOR_POINTS> vtk_writer;
418 vtk_writer.add(row_col,values,row_col.
size());
420 std::string output = std::to_string(out +
"_" + std::to_string(v_cl.
getProcessUnitID()) + std::to_string(
".vtk"));
423 prp_names.add(
"value");
426 return vtk_writer.write(output,prp_names,
"matrix",
"",ft);
434 std::string output = std::to_string(out +
"_" + std::to_string(v_cl.
getProcessUnitID()) + std::to_string(
".csv"));
437 return csv_writer.
write(output,row_col,values);
bool write(std::string file, v_pos &v, v_prp &prp, size_t offset=0)
It write a CSV file.
const Mat & getMat() const
Get the Patsc Matrix object.
Mat & getMat()
Get the Petsc Matrix object.
openfpm::vector< PetscScalar > vals
temporary list of values
size_t l_col
Number of matrix colums (local)
size_t l_row
Number of matrix row (local)
triplet< T, PETSC_BASE > triplet_type
Triplet type.
openfpm::vector< PetscInt > cols
temporary list of colums
T operator()(id_t i, id_t j)
Get the row i and the colum j of the Matrix.
SparseMatrix(size_t N1, size_t N2, size_t n_row_local)
Create an empty Matrix.
T getValue(size_t r, size_t c)
Get the value from triplet.
boost::mpl::int_< PETSC_BASE > triplet_impl
Triplet implementation id.
SparseMatrix(const SparseMatrix< T, id_t, PETSC_BASE > &spm)
Disable copy constructor.
size_t g_col
Number of matrix colums (global)
size_t start_row
starting row for this processor
openfpm::vector< triplet_type > trpl
Triplets of the matrix.
openfpm::vector< triplet_type > & getMatrixTriplets()
Get the Matrix triplets buffer.
bool isMatrixFilled()
Return the state of matrix.
size_t g_row
Number of matrix row (global)
openfpm::vector< PetscInt > d_nnz
PETSC d_nnz.
openfpm::vector< PetscInt > o_nnz
PETSC o_nnz.
SparseMatrix()
Create an empty Matrix.
void fill_petsc()
Fill the petsc Matrix.
void resize(size_t row, size_t col, size_t l_row, size_t l_col)
Resize the Sparse Matrix.
Sparse Matrix implementation.
void execute()
Execute all the requests.
size_t getProcessUnitID()
Get the process unit id.
bool allGather(T &send, openfpm::vector< T, Mem, gr > &v)
Gather the data from all processors.
Implementation of VCluster class.
Implementation of 1-D std::vector like structure.
PetscInt col_
Colum of the sparse matrix.
PetscInt row_
Row of the sparse matrix.
PetscScalar & value()
Return the value of the triplet.
PetscScalar val_
Value of the Matrix.
triplet(long int i, long int j, T val)
Constructor from row, colum and value.
PetscInt & row()
Return the row of the triplet.
PetscInt & col()
Return the colum of the triplet.
It store the non zero elements of the matrix.