41 #include "Epetra_ConfigDefs.h"
43 #include "Epetra_Comm.h"
44 #include "Epetra_Util.h"
45 #include "Epetra_BlockMap.h"
46 #include "Epetra_Map.h"
47 #include "Epetra_IntVector.h"
48 #include "Epetra_IntSerialDenseVector.h"
49 #include "Epetra_Import.h"
52 using namespace EpetraExt;
55 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
65 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
75 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
78 const int lineLength = 1025;
79 char line[lineLength];
80 char token[lineLength];
81 int M, N, numProc, MaxElementSize, MinElementSize, NumMyElements, IndexBase, NumGlobalElements, firstGid;
87 handle = fopen(filename,
"r");
92 if(fgets(line, lineLength, handle)==0)
return(-1);
93 if(sscanf(line,
"%s", token)==0)
return(-1);
94 if (!strcmp(token,
"%NumProc:")) inHeader =
false;
97 if(fgets(line, lineLength, handle)==0)
return(-1);
98 if(sscanf(line,
"%s %d", token, &numProc)==0)
return(-1);
100 if(fgets(line, lineLength, handle)==0)
return(-1);
101 if(fgets(line, lineLength, handle)==0)
return(-1);
102 if(sscanf(line,
"%s %d", token, &MaxElementSize)==0)
return(-1);
104 if(fgets(line, lineLength, handle)==0)
return(-1);
105 if(fgets(line, lineLength, handle)==0)
return(-1);
106 if(sscanf(line,
"%s %d", token, &MinElementSize)==0)
return(-1);
108 if(fgets(line, lineLength, handle)==0)
return(-1);
109 if(fgets(line, lineLength, handle)==0)
return(-1);
110 if(sscanf(line,
"%s %d", token, &IndexBase)==0)
return(-1);
112 if(fgets(line, lineLength, handle)==0)
return(-1);
113 if(fgets(line, lineLength, handle)==0)
return(-1);
114 if(sscanf(line,
"%s %d", token, &NumGlobalElements)==0)
return(-1);
119 if(fgets(line, lineLength, handle)==0)
return(-1);
121 for (
int i=0; i<comm.
MyPID(); i++) {
122 if(fgets(line, lineLength, handle)==0)
return(-1);
123 if(sscanf(line,
"%s %d", token, &NumMyElements)==0)
return(-1);
124 firstGid += NumMyElements;
127 if(fgets(line, lineLength, handle)==0)
return(-1);
128 if(sscanf(line,
"%s %d", token, &NumMyElements)==0)
return(-1);
130 for (
int i=comm.
MyPID()+1; i<numProc; i++) {
131 if(fgets(line, lineLength, handle)==0)
return(-1);
137 if(fgets(line, lineLength, handle)==0)
return(-1);
138 for (
int i=0; i<numProc; i++) {
139 if(fgets(line, lineLength, handle)==0)
return(-1);
142 NumMyElements = NumGlobalElements/comm.
NumProc();
143 firstGid = comm.
MyPID()*NumMyElements;
144 int remainder = NumGlobalElements%comm.
NumProc();
145 if (comm.
MyPID()<remainder) NumMyElements++;
146 int extra = remainder;
147 if (comm.
MyPID()<remainder) extra = comm.
MyPID();
150 if(fgets(line, lineLength, handle)==0)
return(-1);
151 if(sscanf(line,
"%d %d", &M, &N)==0)
return(-1);
153 bool doSizes = (N>1);
156 for (
int i=0; i<firstGid; i++) {
157 if(fgets(line, lineLength, handle)==0)
return(-1);
161 for (
int i=0; i<NumMyElements; i++) {
162 if(fgets(line, lineLength, handle)==0)
return(-1);
163 if(sscanf(line,
"%d %d", &v1[i], &v2[i])==0)
return(-1);
167 for (
int i=0; i<NumMyElements; i++) {
168 if(fgets(line, lineLength, handle)==0)
return(-1);
169 if(sscanf(line,
"%d", &v1[i])==0)
return(-1);
170 v2[i] = MinElementSize;
173 if (fclose(handle))
return(-1);
177 if (MinElementSize==1 && MaxElementSize==1)
185 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
188 const int lineLength = 1025;
189 char line[lineLength];
190 char token[lineLength];
191 int numProc, MaxElementSize, MinElementSize, NumMyElements;
192 long long M, N, IndexBase, NumGlobalElements, firstGid;
196 bool inHeader =
true;
198 handle = fopen(filename,
"r");
203 if(fgets(line, lineLength, handle)==0)
return(-1);
204 if(sscanf(line,
"%s", token)==0)
return(-1);
205 if (!strcmp(token,
"%NumProc:")) inHeader =
false;
208 if(fgets(line, lineLength, handle)==0)
return(-1);
209 if(sscanf(line,
"%s %d", token, &numProc)==0)
return(-1);
211 if(fgets(line, lineLength, handle)==0)
return(-1);
212 if(fgets(line, lineLength, handle)==0)
return(-1);
213 if(sscanf(line,
"%s %d", token, &MaxElementSize)==0)
return(-1);
215 if(fgets(line, lineLength, handle)==0)
return(-1);
216 if(fgets(line, lineLength, handle)==0)
return(-1);
217 if(sscanf(line,
"%s %d", token, &MinElementSize)==0)
return(-1);
219 if(fgets(line, lineLength, handle)==0)
return(-1);
220 if(fgets(line, lineLength, handle)==0)
return(-1);
221 if(sscanf(line,
"%s %lld", token, &IndexBase)==0)
return(-1);
223 if(fgets(line, lineLength, handle)==0)
return(-1);
224 if(fgets(line, lineLength, handle)==0)
return(-1);
225 if(sscanf(line,
"%s %lld", token, &NumGlobalElements)==0)
return(-1);
230 if(fgets(line, lineLength, handle)==0)
return(-1);
232 for (
int i=0; i<comm.
MyPID(); i++) {
233 if(fgets(line, lineLength, handle)==0)
return(-1);
234 if(sscanf(line,
"%s %d", token, &NumMyElements)==0)
return(-1);
235 firstGid += NumMyElements;
238 if(fgets(line, lineLength, handle)==0)
return(-1);
239 if(sscanf(line,
"%s %d", token, &NumMyElements)==0)
return(-1);
241 for (
int i=comm.
MyPID()+1; i<numProc; i++) {
242 if(fgets(line, lineLength, handle)==0)
return(-1);
248 if(fgets(line, lineLength, handle)==0)
return(-1);
249 for (
int i=0; i<numProc; i++) {
250 if(fgets(line, lineLength, handle)==0)
return(-1);
253 NumMyElements = (int) (NumGlobalElements/comm.
NumProc());
254 firstGid = ((
long long) comm.
MyPID())*NumMyElements;
255 int remainder = (int) (NumGlobalElements%comm.
NumProc());
256 if (comm.
MyPID()<remainder) NumMyElements++;
257 int extra = remainder;
258 if (comm.
MyPID()<remainder) extra = comm.
MyPID();
261 if(fgets(line, lineLength, handle)==0)
return(-1);
262 if(sscanf(line,
"%lld %lld", &M, &N)==0)
return(-1);
264 bool doSizes = (N>1);
267 for (
long long i=0; i<firstGid; i++) {
268 if(fgets(line, lineLength, handle)==0)
return(-1);
272 for (
int i=0; i<NumMyElements; i++) {
273 if(fgets(line, lineLength, handle)==0)
return(-1);
274 if(sscanf(line,
"%lld %d", &v1[i], &v2[i])==0)
return(-1);
278 for (
int i=0; i<NumMyElements; i++) {
279 if(fgets(line, lineLength, handle)==0)
return(-1);
280 if(sscanf(line,
"%lld", &v1[i])==0)
return(-1);
281 v2[i] = MinElementSize;
284 if (fclose(handle))
return(-1);
288 if (MinElementSize==1 && MaxElementSize==1)
296 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
301 FILE* infile = fopen(filename,
"r");
305 if (err != 0)
return(err);
312 int numrows, numcols;
314 if (err != 0)
return(err);
323 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
331 FILE* infile = fopen(filename,
"r");
332 if (infile == NULL) {
339 if (err != 0)
return(err);
346 int numrows, numcols, nnz;
348 if (err != 0)
return(err);
360 int num_map_cols = 0, insertPoint, foundOffset;
361 int allocLen = numcols;
362 int* map_cols =
new int[allocLen];
367 for(
int i=0; i<nnz; ++i) {
374 if (rowmap->
MyGID(I)) {
375 foundOffset = Epetra_Util_binary_search(J, map_cols, num_map_cols,
377 if (foundOffset < 0) {
378 Epetra_Util_insert(J, insertPoint, map_cols,
379 num_map_cols, allocLen);
387 colmap =
new Epetra_Map(-1, num_map_cols, map_cols, 0, comm);
398 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
406 FILE* infile = fopen(filename,
"r");
407 if (infile == NULL) {
414 if (err != 0)
return(err);
421 long long numrows, numcols, nnz;
423 if (err != 0)
return(err);
435 int num_map_cols = 0, insertPoint, foundOffset;
437 if(numcols > (
long long) std::numeric_limits<int>::max())
438 allocLen = std::numeric_limits<int>::max();
440 allocLen = (int) numcols;
442 long long* map_cols =
new long long[allocLen];
447 for(
int i=0; i<nnz; ++i) {
454 if (rowmap->
MyGID(I)) {
455 foundOffset = Epetra_Util_binary_search(J, map_cols, num_map_cols,
457 if (foundOffset < 0) {
458 Epetra_Util_insert(J, insertPoint, map_cols,
459 num_map_cols, allocLen);
467 colmap =
new Epetra_Map(-1LL, num_map_cols, map_cols, 0, comm);
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
int mm_read_mtx_array_size(FILE *f, int *M, int *N)
virtual void Barrier() const =0
int MatrixMarketFileToMap64(const char *filename, const Epetra_Comm &comm, Epetra_Map *&map)
virtual int MyPID() const =0
int mm_read_banner(FILE *f, MM_typecode *matcode)
#define mm_is_coordinate(typecode)
int MatrixMarketFileToMap(const char *filename, const Epetra_Comm &comm, Epetra_Map *&map)
Constructs an Epetra_BlockMap object from a Matrix Market format file.
int MatrixMarketFileToRowMap(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&rowmap)
bool MyGID(int GID_in) const
#define mm_is_real(typecode)
virtual int NumProc() const =0
int mm_read_mtx_crd_entry(FILE *f, int *I, int *J, double *real, double *imag, MM_typecode matcode)
#define mm_is_matrix(typecode)
int MatrixMarketFileToBlockMap64(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&map)
#define mm_is_general(typecode)
int MatrixMarketFileToBlockMap(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&map)
Constructs an Epetra_BlockMap object from a Matrix Market format file.
int MatrixMarketFileToBlockMaps(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&rowmap, Epetra_BlockMap *&colmap, Epetra_BlockMap *&rangemap, Epetra_BlockMap *&domainmap)
Constructs row,col,range and domain maps from a matrix-market matrix file.
int MatrixMarketFileToBlockMaps64(const char *filename, const Epetra_Comm &comm, Epetra_BlockMap *&rowmap, Epetra_BlockMap *&colmap, Epetra_BlockMap *&rangemap, Epetra_BlockMap *&domainmap)
Constructs row,col,range and domain maps from a matrix-market matrix file.