xref: /petsc/src/vec/vec/tutorials/ex5.c (revision 34e79e724ed5272e30e829101ec0b1afacc04871)
1 
2 static char help[] = "Tests binary I/O of vectors and illustrates the use of user-defined event logging.\n\n";
3 
4 #include <petscvec.h>
5 
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. */
9 
10 int main(int argc, char **args)
11 {
12   PetscMPIInt   rank, size;
13   PetscInt      i, m = 10, low, high, ldim, iglobal;
14   PetscScalar   v;
15   Vec           u;
16   PetscViewer   viewer;
17   PetscLogEvent VECTOR_GENERATE, VECTOR_READ;
18 
19   PetscFunctionBeginUser;
20   PetscCall(PetscInitialize(&argc, &args, (char *)0, 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));
24 
25   /* PART 1:  Generate vector, then write it in binary format */
26 
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));
43 
44   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing vector in binary to vector.dat ...\n"));
45   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_WRITE, &viewer));
46   PetscCall(VecView(u, viewer));
47   PetscCall(PetscViewerDestroy(&viewer));
48   PetscCall(VecDestroy(&u));
49   PetscCall(PetscOptionsSetValue(NULL, "-viewer_binary_mpiio", ""));
50 
51   PetscCall(PetscLogEventEnd(VECTOR_GENERATE, 0, 0, 0, 0));
52 
53   /* PART 2:  Read in vector in binary format */
54 
55   /* Read new vector in binary format */
56   PetscCall(PetscLogEventRegister("Read Vector", VEC_CLASSID, &VECTOR_READ));
57   PetscCall(PetscLogEventBegin(VECTOR_READ, 0, 0, 0, 0));
58   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading vector in binary from vector.dat ...\n"));
59   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "vector.dat", FILE_MODE_READ, &viewer));
60   PetscCall(VecCreate(PETSC_COMM_WORLD, &u));
61   PetscCall(VecLoad(u, viewer));
62   PetscCall(PetscViewerDestroy(&viewer));
63   PetscCall(PetscLogEventEnd(VECTOR_READ, 0, 0, 0, 0));
64   PetscCall(VecView(u, PETSC_VIEWER_STDOUT_WORLD));
65 
66   /* Free data structures */
67   PetscCall(VecDestroy(&u));
68   PetscCall(PetscFinalize());
69   return 0;
70 }
71 
72 /*TEST
73 
74      test:
75        nsize: 1
76        requires: mpiio
77 
78      test:
79        suffix: 2
80        nsize: 2
81        requires: mpiio
82 
83 TEST*/
84