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 (2001) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 //@HEADER
00028 */
00029 
00030 #include "Epetra_ConfigDefs.h"
00031 //#include "EpetraExt_Version.h"
00032 #ifdef EPETRA_MPI
00033 #include "mpi.h"
00034 #include "Epetra_MpiComm.h"
00035 #else
00036 #include "Epetra_SerialComm.h"
00037 #endif
00038 #include "Epetra_Comm.h"
00039 #include "Epetra_Map.h"
00040 #include "Epetra_BlockMap.h"
00041 #include "Epetra_MultiVector.h"
00042 #include "Epetra_Vector.h"
00043 #include "Epetra_SerialDenseMatrix.h"
00044 #include "Epetra_SerialDenseVector.h"
00045 #include "Epetra_IntSerialDenseMatrix.h"
00046 #include "Epetra_IntSerialDenseVector.h"
00047 #include "Epetra_DataAccess.h"
00048 #include "Epetra_CrsMatrix.h"
00049 
00050 #include "EpetraExt_MatlabEngine.h"
00051 
00052 // the following deal with matlab provided headers:
00053 #include "engine.h"
00054 #include "mex.h"
00055 #undef printf  // matlab has its own printf that we don't want to use
00056 
00057 #define BUFSIZE 200
00058 #define MATLABBUF 1024 * 16
00059 
00060 int main(int argc, char *argv[]) {
00061 cout << "going to setup MPI...\n";
00062 
00063 #ifdef EPETRA_MPI
00064   MPI_Init(&argc,&argv);
00065   Epetra_MpiComm comm (MPI_COMM_WORLD);
00066 #else
00067   Epetra_SerialComm comm;
00068 #endif
00069   cout << "mpit setup complete\n";
00070 
00071   int MyPID = comm.MyPID();
00072 
00073   char s [BUFSIZE] ;
00074   char matlabBuffer [MATLABBUF];
00075   cout << "going to init matlab\n";
00076   EpetraExt::EpetraExt_MatlabEngine * enginePtr = new EpetraExt::EpetraExt_MatlabEngine(comm);
00077   EpetraExt::EpetraExt_MatlabEngine & engine = *enginePtr;
00078   cout << "matlab started\n";
00079   
00080   /* GetCrsMatrix test
00081   engine.EvalString("CRSM=sparse(eye(8,10))", matlabBuffer, MATLABBUF);
00082   cout << matlabBuffer << endl;
00083   int myM=4;
00084   int M = myM * comm.NumProc();
00085   int N = 10;  
00086   Epetra_Map getMap (M, 0, comm);
00087   Epetra_Map colMap(N, N, 0, comm);
00088   Epetra_CrsMatrix getCRSM (Copy, getMap, colMap, N);
00089   double colValue = 0;
00090   for(int row=myM*MyPID; row < myM*(MyPID+1); row++) {
00091     getCRSM.InsertGlobalValues(row, 1, &colValue, &row);
00092   }
00093   getCRSM.FillComplete(colMap, getMap);
00094   //getCRSM.FillComplete();
00095   int ierr = engine.GetCrsMatrix("CRSM", getCRSM, false);
00096   if (ierr) {
00097     cout << "engine.GetCrsMatrix(\"CRSM\", getCRSM, false) failed" << endl;
00098   }
00099   
00100   cout << getCRSM << endl;
00101   
00102   engine.EvalString("whos", matlabBuffer, MATLABBUF);
00103   cout << matlabBuffer << endl;
00104   */
00105   
00106   /* GetIntSerialDenseMatrix test
00107   engine.EvalString("ISDM=rand(8,2)*100", matlabBuffer, MATLABBUF);
00108   cout << matlabBuffer << endl;
00109   int procToGet = 1;
00110   int M = 8;
00111   int N = 2;  
00112   int* A = new int[M*N];
00113   Epetra_IntSerialDenseMatrix getISDM (View, A, M, M, N);
00114   int ierr = engine.GetIntSerialDenseMatrix("ISDM", getISDM, procToGet);
00115   if (ierr) {
00116     cout << "engine.GetIntSerialDenseMatrix(\"ISDM\", getISDM, procToGet) failed" << endl;
00117   }
00118   
00119   if (MyPID == 1) cout << getISDM << endl;
00120   */
00121   
00122   /* GetSerialDenseMatrix test
00123   engine.EvalString("SDM=rand(8,2)", matlabBuffer, MATLABBUF);
00124   cout << matlabBuffer << endl;
00125   int procToGet = 1;
00126   int M = 8;
00127   int N = 2;  
00128   double* A = new double[M*N];
00129   Epetra_SerialDenseMatrix getSDM (View, A, M, M, N);
00130   int ierr = engine.GetSerialDenseMatrix("SDM", getSDM, procToGet);
00131   if (ierr) {
00132     cout << "engine.GetSerialDenseMatrix(\"SDM\", getSDM, procToGet) failed" << endl;
00133   }
00134   
00135   if (MyPID == 1) cout << getSDM << endl;
00136   */
00137   
00138   /* GetMultiVector test
00139   if (comm.NumProc() != 2) {
00140     if (MyPID == 0) cout << "Error: this test must be run with exactly two PE." << endl;
00141     delete &engine;
00142     #ifdef EPETRA_MPI
00143     MPI_Finalize();
00144     #endif
00145     return(-1);
00146   }
00147   engine.EvalString("MV=rand(8,2)", matlabBuffer, MATLABBUF);
00148   cout << matlabBuffer << endl;
00149   int myM = 4;
00150   int M = myM * comm.NumProc();
00151   int N = 2;
00152   Epetra_Map getMap (M, 0, comm);
00153   double* A = new double[myM*N];
00154   Epetra_MultiVector getMV (View, getMap, A, myM, N);
00155   cout << "MultiVector created" << endl;
00156   int ierr = engine.GetMultiVector("MV", getMV);
00157   if (ierr) {
00158     cout << "engine.GetMultiVector(\"MV\", getMV) failed" << endl;
00159   }
00160   
00161   cout << getMV << endl;
00162   */
00163   
00164   /* CrsMatrix test
00165   int numGlobalElements = 8;
00166   int numMyElements = 8/comm.NumProc();
00167   int M=numGlobalElements/comm.NumProc();
00168   int N=10;
00169   int* myGlobalElements = new int[M];
00170   
00171   int minGID = 0;
00172   int startIndex = minGID + M*comm.MyPID();
00173   for(int i=0; i < M; i++) {
00174       myGlobalElements[i] = startIndex++;
00175   }
00176   
00177 
00178   int* colMapGIDs = new int[N];
00179   for (int i=0; i < N; i++) {
00180     colMapGIDs[i] = i;
00181   }
00182   //Epetra_Map map (numGlobalElements, numMyElements, myGlobalElements, minGID, comm);
00183   Epetra_Map map (numGlobalElements, minGID, comm);
00184   Epetra_Map colMap (N, N, colMapGIDs, 0, comm);       
00185 
00186   Epetra_CrsMatrix crsMatrix (Copy, map, colMap, N);
00187   
00188   cout << "crs matrix created\n";
00189   
00190   //int indices[8] = {-4,-3,-2,-1,0,1,2,3};
00191   int indices[10] = {0,1,2,3,4,5,6,7,8,9};
00192   double* values = new double[N];
00193   double value = M * N * comm.MyPID();
00194   for (int i=0; i < M; i++) {
00195     if (i % 2 == 0) {
00196       for (int j=0; j < N; j++) {
00197         values[j] = value++;
00198       }
00199       
00200       crsMatrix.InsertGlobalValues(myGlobalElements[i], N, values, indices);
00201     }
00202   }
00203   
00204   cout << "done putting values\n";
00205   crsMatrix.FillComplete(colMap, map);
00206   cout << "done filling crsMatrix and calling crsMatrix.FillComplete()\n";
00207   
00208   cout << crsMatrix;
00209   
00210   cout << "done printing crsMatrix\n";
00211   
00212   //cout << map;
00213   //cout << "done printing map\n";
00214   
00215   int ierr = engine.PutRowMatrix(crsMatrix, "TEST", true);
00216   //int ierr = engine.PutBlockMap(map, "TEST", true);
00217   //cout << "done calling engine.PutRowMatrix(crsMatrix, \"TEST\", false)\n";
00218   if (ierr != 0) {
00219     cout << "engine.PutRowMatrix(crsMatrix, \"TEST\") returned nonzero result: " << ierr << "\n";
00220     return(-1);
00221   }
00222   
00223   */
00224 
00225   /* MultiVector test
00226   cout << MyPID << " going to do multivector test...\n";
00227   int numGlobalElements = 100;
00228   int M = numGlobalElements/comm.NumProc();
00229   int N = 3;
00230   int numMyElements = M * N;
00231   double* A = new double[numMyElements];
00232   double* Aptr = A;
00233   int startValue = 0;
00234 
00235   cout << MyPID << " allocated space for A, now filling A\n";
00236   for(int col=0; col < N; col++) {
00237   startValue = (col * numGlobalElements) + (M * MyPID);
00238   for(int row=0; row < M; row++) {
00239           *Aptr++ = row+startValue;
00240       }
00241   }
00242   cout << MyPID << " A filled\n";
00243 
00244   Epetra_Map map (numGlobalElements, 0, comm);
00245   Epetra_MultiVector multiVector (Copy, map, A, M, N);
00246   //cout << multiVector;
00247   engine.PutMultiVector(multiVector, "TEST");
00248   */
00249   
00250   /*SerialDenseMatrix test
00251   cout << MyPID << " going to do SerialDenseMatrix test...\n";
00252   double* A = new double[30];
00253   cout << MyPID << " allocated space for A, now filling A\n";
00254   double* Aptr = A;
00255   int M = 5;
00256   int N = 6;
00257   int startValue = M*N*comm.MyPID();
00258   for(int i=0; i < M*N; i++) {
00259       *Aptr++ = i + startValue;
00260   }
00261   cout << MyPID << " A filled\n";
00262 
00263   Epetra_SerialDenseMatrix sdMatrix (View, A, M, M, N);
00264   engine.PutSerialDenseMatrix(sdMatrix, "TEST", 0);
00265   cout << sdMatrix;
00266   */
00267 
00268   /* SerialDenseVector test
00269   double* A = new double[30];
00270   double* Aptr = A;
00271   int length = 30;
00272   for(int i=0; i < length; i++) {
00273       *Aptr++ = i;
00274   }
00275 
00276   Epetra_SerialDenseVector sdVector (Copy, A, length);
00277   engine.PutSerialDenseMatrix(sdVector, "SDVECTOR");
00278   cout << sdVector;
00279   */
00280 
00281   /*IntSerialDenseMatrix test
00282   cout << MyPID << " going to do IntSerialDenseMatrix test...\n";
00283   int* A = new int[30];
00284   cout << MyPID << " allocated space for A, now filling A\n";
00285   int* Aptr = A;
00286   int M = 5;
00287   int N = 6;
00288   int startValue = M*N*comm.MyPID();
00289   for(int i=0; i < M*N; i++) {
00290       *Aptr++ = i + startValue;
00291   }
00292   cout << MyPID << " A filled\n";
00293   Epetra_IntSerialDenseMatrix isdMatrix (Copy, A, M, M, N);
00294   cout << isdMatrix;
00295   engine.PutIntSerialDenseMatrix(isdMatrix, "TEST", 0);
00296   */
00297 
00298 
00299   /* SerialDenseVector test
00300   int* A = new int[30];
00301   int* Aptr = A;
00302   int length = 30;
00303   for(int i=0; i < length; i++) {
00304       *Aptr++ = i;
00305   }
00306 
00307   Epetra_IntSerialDenseVector isdVector (Copy, A, length);
00308   engine.PutIntSerialDenseMatrix(isdVector, "ISDVECTOR");
00309   cout << isdVector;
00310   */
00311 
00312   /*while(1) {
00313 
00314   // do nothing
00315   }*/
00316 
00317   /*if (comm.NumProc() == 1) {
00318   int err;
00319   while(1) {
00320       // Prompt the user and get a string
00321       printf(">> ");
00322       if (fgets(s, BUFSIZE, stdin) == NULL) {
00323           printf("Bye\n");
00324           break ;
00325       }
00326       printf ("command :%s:\n", s) ;
00327       
00328       // Send the command to MATLAB
00329       // output goes to stdout
00330       err = engine.EvalString(s, matlabBuffer, MATLABBUF);
00331       if (err != 0) {
00332           printf("there was an error: %d", err);
00333       err = 0;
00334       }
00335       else {
00336           printf("Matlab Output:\n%s", matlabBuffer);
00337       }
00338   }
00339   }*/
00340 
00341   //delete engine ;
00342 
00343   /*
00344   engine.EvalString("size(TEST)", matlabBuffer, MATLABBUF);
00345   cout << matlabBuffer << "\n";
00346   engine.EvalString("TEST", matlabBuffer, MATLABBUF);
00347   cout << matlabBuffer << "\n";
00348   */
00349   
00350   cout << "\n" << comm.MyPID() << " all done\n";
00351   
00352 #ifdef EPETRA_MPI
00353   MPI_Finalize();
00354 #endif
00355 
00356   delete enginePtr;
00357 
00358   return(0);
00359  
00360 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines