int CopyRowMatrix(mxArray* matlabA, const Epetra_RowMatrix& A) { int valueCount = 0; //int* valueCount = &temp; Epetra_Map map = A.RowMatrixRowMap(); const Epetra_Comm & comm = map.Comm(); int numProc = comm.NumProc(); if (numProc==1) DoCopyRowMatrix(matlabA, valueCount, A); else { int numRows = map.NumMyElements(); //cout << "creating allGidsMap\n"; Epetra_Map allGidsMap(-1, numRows, 0,comm); //cout << "done creating allGidsMap\n"; Epetra_IntVector allGids(allGidsMap); for (int i=0; i<numRows; i++) allGids[i] = map.GID(i); // Now construct a RowMatrix on PE 0 by strip-mining the rows of the input matrix A. int numChunks = numProc; int stripSize = allGids.GlobalLength()/numChunks; int remainder = allGids.GlobalLength()%numChunks; int curStart = 0; int curStripSize = 0; Epetra_IntSerialDenseVector importGidList; int numImportGids = 0; if (comm.MyPID()==0) importGidList.Size(stripSize+1); // Set size of vector to max needed for (int i=0; i<numChunks; i++) { if (comm.MyPID()==0) { // Only PE 0 does this part curStripSize = stripSize; if (i<remainder) curStripSize++; // handle leftovers for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart; curStart += curStripSize; } // The following import map will be non-trivial only on PE 0. //cout << "creating importGidMap\n"; Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm); //cout << "done creating importGidMap\n"; Epetra_Import gidImporter(importGidMap, allGidsMap); Epetra_IntVector importGids(importGidMap); if (importGids.Import(allGids, gidImporter, Insert)) return(-1); // importGids now has a list of GIDs for the current strip of matrix rows. // Use these values to build another importer that will get rows of the matrix. // The following import map will be non-trivial only on PE 0. //cout << "creating importMap\n"; //cout << "A.RowMatrixRowMap().MinAllGID: " << A.RowMatrixRowMap().MinAllGID() << "\n"; Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), A.RowMatrixRowMap().MinAllGID(), comm); //cout << "done creating importMap\n"; Epetra_Import importer(importMap, map); Epetra_CrsMatrix importA(Copy, importMap, 0); if (importA.Import(A, importer, Insert)) return(-1); if (importA.FillComplete()) return(-1); // Finally we are ready to write this strip of the matrix to ostream if (DoCopyRowMatrix(matlabA, valueCount, importA)) return(-1); } } if (A.RowMatrixRowMap().Comm().MyPID() == 0) { // set max cap int* matlabAcolumnIndicesPtr = mxGetJc(matlabA); matlabAcolumnIndicesPtr[A.NumGlobalRows()] = valueCount; } return(0); }
int CopyMultiVector(double** matlabApr, const Epetra_MultiVector& A) { Epetra_BlockMap bmap = A.Map(); const Epetra_Comm & comm = bmap.Comm(); int numProc = comm.NumProc(); if (numProc==1) DoCopyMultiVector(matlabApr, A); else { // In the case of more than one column in the multivector, and writing to MatrixMarket // format, we call this function recursively, passing each vector of the multivector // individually so that we can get all of it written to file before going on to the next // multivector if (A.NumVectors() > 1) { for (int i=0; i < A.NumVectors(); i++) if (CopyMultiVector(matlabApr, *(A(i)))) return(-1); return(0); } Epetra_Map map(-1, bmap.NumMyPoints(), 0, comm); // Create a veiw of this multivector using a map (instead of block map) Epetra_MultiVector A1(View, map, A.Pointers(), A.NumVectors()); int numRows = map.NumMyElements(); Epetra_Map allGidsMap(-1, numRows, 0,comm); Epetra_IntVector allGids(allGidsMap); for (int i=0; i<numRows; i++) allGids[i] = map.GID(i); // Now construct a MultiVector on PE 0 by strip-mining the rows of the input matrix A. int numChunks = numProc; int stripSize = allGids.GlobalLength()/numChunks; int remainder = allGids.GlobalLength()%numChunks; int curStart = 0; int curStripSize = 0; Epetra_IntSerialDenseVector importGidList; int numImportGids = 0; if (comm.MyPID()==0) importGidList.Size(stripSize+1); // Set size of vector to max needed for (int i=0; i<numChunks; i++) { if (comm.MyPID()==0) { // Only PE 0 does this part curStripSize = stripSize; if (i<remainder) curStripSize++; // handle leftovers for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart; curStart += curStripSize; } // The following import map will be non-trivial only on PE 0. Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm); Epetra_Import gidImporter(importGidMap, allGidsMap); Epetra_IntVector importGids(importGidMap); if (importGids.Import(allGids, gidImporter, Insert)) return(-1); // importGids now has a list of GIDs for the current strip of matrix rows. // Use these values to build another importer that will get rows of the matrix. // The following import map will be non-trivial only on PE 0. Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), 0, comm); Epetra_Import importer(importMap, map); Epetra_MultiVector importA(importMap, A1.NumVectors()); if (importA.Import(A1, importer, Insert)) return(-1); // Finally we are ready to write this strip of the matrix to ostream if (DoCopyMultiVector(matlabApr, importA)) return(-1); } } return(0); }
int BlockMapToHandle(FILE * handle, const Epetra_BlockMap & map) { const Epetra_Comm & comm = map.Comm(); int numProc = comm.NumProc(); bool doSizes = !map.ConstantElementSize(); if (numProc==1) { int * myElements = map.MyGlobalElements(); int * elementSizeList = 0; if (doSizes) elementSizeList = map.ElementSizeList(); return(writeBlockMap(handle, map.NumGlobalElements(), myElements, elementSizeList, doSizes)); } int numRows = map.NumMyElements(); Epetra_Map allGidsMap(-1, numRows, 0,comm); Epetra_IntVector allGids(allGidsMap); for (int i=0; i<numRows; i++) allGids[i] = map.GID(i); Epetra_IntVector allSizes(allGidsMap); for (int i=0; i<numRows; i++) allSizes[i] = map.ElementSize(i); // Now construct a Map on PE 0 by strip-mining the rows of the input matrix map. int numChunks = numProc; int stripSize = allGids.GlobalLength()/numChunks; int remainder = allGids.GlobalLength()%numChunks; int curStart = 0; int curStripSize = 0; Epetra_IntSerialDenseVector importGidList; Epetra_IntSerialDenseVector importSizeList; if (comm.MyPID()==0) { importGidList.Size(stripSize+1); // Set size of vector to max needed if (doSizes) importSizeList.Size(stripSize+1); // Set size of vector to max needed } for (int i=0; i<numChunks; i++) { if (comm.MyPID()==0) { // Only PE 0 does this part curStripSize = stripSize; if (i<remainder) curStripSize++; // handle leftovers for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart; curStart += curStripSize; } // The following import map will be non-trivial only on PE 0. Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm); Epetra_Import gidImporter(importGidMap, allGidsMap); Epetra_IntVector importGids(importGidMap); if (importGids.Import(allGids, gidImporter, Insert)) return(-1); Epetra_IntVector importSizes(importGidMap); if (doSizes) if (importSizes.Import(allSizes, gidImporter, Insert)) return(-1); // importGids (and importSizes, if non-trivial block map) // now have a list of GIDs (and sizes, respectively) for the current strip of map. int * myElements = importGids.Values(); int * elementSizeList = 0; if (doSizes) elementSizeList = importSizes.Values(); // Finally we are ready to write this strip of the map to file writeBlockMap(handle, importGids.MyLength(), myElements, elementSizeList, doSizes); } return(0); }
// FIXME long long Epetra_Map Epetra_Util::Create_Root_Map(const Epetra_Map& usermap, int root) { int numProc = usermap.Comm().NumProc(); if (numProc==1) { Epetra_Map newmap(usermap); return(newmap); } const Epetra_Comm & comm = usermap.Comm(); bool isRoot = usermap.Comm().MyPID()==root; //if usermap is already completely owned by root then we'll just return a copy of it. int quickreturn = 0; int globalquickreturn = 0; if (isRoot) { if (usermap.NumMyElements()==usermap.NumGlobalElements64()) quickreturn = 1; } else { if (usermap.NumMyElements()==0) quickreturn = 1; } usermap.Comm().MinAll(&quickreturn, &globalquickreturn, 1); if (globalquickreturn==1) { Epetra_Map newmap(usermap); return(newmap); } // Linear map: Simple case, just put all GIDs linearly on root processor if (usermap.LinearMap() && root!=-1) { int numMyElements = 0; if (isRoot) numMyElements = usermap.MaxAllGID64()+1; // FIXME long long Epetra_Map newmap(-1, numMyElements, usermap.IndexBase(), comm); return(newmap); } if (!usermap.UniqueGIDs()) throw usermap.ReportError("usermap must have unique GIDs",-1); // General map // Build IntVector of the GIDs, then ship them to root processor int numMyElements = usermap.NumMyElements(); Epetra_Map allGidsMap(-1, numMyElements, 0, comm); Epetra_IntVector allGids(allGidsMap); for (int i=0; i<numMyElements; i++) allGids[i] = usermap.GID64(i); int numGlobalElements = usermap.NumGlobalElements64(); if (root!=-1) { int n1 = 0; if (isRoot) n1 = numGlobalElements; Epetra_Map allGidsOnRootMap(-1, n1, 0, comm); Epetra_Import importer(allGidsOnRootMap, allGidsMap); Epetra_IntVector allGidsOnRoot(allGidsOnRootMap); allGidsOnRoot.Import(allGids, importer, Insert); Epetra_Map rootMap(-1, allGidsOnRoot.MyLength(), allGidsOnRoot.Values(), usermap.IndexBase(), comm); return(rootMap); } else { int n1 = numGlobalElements; Epetra_LocalMap allGidsOnRootMap(n1, 0, comm); Epetra_Import importer(allGidsOnRootMap, allGidsMap); Epetra_IntVector allGidsOnRoot(allGidsOnRootMap); allGidsOnRoot.Import(allGids, importer, Insert); Epetra_Map rootMap(-1, allGidsOnRoot.MyLength(), allGidsOnRoot.Values(), usermap.IndexBase(), comm); return(rootMap); } }