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: int main(int argc,char **args)
12: {
13: int i,m = 10,rank,size,low,high,ldim,iglobal,ierr;
14: PetscScalar v;
15: Vec u;
16: PetscViewer viewer;
17: int VECTOR_GENERATE,VECTOR_READ;
19: PetscInitialize(&argc,&args,(char *)0,help);
20: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
21: MPI_Comm_size(PETSC_COMM_WORLD,&size);
22: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
24: /* PART 1: Generate vector, then write it in binary format */
26: PetscLogEventRegister(&VECTOR_GENERATE,"Generate Vector",VEC_COOKIE);
27: PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);
28: /* Generate vector */
29: VecCreate(PETSC_COMM_WORLD,&u);
30: VecSetSizes(u,PETSC_DECIDE,m);
31: VecSetFromOptions(u);
32: VecGetOwnershipRange(u,&low,&high);
33: VecGetLocalSize(u,&ldim);
34: for (i=0; i<ldim; i++) {
35: iglobal = i + low;
36: v = (PetscScalar)(i + 100*rank);
37: VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
38: }
39: VecAssemblyBegin(u);
40: VecAssemblyEnd(u);
41: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
43: PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...n");
45: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",PETSC_BINARY_CREATE,&viewer);
46: VecView(u,viewer);
47: PetscViewerDestroy(viewer);
48: VecDestroy(u);
49: PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);
51: /* PART 2: Read in vector in binary format */
53: /* All processors wait until test vector has been dumped */
54: MPI_Barrier(PETSC_COMM_WORLD);
55: PetscSleep(10);
57: /* Read new vector in binary format */
58: PetscLogEventRegister(&VECTOR_READ,"Read Vector",VEC_COOKIE);
59: PetscLogEventBegin(VECTOR_READ,0,0,0,0);
60: PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...n");
61: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",PETSC_BINARY_RDONLY,&viewer);
62: VecLoad(viewer,&u);
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: }