Actual source code: ex15.c

  1: static char help[] = "Tests Mathematica I/O of vectors and illustrates the use of user-defined event logging.\n\n";

  3: #include <petscvec.h>

  5: /* Note:  Most applications would not read and write a vector within
  6:   the same program.  This example is intended only to demonstrate
  7:   both input and output. */

  9: int main(int argc, char *argv[])
 10: {
 11:   PetscViewer viewer;
 12:   Vec         u;
 13:   PetscScalar v;
 14:   int         VECTOR_GENERATE, VECTOR_READ;
 15:   int         i, m = 10, rank, size, low, high, ldim, iglobal;
 16:   int         ierr;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 20:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 21:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 22:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));

 24:   /* PART 1:  Generate vector, then write it to Mathematica */

 26:   PetscCall(PetscLogEventRegister("Generate Vector", VEC_CLASSID, &VECTOR_GENERATE));
 27:   PetscCall(PetscLogEventBegin(VECTOR_GENERATE, 0, 0, 0, 0));
 28:   /* Generate vector */
 29:   PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
 30:   PetscCall(VecSetSizes(u, PETSC_DECIDE, m));
 31:   PetscCall(VecSetFromOptions(u));
 32:   PetscCall(VecGetOwnershipRange(u, &low, &high));
 33:   PetscCall(VecGetLocalSize(u, &ldim));
 34:   for (i = 0; i < ldim; i++) {
 35:     iglobal = i + low;
 36:     v       = (PetscScalar)(i + 100 * rank);
 37:     PetscCall(VecSetValues(u, 1, &iglobal, &v, INSERT_VALUES));
 38:   }
 39:   PetscCall(VecAssemblyBegin(u));
 40:   PetscCall(VecAssemblyEnd(u));
 41:   PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));

 43:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing vector to Mathematica...\n"));

 45: #if 0
 46:   PetscCall(PetscViewerMathematicaOpen(PETSC_COMM_WORLD, 8000, "192.168.119.1", "Connect", &viewer));
 47:   PetscCall(VecView(u, viewer));
 48: #else
 49:   PetscCall(VecView(u, PETSC_VIEWER_MATHEMATICA_WORLD));
 50: #endif
 51:   v = 0.0;
 52:   PetscCall(VecSet(u, v));
 53:   PetscCall(PetscLogEventEnd(VECTOR_GENERATE, 0, 0, 0, 0));

 55:   /* All processors wait until test vector has been dumped */
 56:   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
 57:   PetscCall(PetscSleep(10));

 59:   /* PART 2:  Read in vector in from Mathematica */

 61:   PetscCall(PetscLogEventRegister("Read Vector", VEC_CLASSID, &VECTOR_READ));
 62:   PetscCall(PetscLogEventBegin(VECTOR_READ, 0, 0, 0, 0));
 63:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading vector from Mathematica...\n"));
 64:   /* Read new vector in binary format */
 65: #if 0
 66:   PetscCall(PetscViewerMathematicaGetVector(viewer, u));
 67:   PetscCall(PetscViewerDestroy(&viewer));
 68: #else
 69:   PetscCall(PetscViewerMathematicaGetVector(PETSC_VIEWER_MATHEMATICA_WORLD, u));
 70: #endif
 71:   PetscCall(PetscLogEventEnd(VECTOR_READ, 0, 0, 0, 0));
 72:   PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));

 74:   /* Free data structures */
 75:   PetscCall(VecDestroy(&u));
 76:   PetscCall(PetscFinalize());
 77:   return 0;
 78: }