21 #ifndef SELDON_FILE_MATRIX_PETSCMATRIX_CXX
23 #include "PetscMatrix.hxx"
35 template <
class T,
class Prop,
class Storage,
class Allocator>
41 ierr = MatDuplicate(A, MAT_COPY_VALUES, &petsc_matrix_);
42 CHKERRABORT(mpi_communicator_, ierr);
43 MatGetSize(A, &this->m_, &this->n_);
44 mpi_communicator_ = MPI_COMM_WORLD;
45 petsc_matrix_deallocated_ =
false;
54 template <
class T,
class Prop,
class Storage,
class Allocator>
57 MatScale(petsc_matrix_, T(0));
62 template <
class T,
class Prop,
class Storage,
class Allocator>
65 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
75 template <
class T,
class Prop,
class Storage,
class Allocator>
78 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
87 template <
class T,
class Prop,
class Storage,
class Allocator>
91 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
92 "::Fill(const T0& x)");
100 template <
class T,
class Prop,
class Storage,
class Allocator>
103 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
120 template <
class T,
class Prop,
class Storage,
class Allocator>
124 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
125 "::Print(int a, int b, int m, int n) const");
138 template <
class T,
class Prop,
class Storage,
class Allocator>
141 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
142 "::Print(int l) const");
159 template <
class T,
class Prop,
class Storage,
class Allocator>
161 ::Write(
string FileName,
bool with_size)
const
164 FileStream.open(FileName.c_str());
166 #ifdef SELDON_CHECK_IO
168 if (!FileStream.is_open())
169 throw IOError(
"PetscMatrix::Write(string FileName)",
170 string(
"Unable to open file \"") + FileName +
"\".");
173 this->Write(FileStream, with_size);
187 template <
class T,
class Prop,
class Storage,
class Allocator>
189 ::Write(ostream& FileStream,
bool with_size)
const
191 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
192 "::Write(ostream& FileStream, bool with_size) const");
204 template <
class T,
class Prop,
class Storage,
class Allocator>
209 FileStream.precision(cout.precision());
210 FileStream.flags(cout.flags());
211 FileStream.open(FileName.c_str());
213 #ifdef SELDON_CHECK_IO
215 if (!FileStream.is_open())
216 throw IOError(
"PetscMatrix::WriteText(string FileName)",
217 string(
"Unable to open file \"") + FileName +
"\".");
220 this->WriteText(FileStream);
234 template <
class T,
class Prop,
class Storage,
class Allocator>
238 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
239 "::WriteText(ostream& FileStream) const");
251 template <
class T,
class Prop,
class Storage,
class Allocator>
255 FileStream.open(FileName.c_str());
257 #ifdef SELDON_CHECK_IO
259 if (!FileStream.is_open())
260 throw IOError(
"PetscMatrix::Read(string FileName)",
261 string(
"Unable to open file \"") + FileName +
"\".");
264 this->Read(FileStream);
278 template <
class T,
class Prop,
class Storage,
class Allocator>
282 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
283 "::Read(istream& FileStream) const");
292 template <
class T,
class Prop,
class Storage,
class Allocator>
296 FileStream.open(FileName.c_str());
298 #ifdef SELDON_CHECK_IO
300 if (!FileStream.is_open())
301 throw IOError(
"Matrix_Pointers::ReadText(string FileName)",
302 string(
"Unable to open file \"") + FileName +
"\".");
305 this->ReadText(FileStream);
316 template <
class T,
class Prop,
class Storage,
class Allocator>
320 throw Undefined(
"void PetscMatrix<T, Prop, Storage, Allocator>"
321 "::ReadText(istream& FileStream)");
336 template <
class T,
class Prop,
class Allocator>
340 ierr = MatView(this->petsc_matrix_, PETSC_VIEWER_STDOUT_SELF);
341 CHKERRABORT(this->mpi_communicator_, ierr);
356 template <
class T,
class Prop,
class Allocator>
360 ierr = MatView(this->petsc_matrix_, PETSC_VIEWER_STDOUT_WORLD);
361 CHKERRABORT(this->mpi_communicator_, ierr);
376 template <
class T,
class Prop,
class Allocator>
377 template <
class T0,
class Allocator0>
384 ierr = MatCreate(this->mpi_communicator_, &this->petsc_matrix_);
385 CHKERRABORT(this->mpi_communicator_, ierr);
389 int nnz = A.GetDataSize();
390 double *value = A.GetData();
391 int *column = A.GetInd();
392 int *ptr = A.GetPtr();
397 ierr = MatSetType(this->petsc_matrix_, MATMPIAIJ);
398 CHKERRABORT(this->mpi_communicator_, ierr);
399 ierr = MatSetSizes(this->petsc_matrix_, PETSC_DECIDE, PETSC_DECIDE,
401 CHKERRABORT(this->mpi_communicator_, ierr);
402 int loc_start, loc_end;
403 ierr = MatGetOwnershipRange(this->petsc_matrix_,
404 &loc_start, &loc_end);
405 CHKERRABORT(this->mpi_communicator_, ierr);
406 for (
int i = loc_start; i < loc_end; i++)
407 for (
int j = ptr[i]; j < ptr[i + 1]; j++)
408 ierr = MatSetValues(this->petsc_matrix_, 1, &i, 1, &column[j],
409 &value[j], INSERT_VALUES);
410 ierr = MatAssemblyBegin(this->petsc_matrix_,MAT_FINAL_ASSEMBLY);
411 CHKERRABORT(this->mpi_communicator_, ierr);
412 ierr = MatAssemblyEnd(this->petsc_matrix_,MAT_FINAL_ASSEMBLY);
413 CHKERRABORT(this->mpi_communicator_, ierr);
421 template <
class T,
class Prop,
class Allocator>
426 ierr = MatGetLocalSize(this->petsc_matrix_, &m, PETSC_NULL);
427 CHKERRABORT(this->mpi_communicator_, ierr);
436 template <
class T,
class Prop,
class Allocator>
441 ierr = MatGetLocalSize(this->petsc_matrix_, PETSC_NULL, &n);
442 CHKERRABORT(this->mpi_communicator_, ierr);
453 template <
class T,
class Prop,
class Allocator>
457 ierr = MatView(this->petsc_matrix_, PETSC_VIEWER_STDOUT_WORLD);
458 CHKERRABORT(this->mpi_communicator_, ierr);
465 #define SELDON_FILE_MATRIX_PETSCMATRIX_CXX