47 #include "Epetra_Comm.h" 48 #include "Epetra_MultiVector.h" 49 #include "Epetra_SerialDenseMatrix.h" 50 #include "Epetra_IntSerialDenseMatrix.h" 51 #include "Epetra_IntVector.h" 52 #include "Epetra_RowMatrix.h" 53 #include "Epetra_DataAccess.h" 54 #include "Epetra_Import.h" 55 #include "Epetra_Export.h" 56 #include "Epetra_CombineMode.h" 57 #include "Epetra_CrsMatrix.h" 58 #include "Epetra_Map.h" 59 #include "Epetra_CrsMatrix.h" 66 if (
Comm_.MyPID() == 0) {
74 if (
Comm_.MyPID() == 0) {
83 if (
Comm_.MyPID() == 0) {
84 if (outputBuffer != NULL) {
85 if (engOutputBuffer(
Engine_, outputBuffer, outputBufferSize)) {
89 if (engEvalString(
Engine_, command)) {
101 if (
Comm_.MyPID() == 0) {
102 matlabA = mxCreateDoubleMatrix(A.GlobalLength(), A.NumVectors(), mxREAL);
103 pr = mxGetPr(matlabA);
107 mxDestroyArray(matlabA);
111 if (
Comm_.MyPID() == 0)
113 mxDestroyArray(matlabA);
117 mxDestroyArray(matlabA);
123 mxArray* matlabA = 0;
124 if (
Comm_.MyPID() == 0) {
126 matlabA = mxCreateSparse(A.OperatorDomainMap().MaxAllGID() - A.OperatorDomainMap().MinAllGID()+1, A.OperatorRangeMap().MaxAllGID() - A.OperatorRangeMap().MinAllGID() + 1, A.NumGlobalNonzeros(), mxREAL);
132 mxDestroyArray(matlabA);
137 if (
Comm_.MyPID() == 0) {
149 mxDestroyArray(matlabA);
154 char* buff =
new char[128];;
155 sprintf(buff,
"%s = %s'", variableName, variableName);
157 mxDestroyArray(matlabA);
164 mxDestroyArray(matlabA);
177 if (
Comm_.MyPID() == 0) {
178 mxArray* matlabA = 0;
183 matlabA = mxCreateDoubleMatrix(numRows, numCols, mxREAL);
187 double* targetPtr = 0;
188 double* sourcePtr = 0;
189 double* source = (
double*)A.A();
190 double* target = (
double*)mxGetPr(matlabA);
191 int source_LDA = A.LDA();
192 int target_LDA = A.LDA();
193 for (col = 0; col < numCols; col++) {
194 targetPtr = target + (col * target_LDA);
195 sourcePtr = source + (col * source_LDA);
196 for (row = 0; row < numRows; row++) {
197 *targetPtr++ = *sourcePtr++;
202 mxDestroyArray(matlabA);
206 mxDestroyArray(matlabA);
212 Epetra_MultiVector* tempMultiVector = 0;
213 if (proc ==
Comm_.MyPID()) {
214 int* numVectors =
new int[1];
215 int* temp =
new int[1];
217 Comm_.MaxAll (temp, numVectors, 1);
218 const Epetra_BlockMap tempBlockMap (-1, A.LDA(), 1, 0,
Comm_);
219 tempMultiVector =
new Epetra_MultiVector (View, tempBlockMap, A.A(), A.LDA(), A.N());
222 int* numVectors =
new int[1];
223 int* temp =
new int[1];
225 Comm_.MaxAll (temp, numVectors, 1);
226 const Epetra_BlockMap tempBlockMap (-1, 0, 1, 0,
Comm_);
227 tempMultiVector =
new Epetra_MultiVector (tempBlockMap, numVectors[0],
false);
236 mxArray* matlabA = 0;
239 if (
Comm_.MyPID() == 0) {
243 matlabA = mxCreateDoubleMatrix(numRows, numCols, mxREAL);
247 double* targetPtr = 0;
249 int* source = (
int*)A.A();
250 double* target = (
double*)mxGetPr(matlabA);
251 int source_LDA = A.LDA();
252 int target_LDA = A.LDA();
253 for (col = 0; col < numCols; col++) {
254 targetPtr = target + (col * target_LDA);
255 sourcePtr = source + (col * source_LDA);
256 for (row = 0; row < numRows; row++) {
257 *targetPtr++ = *sourcePtr++;
262 mxDestroyArray(matlabA);
268 int* numVectors =
new int[2];
269 if (proc ==
Comm_.MyPID()) {
270 int* temp =
new int[2];
273 Comm_.MaxAll (temp, numVectors, 2);
276 int* temp =
new int[2];
279 Comm_.MaxAll (temp, numVectors, 2);
282 int numRows = numVectors[1];
283 int numCols = numVectors[0];
284 double* targetPtr = 0;
288 const Epetra_BlockMap* tgBlockMap = 0;
289 if (
Comm_.MyPID() == 0) {
290 matlabA = mxCreateDoubleMatrix(numRows, numCols, mxREAL);
291 target = (
double*)mxGetPr(matlabA);
292 tgBlockMap =
new Epetra_BlockMap(-1, numRows, 1, 0,
Comm_);
295 tgBlockMap =
new Epetra_BlockMap(-1, 0, 1, 0,
Comm_);
298 const Epetra_BlockMap* srcBlockMap = 0;
299 Epetra_IntVector* srcIntVector = 0;
300 Epetra_IntVector tgIntVector (*tgBlockMap,
false);
301 if (proc ==
Comm_.MyPID()) {
302 srcBlockMap =
new Epetra_BlockMap(-1, A.LDA(), 1, 0,
Comm_);
305 srcBlockMap =
new Epetra_BlockMap(-1, 0, 1, 0,
Comm_);
308 Epetra_Import importer (*tgBlockMap, *srcBlockMap);
310 for(
int i=0; i < numVectors[0]; i++) {
311 if (proc ==
Comm_.MyPID()) {
312 srcIntVector =
new Epetra_IntVector(View, *srcBlockMap, (
int*)A[i]);
315 srcIntVector =
new Epetra_IntVector(*srcBlockMap,
false);
318 tgIntVector.Import(*srcIntVector, importer, Insert);
319 if (
Comm_.MyPID() == 0) {
320 targetPtr = target + (i * numRows);
321 sourcePtr = (
int*)tgIntVector.Values();
322 for (row = 0; row < numRows; row++) {
323 *targetPtr++ = *sourcePtr++;
329 mxDestroyArray(matlabA);
334 mxDestroyArray(matlabA);
340 mxArray* matlabA = 0;
341 if (
Comm_.MyPID() == 0) {
342 int M = blockMap.NumGlobalElements();
345 if (blockMap.MaxElementSize()>1)
N = 2;
347 matlabA = mxCreateSparse(
N, M, M*
N, mxREAL);
348 int* matlabAcolumnIndicesPtr = mxGetJc(matlabA);
349 matlabAcolumnIndicesPtr += M;
350 *matlabAcolumnIndicesPtr = M*
N;
354 mxDestroyArray(matlabA);
358 if (
Comm_.MyPID() == 0) {
360 mxDestroyArray(matlabA);
365 char* buff =
new char[128];;
366 sprintf(buff,
"%s = %s'", variableName, variableName);
368 mxDestroyArray(matlabA);
374 mxDestroyArray(matlabA);
380 if (
Comm_.MyPID() != 0) {
383 #ifdef USE_ENGPUTARRAY 385 mxSetName(matlabA, variableName);
386 return(engPutArray(
Engine_, matlabA));
389 return(engPutVariable(
Engine_, variableName, matlabA));
395 mxArray* matlabA = 0;
397 if (ierr =
GetmxArray(variableName, &matlabA)) {
398 mxDestroyArray(matlabA);
402 bool isSparse =
false;
408 mxDestroyArray(matlabA);
412 if ((
Comm_.MyPID() == 0) && isSparse) {
413 mxDestroyArray(matlabA);
418 Epetra_Map tempMap (-1, numRows, 0,
Comm_);
421 Comm_.MaxAll (&numCols, &numVectors, 1);
422 double* tempValues = 0;
423 if (
Comm_.MyPID() == 0) {
424 tempValues = mxGetPr(matlabA);
426 Epetra_MultiVector tempMV (View, tempMap, tempValues, numRows, numVectors);
427 Epetra_Export exporter (tempMap, A.Map());
428 A.Export(tempMV, exporter, Insert);
430 mxDestroyArray(matlabA);
438 if (
Comm_.MyPID() == proc) {
441 Epetra_Map tempMap (-1, numMyGIDs, 0,
Comm_);
442 Epetra_MultiVector tempMV (View, tempMap, A.A(), numMyGIDs, A.N());
448 if (
Comm_.MyPID() == proc) {
449 double* aValues = A.A();
450 double* mvValues = tempMV.Values();
451 for(
int i=0; i < A.M() * A.N(); i++) {
452 *aValues++ = *mvValues++;
464 if (
Comm_.MyPID() == proc) {
466 int* aValues = A.A();
467 values =
new double[A.M() * A.N()];
468 for (
int i=0; i < A.M() * A.N(); i++) {
469 *values++ = *aValues++;
472 Epetra_Map tempMap (-1, numMyGIDs, 0,
Comm_);
473 Epetra_MultiVector tempMV (View, tempMap, values, numMyGIDs, A.N());
479 if (
Comm_.MyPID() == proc) {
480 int* aValues = A.A();
481 double* mvValues = tempMV.Values();
482 for(
int i=0; i < A.M() * A.N(); i++) {
483 *aValues++ = *mvValues++;
492 const char* variableName;
495 int inVariableNameLength = strlen(inVariableName);
496 char* buff =
new char[inVariableNameLength*2 + 11];
497 sprintf(buff,
"TRANS_%s = %s'", inVariableName, inVariableName);
501 char* tempStr =
new char[inVariableNameLength + 7];
502 sprintf(tempStr,
"TRANS_%s", inVariableName);
503 variableName = tempStr;
506 variableName = inVariableName;
509 mxArray* matlabA = 0;
511 if (ierr =
GetmxArray(variableName, &matlabA)) {
512 mxDestroyArray(matlabA);
517 char* buff =
new char[strlen(variableName) + 7];
518 sprintf(buff,
"clear %s", variableName);
524 bool isSparse =
false;
532 mxDestroyArray(matlabA);
536 if ((
Comm_.MyPID() == 0) && !isSparse) {
537 mxDestroyArray(matlabA);
542 Epetra_Map tempMap (-1, numRows, 0,
Comm_);
545 Comm_.MaxAll (&numCols, &numVectors, 1);
546 Epetra_CrsMatrix tempCRSM (View, tempMap, numVectors);
547 if (
Comm_.MyPID() == 0) {
548 int* rowIndex = mxGetJc(matlabA);
549 int* colIndex = mxGetIr(matlabA);
550 double* values = mxGetPr(matlabA);
552 for(
int row = 0; row < numRows; row++) {
553 numCols = *(rowIndex+1) - *rowIndex;
555 int* indices =
new int[numCols];
556 int* indicesPtr = indices;
557 for(
int col=0; col < numCols; col++) {
558 *indicesPtr++ = *colIndex++;
560 tempCRSM.InsertGlobalValues(row, numCols, values, indices);
567 tempCRSM.FillComplete();
568 Epetra_Export exporter (tempMap, A.RowMatrixRowMap());
569 A.Export(tempCRSM, exporter, Insert);
571 mxDestroyArray(matlabA);
577 if (
Comm_.MyPID() == 0) {
578 if (mxGetNumberOfDimensions(matlabA) > 2) {
582 const int* dimensions = mxGetDimensions(matlabA);
583 numRows = dimensions[0];
584 numCols = dimensions[1];
585 isSparse = mxIsSparse(matlabA);
588 numNonZeros = mxGetNzmax(matlabA);
591 numNonZeros = numRows * numCols;
600 if (
Comm_.MyPID() == 0) {
601 #ifdef USE_ENGPUTARRAY 603 *matlabA = engGetArray(
Engine_, variableName);
606 *matlabA = engGetVariable(
Engine_, variableName);
608 if (matlabA == NULL) {
int CopyMultiVector(double **matlabApr, const Epetra_MultiVector &A)
int GetIntSerialDenseMatrix(const char *variableName, Epetra_IntSerialDenseMatrix &A, int proc=0)
Puts a Matlab variable into a IntSerialDenseMatrix on the specified PE.
int CopyBlockMap(mxArray *matlabA, const Epetra_BlockMap &map)
int GetCrsMatrix(const char *variableName, Epetra_CrsMatrix &A, bool getTrans)
Puts a Matlab variable into a CrsMatrix.
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.
EpetraExt_MatlabEngine(const Epetra_Comm &Comm)
EpetraExt_MatlabEngine constructor which creates a MatlabEngine object with a connection to an instan...
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
int GetSerialDenseMatrix(const char *variableName, Epetra_SerialDenseMatrix &A, int proc=0)
Puts a Matlab variable into a SerialDenseMatrix on the specified PE.
int GetMultiVector(const char *variableName, Epetra_MultiVector &A)
Puts a Matlab variable into a MultiVector.
int CopyRowMatrix(mxArray *matlabA, const Epetra_RowMatrix &A)
~EpetraExt_MatlabEngine()
EpetraExt_MatlabEngine destructor which closes the connection to Matlab which causes the Matlab proce...
int GetmxArray(const char *variableName, mxArray **matlabA)
Get a mxArray from Matlab. For internal use but can be used by an advanced user.
const Epetra_Comm & Comm_
int GetmxArrayDimensions(mxArray *matlabA, bool &isSparse, int &numRows, int &numCols, int &numNonZeros)
Get general information about the mxArray. For internal use but can be used by an advanced user...
int PutCrsGraph(const Epetra_CrsGraph &A, const char *variableName, bool transA)
not implemented yet
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.
int PutIntoMatlab(const char *variableName, mxArray *matlabA)
Puts a mxArray into Matlab.