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