Actual source code: ex5.c
2: static char help[] = "Tests binary 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 **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: #if defined(PETSC_USE_LOG)
18: PetscLogEvent VECTOR_GENERATE,VECTOR_READ;
19: #endif
21: PetscInitialize(&argc,&args,(char*)0,help);
22: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
23: MPI_Comm_size(PETSC_COMM_WORLD,&size);
24: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
26: /* PART 1: Generate vector, then write it in binary format */
28: PetscLogEventRegister("Generate Vector",VEC_CLASSID,&VECTOR_GENERATE);
29: PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);
30: /* Generate vector */
31: VecCreate(PETSC_COMM_WORLD,&u);
32: VecSetSizes(u,PETSC_DECIDE,m);
33: VecSetFromOptions(u);
34: VecGetOwnershipRange(u,&low,&high);
35: VecGetLocalSize(u,&ldim);
36: for (i=0; i<ldim; i++) {
37: iglobal = i + low;
38: v = (PetscScalar)(i + 100*rank);
39: VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
40: }
41: VecAssemblyBegin(u);
42: VecAssemblyEnd(u);
43: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
45: PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
46: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
47: VecView(u,viewer);
48: PetscViewerDestroy(&viewer);
49: VecDestroy(&u);
50: PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");
52: PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);
54: /* PART 2: Read in vector in binary format */
56: /* Read new vector in binary format */
57: PetscLogEventRegister("Read Vector",VEC_CLASSID,&VECTOR_READ);
58: PetscLogEventBegin(VECTOR_READ,0,0,0,0);
59: PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
60: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
61: VecCreate(PETSC_COMM_WORLD,&u);
62: VecLoad(u,viewer);
63: PetscViewerDestroy(&viewer);
64: PetscLogEventEnd(VECTOR_READ,0,0,0,0);
65: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
67: /* Free data structures */
68: VecDestroy(&u);
69: PetscFinalize();
70: return 0;
71: }
73: /*TEST
75: test:
76: nsize: 1
77: requires: mpiio
79: test:
80: suffix: 2
81: nsize: 2
82: requires: mpiio
84: TEST*/