PetscMatrix.cxx
1 // Copyright (C) 2012 INRIA
2 // Author(s): Marc Fragu
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_MATRIX_PETSCMATRIX_CXX
22 
23 #include "PetscMatrix.hxx"
24 
25 
26 namespace Seldon
27 {
28 
30 
35  template <class T, class Prop, class Storage, class Allocator>
37  ::Copy(const Mat& A)
38  {
39  Clear();
40  int ierr;
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;
46  }
47 
48 
50 
54  template <class T, class Prop, class Storage, class Allocator>
56  {
57  MatScale(petsc_matrix_, T(0));
58  }
59 
60 
62  template <class T, class Prop, class Storage, class Allocator>
64  {
65  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
66  "::SetIdentity()");
67  }
68 
69 
71 
75  template <class T, class Prop, class Storage, class Allocator>
77  {
78  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
79  "::Fill()");
80  }
81 
82 
84 
87  template <class T, class Prop, class Storage, class Allocator>
88  template <class T0>
90  {
91  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
92  "::Fill(const T0& x)");
93  }
94 
95 
97 
100  template <class T, class Prop, class Storage, class Allocator>
102  {
103  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
104  "::FillRand()");
105  }
106 
107 
109 
120  template <class T, class Prop, class Storage, class Allocator>
122  ::Print(int a, int b, int m, int n) const
123  {
124  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
125  "::Print(int a, int b, int m, int n) const");
126  }
127 
128 
130 
138  template <class T, class Prop, class Storage, class Allocator>
140  {
141  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
142  "::Print(int l) const");
143  }
144 
145 
146  /**************************
147  * INPUT/OUTPUT FUNCTIONS *
148  **************************/
149 
150 
152 
159  template <class T, class Prop, class Storage, class Allocator>
161  ::Write(string FileName, bool with_size) const
162  {
163  ofstream FileStream;
164  FileStream.open(FileName.c_str());
165 
166 #ifdef SELDON_CHECK_IO
167  // Checks if the file was opened.
168  if (!FileStream.is_open())
169  throw IOError("PetscMatrix::Write(string FileName)",
170  string("Unable to open file \"") + FileName + "\".");
171 #endif
172 
173  this->Write(FileStream, with_size);
174 
175  FileStream.close();
176  }
177 
178 
180 
187  template <class T, class Prop, class Storage, class Allocator>
189  ::Write(ostream& FileStream, bool with_size) const
190  {
191  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
192  "::Write(ostream& FileStream, bool with_size) const");
193  }
194 
195 
197 
204  template <class T, class Prop, class Storage, class Allocator>
206  ::WriteText(string FileName) const
207  {
208  ofstream FileStream;
209  FileStream.precision(cout.precision());
210  FileStream.flags(cout.flags());
211  FileStream.open(FileName.c_str());
212 
213 #ifdef SELDON_CHECK_IO
214  // Checks if the file was opened.
215  if (!FileStream.is_open())
216  throw IOError("PetscMatrix::WriteText(string FileName)",
217  string("Unable to open file \"") + FileName + "\".");
218 #endif
219 
220  this->WriteText(FileStream);
221 
222  FileStream.close();
223  }
224 
225 
227 
234  template <class T, class Prop, class Storage, class Allocator>
236  ::WriteText(ostream& FileStream) const
237  {
238  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
239  "::WriteText(ostream& FileStream) const");
240  }
241 
242 
244 
251  template <class T, class Prop, class Storage, class Allocator>
253  {
254  ifstream FileStream;
255  FileStream.open(FileName.c_str());
256 
257 #ifdef SELDON_CHECK_IO
258  // Checks if the file was opened.
259  if (!FileStream.is_open())
260  throw IOError("PetscMatrix::Read(string FileName)",
261  string("Unable to open file \"") + FileName + "\".");
262 #endif
263 
264  this->Read(FileStream);
265 
266  FileStream.close();
267  }
268 
269 
271 
278  template <class T, class Prop, class Storage, class Allocator>
280  ::Read(istream& FileStream)
281  {
282  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
283  "::Read(istream& FileStream) const");
284  }
285 
286 
288 
292  template <class T, class Prop, class Storage, class Allocator>
294  {
295  ifstream FileStream;
296  FileStream.open(FileName.c_str());
297 
298 #ifdef SELDON_CHECK_IO
299  // Checks if the file was opened.
300  if (!FileStream.is_open())
301  throw IOError("Matrix_Pointers::ReadText(string FileName)",
302  string("Unable to open file \"") + FileName + "\".");
303 #endif
304 
305  this->ReadText(FileStream);
306 
307  FileStream.close();
308  }
309 
310 
312 
316  template <class T, class Prop, class Storage, class Allocator>
318  ::ReadText(istream& FileStream)
319  {
320  throw Undefined("void PetscMatrix<T, Prop, Storage, Allocator>"
321  "::ReadText(istream& FileStream)");
322  }
323 
324 
326  // Matrix<PETScSeqDense> //
328 
329 
331 
336  template <class T, class Prop, class Allocator>
338  {
339  int ierr;
340  ierr = MatView(this->petsc_matrix_, PETSC_VIEWER_STDOUT_SELF);
341  CHKERRABORT(this->mpi_communicator_, ierr);
342  }
343 
344 
346  // Matrix<PETScMPIDense> //
348 
349 
351 
356  template <class T, class Prop, class Allocator>
358  {
359  int ierr;
360  ierr = MatView(this->petsc_matrix_, PETSC_VIEWER_STDOUT_WORLD);
361  CHKERRABORT(this->mpi_communicator_, ierr);
362  }
363 
364 
366  // Matrix<PETScMPIAIJ> //
368 
369 
371 
376  template <class T, class Prop, class Allocator>
377  template <class T0, class Allocator0>
380  {
381  this->Clear();
382 
383  int ierr;
384  ierr = MatCreate(this->mpi_communicator_, &this->petsc_matrix_);
385  CHKERRABORT(this->mpi_communicator_, ierr);
386 
387  int ma = A.GetM();
388  int na = A.GetN();
389  int nnz = A.GetDataSize();
390  double *value = A.GetData();
391  int *column = A.GetInd();
392  int *ptr = A.GetPtr();
393 
394  this->m_ = ma;
395  this->n_ = na;
396 
397  ierr = MatSetType(this->petsc_matrix_, MATMPIAIJ);
398  CHKERRABORT(this->mpi_communicator_, ierr);
399  ierr = MatSetSizes(this->petsc_matrix_, PETSC_DECIDE, PETSC_DECIDE,
400  this->m_, this->n_);
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);
414  }
415 
416 
418 
421  template <class T, class Prop, class Allocator>
423  {
424  int ierr;
425  int m;
426  ierr = MatGetLocalSize(this->petsc_matrix_, &m, PETSC_NULL);
427  CHKERRABORT(this->mpi_communicator_, ierr);
428  return m;
429  }
430 
431 
433 
436  template <class T, class Prop, class Allocator>
438  {
439  int ierr;
440  int n;
441  ierr = MatGetLocalSize(this->petsc_matrix_, PETSC_NULL, &n);
442  CHKERRABORT(this->mpi_communicator_, ierr);
443  return n;
444  }
445 
446 
448 
453  template <class T, class Prop, class Allocator>
455  {
456  int ierr;
457  ierr = MatView(this->petsc_matrix_, PETSC_VIEWER_STDOUT_WORLD);
458  CHKERRABORT(this->mpi_communicator_, ierr);
459  }
460 
461 
462 }
463 
464 
465 #define SELDON_FILE_MATRIX_PETSCMATRIX_CXX
466 #endif
Seldon::PetscMatrix::WriteText
void WriteText(string FileName) const
Writes the matrix in a file.
Definition: PetscMatrix.cxx:206
Seldon::PetscMatrix::Zero
void Zero()
Sets all elements to zero.
Definition: PetscMatrix.cxx:55
Seldon::PetscMatrix::Write
void Write(string FileName, bool with_size) const
Writes the matrix in a file.
Definition: PetscMatrix.cxx:161
Seldon::PetscMatrix::Print
void Print(int a, int b, int m, int n) const
Displays a sub-matrix on the standard output.
Definition: PetscMatrix.cxx:122
Seldon::PetscMatrix::SetIdentity
void SetIdentity()
Sets the matrix to the identity.
Definition: PetscMatrix.cxx:63
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::PetscMatrix::ReadText
void ReadText(string FileName)
Reads the matrix from a file.
Definition: PetscMatrix.cxx:293
Seldon::Undefined
Definition: Errors.hxx:62
Seldon::PetscMatrix::FillRand
void FillRand()
Fills a matrix randomly.
Definition: PetscMatrix.cxx:101
Seldon::PetscMatrix::Read
void Read(string FileName)
Reads the matrix from a file.
Definition: PetscMatrix.cxx:252
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::IOError
Definition: Errors.hxx:150
Seldon::PetscMatrix::Copy
void Copy(const Mat &A)
Duplicates a matrix.
Definition: PetscMatrix.cxx:37
Seldon::PetscMatrix::Fill
void Fill()
Fills the matrix with 0, 1, 2, ...
Definition: PetscMatrix.cxx:76