44 #include "Epetra_ConfigDefs.h" 48 #include "Epetra_MpiComm.h" 50 #include "Epetra_SerialComm.h" 53 #include "Epetra_Comm.h" 54 #include "Epetra_Map.h" 55 #include "Epetra_BlockMap.h" 56 #include "Epetra_MultiVector.h" 57 #include "Epetra_Vector.h" 58 #include "Epetra_SerialDenseMatrix.h" 59 #include "Epetra_SerialDenseVector.h" 60 #include "Epetra_IntSerialDenseMatrix.h" 61 #include "Epetra_IntSerialDenseVector.h" 62 #include "Epetra_DataAccess.h" 63 #include "Epetra_CrsMatrix.h" 67 int main(
int argc,
char *argv[]) {
71 MPI_Init(&argc,&argv);
72 Epetra_MpiComm comm (MPI_COMM_WORLD);
74 Epetra_SerialComm comm;
77 int MyPID = comm.MyPID();
79 bool verbose = (0 == MyPID);
80 bool reportErrors = (0 == MyPID);
82 if (verbose) cout <<
"going to startup a matlab process...\n";
84 if (verbose) cout <<
"matlab started\n";
88 int numGlobalElements = M * comm.NumProc();
90 int numMyEntries = M *
N;
91 double* A =
new double[numMyEntries];
93 int startValue = numMyEntries * MyPID;
95 for(
int col=0; col <
N; col++) {
96 for(
int row=0; row < M; row++) {
97 *Aptr++ = startValue++;
102 int* intA =
new int[numMyEntries];
104 int intStartValue = numMyEntries * MyPID;
105 for(
int i=0; i < M*
N; i++) {
106 *intAptr++ = intStartValue++;
110 Epetra_Map map (numGlobalElements, 0, comm);
114 if (verbose) cout <<
" constructing CrsMatrix...\n";
115 Epetra_CrsMatrix crsMatrix (Copy, map,
N);
116 int* indices =
new int[
N];
117 for (
int col=0; col <
N; col++) {
121 double value = startValue;
122 double* values =
new double[numMyEntries];
123 int minMyGID = map.MinMyGID();
124 for (
int row=0; row < M; row++) {
125 for (
int col=0; col <
N; col++) {
126 values[col] = value++;
129 crsMatrix.InsertGlobalValues(minMyGID + row,
N, values, indices);
132 crsMatrix.FillComplete();
133 if (verbose) cout <<
" CrsMatrix constructed\n";
134 if (verbose) cout <<
" putting CrsMatrix into Matlab as CRSM\n";
137 if (reportErrors) cout <<
"There was an error in engine.PutRowMatrix(crsMatrix, \"CRSM\", false): " << ierr << endl;
142 if (verbose) cout <<
" putting Map into Matlab as MAP\n";
145 if (reportErrors) cout <<
"There was an error in engine.PutBlockMap(map, \"MAP\", false);: " << ierr << endl;
150 if (verbose) cout <<
" constructing MultiVector...\n";
151 Epetra_MultiVector multiVector (Copy, map, A, M,
N);
152 if (verbose) cout <<
" MultiVector constructed\n";
153 if (verbose) cout <<
" putting MultiVector into Matlab as MV\n";
156 if (reportErrors) cout <<
"There was an error in engine.PutMultiVector(multiVector, \"MV\"): " << ierr << endl;
161 if (verbose) cout <<
" constructing a SerialDenseMatrix...\n";
162 Epetra_SerialDenseMatrix sdMatrix (Copy, A, M, M,
N);
163 if (verbose) cout <<
" SerialDenseMatrix constructed\n";
164 if (verbose) cout <<
" putting SerialDenseMatrix from PE0 into Matlab as SDM_PE0\n";
168 if (reportErrors) cout <<
"There was an error in engine.PutSerialDenseMatrix(sdMatrix, \"SDM_PE0\"): " << ierr << endl;
170 if (comm.NumProc() > 1) {
171 if (verbose) cout <<
" putting SerialDenseMatrix from PE1 into Matlab as SDM_PE1\n";
175 if (reportErrors) cout <<
"There was an error in engine.PutSerialDenseMatrix(sdMatrix, \"SDM_PE1\", 1): " << ierr << endl;
182 if (verbose) cout <<
" constructing a SerialDenseVector...\n";
183 Epetra_SerialDenseVector sdVector (Copy, A, M);
184 if (verbose) cout <<
" SerialDenseVector constructed\n";
186 if (verbose) cout <<
" putting SerialDenseVector from PE0 into Matlab as SDV_PE0\n";
189 if (reportErrors) cout <<
"There was an error in engine.PutSerialDenseMatrix(sdVector, \"SDV_PE0\"): " << ierr << endl;
191 if (comm.NumProc() > 1) {
192 if (verbose) cout <<
" putting SerialDenseVector from PE1 into Matlab as SDV_PE1\n";
196 if (reportErrors) cout <<
"There was an error in engine.PutSerialDenseMatrix(sdMatrix, \"SDV_PE1\", 1): " << ierr << endl;
202 if (verbose) cout <<
" constructing a IntSerialDenseMatrix...\n";
203 Epetra_IntSerialDenseMatrix isdMatrix (Copy, intA, M, M,
N);
204 if (verbose) cout <<
" IntSerialDenseMatrix constructed\n";
206 if (verbose) cout <<
" putting IntSerialDenseMatrix from PE0 into Matlab as ISDM_PE0\n";
209 if (reportErrors) cout <<
"There was an error in engine.PutIntSerialDenseMatrix(isdMatrix, \"ISDM_PE0\"): " << ierr << endl;
211 if (comm.NumProc() > 1) {
212 if (verbose) cout <<
" putting IntSerialDenseMatrix from PE1 into Matlab as ISDM_PE1\n";
216 if (reportErrors) cout <<
"There was an error in engine.PutSerialDenseMatrix(isdMatrix, \"ISDM_PE1\", 1): " << ierr << endl;
223 if (verbose) cout <<
" constructing a IntSerialDenseVector...\n";
224 Epetra_IntSerialDenseVector isdVector (Copy, intA, M);
225 if (verbose) cout <<
" IntSerialDenseVector constructed\n";
227 if (verbose) cout <<
" putting IntSerialDenseVector from PE0 into Matlab as ISDV_PE0\n";
230 if (reportErrors) cout <<
"There was an error in engine.PutIntSerialDenseMatrix(isdVector, \"ISDV_PE0\"): " << ierr << endl;
232 if (comm.NumProc() > 1) {
233 if (verbose) cout <<
" putting IntSerialDenseVector from PE1 into Matlab as ISDV_PE1\n";
237 if (reportErrors) cout <<
"There was an error in engine.PutSerialDenseMatrix(isdVector, \"ISDV_PE1\", 1): " << ierr << endl;
249 const int bufSize = 200;
251 const int matlabBufferSize = 1024 * 16;
252 char matlabBuffer [matlabBufferSize];
255 engine.
EvalString(
"whos", matlabBuffer, matlabBufferSize);
256 if (verbose) cout << matlabBuffer << endl;
257 engine.
EvalString(
"SDV_PE0", matlabBuffer, matlabBufferSize);
258 if (verbose) cout << matlabBuffer << endl;
259 if (comm.NumProc() > 1) {
260 engine.
EvalString(
"SDV_PE1", matlabBuffer, matlabBufferSize);
261 if (verbose) cout << matlabBuffer << endl;
269 if (fgets(s, bufSize, stdin) == NULL) {
273 printf (
"command :%s:\n", s) ;
277 ierr = engine.
EvalString(s, matlabBuffer, matlabBufferSize);
279 printf(
"there was an error: %d", ierr);
283 printf(
"Matlab Output:\n%s", matlabBuffer);
287 if (verbose) cout << endl <<
" all done\n";
int PutIntSerialDenseMatrix(const Epetra_IntSerialDenseMatrix &A, const char *variableName, int proc=0)
Puts a copy of the IntSerialDenseMatrix into the Matlab workspace.
int PutBlockMap(const Epetra_BlockMap &blockMap, const char *variableName, bool transA)
Puts a copy of the BlockMap or Map into the Matlab workspace.
int PutSerialDenseMatrix(const Epetra_SerialDenseMatrix &A, const char *variableName, int proc=0)
Puts a copy of the SerialDenseMatrix into the Matlab workspace.
int main(int argc, char *argv[])
int PutRowMatrix(const Epetra_RowMatrix &A, const char *variableName, bool transA)
Puts a copy of the serial or distributed RowMatrix into the Matlab workspace.
int PutMultiVector(const Epetra_MultiVector &A, const char *variableName)
Puts a copy of the serial or distributed MultiVector into the Matlab workspace.
int EvalString(char *command, char *outputBuffer=NULL, int outputBufferSize=-1)
Sends a command to Matlab.
A class which provides data and command access to Matlab from Epetra.