43 #include "Epetra_Comm.h" 44 #include "Epetra_Map.h" 45 #include "Epetra_Vector.h" 46 #include "Epetra_IntVector.h" 47 #include "Epetra_LongLongVector.h" 48 #include "Epetra_GIDTypeVector.h" 49 #include "Epetra_SerialDenseVector.h" 50 #include "Epetra_IntSerialDenseVector.h" 51 #include "Epetra_LongLongSerialDenseVector.h" 52 #include "Epetra_GIDTypeSerialDenseVector.h" 53 #include "Epetra_Import.h" 54 #include "Epetra_CrsMatrix.h" 67 const char * matrixName,
68 const char *matrixDescription,
70 long long M = A.NumGlobalRows64();
71 long long N = A.NumGlobalCols64();
72 long long nz = A.NumGlobalNonzeros64();
76 if (A.RowMatrixRowMap().Comm().MyPID()==0) {
78 handle = fopen(filename,
"w");
79 if (!handle) {EPETRA_CHK_ERR(-1);}
86 if (writeHeader==
true) {
90 if (matrixName!=0) fprintf(handle,
"%% \n%% %s\n", matrixName);
91 if (matrixDescription!=0) fprintf(handle,
"%% %s\n%% \n", matrixDescription);
99 if (A.RowMatrixRowMap().Comm().MyPID()==0)
100 if (fclose(handle)!=0) {EPETRA_CHK_ERR(-1);}
104 template<
typename int_type>
107 Epetra_Map map = A.RowMatrixRowMap();
108 const Epetra_Comm & comm = map.Comm();
109 int numProc = comm.NumProc();
111 if (numProc==1 || !A.Map().DistributedGlobal())
114 int numRows = map.NumMyElements();
116 Epetra_Map allGidsMap((int_type) -1, numRows, (int_type) 0,comm);
118 typename Epetra_GIDTypeVector<int_type>::impl allGids(allGidsMap);
119 for (
int i=0; i<numRows; i++) allGids[i] = (int_type) map.GID64(i);
122 int numChunks = numProc;
123 int stripSize = allGids.GlobalLength64()/numChunks;
124 int remainder = allGids.GlobalLength64()%numChunks;
126 int curStripSize = 0;
127 typename Epetra_GIDTypeSerialDenseVector<int_type>::impl importGidList;
129 importGidList.Size(stripSize+1);
130 for (
int i=0; i<numChunks; i++) {
131 if (comm.MyPID()==0) {
132 curStripSize = stripSize;
133 if (i<remainder) curStripSize++;
134 for (
int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
135 curStart += curStripSize;
138 if (comm.MyPID()>0) assert(curStripSize==0);
139 Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
140 Epetra_Import gidImporter(importGidMap, allGidsMap);
141 typename Epetra_GIDTypeVector<int_type>::impl importGids(importGidMap);
142 if (importGids.Import(allGids, gidImporter, Insert)!=0) {EPETRA_CHK_ERR(-1); }
148 Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), map.IndexBase64(), comm);
149 Epetra_Import importer(importMap, map);
150 Epetra_CrsMatrix importA(Copy, importMap, 0);
151 if (importA.Import(A, importer, Insert)!=0) {EPETRA_CHK_ERR(-1); }
152 if (importA.FillComplete(A.OperatorDomainMap(), importMap)!=0) {EPETRA_CHK_ERR(-1);}
162 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES 163 if(A.RowMatrixRowMap().GlobalIndicesInt()) {
164 return RowMatrixToHandle<int>(handle, A);
168 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 169 if(A.RowMatrixRowMap().GlobalIndicesLongLong()) {
170 return RowMatrixToHandle<long long>(handle, A);
174 throw "EpetraExt::RowMatrixToHandle: GlobalIndices type unknown";
179 long long numRows_LL = A.NumGlobalRows64();
180 if(numRows_LL > std::numeric_limits<int>::max())
181 throw "EpetraExt::writeRowMatrix: numRows_LL > std::numeric_limits<int>::max()";
183 int numRows =
static_cast<int>(numRows_LL);
184 Epetra_Map rowMap = A.RowMatrixRowMap();
185 Epetra_Map colMap = A.RowMatrixColMap();
186 const Epetra_Comm & comm = rowMap.Comm();
187 long long ioffset = 1 - rowMap.IndexBase64();
188 long long joffset = 1 - colMap.IndexBase64();
189 if (comm.MyPID()!=0) {
190 if (A.NumMyRows()!=0) {EPETRA_CHK_ERR(-1);}
191 if (A.NumMyCols()!=0) {EPETRA_CHK_ERR(-1);}
194 if (numRows!=A.NumMyRows()) {EPETRA_CHK_ERR(-1);}
195 Epetra_SerialDenseVector values(A.MaxNumEntries());
196 Epetra_IntSerialDenseVector indices(A.MaxNumEntries());
197 for (
int i=0; i<numRows; i++) {
198 long long I = rowMap.GID64(i) + ioffset;
200 if (A.ExtractMyRowCopy(i, values.Length(), numEntries,
201 values.Values(), indices.Values())!=0) {EPETRA_CHK_ERR(-1);}
202 for (
int j=0; j<numEntries; j++) {
203 long long J = colMap.GID64(indices[j]) + joffset;
204 double val = values[j];
205 fprintf(handle,
"%lld %lld %22.16e\n", I, J, val);
int RowMatrixToHandle(FILE *handle, const Epetra_RowMatrix &A)
#define mm_set_coordinate(typecode)
#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 mm_write_mtx_crd_size(FILE *f, long long M, long long N, long long nz)
int RowMatrixToMatlabFile(const char *filename, const Epetra_RowMatrix &A)
Writes an Epetra_RowMatrix object to a file that is compatible with Matlab.
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.
#define mm_initialize_typecode(typecode)