Actual source code: ex15.c
2: static char help[] = "Tests Mathematica I/O of vectors and illustrates the use of user-defined event logging.\n\n";
4: #include <petscvec.h>
6: /* Note: Most applications would not read and write a vector within
7: the same program. This example is intended only to demonstrate
8: both input and output. */
10: int main(int argc, char *argv[])
11: {
12: PetscViewer viewer;
13: Vec u;
14: PetscScalar v;
15: int VECTOR_GENERATE, VECTOR_READ;
16: int i, m = 10, rank, size, low, high, ldim, iglobal;
17: int ierr;
19: PetscFunctionBeginUser;
20: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
22: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
23: PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
25: /* PART 1: Generate vector, then write it to Mathematica */
27: PetscCall(PetscLogEventRegister("Generate Vector", VEC_CLASSID, &VECTOR_GENERATE));
28: PetscCall(PetscLogEventBegin(VECTOR_GENERATE, 0, 0, 0, 0));
29: /* Generate vector */
30: PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
31: PetscCall(VecSetSizes(u, PETSC_DECIDE, m));
32: PetscCall(VecSetFromOptions(u));
33: PetscCall(VecGetOwnershipRange(u, &low, &high));
34: PetscCall(VecGetLocalSize(u, &ldim));
35: for (i = 0; i < ldim; i++) {
36: iglobal = i + low;
37: v = (PetscScalar)(i + 100 * rank);
38: PetscCall(VecSetValues(u, 1, &iglobal, &v, INSERT_VALUES));
39: }
40: PetscCall(VecAssemblyBegin(u));
41: PetscCall(VecAssemblyEnd(u));
42: PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));
44: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing vector to Mathematica...\n"));
46: #if 0
47: PetscCall(PetscViewerMathematicaOpen(PETSC_COMM_WORLD, 8000, "192.168.119.1", "Connect", &viewer));
48: PetscCall(VecView(u, viewer));
49: #else
50: PetscCall(VecView(u, PETSC_VIEWER_MATHEMATICA_WORLD));
51: #endif
52: v = 0.0;
53: PetscCall(VecSet(u, v));
54: PetscCall(PetscLogEventEnd(VECTOR_GENERATE, 0, 0, 0, 0));
56: /* All processors wait until test vector has been dumped */
57: PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
58: PetscCall(PetscSleep(10));
60: /* PART 2: Read in vector in from Mathematica */
62: PetscCall(PetscLogEventRegister("Read Vector", VEC_CLASSID, &VECTOR_READ));
63: PetscCall(PetscLogEventBegin(VECTOR_READ, 0, 0, 0, 0));
64: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading vector from Mathematica...\n"));
65: /* Read new vector in binary format */
66: #if 0
67: PetscCall(PetscViewerMathematicaGetVector(viewer, u));
68: PetscCall(PetscViewerDestroy(&viewer));
69: #else
70: PetscCall(PetscViewerMathematicaGetVector(PETSC_VIEWER_MATHEMATICA_WORLD, u));
71: #endif
72: PetscCall(PetscLogEventEnd(VECTOR_READ, 0, 0, 0, 0));
73: PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));
75: /* Free data structures */
76: PetscCall(VecDestroy(&u));
77: PetscCall(PetscFinalize());
78: return 0;
79: }