77 using namespace EpetraExt;
80 template<
typename int_type>
81 static void sort_three(int_type* list, int_type *parlista,
double *parlistb,
85 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
98 0, 0, 0, 0, transpose));
109 transpose, verbose));
122 &rangeMap, &domainMap,
123 transpose, verbose));
135 transpose, verbose));
147 &rowMap, &colMap, 0, 0,
148 transpose, verbose));
162 &rangeMap, &domainMap,
163 transpose, verbose));
168 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
181 0, 0, 0, 0, transpose));
192 transpose, verbose));
205 &rangeMap, &domainMap,
206 transpose, verbose));
218 transpose, verbose));
230 &rowMap, &colMap, 0, 0,
231 transpose, verbose));
245 &rangeMap, &domainMap,
246 transpose, verbose));
252 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
260 const bool transpose,
264 const int chunk_read = 500000;
266 const int headerlineLength = 257;
267 const int lineLength = 81;
268 const int tokenLength = 35;
269 char line[headerlineLength];
270 char token1[tokenLength];
271 char token2[tokenLength];
272 char token3[tokenLength];
273 char token4[tokenLength];
274 char token5[tokenLength];
276 int me = comm.
MyPID();
281 if ((domainMap!=0 && rangeMap==0) || (domainMap==0 && rangeMap!=0)) {
302 if (verbose) std::cout <<
"Reading MatrixMarket file " << filename << std::endl;
303 handle = fopen(filename,
"r");
310 if( (rv != -1) && fgets(line, headerlineLength, handle)==0 ) {
311 if (handle!=0) fclose(handle);
314 if( (rv != -1) && sscanf(line,
"%s %s %s %s %s", token1,token2,token3,token4,token5)==0) {
315 if (handle!=0) fclose(handle);
318 if ( (rv != -1) && (strcmp(token1,
"%%MatrixMarket") ||
319 strcmp(token2,
"matrix") ||
320 strcmp(token3,
"coordinate") ||
321 strcmp(token4,
"real") ||
322 strcmp(token5,
"general")) ) {
323 if (handle!=0) fclose(handle);
330 if(fgets(line, headerlineLength, handle)==0) {
331 if (handle!=0) fclose(handle);
335 }
while (line[0] ==
'%');
339 if((rv != -1) && sscanf(line,
"%d %d %d", &M, &N, &NZ)==0) {
340 if (handle!=0) fclose(handle);
356 char *buffer =
new char[chunk_read*lineLength];
363 const int localblock = 100000;
364 int localsize = NZ / comm.
NumProc() + localblock;
365 int *iv = (
int *) malloc(localsize *
sizeof(
int));
366 int *jv = (
int *) malloc(localsize *
sizeof(
int));
367 double *vv = (
double *) malloc(localsize *
sizeof(
double));
370 if (!iv || !jv || !vv)
374 bool allocatedHere=
false;
379 allocatedHere =
true;
382 int joffset = (colMap != 0 ? colMap->
IndexBase()-1 : ioffset);
389 if (NZ-nread > chunk_read) nchunk = chunk_read;
390 else nchunk = NZ - nread;
395 for (
int i = 0; i < nchunk; i++) {
396 eof = fgets(&buffer[rlen],lineLength,handle);
398 fprintf(stderr,
"%s",
"Unexpected end of matrix file.");
401 rlen += strlen(&buffer[rlen]);
408 buffer[rlen++] =
'\0';
412 char *lineptr = buffer;
413 for (rlen = 0; rlen < nchunk; rlen++) {
414 char *next = strchr(lineptr,
'\n');
415 int I = atoi(strtok(lineptr,
" \t\n")) + ioffset;
416 int J = atoi(strtok(NULL,
" \t\n")) + joffset;
417 double V = atof(strtok(NULL,
" \t\n"));
425 if (rowMap1->
MyGID(I)) {
427 if (lnz >= localsize) {
429 localsize += localblock;
430 iv = (
int *) realloc(iv, localsize *
sizeof(
int));
431 jv = (
int *) realloc(jv, localsize *
sizeof(
int));
432 vv = (
double *) realloc(vv, localsize *
sizeof(
double));
438 if (I < prevrow) rowmajor = 0;
444 if (nread / 1000000 > nmillion) {
446 if (verbose && me == 0) std::cout << nmillion <<
"M ";
455 if (verbose && me == 0) std::cout << std::endl <<
" Sorting local nonzeros" << std::endl;
464 if (verbose && me == 0) std::cout << std::endl <<
" Constructing the matrix" << std::endl;
466 int *numNonzerosPerRow =
new int[numRows];
467 for (
int i = 0; i < numRows; i++) numNonzerosPerRow[i] = 0;
468 for (
int i = 0; i < lnz; i++)
469 numNonzerosPerRow[rowMap1->
LID(iv[i])]++;
471 if (rowMap!=0 && colMap !=0)
473 else if (rowMap!=0) {
483 A->SetTracebackMode(1);
487 int *gidList =
new int[numRows];
488 for (
int i = 0; i < numRows; i++) gidList[i] = rowMap1->
GID(i);
493 if (verbose && me == 0) std::cout <<
" Inserting global values" << std::endl;
496 for (
int sum = 0; i < numRows; i++) {
497 if (numNonzerosPerRow[i]) {
501 sum += numNonzerosPerRow[i];
506 delete [] numNonzerosPerRow;
511 if (verbose && me == 0) std::cout <<
" Completing matrix fill" << std::endl;
512 if (rangeMap != 0 && domainMap != 0) {
523 if (allocatedHere)
delete rowMap1;
525 if (handle!=0) fclose(handle);
527 if (verbose && me == 0) std::cout <<
"File Read time (secs): " << dt << std::endl;
532 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
540 const bool transpose,
544 const int chunk_read = 500000;
546 const int headerlineLength = 257;
547 const int lineLength = 81;
548 const int tokenLength = 35;
549 char line[headerlineLength];
550 char token1[tokenLength];
551 char token2[tokenLength];
552 char token3[tokenLength];
553 char token4[tokenLength];
554 char token5[tokenLength];
556 int me = comm.
MyPID();
561 if ((domainMap!=0 && rangeMap==0) || (domainMap==0 && rangeMap!=0)) {
581 if (verbose) std::cout <<
"Reading MatrixMarket file " << filename << std::endl;
582 handle = fopen(filename,
"r");
588 if(fgets(line, headerlineLength, handle)==0) {
589 if (handle!=0) fclose(handle);
592 if(sscanf(line,
"%s %s %s %s %s", token1,token2,token3,token4,token5)==0) {
593 if (handle!=0) fclose(handle);
596 if (strcmp(token1,
"%%MatrixMarket") ||
597 strcmp(token2,
"matrix") ||
598 strcmp(token3,
"coordinate") ||
599 strcmp(token4,
"real") ||
600 strcmp(token5,
"general")) {
601 if (handle!=0) fclose(handle);
607 if(fgets(line, headerlineLength, handle)==0) {
608 if (handle!=0) fclose(handle);
611 }
while (line[0] ==
'%');
614 if(sscanf(line,
"%lld %lld %lld", &M, &N, &NZ)==0) {
615 if (handle!=0) fclose(handle);
627 char *buffer =
new char[chunk_read*lineLength];
634 const int localblock = 100000;
635 int localsize = (int) (NZ / comm.
NumProc()) + localblock;
636 long long *iv = (
long long *) malloc(localsize *
sizeof(
long long));
637 long long *jv = (
long long *) malloc(localsize *
sizeof(
long long));
638 double *vv = (
double *) malloc(localsize *
sizeof(
double));
641 if (!iv || !jv || !vv)
645 bool allocatedHere=
false;
650 allocatedHere =
true;
653 long long joffset = (colMap != 0 ? colMap->
IndexBase64()-1 : ioffset);
656 long long prevrow = -1;
660 if (NZ-nread > chunk_read) nchunk = chunk_read;
661 else nchunk = NZ - nread;
666 for (
int i = 0; i < nchunk; i++) {
667 eof = fgets(&buffer[rlen],lineLength,handle);
669 fprintf(stderr,
"%s",
"Unexpected end of matrix file.");
672 rlen += strlen(&buffer[rlen]);
679 buffer[rlen++] =
'\0';
683 char *lineptr = buffer;
684 for (rlen = 0; rlen < nchunk; rlen++) {
685 char *next = strchr(lineptr,
'\n');
688 #if defined(_MSC_VER)
689 long long I = _strtoi64(strtok(lineptr,
" \t\n"), &endp, base) + ioffset;
690 long long J = _strtoi64(strtok(NULL,
" \t\n"), &endp, base) + joffset;
694 std::istringstream ssI(strtok(lineptr,
" \t\n"));
695 ssI >> I; I += ioffset;
696 std::istringstream ssJ(strtok(NULL,
" \t\n"));
697 ssJ >> J; J += joffset;
699 long long I = strtoll(strtok(lineptr,
" \t\n"), &endp, base) + ioffset;
700 long long J = strtoll(strtok(NULL,
" \t\n"), &endp, base) + joffset;
703 double V = atof(strtok(NULL,
" \t\n"));
711 if (rowMap1->
MyGID(I)) {
713 if (lnz >= localsize) {
715 localsize += localblock;
716 iv = (
long long *) realloc(iv, localsize *
sizeof(
long long));
717 jv = (
long long *) realloc(jv, localsize *
sizeof(
long long));
718 vv = (
double *) realloc(vv, localsize *
sizeof(
double));
724 if (I < prevrow) rowmajor = 0;
730 if (nread / 1000000 > nmillion) {
732 if (verbose && me == 0) std::cout << nmillion <<
"M ";
741 if (verbose && me == 0) std::cout << std::endl <<
" Sorting local nonzeros" << std::endl;
750 if (verbose && me == 0) std::cout << std::endl <<
" Constructing the matrix" << std::endl;
752 int *numNonzerosPerRow =
new int[numRows];
753 for (
int i = 0; i < numRows; i++) numNonzerosPerRow[i] = 0;
754 for (
int i = 0; i < lnz; i++)
755 numNonzerosPerRow[rowMap1->
LID(iv[i])]++;
757 if (rowMap!=0 && colMap !=0)
759 else if (rowMap!=0) {
769 A->SetTracebackMode(1);
773 long long *gidList =
new long long[numRows];
774 for (
int i = 0; i < numRows; i++) gidList[i] = rowMap1->
GID64(i);
779 if (verbose && me == 0) std::cout <<
" Inserting global values" << std::endl;
782 for (
int sum = 0; i < numRows; i++) {
783 if (numNonzerosPerRow[i]) {
787 sum += numNonzerosPerRow[i];
792 delete [] numNonzerosPerRow;
797 if (verbose && me == 0) std::cout <<
" Completing matrix fill" << std::endl;
798 if (rangeMap != 0 && domainMap != 0) {
809 if (allocatedHere)
delete rowMap1;
811 if (handle!=0) fclose(handle);
813 if (verbose && me == 0) std::cout <<
"File Read time (secs): " << dt << std::endl;
823 template<
typename int_type>
825 int_type *list, int_type *parlista,
double *parlistb,
826 int start,
int end,
int *equal,
int *larger)
832 key = list ? list[(end+start)/2] : 1;
834 *equal = *larger = start;
835 for (i = start; i <=
end; i++)
838 parlista[i] = parlista[*larger];
839 parlista[(*larger)] = parlista[*equal];
840 parlista[(*equal)] = itmp;
842 parlistb[i] = parlistb[*larger];
843 parlistb[(*larger)] = parlistb[*equal];
844 parlistb[(*equal)] = dtmp;
846 list[i] = list[*larger];
847 list[(*larger)++] = list[*equal];
848 list[(*equal)++] = itmp;
850 else if (list[i] == key) {
852 parlista[i] = parlista[*larger];
853 parlista[(*larger)] = itmp;
855 parlistb[i] = parlistb[*larger];
856 parlistb[(*larger)] = dtmp;
857 list[i] = list[*larger];
858 list[(*larger)++] = key;
863 template<
typename int_type>
864 static void sort_three(int_type* list, int_type *parlista,
double *parlistb,
872 sort_three(list, parlista, parlistb, start, equal-1);
873 sort_three(list, parlista, parlistb, larger, end);
878 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
883 const int lineLength = 1025;
884 char line[lineLength];
890 handle = fopen(filename,
"r");
894 int numGlobalRows = 0;
895 int numGlobalCols = 0;
896 while(fgets(line, lineLength, handle)!=0) {
897 if(sscanf(line,
"%d %d %lg\n", &I, &J, &V)==0) {
if (handle!=0) fclose(handle);
EPETRA_CHK_ERR(-1);}
898 if (I>numGlobalRows) numGlobalRows = I;
899 if (J>numGlobalCols) numGlobalCols = J;
902 if (handle!=0) fclose(handle);
912 handle = fopen(filename,
"r");
916 while (fgets(line, lineLength, handle)!=0) {
917 if(sscanf(line,
"%d %d %lg\n", &I, &J, &V)==0) {
if (handle!=0) fclose(handle);
EPETRA_CHK_ERR(-1);}
919 if (rowMap1.
MyGID(I)) {
927 if (handle!=0) fclose(handle);
932 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
937 const int lineLength = 1025;
938 char line[lineLength];
944 handle = fopen(filename,
"r");
948 long long numGlobalRows = 0;
949 long long numGlobalCols = 0;
950 while(fgets(line, lineLength, handle)!=0) {
951 if(sscanf(line,
"%lld %lld %lg\n", &I, &J, &V)==0) {
if (handle!=0) fclose(handle);
EPETRA_CHK_ERR(-1);}
952 if (I>numGlobalRows) numGlobalRows = I;
953 if (J>numGlobalCols) numGlobalCols = J;
956 if (handle!=0) fclose(handle);
966 handle = fopen(filename,
"r");
970 while (fgets(line, lineLength, handle)!=0) {
971 if(sscanf(line,
"%lld %lld %lg\n", &I, &J, &V)==0) {
if (handle!=0) fclose(handle);
EPETRA_CHK_ERR(-1);}
973 if (rowMap1.
MyGID(I)) {
981 if (handle!=0) fclose(handle);
986 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
988 int MyPID = comm.
MyPID();
990 double filePID = (double)MyPID/(
double)100000;
991 std::ostringstream stream;
993 stream << std::setiosflags(std::ios::fixed) << std::setprecision(5) << filePID;
995 std::string fileName(filename);
996 fileName += stream.str().substr(1,7);
998 std::ifstream file(fileName.c_str());
1001 std::getline(file, line);
1003 std::istringstream istream(line);
1008 Epetra_Map RowMap(-1, iupper-ilower+1, 0, comm);
1012 std::vector<int> indices;
1013 std::vector<double> values;
1015 std::getline(file, line);
1016 std::istringstream lineStr(line);
1022 if(currRow == -1) currRow = row;
1025 counter = counter + 1;
1026 indices.push_back(col);
1027 values.push_back(val);
1035 indices.push_back(col);
1036 values.push_back(val);
1037 counter = counter + 1;
1046 std::cout <<
"\nERROR:\nCouldn't open " << fileName <<
".\n";
1052 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
1054 int MyPID = comm.
MyPID();
1056 double filePID = (double)MyPID/(
double)100000;
1057 std::ostringstream stream;
1059 stream << std::setiosflags(std::ios::fixed) << std::setprecision(5) << filePID;
1061 std::string fileName(filename);
1062 fileName += stream.str().substr(1,7);
1064 std::ifstream file(fileName.c_str());
1067 std::getline(file, line);
1069 std::istringstream istream(line);
1074 Epetra_Map RowMap(-1LL, iupper-ilower+1, 0LL, comm);
1076 long long currRow = -1;
1078 std::vector<long long> indices;
1079 std::vector<double> values;
1081 std::getline(file, line);
1082 std::istringstream lineStr(line);
1088 if(currRow == -1) currRow = row;
1091 counter = counter + 1;
1092 indices.push_back(col);
1093 values.push_back(val);
1101 indices.push_back(col);
1102 values.push_back(val);
1103 counter = counter + 1;
1112 std::cout <<
"\nERROR:\nCouldn't open " << fileName <<
".\n";
long long IndexBase64() const
double ElapsedTime(void) const
virtual int InsertGlobalValues(int GlobalRow, int NumEntries, const double *Values, const int *Indices)
#define EPETRA_CHK_ERR(a)
int MatrixMarketFileToCrsMatrix64(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&A)
virtual void Barrier() const =0
int HypreFileToCrsMatrix64(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&Matrix)
virtual int MyPID() const =0
int FillComplete(bool OptimizeDataStorage=true)
static void quickpart_list_inc_int(int_type *list, int_type *parlista, double *parlistb, int start, int end, int *equal, int *larger)
const Epetra_Map & RowMap() const
int NumMyElements() const
long long GID64(int LID) const
int MatrixMarketFileToCrsMatrix(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&A)
int MatlabFileToCrsMatrix64(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)
bool MyGID(int GID_in) const
virtual int Broadcast(double *MyVals, int Count, int Root) const =0
const Epetra_Comm & Comm() const
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...
virtual int NumProc() const =0
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 Sort(bool SortAscending, int NumKeys, T *Keys, int NumDoubleCompanions, double **DoubleCompanions, int NumIntCompanions, int **IntCompanions, int NumLongLongCompanions, long long **LongLongCompanions)
int MatrixMarketFileToCrsMatrixHandle64(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)
const Epetra_Comm & Comm() const
static void sort_three(int_type *list, int_type *parlista, double *parlistb, int start, int end)