Actual source code: ex5.c
1: /*$Id: ex5.c,v 1.50 2001/08/07 03:02:34 balay Exp $*/
3: static char help[] = "Tests binary I/O of vectors and illustrates the use of user-defined event logging.nn";
5: #include petscvec.h
7: /* Note: Most applications would not read and write a vector within
8: the same program. This example is intended only to demonstrate
9: both input and output. */
11: #undef __FUNCT__
13: int main(int argc,char **args)
14: {
15: int i,m = 10,rank,size,low,high,ldim,iglobal,ierr;
16: PetscScalar v;
17: Vec u;
18: PetscViewer viewer;
19: int VECTOR_GENERATE,VECTOR_READ;
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(PETSC_NULL,"-m",&m,PETSC_NULL);
26: /* PART 1: Generate vector, then write it in binary format */
28: PetscLogEventRegister(&VECTOR_GENERATE,"Generate Vector",VEC_COOKIE);
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");
47: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",PETSC_BINARY_CREATE,&viewer);
48: VecView(u,viewer);
49: PetscViewerDestroy(viewer);
50: VecDestroy(u);
51: PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);
53: /* PART 2: Read in vector in binary format */
55: /* All processors wait until test vector has been dumped */
56: MPI_Barrier(PETSC_COMM_WORLD);
57: PetscSleep(10);
59: /* Read new vector in binary format */
60: PetscLogEventRegister(&VECTOR_READ,"Read Vector",VEC_COOKIE);
61: PetscLogEventBegin(VECTOR_READ,0,0,0,0);
62: PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...n");
63: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",PETSC_BINARY_RDONLY,&viewer);
64: VecLoad(viewer,&u);
65: PetscViewerDestroy(viewer);
66: PetscLogEventEnd(VECTOR_READ,0,0,0,0);
67: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
69: /* Free data structures */
70: VecDestroy(u);
71: PetscFinalize();
72: return 0;
73: }