EpetraExt  Development
EpetraExt_RowMatrixOut.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 #include "EpetraExt_RowMatrixOut.h"
42 #include "EpetraExt_mmio.h"
43 #include "Epetra_Comm.h"
44 #include "Epetra_Map.h"
45 #include "Epetra_Vector.h"
46 #include "Epetra_IntVector.h"
47 #include "Epetra_SerialDenseVector.h"
48 #include "Epetra_IntSerialDenseVector.h"
49 #include "Epetra_Import.h"
50 #include "Epetra_CrsMatrix.h"
51 
52 using namespace EpetraExt;
53 namespace EpetraExt {
54 
55 int RowMatrixToMatlabFile( const char *filename, const Epetra_RowMatrix & A) {
56 
57  // Simple wrapper to make it clear what can be used to write to Matlab format
58  return(RowMatrixToMatrixMarketFile(filename, A, 0, 0, false));
59 }
60 
61 int RowMatrixToMatrixMarketFile( const char *filename, const Epetra_RowMatrix & A,
62  const char * matrixName,
63  const char *matrixDescription,
64  bool writeHeader) {
65  int M = A.NumGlobalRows();
66  int N = A.NumGlobalCols();
67  int nz = A.NumGlobalNonzeros();
68 
69  FILE * handle = 0;
70 
71  if (A.RowMatrixRowMap().Comm().MyPID()==0) { // Only PE 0 does this section
72 
73  handle = fopen(filename,"w");
74  if (!handle) {EPETRA_CHK_ERR(-1);}
75  MM_typecode matcode;
76  mm_initialize_typecode(&matcode);
77  mm_set_matrix(&matcode);
78  mm_set_coordinate(&matcode);
79  mm_set_real(&matcode);
80 
81  if (writeHeader==true) { // Only write header if requested (true by default)
82 
83  if (mm_write_banner(handle, matcode)!=0) {EPETRA_CHK_ERR(-1);}
84 
85  if (matrixName!=0) fprintf(handle, "%% \n%% %s\n", matrixName);
86  if (matrixDescription!=0) fprintf(handle, "%% %s\n%% \n", matrixDescription);
87 
88  if (mm_write_mtx_crd_size(handle, M, N, nz)!=0) {EPETRA_CHK_ERR(-1);}
89  }
90  }
91 
92  if (RowMatrixToHandle(handle, A)!=0) {EPETRA_CHK_ERR(-1);}// Everybody calls this routine
93 
94  if (A.RowMatrixRowMap().Comm().MyPID()==0) // Only PE 0 opened a file
95  if (fclose(handle)!=0) {EPETRA_CHK_ERR(-1);}
96  return(0);
97 }
98 
99 int RowMatrixToHandle(FILE * handle, const Epetra_RowMatrix & A) {
100 
101  Epetra_Map map = A.RowMatrixRowMap();
102  const Epetra_Comm & comm = map.Comm();
103  int numProc = comm.NumProc();
104 
105  if (numProc==1 || !A.Map().DistributedGlobal())
106  writeRowMatrix(handle, A);
107  else {
108  int numRows = map.NumMyElements();
109 
110  Epetra_Map allGidsMap(-1, numRows, 0,comm);
111 
112  Epetra_IntVector allGids(allGidsMap);
113  for (int i=0; i<numRows; i++) allGids[i] = map.GID(i);
114 
115  // Now construct a RowMatrix on PE 0 by strip-mining the rows of the input matrix A.
116  int numChunks = numProc;
117  int stripSize = allGids.GlobalLength()/numChunks;
118  int remainder = allGids.GlobalLength()%numChunks;
119  int curStart = 0;
120  int curStripSize = 0;
121  Epetra_IntSerialDenseVector importGidList;
122  if (comm.MyPID()==0)
123  importGidList.Size(stripSize+1); // Set size of vector to max needed
124  for (int i=0; i<numChunks; i++) {
125  if (comm.MyPID()==0) { // Only PE 0 does this part
126  curStripSize = stripSize;
127  if (i<remainder) curStripSize++; // handle leftovers
128  for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
129  curStart += curStripSize;
130  }
131  // The following import map will be non-trivial only on PE 0.
132  if (comm.MyPID()>0) assert(curStripSize==0);
133  Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
134  Epetra_Import gidImporter(importGidMap, allGidsMap);
135  Epetra_IntVector importGids(importGidMap);
136  if (importGids.Import(allGids, gidImporter, Insert)!=0) {EPETRA_CHK_ERR(-1); }
137 
138  // importGids now has a list of GIDs for the current strip of matrix rows.
139  // Use these values to build another importer that will get rows of the matrix.
140 
141  // The following import map will be non-trivial only on PE 0.
142  Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), map.IndexBase(), comm);
143  Epetra_Import importer(importMap, map);
144  Epetra_CrsMatrix importA(Copy, importMap, 0);
145  if (importA.Import(A, importer, Insert)!=0) {EPETRA_CHK_ERR(-1); }
146  if (importA.FillComplete(A.OperatorDomainMap(), importMap)!=0) {EPETRA_CHK_ERR(-1);}
147 
148  // Finally we are ready to write this strip of the matrix to ostream
149  if (writeRowMatrix(handle, importA)!=0) {EPETRA_CHK_ERR(-1);}
150  }
151  }
152  return(0);
153 }
154 int writeRowMatrix(FILE * handle, const Epetra_RowMatrix & A) {
155 
156  int numRows = A.NumGlobalRows();
157  Epetra_Map rowMap = A.RowMatrixRowMap();
158  Epetra_Map colMap = A.RowMatrixColMap();
159  const Epetra_Comm & comm = rowMap.Comm();
160  int ioffset = 1 - rowMap.IndexBase(); // Matlab indices start at 1
161  int joffset = 1 - colMap.IndexBase(); // Matlab indices start at 1
162  if (comm.MyPID()!=0) {
163  if (A.NumMyRows()!=0) {EPETRA_CHK_ERR(-1);}
164  if (A.NumMyCols()!=0) {EPETRA_CHK_ERR(-1);}
165  }
166  else {
167  if (numRows!=A.NumMyRows()) {EPETRA_CHK_ERR(-1);}
168  Epetra_SerialDenseVector values(A.MaxNumEntries());
169  Epetra_IntSerialDenseVector indices(A.MaxNumEntries());
170  for (int i=0; i<numRows; i++) {
171  int I = rowMap.GID(i) + ioffset;
172  int numEntries;
173  if (A.ExtractMyRowCopy(i, values.Length(), numEntries,
174  values.Values(), indices.Values())!=0) {EPETRA_CHK_ERR(-1);}
175  for (int j=0; j<numEntries; j++) {
176  int J = colMap.GID(indices[j]) + joffset;
177  double val = values[j];
178  fprintf(handle, "%d %d %22.16e\n", I, J, val);
179  }
180  }
181  }
182  return(0);
183 }
184 } // namespace EpetraExt
#define mm_set_coordinate(typecode)
int RowMatrixToHandle(FILE *handle, const Epetra_RowMatrix &A)
#define mm_set_real(typecode)
#define mm_set_matrix(typecode)
int mm_write_banner(FILE *f, MM_typecode matcode)
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
int RowMatrixToMatlabFile(const char *filename, const Epetra_RowMatrix &A)
Writes an Epetra_RowMatrix object to a file that is compatible with Matlab.
char MM_typecode[4]
int writeRowMatrix(FILE *handle, const Epetra_RowMatrix &A)
int RowMatrixToMatrixMarketFile(const char *filename, const Epetra_RowMatrix &A, const char *matrixName, const char *matrixDescription, bool writeHeader)
Writes an Epetra_RowMatrix object to a Matrix Market format file.
int mm_write_mtx_crd_size(FILE *f, int M, int N, int nz)
#define mm_initialize_typecode(typecode)