EpetraExt Package Browser (Single Doxygen Collection) Development
test/Matlab/cxx_main.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2011) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 */
00042 
00043 #include "Epetra_ConfigDefs.h"
00044 //#include "EpetraExt_Version.h"
00045 #ifdef EPETRA_MPI
00046 #include "mpi.h"
00047 #include "Epetra_MpiComm.h"
00048 #else
00049 #include "Epetra_SerialComm.h"
00050 #endif
00051 #include "Epetra_Comm.h"
00052 #include "Epetra_Map.h"
00053 #include "Epetra_BlockMap.h"
00054 #include "Epetra_MultiVector.h"
00055 #include "Epetra_Vector.h"
00056 #include "Epetra_SerialDenseMatrix.h"
00057 #include "Epetra_SerialDenseVector.h"
00058 #include "Epetra_IntSerialDenseMatrix.h"
00059 #include "Epetra_IntSerialDenseVector.h"
00060 #include "Epetra_DataAccess.h"
00061 #include "Epetra_CrsMatrix.h"
00062 
00063 #include "EpetraExt_MatlabEngine.h"
00064 
00065 // the following deal with matlab provided headers:
00066 #include "engine.h"
00067 #include "mex.h"
00068 #undef printf  // matlab has its own printf that we don't want to use
00069 
00070 #define BUFSIZE 200
00071 #define MATLABBUF 1024 * 16
00072 
00073 int main(int argc, char *argv[]) {
00074 cout << "going to setup MPI...\n";
00075 
00076 #ifdef EPETRA_MPI
00077   MPI_Init(&argc,&argv);
00078   Epetra_MpiComm comm (MPI_COMM_WORLD);
00079 #else
00080   Epetra_SerialComm comm;
00081 #endif
00082   cout << "mpit setup complete\n";
00083 
00084   int MyPID = comm.MyPID();
00085 
00086   char s [BUFSIZE] ;
00087   char matlabBuffer [MATLABBUF];
00088   cout << "going to init matlab\n";
00089   EpetraExt::EpetraExt_MatlabEngine * enginePtr = new EpetraExt::EpetraExt_MatlabEngine(comm);
00090   EpetraExt::EpetraExt_MatlabEngine & engine = *enginePtr;
00091   cout << "matlab started\n";
00092   
00093   /* GetCrsMatrix test
00094   engine.EvalString("CRSM=sparse(eye(8,10))", matlabBuffer, MATLABBUF);
00095   cout << matlabBuffer << endl;
00096   int myM=4;
00097   int M = myM * comm.NumProc();
00098   int N = 10;  
00099   Epetra_Map getMap (M, 0, comm);
00100   Epetra_Map colMap(N, N, 0, comm);
00101   Epetra_CrsMatrix getCRSM (Copy, getMap, colMap, N);
00102   double colValue = 0;
00103   for(int row=myM*MyPID; row < myM*(MyPID+1); row++) {
00104     getCRSM.InsertGlobalValues(row, 1, &colValue, &row);
00105   }
00106   getCRSM.FillComplete(colMap, getMap);
00107   //getCRSM.FillComplete();
00108   int ierr = engine.GetCrsMatrix("CRSM", getCRSM, false);
00109   if (ierr) {
00110     cout << "engine.GetCrsMatrix(\"CRSM\", getCRSM, false) failed" << endl;
00111   }
00112   
00113   cout << getCRSM << endl;
00114   
00115   engine.EvalString("whos", matlabBuffer, MATLABBUF);
00116   cout << matlabBuffer << endl;
00117   */
00118   
00119   /* GetIntSerialDenseMatrix test
00120   engine.EvalString("ISDM=rand(8,2)*100", matlabBuffer, MATLABBUF);
00121   cout << matlabBuffer << endl;
00122   int procToGet = 1;
00123   int M = 8;
00124   int N = 2;  
00125   int* A = new int[M*N];
00126   Epetra_IntSerialDenseMatrix getISDM (View, A, M, M, N);
00127   int ierr = engine.GetIntSerialDenseMatrix("ISDM", getISDM, procToGet);
00128   if (ierr) {
00129     cout << "engine.GetIntSerialDenseMatrix(\"ISDM\", getISDM, procToGet) failed" << endl;
00130   }
00131   
00132   if (MyPID == 1) cout << getISDM << endl;
00133   */
00134   
00135   /* GetSerialDenseMatrix test
00136   engine.EvalString("SDM=rand(8,2)", matlabBuffer, MATLABBUF);
00137   cout << matlabBuffer << endl;
00138   int procToGet = 1;
00139   int M = 8;
00140   int N = 2;  
00141   double* A = new double[M*N];
00142   Epetra_SerialDenseMatrix getSDM (View, A, M, M, N);
00143   int ierr = engine.GetSerialDenseMatrix("SDM", getSDM, procToGet);
00144   if (ierr) {
00145     cout << "engine.GetSerialDenseMatrix(\"SDM\", getSDM, procToGet) failed" << endl;
00146   }
00147   
00148   if (MyPID == 1) cout << getSDM << endl;
00149   */
00150   
00151   /* GetMultiVector test
00152   if (comm.NumProc() != 2) {
00153     if (MyPID == 0) cout << "Error: this test must be run with exactly two PE." << endl;
00154     delete &engine;
00155     #ifdef EPETRA_MPI
00156     MPI_Finalize();
00157     #endif
00158     return(-1);
00159   }
00160   engine.EvalString("MV=rand(8,2)", matlabBuffer, MATLABBUF);
00161   cout << matlabBuffer << endl;
00162   int myM = 4;
00163   int M = myM * comm.NumProc();
00164   int N = 2;
00165   Epetra_Map getMap (M, 0, comm);
00166   double* A = new double[myM*N];
00167   Epetra_MultiVector getMV (View, getMap, A, myM, N);
00168   cout << "MultiVector created" << endl;
00169   int ierr = engine.GetMultiVector("MV", getMV);
00170   if (ierr) {
00171     cout << "engine.GetMultiVector(\"MV\", getMV) failed" << endl;
00172   }
00173   
00174   cout << getMV << endl;
00175   */
00176   
00177   /* CrsMatrix test
00178   int numGlobalElements = 8;
00179   int numMyElements = 8/comm.NumProc();
00180   int M=numGlobalElements/comm.NumProc();
00181   int N=10;
00182   int* myGlobalElements = new int[M];
00183   
00184   int minGID = 0;
00185   int startIndex = minGID + M*comm.MyPID();
00186   for(int i=0; i < M; i++) {
00187       myGlobalElements[i] = startIndex++;
00188   }
00189   
00190 
00191   int* colMapGIDs = new int[N];
00192   for (int i=0; i < N; i++) {
00193     colMapGIDs[i] = i;
00194   }
00195   //Epetra_Map map (numGlobalElements, numMyElements, myGlobalElements, minGID, comm);
00196   Epetra_Map map (numGlobalElements, minGID, comm);
00197   Epetra_Map colMap (N, N, colMapGIDs, 0, comm);       
00198 
00199   Epetra_CrsMatrix crsMatrix (Copy, map, colMap, N);
00200   
00201   cout << "crs matrix created\n";
00202   
00203   //int indices[8] = {-4,-3,-2,-1,0,1,2,3};
00204   int indices[10] = {0,1,2,3,4,5,6,7,8,9};
00205   double* values = new double[N];
00206   double value = M * N * comm.MyPID();
00207   for (int i=0; i < M; i++) {
00208     if (i % 2 == 0) {
00209       for (int j=0; j < N; j++) {
00210         values[j] = value++;
00211       }
00212       
00213       crsMatrix.InsertGlobalValues(myGlobalElements[i], N, values, indices);
00214     }
00215   }
00216   
00217   cout << "done putting values\n";
00218   crsMatrix.FillComplete(colMap, map);
00219   cout << "done filling crsMatrix and calling crsMatrix.FillComplete()\n";
00220   
00221   cout << crsMatrix;
00222   
00223   cout << "done printing crsMatrix\n";
00224   
00225   //cout << map;
00226   //cout << "done printing map\n";
00227   
00228   int ierr = engine.PutRowMatrix(crsMatrix, "TEST", true);
00229   //int ierr = engine.PutBlockMap(map, "TEST", true);
00230   //cout << "done calling engine.PutRowMatrix(crsMatrix, \"TEST\", false)\n";
00231   if (ierr != 0) {
00232     cout << "engine.PutRowMatrix(crsMatrix, \"TEST\") returned nonzero result: " << ierr << "\n";
00233     return(-1);
00234   }
00235   
00236   */
00237 
00238   /* MultiVector test
00239   cout << MyPID << " going to do multivector test...\n";
00240   int numGlobalElements = 100;
00241   int M = numGlobalElements/comm.NumProc();
00242   int N = 3;
00243   int numMyElements = M * N;
00244   double* A = new double[numMyElements];
00245   double* Aptr = A;
00246   int startValue = 0;
00247 
00248   cout << MyPID << " allocated space for A, now filling A\n";
00249   for(int col=0; col < N; col++) {
00250   startValue = (col * numGlobalElements) + (M * MyPID);
00251   for(int row=0; row < M; row++) {
00252           *Aptr++ = row+startValue;
00253       }
00254   }
00255   cout << MyPID << " A filled\n";
00256 
00257   Epetra_Map map (numGlobalElements, 0, comm);
00258   Epetra_MultiVector multiVector (Copy, map, A, M, N);
00259   //cout << multiVector;
00260   engine.PutMultiVector(multiVector, "TEST");
00261   */
00262   
00263   /*SerialDenseMatrix test
00264   cout << MyPID << " going to do SerialDenseMatrix test...\n";
00265   double* A = new double[30];
00266   cout << MyPID << " allocated space for A, now filling A\n";
00267   double* Aptr = A;
00268   int M = 5;
00269   int N = 6;
00270   int startValue = M*N*comm.MyPID();
00271   for(int i=0; i < M*N; i++) {
00272       *Aptr++ = i + startValue;
00273   }
00274   cout << MyPID << " A filled\n";
00275 
00276   Epetra_SerialDenseMatrix sdMatrix (View, A, M, M, N);
00277   engine.PutSerialDenseMatrix(sdMatrix, "TEST", 0);
00278   cout << sdMatrix;
00279   */
00280 
00281   /* SerialDenseVector test
00282   double* A = new double[30];
00283   double* Aptr = A;
00284   int length = 30;
00285   for(int i=0; i < length; i++) {
00286       *Aptr++ = i;
00287   }
00288 
00289   Epetra_SerialDenseVector sdVector (Copy, A, length);
00290   engine.PutSerialDenseMatrix(sdVector, "SDVECTOR");
00291   cout << sdVector;
00292   */
00293 
00294   /*IntSerialDenseMatrix test
00295   cout << MyPID << " going to do IntSerialDenseMatrix test...\n";
00296   int* A = new int[30];
00297   cout << MyPID << " allocated space for A, now filling A\n";
00298   int* Aptr = A;
00299   int M = 5;
00300   int N = 6;
00301   int startValue = M*N*comm.MyPID();
00302   for(int i=0; i < M*N; i++) {
00303       *Aptr++ = i + startValue;
00304   }
00305   cout << MyPID << " A filled\n";
00306   Epetra_IntSerialDenseMatrix isdMatrix (Copy, A, M, M, N);
00307   cout << isdMatrix;
00308   engine.PutIntSerialDenseMatrix(isdMatrix, "TEST", 0);
00309   */
00310 
00311 
00312   /* SerialDenseVector test
00313   int* A = new int[30];
00314   int* Aptr = A;
00315   int length = 30;
00316   for(int i=0; i < length; i++) {
00317       *Aptr++ = i;
00318   }
00319 
00320   Epetra_IntSerialDenseVector isdVector (Copy, A, length);
00321   engine.PutIntSerialDenseMatrix(isdVector, "ISDVECTOR");
00322   cout << isdVector;
00323   */
00324 
00325   /*while(1) {
00326 
00327   // do nothing
00328   }*/
00329 
00330   /*if (comm.NumProc() == 1) {
00331   int err;
00332   while(1) {
00333       // Prompt the user and get a string
00334       printf(">> ");
00335       if (fgets(s, BUFSIZE, stdin) == NULL) {
00336           printf("Bye\n");
00337           break ;
00338       }
00339       printf ("command :%s:\n", s) ;
00340       
00341       // Send the command to MATLAB
00342       // output goes to stdout
00343       err = engine.EvalString(s, matlabBuffer, MATLABBUF);
00344       if (err != 0) {
00345           printf("there was an error: %d", err);
00346       err = 0;
00347       }
00348       else {
00349           printf("Matlab Output:\n%s", matlabBuffer);
00350       }
00351   }
00352   }*/
00353 
00354   //delete engine ;
00355 
00356   /*
00357   engine.EvalString("size(TEST)", matlabBuffer, MATLABBUF);
00358   cout << matlabBuffer << "\n";
00359   engine.EvalString("TEST", matlabBuffer, MATLABBUF);
00360   cout << matlabBuffer << "\n";
00361   */
00362   
00363   cout << "\n" << comm.MyPID() << " all done\n";
00364   
00365 #ifdef EPETRA_MPI
00366   MPI_Finalize();
00367 #endif
00368 
00369   delete enginePtr;
00370 
00371   return(0);
00372  
00373 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines