EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_MultiVectorOut.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
42 #include "EpetraExt_mmio.h"
43 #include "Epetra_Comm.h"
44 #include "Epetra_BlockMap.h"
45 #include "Epetra_Map.h"
46 #include "Epetra_Vector.h"
47 #include "Epetra_IntVector.h"
48 #include "Epetra_SerialDenseVector.h"
49 #include "Epetra_IntSerialDenseVector.h"
50 #include "Epetra_Import.h"
51 #include "Epetra_CrsMatrix.h"
52 
53 using namespace EpetraExt;
54 namespace EpetraExt {
55 
56 int MultiVectorToMatlabFile( const char *filename, const Epetra_MultiVector & A) {
57 
58  std::FILE * handle = 0;
59  if (A.Map().Comm().MyPID()==0) { // Only PE 0 does this section
60  handle = fopen(filename,"w");
61  if (!handle) return(-1);
62  }
63  if (MultiVectorToMatlabHandle(handle, A)) return(-1); // Everybody calls this routine
64 
65  if (A.Map().Comm().MyPID()==0) // Only PE 0 opened a file
66  if (fclose(handle)) return(-1);
67  return(0);
68 }
69 
70 int MultiVectorToMatrixMarketFile( const char *filename, const Epetra_MultiVector & A,
71  const char * matrixName,
72  const char *matrixDescription,
73  bool writeHeader) {
74  int M = A.GlobalLength();
75  int N = A.NumVectors();
76 
77  FILE * handle = 0;
78 
79  if (A.Map().Comm().MyPID()==0) { // Only PE 0 does this section
80 
81  handle = fopen(filename,"w");
82  if (!handle) return(-1);
83  MM_typecode matcode;
84  mm_initialize_typecode(&matcode);
85  mm_set_matrix(&matcode);
86  mm_set_array(&matcode);
87  mm_set_real(&matcode);
88 
89  if (writeHeader==true) { // Only write header if requested (true by default)
90 
91  if (mm_write_banner(handle, matcode)) return(-1);
92 
93  if (matrixName!=0) fprintf(handle, "%% \n%% %s\n", matrixName);
94  if (matrixDescription!=0) fprintf(handle, "%% %s\n%% \n", matrixDescription);
95 
96  if (mm_write_mtx_array_size(handle, M, N)) return(-1);
97  }
98  }
99 
100  if (MultiVectorToMatrixMarketHandle(handle, A)) return(-1); // Everybody calls this routine
101 
102  if (A.Map().Comm().MyPID()==0) // Only PE 0 opened a file
103  if (fclose(handle)) return(-1);
104  return(0);
105 }
106 
107 int MultiVectorToMatlabHandle(FILE * handle, const Epetra_MultiVector & A) {
108  return(MultiVectorToHandle(handle, A, false));
109 }
110 int MultiVectorToMatrixMarketHandle(FILE * handle, const Epetra_MultiVector & A) {
111  return(MultiVectorToHandle(handle, A, true));
112 }
113 int MultiVectorToHandle(FILE * handle, const Epetra_MultiVector & A, bool mmFormat) {
114 
115  Epetra_BlockMap bmap = A.Map();
116  const Epetra_Comm & comm = bmap.Comm();
117  int numProc = comm.NumProc();
118 
119  if (numProc==1)
120  writeMultiVector(handle, A, mmFormat);
121  else {
122 
123  // In the case of more than one column in the multivector, and writing to MatrixMarket
124  // format, we call this function recursively, passing each vector of the multivector
125  // individually so that we can get all of it written to file before going on to the next
126  // multivector
127  if (A.NumVectors()>1 && mmFormat) {
128  for (int i=0; i<A.NumVectors(); i++)
129  if (MultiVectorToHandle(handle, *(A(i)), mmFormat)) return(-1);
130  return(0);
131  }
132 
133  Epetra_Map map(-1, bmap.NumMyPoints(), 0, comm);
134  // Create a veiw of this multivector using a map (instead of block map)
135  Epetra_MultiVector A1(View, map, A.Pointers(), A.NumVectors());
136  int numRows = map.NumMyElements();
137 
138  Epetra_Map allGidsMap(-1, numRows, 0,comm);
139 
140  Epetra_IntVector allGids(allGidsMap);
141  for (int i=0; i<numRows; i++) allGids[i] = map.GID(i);
142 
143  // Now construct a MultiVector on PE 0 by strip-mining the rows of the input matrix A.
144  int numChunks = numProc;
145  int stripSize = allGids.GlobalLength()/numChunks;
146  int remainder = allGids.GlobalLength()%numChunks;
147  int curStart = 0;
148  int curStripSize = 0;
149  Epetra_IntSerialDenseVector importGidList;
150  if (comm.MyPID()==0)
151  importGidList.Size(stripSize+1); // Set size of vector to max needed
152  for (int i=0; i<numChunks; i++) {
153  if (comm.MyPID()==0) { // Only PE 0 does this part
154  curStripSize = stripSize;
155  if (i<remainder) curStripSize++; // handle leftovers
156  for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
157  curStart += curStripSize;
158  }
159  // The following import map will be non-trivial only on PE 0.
160  Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
161  Epetra_Import gidImporter(importGidMap, allGidsMap);
162  Epetra_IntVector importGids(importGidMap);
163  if (importGids.Import(allGids, gidImporter, Insert)) return(-1);
164 
165  // importGids now has a list of GIDs for the current strip of matrix rows.
166  // Use these values to build another importer that will get rows of the matrix.
167 
168  // The following import map will be non-trivial only on PE 0.
169  Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), 0, comm);
170  Epetra_Import importer(importMap, map);
171  Epetra_MultiVector importA(importMap, A1.NumVectors());
172  if (importA.Import(A1, importer, Insert)) return(-1);
173 
174  // Finally we are ready to write this strip of the matrix to ostream
175  if (writeMultiVector(handle, importA, mmFormat)) return(-1);
176  }
177  }
178  return(0);
179 }
180 int writeMultiVector(FILE * handle, const Epetra_MultiVector & A, bool mmFormat) {
181 
182  int ierr = 0;
183  int length = A.GlobalLength();
184  int numVectors = A.NumVectors();
185  const Epetra_Comm & comm = A.Map().Comm();
186  if (comm.MyPID()!=0) {
187  if (A.MyLength()!=0) ierr = -1;
188  }
189  else {
190  if (length!=A.MyLength()) ierr = -1;
191  for (int j=0; j<numVectors; j++) {
192  for (int i=0; i<length; i++) {
193  double val = A[j][i];
194  if (mmFormat)
195  fprintf(handle, "%22.16e\n", val);
196  else
197  fprintf(handle, "%22.16e ", val);
198  }
199  if (!mmFormat) fprintf(handle, "%s", "\n");
200  }
201  }
202  int ierrGlobal;
203  comm.MinAll(&ierr, &ierrGlobal, 1); // If any processor has -1, all return -1
204  return(ierrGlobal);
205 }
206 } // namespace EpetraExt
#define mm_set_real(typecode)
int MultiVectorToMatlabFile(const char *filename, const Epetra_MultiVector &A)
Writes an Epetra_MultiVector object to a file that is compatible with Matlab.
#define mm_set_matrix(typecode)
int mm_write_banner(FILE *f, MM_typecode matcode)
int MultiVectorToMatlabHandle(FILE *handle, const Epetra_MultiVector &A)
int MultiVectorToMatrixMarketFile(const char *filename, const Epetra_MultiVector &A, const char *matrixName, const char *matrixDescription, bool writeHeader)
Writes an Epetra_MultiVector object to a Matrix Market format file.
char MM_typecode[4]
int writeMultiVector(FILE *handle, const Epetra_MultiVector &A, bool mmFormat)
int mm_write_mtx_array_size(FILE *f, int M, int N)
#define mm_set_array(typecode)
int MultiVectorToHandle(FILE *handle, const Epetra_MultiVector &A, bool mmFormat)
#define mm_initialize_typecode(typecode)
int MultiVectorToMatrixMarketHandle(FILE *handle, const Epetra_MultiVector &A)