00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "EpetraExt_PutRowMatrix.h"
00030 #include "Epetra_Comm.h"
00031 #include "Epetra_Map.h"
00032 #include "Epetra_Vector.h"
00033 #include "Epetra_IntVector.h"
00034 #include "Epetra_SerialDenseVector.h"
00035 #include "Epetra_IntSerialDenseVector.h"
00036 #include "Epetra_Import.h"
00037 #include "Epetra_RowMatrix.h"
00038 #include "Epetra_CrsMatrix.h"
00039
00040 using namespace Matlab;
00041 namespace Matlab {
00042
00043 int CopyRowMatrix(mxArray* matlabA, const Epetra_RowMatrix& A) {
00044 int valueCount = 0;
00045
00046
00047 Epetra_Map map = A.RowMatrixRowMap();
00048 const Epetra_Comm & comm = map.Comm();
00049 int numProc = comm.NumProc();
00050
00051 if (numProc==1)
00052 DoCopyRowMatrix(matlabA, valueCount, A);
00053 else {
00054 int numRows = map.NumMyElements();
00055
00056
00057 Epetra_Map allGidsMap(-1, numRows, 0,comm);
00058
00059
00060 Epetra_IntVector allGids(allGidsMap);
00061 for (int i=0; i<numRows; i++) allGids[i] = map.GID(i);
00062
00063
00064 int numChunks = numProc;
00065 int stripSize = allGids.GlobalLength()/numChunks;
00066 int remainder = allGids.GlobalLength()%numChunks;
00067 int curStart = 0;
00068 int curStripSize = 0;
00069 Epetra_IntSerialDenseVector importGidList;
00070 int numImportGids = 0;
00071 if (comm.MyPID()==0)
00072 importGidList.Size(stripSize+1);
00073 for (int i=0; i<numChunks; i++) {
00074 if (comm.MyPID()==0) {
00075 curStripSize = stripSize;
00076 if (i<remainder) curStripSize++;
00077 for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
00078 curStart += curStripSize;
00079 }
00080
00081
00082 Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
00083
00084 Epetra_Import gidImporter(importGidMap, allGidsMap);
00085 Epetra_IntVector importGids(importGidMap);
00086 if (importGids.Import(allGids, gidImporter, Insert)) return(-1);
00087
00088
00089
00090
00091
00092
00093
00094 Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), A.RowMatrixRowMap().MinAllGID(), comm);
00095
00096 Epetra_Import importer(importMap, map);
00097 Epetra_CrsMatrix importA(Copy, importMap, 0);
00098 if (importA.Import(A, importer, Insert)) return(-1);
00099 if (importA.FillComplete()) return(-1);
00100
00101
00102 if (DoCopyRowMatrix(matlabA, valueCount, importA)) return(-1);
00103 }
00104 }
00105
00106 if (A.RowMatrixRowMap().Comm().MyPID() == 0) {
00107
00108 int* matlabAcolumnIndicesPtr = mxGetJc(matlabA);
00109 matlabAcolumnIndicesPtr[A.NumGlobalRows()] = valueCount;
00110 }
00111
00112 return(0);
00113 }
00114
00115 int DoCopyRowMatrix(mxArray* matlabA, int& valueCount, const Epetra_RowMatrix& A) {
00116
00117 int ierr = 0;
00118 int numRows = A.NumGlobalRows();
00119
00120 Epetra_Map rowMap = A.RowMatrixRowMap();
00121 Epetra_Map colMap = A.RowMatrixColMap();
00122 int minAllGID = rowMap.MinAllGID();
00123
00124 const Epetra_Comm & comm = rowMap.Comm();
00125
00126 if (comm.MyPID()!=0) {
00127 if (A.NumMyRows()!=0) ierr = -1;
00128 if (A.NumMyCols()!=0) ierr = -1;
00129 }
00130 else {
00131
00132 double* matlabAvaluesPtr = mxGetPr(matlabA);
00133 int* matlabAcolumnIndicesPtr = mxGetJc(matlabA);
00134 int* matlabArowIndicesPtr = mxGetIr(matlabA);
00135
00136
00137 matlabAvaluesPtr += valueCount;
00138 matlabArowIndicesPtr += valueCount;
00139
00140 if (numRows!=A.NumMyRows()) ierr = -1;
00141 Epetra_SerialDenseVector values(A.MaxNumEntries());
00142 Epetra_IntSerialDenseVector indices(A.MaxNumEntries());
00143
00144 for (int i=0; i<numRows; i++) {
00145
00146 int I = rowMap.GID(i);
00147 int numEntries = 0;
00148 if (A.ExtractMyRowCopy(i, values.Length(), numEntries,
00149 values.Values(), indices.Values())) return(-1);
00150 matlabAcolumnIndicesPtr[I - minAllGID] = valueCount;
00151 double* serialValuesPtr = values.Values();
00152 for (int j=0; j<numEntries; j++) {
00153 int J = colMap.GID(indices[j]);
00154 *matlabAvaluesPtr = *serialValuesPtr++;
00155 *matlabArowIndicesPtr = J;
00156
00157 matlabAvaluesPtr++;
00158 matlabArowIndicesPtr++;
00159 valueCount++;
00160 }
00161 }
00162
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 int ierrGlobal;
00182 comm.MinAll(&ierr, &ierrGlobal, 1);
00183 return(ierrGlobal);
00184 }
00185
00186 }