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" 
   54 #include "Epetra_Import.h" 
   55 #include "Epetra_Export.h" 
   57 #include "Epetra_CrsMatrix.h" 
   58 #include "Epetra_Map.h" 
   59 #include "Epetra_CrsMatrix.h" 
   61 using namespace EpetraExt;
 
   66   if (Comm_.
MyPID() == 0) {
 
   68     Engine_ = engOpen(NULL);
 
   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) {
 
  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);
 
  213     if (proc == Comm_.
MyPID()) {
 
  214       int* numVectors = 
new int[1];
 
  215       int* temp = 
new int[1];
 
  217       Comm_.
MaxAll (temp, numVectors, 1);
 
  222       int* numVectors = 
new int[1];
 
  223       int* temp = 
new int[1];
 
  225       Comm_.
MaxAll (temp, numVectors, 1);
 
  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;
 
  289     if (Comm_.
MyPID() == 0) {
 
  290       matlabA = mxCreateDoubleMatrix(numRows, numCols, mxREAL);
 
  291       target = (
double*)mxGetPr(matlabA);
 
  301     if (proc == Comm_.
MyPID()) {
 
  310     for(
int i=0; i < numVectors[0]; i++) {
 
  311       if (proc == Comm_.
MyPID()) {
 
  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) {    
 
  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);
 
  421   Comm_.
MaxAll (&numCols, &numVectors, 1);
 
  422   double* tempValues = 0;
 
  423   if (Comm_.
MyPID() == 0) {
 
  424     tempValues = mxGetPr(matlabA);
 
  428   A.Export(tempMV, exporter, 
Insert);
 
  430   mxDestroyArray(matlabA);
 
  438   if (Comm_.
MyPID() == proc) {
 
  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++;
 
  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);
 
  545   Comm_.
MaxAll (&numCols, &numVectors, 1);
 
  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++;
 
  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 NumGlobalElements() const 
 
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. 
 
const Epetra_Map & RowMatrixRowMap() const 
 
int PutSerialDenseMatrix(const Epetra_SerialDenseMatrix &A, const char *variableName, int proc=0)
Puts a copy of the SerialDenseMatrix into the Matlab workspace. 
 
virtual const Epetra_Map & OperatorDomainMap() const =0
 
EpetraExt_MatlabEngine(const Epetra_Comm &Comm)
EpetraExt_MatlabEngine constructor which creates a MatlabEngine object with a connection to an instan...
 
virtual int NumGlobalNonzeros() const =0
 
virtual int MyPID() const =0
 
int FillComplete(bool OptimizeDataStorage=true)
 
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. 
 
virtual int MaxAll(double *PartialMaxs, double *GlobalMaxs, int Count) const =0
 
virtual const Epetra_Map & OperatorRangeMap() const =0
 
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. 
 
virtual int InsertGlobalValues(int GlobalRow, int NumEntries, const double *Values, const int *Indices)
 
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 MaxElementSize() const 
 
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.