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;

 20:   PetscInitialize(&argc, &argv, NULL, help);
 21:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
 22:   MPI_Comm_size(PETSC_COMM_WORLD, &size);
 23:   PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL);

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

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

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

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

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

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

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

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