42 #include "Epetra_Comm.h"
43 #include "Epetra_CrsMatrix.h"
44 #include "Epetra_Map.h"
45 #include "Epetra_IntVector.h"
46 #include "Epetra_IntSerialDenseVector.h"
47 #include "Epetra_Import.h"
48 #include "Epetra_Time.h"
49 #include "Epetra_Util.h"
70 using namespace EpetraExt;
73 static void sort_three(
int* list,
int *parlista,
double *parlistb,
77 const Epetra_Comm & comm, Epetra_CrsMatrix * & A)
85 const Epetra_Comm & comm, Epetra_CrsMatrix * & A,
const bool transpose)
88 0, 0, 0, 0, transpose));
94 const Epetra_Comm & comm, Epetra_CrsMatrix * & A,
const bool transpose,
105 const Epetra_Map & rowMap,
const Epetra_Map& rangeMap,
106 const Epetra_Map& domainMap, Epetra_CrsMatrix * & A,
const bool transpose,
112 &rangeMap, &domainMap,
113 transpose, verbose));
119 const Epetra_Map & rowMap, Epetra_CrsMatrix * & A,
const bool transpose,
125 transpose, verbose));
131 const Epetra_Map & rowMap,
const Epetra_Map & colMap,
132 Epetra_CrsMatrix * & A,
const bool transpose,
137 &rowMap, &colMap, 0, 0,
138 transpose, verbose));
144 const Epetra_Map & rowMap,
const Epetra_Map & colMap,
145 const Epetra_Map& rangeMap,
const Epetra_Map& domainMap,
146 Epetra_CrsMatrix * & A,
const bool transpose,
152 &rangeMap, &domainMap,
153 transpose, verbose));
159 const Epetra_Comm & comm,
160 Epetra_CrsMatrix * & A,
161 const Epetra_Map * rowMap,
162 const Epetra_Map * colMap,
163 const Epetra_Map * rangeMap,
164 const Epetra_Map * domainMap,
165 const bool transpose,
169 const int chunk_read = 500000;
171 const int headerlineLength = 257;
172 const int lineLength = 81;
173 const int tokenLength = 35;
174 char line[headerlineLength];
175 char token1[tokenLength];
176 char token2[tokenLength];
177 char token3[tokenLength];
178 char token4[tokenLength];
179 char token5[tokenLength];
182 int me = comm.MyPID();
184 Epetra_Time timer(comm);
187 if ((domainMap!=0 && rangeMap==0) || (domainMap==0 && rangeMap!=0)) {
194 if (!domainMap->UniqueGIDs()) {EPETRA_CHK_ERR(-2);}
195 if (!rangeMap->UniqueGIDs()) {EPETRA_CHK_ERR(-2);}
201 if (!rowMap->UniqueGIDs()) {EPETRA_CHK_ERR(-2);}
207 if (verbose) cout <<
"Reading MatrixMarket file " << filename << endl;
208 handle = fopen(filename,
"r");
214 if(fgets(line, headerlineLength, handle)==0) {
215 if (handle!=0) fclose(handle);
218 if(sscanf(line,
"%s %s %s %s %s", token1,token2,token3,token4,token5)==0) {
219 if (handle!=0) fclose(handle);
222 if (strcmp(token1,
"%%MatrixMarket") ||
223 strcmp(token2,
"matrix") ||
224 strcmp(token3,
"coordinate") ||
225 strcmp(token4,
"real") ||
226 strcmp(token5,
"general")) {
227 if (handle!=0) fclose(handle);
233 if(fgets(line, headerlineLength, handle)==0) {
234 if (handle!=0) fclose(handle);
237 }
while (line[0] ==
'%');
240 if(sscanf(line,
"%d %d %d", &M, &N, &NZ)==0) {
241 if (handle!=0) fclose(handle);
245 comm.Broadcast(&M, 1, 0);
246 comm.Broadcast(&N, 1, 0);
247 comm.Broadcast(&NZ, 1, 0);
253 char *buffer =
new char[chunk_read*lineLength];
260 const int localblock = 100000;
261 int localsize = NZ / comm.NumProc() + localblock;
262 int *iv = (
int *) malloc(localsize *
sizeof(
int));
263 int *jv = (
int *) malloc(localsize *
sizeof(
int));
264 double *vv = (
double *) malloc(localsize *
sizeof(
double));
267 if (!iv || !jv || !vv)
271 bool allocatedHere=
false;
273 rowMap1 =
const_cast<Epetra_Map *
>(rowMap);
275 rowMap1 =
new Epetra_Map(M, 0, comm);
276 allocatedHere =
true;
278 int ioffset = rowMap1->IndexBase()-1;
279 int joffset = (colMap != 0 ? colMap->IndexBase()-1 : ioffset);
286 if (NZ-nread > chunk_read) nchunk = chunk_read;
287 else nchunk = NZ - nread;
292 for (
int i = 0; i < nchunk; i++) {
293 eof = fgets(&buffer[rlen],lineLength,handle);
295 fprintf(stderr,
"%s",
"Unexpected end of matrix file.");
298 rlen += strlen(&buffer[rlen]);
302 comm.Broadcast(&rlen, 1, 0);
303 comm.Broadcast(buffer, rlen, 0);
305 buffer[rlen++] =
'\0';
309 char *lineptr = buffer;
310 for (rlen = 0; rlen < nchunk; rlen++) {
311 char *next = strchr(lineptr,
'\n');
312 int I = atoi(strtok(lineptr,
" \t\n")) + ioffset;
313 int J = atoi(strtok(NULL,
" \t\n")) + joffset;
314 double V = atof(strtok(NULL,
" \t\n"));
322 if (rowMap1->MyGID(I)) {
324 if (lnz >= localsize) {
326 localsize += localblock;
327 iv = (
int *) realloc(iv, localsize *
sizeof(
int));
328 jv = (
int *) realloc(jv, localsize *
sizeof(
int));
329 vv = (
double *) realloc(vv, localsize *
sizeof(
double));
335 if (I < prevrow) rowmajor = 0;
341 if (nread / 1000000 > nmillion) {
343 if (verbose && me == 0) cout << nmillion <<
"M ";
352 if (verbose && me == 0) cout << endl <<
" Sorting local nonzeros" << endl;
361 if (verbose && me == 0) cout << endl <<
" Constructing the matrix" << endl;
362 int numRows = rowMap1->NumMyElements();
363 int *numNonzerosPerRow =
new int[numRows];
364 for (i = 0; i < numRows; i++) numNonzerosPerRow[i] = 0;
365 for (i = 0; i < lnz; i++)
366 numNonzerosPerRow[rowMap1->LID(iv[i])]++;
368 if (rowMap!=0 && colMap !=0)
369 A =
new Epetra_CrsMatrix(Copy, *rowMap, *colMap, numNonzerosPerRow);
370 else if (rowMap!=0) {
373 A =
new Epetra_CrsMatrix(Copy, *rowMap, numNonzerosPerRow,
true);
378 A =
new Epetra_CrsMatrix(Copy, *rowMap1, numNonzerosPerRow,
true);
380 A->SetTracebackMode(1);
384 int *gidList =
new int[numRows];
385 for (i = 0; i < numRows; i++) gidList[i] = rowMap1->GID(i);
386 Epetra_Util::Sort(
true,numRows,gidList,0,0,1,&numNonzerosPerRow);
390 if (verbose && me == 0) cout <<
" Inserting global values" << endl;
391 for (
int sum = 0, i = 0; i < numRows; i++) {
392 if (numNonzerosPerRow[i]) {
393 int ierr = A->InsertGlobalValues(iv[sum], numNonzerosPerRow[i],
395 if (ierr<0) EPETRA_CHK_ERR(ierr);
396 sum += numNonzerosPerRow[i];
400 delete [] numNonzerosPerRow;
405 if (verbose && me == 0) cout <<
" Completing matrix fill" << endl;
406 if (rangeMap != 0 && domainMap != 0) {
407 EPETRA_CHK_ERR(A->FillComplete(*domainMap, *rangeMap));
410 Epetra_Map newDomainMap(N,rowMap1->IndexBase(), comm);
411 EPETRA_CHK_ERR(A->FillComplete(newDomainMap, *rowMap1));
414 EPETRA_CHK_ERR(A->FillComplete());
417 if (allocatedHere)
delete rowMap1;
419 if (handle!=0) fclose(handle);
420 double dt = timer.ElapsedTime();
421 if (verbose && me == 0) cout <<
"File Read time (secs): " << dt << endl;
431 int *list,
int *parlista,
double *parlistb,
432 int start,
int end,
int *equal,
int *larger)
437 key = list ? list[(end+start)/2] : 1;
439 *equal = *larger = start;
440 for (i = start; i <=
end; i++)
443 parlista[i] = parlista[*larger];
444 parlista[(*larger)] = parlista[*equal];
445 parlista[(*equal)] = itmp;
447 parlistb[i] = parlistb[*larger];
448 parlistb[(*larger)] = parlistb[*equal];
449 parlistb[(*equal)] = dtmp;
451 list[i] = list[*larger];
452 list[(*larger)++] = list[*equal];
453 list[(*equal)++] = itmp;
455 else if (list[i] == key) {
457 parlista[i] = parlista[*larger];
458 parlista[(*larger)] = itmp;
460 parlistb[i] = parlistb[*larger];
461 parlistb[(*larger)] = dtmp;
462 list[i] = list[*larger];
463 list[(*larger)++] = key;
468 static void sort_three(
int* list,
int *parlista,
double *parlistb,
476 sort_three(list, parlista, parlistb, start, equal-1);
477 sort_three(list, parlista, parlistb, larger, end);
483 const Epetra_Comm & comm,
484 Epetra_CrsMatrix * & A)
486 const int lineLength = 1025;
487 char line[lineLength];
493 handle = fopen(filename,
"r");
497 int numGlobalRows = 0;
498 int numGlobalCols = 0;
499 while(fgets(line, lineLength, handle)!=0) {
500 if(sscanf(line,
"%d %d %lg\n", &I, &J, &V)==0) {
if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
501 if (I>numGlobalRows) numGlobalRows = I;
502 if (J>numGlobalCols) numGlobalCols = J;
505 if (handle!=0) fclose(handle);
506 Epetra_Map rangeMap(numGlobalRows, 0, comm);
507 Epetra_Map domainMap(numGlobalCols, 0, comm);
508 A =
new Epetra_CrsMatrix(Copy, rangeMap, 0);
511 const Epetra_Map & rowMap1 = A->RowMap();
515 handle = fopen(filename,
"r");
519 while (fgets(line, lineLength, handle)!=0) {
520 if(sscanf(line,
"%d %d %lg\n", &I, &J, &V)==0) {
if (handle!=0) fclose(handle); EPETRA_CHK_ERR(-1);}
522 if (rowMap1.MyGID(I)) {
523 int ierr = A->InsertGlobalValues(I, 1, &V, &J);
524 if (ierr<0) EPETRA_CHK_ERR(ierr);
528 EPETRA_CHK_ERR(A->FillComplete(domainMap, rangeMap));
530 if (handle!=0) fclose(handle);
535 int MyPID = comm.MyPID();
537 double filePID = (double)MyPID/(
double)100000;
538 std::ostringstream stream;
540 stream << std::setiosflags(std::ios::fixed) << std::setprecision(5) << filePID;
542 std::string fileName(filename);
543 fileName += stream.str().substr(1,7);
545 std::ifstream file(fileName.c_str());
548 std::getline(file, line);
550 std::istringstream istream(line);
555 Epetra_Map RowMap(-1, iupper-ilower+1, 0, comm);
556 Matrix =
new Epetra_CrsMatrix(Copy, RowMap, 0);
559 std::vector<int> indices;
560 std::vector<double> values;
562 std::getline(file, line);
563 std::istringstream lineStr(line);
569 if(currRow == -1) currRow = row;
572 counter = counter + 1;
573 indices.push_back(col);
574 values.push_back(val);
576 Matrix->InsertGlobalValues(currRow, counter, &values[0], &indices[0]);
582 indices.push_back(col);
583 values.push_back(val);
584 counter = counter + 1;
587 Matrix->InsertGlobalValues(currRow, counter, &values[0], &indices[0]);
588 Matrix->Comm().Barrier();
589 Matrix->FillComplete();
593 cout <<
"\nERROR:\nCouldn't open " << fileName <<
".\n";
int MatrixMarketFileToCrsMatrix(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&A)
int MatrixMarketFileToCrsMatrixHandle(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&A, const Epetra_Map *rowMap, const Epetra_Map *colMap, const Epetra_Map *rangeMap, const Epetra_Map *domainMap, const bool transpose, const bool verbose)
int MatlabFileToCrsMatrix(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&A)
Constructs an Epetra_CrsMatrix object from a Matlab format file, distributes rows evenly across proce...
int HypreFileToCrsMatrix(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&Matrix)
Constructs an Epetra_CrsMatrix object from a Hypre Matrix Print command, the row map is specified...
static void quickpart_list_inc_int(int *list, int *parlista, double *parlistb, int start, int end, int *equal, int *larger)
static void sort_three(int *list, int *parlista, double *parlistb, int start, int end)