EpetraExt Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
test/Matlab/cxx_main.cpp
Go to the documentation of this file.
1//@HEADER
2// ***********************************************************************
3//
4// EpetraExt: Epetra Extended - Linear Algebra Services Package
5// Copyright (2011) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40//@HEADER
41*/
42
43#include "Epetra_ConfigDefs.h"
44//#include "EpetraExt_Version.h"
45#ifdef EPETRA_MPI
46#include "mpi.h"
47#include "Epetra_MpiComm.h"
48#else
49#include "Epetra_SerialComm.h"
50#endif
51#include "Epetra_Comm.h"
52#include "Epetra_Map.h"
53#include "Epetra_BlockMap.h"
54#include "Epetra_MultiVector.h"
55#include "Epetra_Vector.h"
60#include "Epetra_DataAccess.h"
61#include "Epetra_CrsMatrix.h"
62
64
65// the following deal with matlab provided headers:
66#include "engine.h"
67#include "mex.h"
68#undef printf // matlab has its own printf that we don't want to use
69
70#define BUFSIZE 200
71#define MATLABBUF 1024 * 16
72
73int main(int argc, char *argv[]) {
74cout << "going to setup MPI...\n";
75
76#ifdef EPETRA_MPI
77 MPI_Init(&argc,&argv);
78 Epetra_MpiComm comm (MPI_COMM_WORLD);
79#else
81#endif
82 cout << "mpit setup complete\n";
83
84 int MyPID = comm.MyPID();
85
86 char s [BUFSIZE] ;
87 char matlabBuffer [MATLABBUF];
88 cout << "going to init matlab\n";
90 EpetraExt::EpetraExt_MatlabEngine & engine = *enginePtr;
91 cout << "matlab started\n";
92
93 /* GetCrsMatrix test
94 engine.EvalString("CRSM=sparse(eye(8,10))", matlabBuffer, MATLABBUF);
95 cout << matlabBuffer << endl;
96 int myM=4;
97 int M = myM * comm.NumProc();
98 int N = 10;
99 Epetra_Map getMap (M, 0, comm);
100 Epetra_Map colMap(N, N, 0, comm);
101 Epetra_CrsMatrix getCRSM (Copy, getMap, colMap, N);
102 double colValue = 0;
103 for(int row=myM*MyPID; row < myM*(MyPID+1); row++) {
104 getCRSM.InsertGlobalValues(row, 1, &colValue, &row);
105 }
106 getCRSM.FillComplete(colMap, getMap);
107 //getCRSM.FillComplete();
108 int ierr = engine.GetCrsMatrix("CRSM", getCRSM, false);
109 if (ierr) {
110 cout << "engine.GetCrsMatrix(\"CRSM\", getCRSM, false) failed" << endl;
111 }
112
113 cout << getCRSM << endl;
114
115 engine.EvalString("whos", matlabBuffer, MATLABBUF);
116 cout << matlabBuffer << endl;
117 */
118
119 /* GetIntSerialDenseMatrix test
120 engine.EvalString("ISDM=rand(8,2)*100", matlabBuffer, MATLABBUF);
121 cout << matlabBuffer << endl;
122 int procToGet = 1;
123 int M = 8;
124 int N = 2;
125 int* A = new int[M*N];
126 Epetra_IntSerialDenseMatrix getISDM (View, A, M, M, N);
127 int ierr = engine.GetIntSerialDenseMatrix("ISDM", getISDM, procToGet);
128 if (ierr) {
129 cout << "engine.GetIntSerialDenseMatrix(\"ISDM\", getISDM, procToGet) failed" << endl;
130 }
131
132 if (MyPID == 1) cout << getISDM << endl;
133 */
134
135 /* GetSerialDenseMatrix test
136 engine.EvalString("SDM=rand(8,2)", matlabBuffer, MATLABBUF);
137 cout << matlabBuffer << endl;
138 int procToGet = 1;
139 int M = 8;
140 int N = 2;
141 double* A = new double[M*N];
142 Epetra_SerialDenseMatrix getSDM (View, A, M, M, N);
143 int ierr = engine.GetSerialDenseMatrix("SDM", getSDM, procToGet);
144 if (ierr) {
145 cout << "engine.GetSerialDenseMatrix(\"SDM\", getSDM, procToGet) failed" << endl;
146 }
147
148 if (MyPID == 1) cout << getSDM << endl;
149 */
150
151 /* GetMultiVector test
152 if (comm.NumProc() != 2) {
153 if (MyPID == 0) cout << "Error: this test must be run with exactly two PE." << endl;
154 delete &engine;
155 #ifdef EPETRA_MPI
156 MPI_Finalize();
157 #endif
158 return(-1);
159 }
160 engine.EvalString("MV=rand(8,2)", matlabBuffer, MATLABBUF);
161 cout << matlabBuffer << endl;
162 int myM = 4;
163 int M = myM * comm.NumProc();
164 int N = 2;
165 Epetra_Map getMap (M, 0, comm);
166 double* A = new double[myM*N];
167 Epetra_MultiVector getMV (View, getMap, A, myM, N);
168 cout << "MultiVector created" << endl;
169 int ierr = engine.GetMultiVector("MV", getMV);
170 if (ierr) {
171 cout << "engine.GetMultiVector(\"MV\", getMV) failed" << endl;
172 }
173
174 cout << getMV << endl;
175 */
176
177 /* CrsMatrix test
178 int numGlobalElements = 8;
179 int numMyElements = 8/comm.NumProc();
180 int M=numGlobalElements/comm.NumProc();
181 int N=10;
182 int* myGlobalElements = new int[M];
183
184 int minGID = 0;
185 int startIndex = minGID + M*comm.MyPID();
186 for(int i=0; i < M; i++) {
187 myGlobalElements[i] = startIndex++;
188 }
189
190
191 int* colMapGIDs = new int[N];
192 for (int i=0; i < N; i++) {
193 colMapGIDs[i] = i;
194 }
195 //Epetra_Map map (numGlobalElements, numMyElements, myGlobalElements, minGID, comm);
196 Epetra_Map map (numGlobalElements, minGID, comm);
197 Epetra_Map colMap (N, N, colMapGIDs, 0, comm);
198
199 Epetra_CrsMatrix crsMatrix (Copy, map, colMap, N);
200
201 cout << "crs matrix created\n";
202
203 //int indices[8] = {-4,-3,-2,-1,0,1,2,3};
204 int indices[10] = {0,1,2,3,4,5,6,7,8,9};
205 double* values = new double[N];
206 double value = M * N * comm.MyPID();
207 for (int i=0; i < M; i++) {
208 if (i % 2 == 0) {
209 for (int j=0; j < N; j++) {
210 values[j] = value++;
211 }
212
213 crsMatrix.InsertGlobalValues(myGlobalElements[i], N, values, indices);
214 }
215 }
216
217 cout << "done putting values\n";
218 crsMatrix.FillComplete(colMap, map);
219 cout << "done filling crsMatrix and calling crsMatrix.FillComplete()\n";
220
221 cout << crsMatrix;
222
223 cout << "done printing crsMatrix\n";
224
225 //cout << map;
226 //cout << "done printing map\n";
227
228 int ierr = engine.PutRowMatrix(crsMatrix, "TEST", true);
229 //int ierr = engine.PutBlockMap(map, "TEST", true);
230 //cout << "done calling engine.PutRowMatrix(crsMatrix, \"TEST\", false)\n";
231 if (ierr != 0) {
232 cout << "engine.PutRowMatrix(crsMatrix, \"TEST\") returned nonzero result: " << ierr << "\n";
233 return(-1);
234 }
235
236 */
237
238 /* MultiVector test
239 cout << MyPID << " going to do multivector test...\n";
240 int numGlobalElements = 100;
241 int M = numGlobalElements/comm.NumProc();
242 int N = 3;
243 int numMyElements = M * N;
244 double* A = new double[numMyElements];
245 double* Aptr = A;
246 int startValue = 0;
247
248 cout << MyPID << " allocated space for A, now filling A\n";
249 for(int col=0; col < N; col++) {
250 startValue = (col * numGlobalElements) + (M * MyPID);
251 for(int row=0; row < M; row++) {
252 *Aptr++ = row+startValue;
253 }
254 }
255 cout << MyPID << " A filled\n";
256
257 Epetra_Map map (numGlobalElements, 0, comm);
258 Epetra_MultiVector multiVector (Copy, map, A, M, N);
259 //cout << multiVector;
260 engine.PutMultiVector(multiVector, "TEST");
261 */
262
263 /*SerialDenseMatrix test
264 cout << MyPID << " going to do SerialDenseMatrix test...\n";
265 double* A = new double[30];
266 cout << MyPID << " allocated space for A, now filling A\n";
267 double* Aptr = A;
268 int M = 5;
269 int N = 6;
270 int startValue = M*N*comm.MyPID();
271 for(int i=0; i < M*N; i++) {
272 *Aptr++ = i + startValue;
273 }
274 cout << MyPID << " A filled\n";
275
276 Epetra_SerialDenseMatrix sdMatrix (View, A, M, M, N);
277 engine.PutSerialDenseMatrix(sdMatrix, "TEST", 0);
278 cout << sdMatrix;
279 */
280
281 /* SerialDenseVector test
282 double* A = new double[30];
283 double* Aptr = A;
284 int length = 30;
285 for(int i=0; i < length; i++) {
286 *Aptr++ = i;
287 }
288
289 Epetra_SerialDenseVector sdVector (Copy, A, length);
290 engine.PutSerialDenseMatrix(sdVector, "SDVECTOR");
291 cout << sdVector;
292 */
293
294 /*IntSerialDenseMatrix test
295 cout << MyPID << " going to do IntSerialDenseMatrix test...\n";
296 int* A = new int[30];
297 cout << MyPID << " allocated space for A, now filling A\n";
298 int* Aptr = A;
299 int M = 5;
300 int N = 6;
301 int startValue = M*N*comm.MyPID();
302 for(int i=0; i < M*N; i++) {
303 *Aptr++ = i + startValue;
304 }
305 cout << MyPID << " A filled\n";
306 Epetra_IntSerialDenseMatrix isdMatrix (Copy, A, M, M, N);
307 cout << isdMatrix;
308 engine.PutIntSerialDenseMatrix(isdMatrix, "TEST", 0);
309 */
310
311
312 /* SerialDenseVector test
313 int* A = new int[30];
314 int* Aptr = A;
315 int length = 30;
316 for(int i=0; i < length; i++) {
317 *Aptr++ = i;
318 }
319
320 Epetra_IntSerialDenseVector isdVector (Copy, A, length);
321 engine.PutIntSerialDenseMatrix(isdVector, "ISDVECTOR");
322 cout << isdVector;
323 */
324
325 /*while(1) {
326
327 // do nothing
328 }*/
329
330 /*if (comm.NumProc() == 1) {
331 int err;
332 while(1) {
333 // Prompt the user and get a string
334 printf(">> ");
335 if (fgets(s, BUFSIZE, stdin) == NULL) {
336 printf("Bye\n");
337 break ;
338 }
339 printf ("command :%s:\n", s) ;
340
341 // Send the command to MATLAB
342 // output goes to stdout
343 err = engine.EvalString(s, matlabBuffer, MATLABBUF);
344 if (err != 0) {
345 printf("there was an error: %d", err);
346 err = 0;
347 }
348 else {
349 printf("Matlab Output:\n%s", matlabBuffer);
350 }
351 }
352 }*/
353
354 //delete engine ;
355
356 /*
357 engine.EvalString("size(TEST)", matlabBuffer, MATLABBUF);
358 cout << matlabBuffer << "\n";
359 engine.EvalString("TEST", matlabBuffer, MATLABBUF);
360 cout << matlabBuffer << "\n";
361 */
362
363 cout << "\n" << comm.MyPID() << " all done\n";
364
365#ifdef EPETRA_MPI
366 MPI_Finalize();
367#endif
368
369 delete enginePtr;
370
371 return(0);
372
373}
A class which provides data and command access to Matlab from Epetra.
int MyPID() const
int main(int argc, char *argv[])
#define MATLABBUF
#define BUFSIZE