MatrixCollection.cxx
1 // Copyright (C) 2010 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_COLLECTION_CXX
22 
23 #include "MatrixCollection.hxx"
24 
25 
26 namespace Seldon
27 {
28 
29 
31  // MATRIXCOLLECTION //
33 
34 
35  /****************
36  * CONSTRUCTORS *
37  ****************/
38 
39 
41 
44  template <class T, class Prop, class Storage, class Allocator>
46  Matrix_Base<T, Allocator>(), Mlocal_(), Mlocal_sum_(1),
47  Nlocal_(), Nlocal_sum_(1), matrix_()
48  {
49  nz_ = 0;
50  Mmatrix_ = 0;
51  Nmatrix_ = 0;
52  Mlocal_sum_.Fill(0);
53  Nlocal_sum_.Fill(0);
54  }
55 
56 
58 
62  template <class T, class Prop, class Storage, class Allocator>
64  ::MatrixCollection(int i, int j): Matrix_Base<T, Allocator>(i, j),
65  Mlocal_(i), Mlocal_sum_(i + 1),
66  Nlocal_(j), Nlocal_sum_(j + 1),
67  matrix_(i, j)
68  {
69  nz_ = 0;
70  Mmatrix_ = i;
71  Nmatrix_ = j;
72  Mlocal_.Fill(0);
73  Nlocal_.Fill(0);
74  Mlocal_sum_.Fill(0);
75  Nlocal_sum_.Fill(0);
76  }
77 
78 
80 
84  template <class T, class Prop, class Storage, class Allocator>
86  {
87  for (int i = 0; i < Mmatrix_; i++)
88  for (int j = 0; j < Nmatrix_; j++)
89  matrix_(i, j).Nullify();
90 
91  matrix_.Clear();
92 
93  nz_ = 0;
94  Mmatrix_ = 0;
95  Nmatrix_ = 0;
96  Mlocal_.Clear();
97  Nlocal_.Clear();
98  Mlocal_sum_.Clear();
99  Nlocal_sum_.Clear();
100  }
101 
102 
104 
108  template <class T, class Prop, class Storage, class Allocator>
110  {
111  for (int i = 0; i < Mmatrix_; i++)
112  for (int j = 0; j < Nmatrix_; j++)
113  matrix_(i, j).Nullify();
114 
115  nz_ = 0;
116  Mmatrix_ = 0;
117  Nmatrix_ = 0;
118  Mlocal_.Clear();
119  Nlocal_.Clear();
120  Mlocal_sum_.Clear();
121  Nlocal_sum_.Clear();
122  }
123 
124 
126 
130  template <class T, class Prop, class Storage, class Allocator>
132  ::Nullify(int i, int j)
133  {
134  nz_ -= matrix_(i, j).GetDataSize();
135  matrix_(i, j).Nullify();
136  }
137 
138 
140  template <class T, class Prop, class Storage, class Allocator>
142  {
143  for (int i = 0; i < Mmatrix_; i++)
144  for (int j = 0; j < Nmatrix_; j++)
145  GetMatrix(i, j).Clear();
146  this->Clear();
147  }
148 
149 
150  /*********************
151  * MEMORY MANAGEMENT *
152  *********************/
153 
154 
156 
162  template <class T, class Prop, class Storage, class Allocator>
164  ::Reallocate(int i, int j)
165  {
166  nz_ = 0;
167  Mmatrix_ = i;
168  Nmatrix_ = j;
169  Mlocal_.Reallocate(i);
170  Nlocal_.Reallocate(j);
171  Mlocal_sum_.Reallocate(i + 1);
172  Nlocal_sum_.Reallocate(j + 1);
173  Mlocal_.Fill(0.);
174  Nlocal_.Fill(0.);
175  Mlocal_sum_.Fill(0.);
176  Nlocal_sum_.Fill(0.);
177  matrix_.Reallocate(i, j);
178  }
179 
180 
182 
187  template <class T, class Prop, class Storage, class Allocator>
188  template <class T0, class Prop0, class Storage0, class Allocator0>
191  {
192 #ifdef SELDON_CHECK_BOUNDS
193  if (i < 0 || i >= Mmatrix_)
194  throw WrongRow("MatrixCollection::SetMatrix()",
195  string("Line index should be in [0, ")
196  + to_str(Mmatrix_ - 1) + "], but is equal to "
197  + to_str(i) + ".");
198  if (j < 0 || j >= Nmatrix_)
199  throw WrongCol("MatrixCollection::SetMatrix()",
200  string("Column index should be in [0, ")
201  + to_str(Nmatrix_ - 1) + "], but is equal to "
202  + to_str(j) + ".");
203  if ((Mlocal_(i) != 0) && (Mlocal_(i) != A.GetM()))
204  throw WrongDim("MatrixCollection::SetMatrix()",
205  string("The matrix expected should have ")
206  + to_str(this->Mlocal_(i)) + " rows, but has "
207  + to_str(A.GetM()) + " rows.");
208  if ((Nlocal_(j) != 0) && (Nlocal_(j) != A.GetN()))
209  throw WrongDim("MatrixCollection::SetMatrix()",
210  string("The matrix expected should have ")
211  + to_str(this->Nlocal_(j)) + " columns, but has "
212  + to_str(A.GetN()) + " columns.");
213 #endif
214 
215  nz_ = A.GetDataSize() - matrix_(i, j).GetDataSize();
216 
217  int Mdiff = A.GetM() - Mlocal_(i);
218  int Ndiff = A.GetN() - Nlocal_(j);
219 
220  Mlocal_(i) = A.GetM();
221  Nlocal_(j) = A.GetN();
222 
223  for (int k = i + 1; k < Mmatrix_ + 1; k++)
224  Mlocal_sum_(k) += Mdiff;
225 
226  for (int k = j + 1; k < Nmatrix_ + 1; k++)
227  Nlocal_sum_(k) += Ndiff;
228 
229  this->m_ = Mlocal_sum_(Mmatrix_);
230  this->n_ = Nlocal_sum_(Nmatrix_);
231 
232  matrix_(i, j).SetData(A.GetM(), A.GetN(), A.GetData());
233  }
234 
235 
237 
242  template <class T, class Prop, class Storage, class Allocator>
243  template <class T0, class Prop0, class Allocator0>
246  {
247 #ifdef SELDON_CHECK_BOUNDS
248  if (i < 0 || i >= Mmatrix_)
249  throw WrongRow("MatrixCollection::SetMatrix()",
250  string("Index should be in [0, ")
251  + to_str(Mmatrix_ - 1) + "], but is equal to "
252  + to_str(i) + ".");
253  if (j < 0 || j >= Nmatrix_)
254  throw WrongCol("MatrixCollection::SetMatrix()",
255  string("Index should be in [0, ")
256  + to_str(Nmatrix_ - 1) + "], but is equal to "
257  + to_str(j) + ".");
258  if ((Mlocal_(i) != 0) && (Mlocal_(i) != A.GetM()))
259  throw WrongDim("MatrixCollection::SetMatrix()",
260  string("The matrix expected should have ")
261  + to_str(this->Mlocal_(i)) + " rows, but has "
262  + to_str(A.GetM()) + " rows.");
263  if ((Nlocal_(j) != 0) && (Nlocal_(j) != A.GetN()))
264  throw WrongDim("MatrixCollection::SetMatrix()",
265  string("The matrix expected should have ")
266  + to_str(this->Nlocal_(j)) + " columns, but has "
267  + to_str(A.GetN()) + " columns.");
268 #endif
269 
270  nz_ = A.GetDataSize() - matrix_(i, j).GetDataSize();
271 
272  int Mdiff = A.GetM() - Mlocal_(i);
273  int Ndiff = A.GetN() - Nlocal_(j);
274 
275  Mlocal_(i) = A.GetM();
276  Nlocal_(j) = A.GetN();
277 
278  for (int k = i + 1; k < Mmatrix_ + 1; k++)
279  Mlocal_sum_(k) += Mdiff;
280 
281  for (int k = j + 1; k < Nmatrix_ + 1; k++)
282  Nlocal_sum_(k) += Ndiff;
283 
284  this->m_ = Mlocal_sum_(Mmatrix_);
285  this->n_ = Nlocal_sum_(Nmatrix_);
286 
287  matrix_(i, j).SetData(A.GetM(), A.GetN(), A.GetNonZeros(), A.GetData(),
288  A.GetPtr(), A.GetInd());
289  }
290 
291 
292 
294 
300  template <class T, class Prop, class Storage, class Allocator>
302  ::value_type
304  int j) const
305  {
306 #ifdef SELDON_CHECK_BOUNDS
307  if (i < 0 || i >= this->Mlocal_sum_(Mmatrix_))
308  throw WrongRow("MatrixCollection::operator(int, int)",
309  string("Row index should be in [0, ")
310  + to_str(this->Mlocal_sum_(Mmatrix_) - 1)
311  + "], but is equal to " + to_str(i) + ".");
312  if (j < 0 || j >= this->Nlocal_sum_(Nmatrix_))
313  throw WrongCol("MatrixCollection::operator(int, int)",
314  string("Column index should be in [0, ")
315  + to_str(this->Nlocal_sum_(Nmatrix_) - 1)
316  + "], but is equal to " + to_str(j) + ".");
317 #endif
318 
319  int i_global = 0;
320  while (i >= Mlocal_sum_(i_global))
321  i_global++;
322  i_global--;
323 
324  int j_global = 0;
325  while (j >= Nlocal_sum_(j_global))
326  j_global++;
327  j_global--;
328 
329  return matrix_(i_global, j_global)(i - Mlocal_sum_(i_global),
330  j - Nlocal_sum_(j_global));
331  }
332 
333 
335 
340  template <class T, class Prop, class Storage, class Allocator>
344  {
345  this->Copy(A);
346  return *this;
347  }
348 
349 
351 
356  template <class T, class Prop, class Storage, class Allocator>
359  {
360  Clear();
361 
362  this->nz_ = A.nz_;
363  this->m_ = A.GetM();
364  this->n_ = A.GetN();
365  Mmatrix_ = A.Mmatrix_;
366  Nmatrix_ = A.Nmatrix_;
367  this->Mlocal_ = A.Mlocal_;
368  this->Mlocal_sum_ = A.Mlocal_sum_;
369  this->Nlocal_ = A.Nlocal_;
370  this->Nlocal_sum_ = A.Nlocal_sum_;
371 
372  matrix_.Reallocate(Mmatrix_, Nmatrix_);
373 
374  for (int i = 0; i < Mmatrix_; i++)
375  for (int j = 0; j < Nmatrix_; j++)
376  SetMatrix(i, j, A.GetMatrix(i, j));
377  }
378 
379 
380  /************************
381  * CONVENIENT FUNCTIONS *
382  ************************/
383 
384 
386 
389  template <class T, class Prop, class Storage, class Allocator>
391  {
392  for (int i = 0; i < Mmatrix_; i++)
393  {
394  for (int j = 0; j < Nmatrix_; j++)
395  cout << GetMatrix(i, j) << endl;
396  cout << endl;
397  }
398  }
399 
400 
402 
406  template <class T, class Prop, class Storage, class Allocator>
408  ::Print(int i, int j) const
409  {
410 #ifdef SELDON_CHECK_BOUNDS
411  if (i < 0 || i >= Mmatrix_)
412  throw WrongRow("MatrixCollection::Print(int, int)",
413  string("Row index should be in [0, ")
414  + to_str(Mmatrix_ - 1) + "], but is equal to "
415  + to_str(i) + ".");
416  if (j < 0 || j >= Nmatrix_)
417  throw WrongCol("MatrixCollection::Print(int, int)",
418  string("Column index should be in [0, ")
419  + to_str(Nmatrix_ - 1) + "], but is equal to "
420  + to_str(j) + ".");
421 #endif
422 
423  cout << matrix_(i, j) << endl;
424  }
425 
426 
428 
436  template <class T, class Prop, class Storage, class Allocator>
438  ::Write(string FileName, bool with_size) const
439  {
440  ofstream FileStream;
441  FileStream.open(FileName.c_str(), ofstream::binary);
442 
443 #ifdef SELDON_CHECK_IO
444  // Checks if the file was opened.
445  if (!FileStream.is_open())
446  throw IOError("MatrixCollection::Write(string FileName, bool)",
447  string("Unable to open file \"") + FileName + "\".");
448 #endif
449 
450  this->Write(FileStream, with_size);
451 
452  FileStream.close();
453  }
454 
455 
457 
465  template <class T, class Prop, class Storage, class Allocator>
467  ::Write(ostream& FileStream, bool with_size = true) const
468  {
469 
470 #ifdef SELDON_CHECK_IO
471  // Checks if the stream is ready.
472  if (!FileStream.good())
473  throw IOError("MatrixCollection::Write(ostream& FileStream, bool)",
474  "The stream is not ready.");
475 #endif
476 
477  if (with_size)
478  {
479  FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&Mmatrix_)),
480  sizeof(int));
481  FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&Nmatrix_)),
482  sizeof(int));
483  }
484 
485  int i, j;
486  for (i = 0; i < this->GetMmatrix(); i++)
487  {
488  for (j = 0; j < this->GetNmatrix(); j++)
489  GetMatrix(i, j).Write(FileStream, with_size);
490  }
491 
492 #ifdef SELDON_CHECK_IO
493  // Checks if data was written.
494  if (!FileStream.good())
495  throw IOError("MatrixCollection::Write(ostream& FileStream, bool)",
496  "Output operation failed.");
497 #endif
498 
499  }
500 
501 
503 
508  template <class T, class Prop, class Storage, class Allocator>
510  ::WriteText(string FileName) const
511  {
512  ofstream FileStream;
513  FileStream.precision(cout.precision());
514  FileStream.flags(cout.flags());
515  FileStream.open(FileName.c_str());
516 
517 #ifdef SELDON_CHECK_IO
518  // Checks if the file was opened.
519  if (!FileStream.is_open())
520  throw IOError("MatrixCollection::WriteText(string FileName)",
521  string("Unable to open file \"") + FileName + "\".");
522 #endif
523 
524  this->WriteText(FileStream);
525 
526  FileStream.close();
527  }
528 
529 
531 
537  template <class T, class Prop, class Storage, class Allocator>
539  ::WriteText(ostream& FileStream) const
540  {
541 
542 #ifdef SELDON_CHECK_IO
543  // Checks if the file is ready.
544  if (!FileStream.good())
545  throw IOError("MatrixCollection::WriteText(ostream& FileStream)",
546  "The stream is not ready.");
547 #endif
548 
549  int i, j;
550  for (i = 0; i < this->GetMmatrix(); i++)
551  {
552  for (j = 0; j < this->GetNmatrix(); j++)
553  GetMatrix(i, j).WriteText(FileStream);
554  FileStream << endl;
555  }
556 
557 #ifdef SELDON_CHECK_IO
558  // Checks if data was written.
559  if (!FileStream.good())
560  throw IOError("MatrixCollection::WriteText(ostream& FileStream)",
561  "Output operation failed.");
562 #endif
563 
564  }
565 
566 
568 
574  template <class T, class Prop, class Storage, class Allocator>
576  {
577  ifstream FileStream;
578  FileStream.open(FileName.c_str(), ifstream::binary);
579 
580 #ifdef SELDON_CHECK_IO
581  // Checks if the file was opened.
582  if (!FileStream.is_open())
583  throw IOError("MatrixCollection::Read(string FileName)",
584  string("Unable to open file \"") + FileName + "\".");
585 #endif
586 
587  this->Read(FileStream);
588 
589  FileStream.close();
590  }
591 
592 
594 
600  template <class T, class Prop, class Storage, class Allocator>
602  ::Read(istream& FileStream)
603  {
604 
605 #ifdef SELDON_CHECK_IO
606  // Checks if the stream is ready.
607  if (!FileStream.good())
608  throw IOError("MatrixCollection::Read(istream& FileStream)",
609  "The stream is not ready.");
610 #endif
611 
612  int *new_m, *new_n;
613  new_m = new int;
614  new_n = new int;
615 
616  FileStream.read(reinterpret_cast<char*>(new_m), sizeof(int));
617  FileStream.read(reinterpret_cast<char*>(new_n), sizeof(int));
618 
619  this->Reallocate(*new_m, *new_n);
620 
621  T working_matrix;
622  int i, j;
623  for (i = 0; i < *new_m; i++)
624  for (j = 0; j < *new_n; j++)
625  {
626  working_matrix.Read(FileStream);
627  SetMatrix(i, j, working_matrix);
628  working_matrix.Nullify();
629  }
630 
631 
632  delete new_n;
633  delete new_m;
634 
635 #ifdef SELDON_CHECK_IO
636  // Checks if data was read.
637  if (!FileStream.good())
638  throw IOError("MatrixCollection::Read(istream& FileStream)",
639  "Input operation failed.");
640 #endif
641 
642  }
643 
644 
645 } // namespace Seldon.
646 
647 
648 #define SELDON_FILE_MATRIX_COLLECTION_CXX
649 #endif
Seldon::MatrixCollection::Nullify
void Nullify()
Clears the matrix.
Definition: MatrixCollection.cxx:109
Seldon::Matrix_Base
Base class for all matrices.
Definition: Matrix_Base.hxx:143
Seldon::MatrixCollection::Mlocal_sum_
Vector< int, VectFull, CallocAlloc< int > > Mlocal_sum_
Cumulative number of rows in the underlying matrices.
Definition: MatrixCollection.hxx:70
Seldon::to_str
std::string to_str(const T &input)
Converts most types to string.
Definition: CommonInline.cxx:137
Seldon::MatrixCollection
Matrix class made of a collection of matrices.
Definition: MatrixCollection.hxx:37
Seldon::WrongCol
Definition: Errors.hxx:138
Seldon::MatrixCollection::Nmatrix_
int Nmatrix_
Number of columns of matrices.
Definition: MatrixCollection.hxx:66
Seldon::Matrix
Definition: SeldonHeader.hxx:226
Seldon::MatrixCollection::GetM
int GetM() const
Returns the number of rows.
Definition: MatrixCollectionInline.cxx:69
Seldon::WrongRow
Definition: Errors.hxx:126
Seldon::MatrixCollection::Nlocal_
Vector< int, VectFull, CallocAlloc< int > > Nlocal_
Number of columns in the underlying matrices.
Definition: MatrixCollection.hxx:72
Seldon::MatrixCollection::GetMatrix
matrix_reference GetMatrix(int i, int j)
Access to an underlying matrix.
Definition: MatrixCollectionInline.cxx:198
Seldon::MatrixCollection::MatrixCollection
MatrixCollection()
Default constructor.
Definition: MatrixCollection.cxx:45
Seldon::MatrixCollection::Mlocal_
Vector< int, VectFull, CallocAlloc< int > > Mlocal_
Number of rows in the underlying matrices.
Definition: MatrixCollection.hxx:68
Seldon::MatrixCollection::GetN
int GetN() const
Returns the number of columns.
Definition: MatrixCollectionInline.cxx:113
Seldon::MatrixCollection::GetDataSize
int GetDataSize() const
Returns the number of elements stored in memory.
Definition: MatrixCollectionInline.cxx:168
Seldon::MatrixCollection::nz_
int nz_
Number of non-zero elements.
Definition: MatrixCollection.hxx:62
Seldon::MatrixCollection::Nlocal_sum_
Vector< int, VectFull, CallocAlloc< int > > Nlocal_sum_
Cumulative number of columns in the underlying matrices.
Definition: MatrixCollection.hxx:74
Seldon::WrongDim
Definition: Errors.hxx:102
Seldon::MatrixCollection::Clear
void Clear()
Clears the matrix.
Definition: MatrixCollection.cxx:85
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::IOError
Definition: Errors.hxx:150
Seldon::MatrixCollection::Mmatrix_
int Mmatrix_
Number of rows of matrices.
Definition: MatrixCollection.hxx:64