Actual source code: ex10.c
petsc-3.4.5 2014-06-29
2: static char help[] = "Tests I/O of vectors for different data formats (binary,HDF5,NetCDF) and illustrates the use of user-defined event logging\n\n";
4: #include <petscvec.h>
5: #include <petscviewerhdf5.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 and is written for use with either 1,2,or 4 processors. */
13: int main(int argc,char **args)
14: {
16: PetscMPIInt rank,size;
17: PetscInt i,m = 20,low,high,ldim,iglobal,lsize;
18: PetscScalar v;
19: Vec u;
20: PetscViewer viewer;
21: PetscBool vstage2,vstage3,mpiio_use,isbinary,ishdf5;
22: #if defined(PETSC_USE_LOG)
23: PetscLogEvent VECTOR_GENERATE,VECTOR_READ;
24: #endif
26: PetscInitialize(&argc,&args,(char*)0,help);
27: isbinary = ishdf5 = PETSC_FALSE;
28: mpiio_use = vstage2 = vstage3 = PETSC_FALSE;
30: PetscOptionsGetBool(NULL,"-binary",&isbinary,NULL);
31: PetscOptionsGetBool(NULL,"-hdf5",&ishdf5,NULL);
32: PetscOptionsGetBool(NULL,"-mpiio",&mpiio_use,NULL);
33: PetscOptionsGetBool(NULL,"-sizes_set",&vstage2,NULL);
34: PetscOptionsGetBool(NULL,"-type_set",&vstage3,NULL);
36: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
37: MPI_Comm_size(PETSC_COMM_WORLD,&size);
38: PetscOptionsGetInt(NULL,"-m",&m,NULL);
40: /* PART 1: Generate vector, then write it in the given data format */
42: PetscLogEventRegister("Generate Vector",VEC_CLASSID,&VECTOR_GENERATE);
43: PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);
44: /* Generate vector */
45: VecCreate(PETSC_COMM_WORLD,&u);
46: PetscObjectSetName((PetscObject)u, "Test_Vec");
47: VecSetSizes(u,PETSC_DECIDE,m);
48: VecSetFromOptions(u);
49: VecGetOwnershipRange(u,&low,&high);
50: VecGetLocalSize(u,&ldim);
51: for (i=0; i<ldim; i++) {
52: iglobal = i + low;
53: v = (PetscScalar)(i + 100*rank);
54: VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
55: }
56: VecAssemblyBegin(u);
57: VecAssemblyEnd(u);
58: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
60: if (isbinary) {
61: PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
62: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
63: #if defined(PETSC_HAVE_HDF5)
64: } else if (ishdf5) {
65: PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");
66: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
67: #endif
68: } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No data format specified, run with either -binary or -hdf5 option\n");
69: VecView(u,viewer);
70: PetscViewerDestroy(&viewer);
71: VecDestroy(&u);
72: /* PetscOptionsClear(); */
75: PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);
77: /* PART 2: Read in vector in binary format */
79: /* Read new vector in binary format */
80: PetscLogEventRegister("Read Vector",VEC_CLASSID,&VECTOR_READ);
81: PetscLogEventBegin(VECTOR_READ,0,0,0,0);
82: if (mpiio_use) {
83: PetscPrintf(PETSC_COMM_WORLD,"Using MPI IO for reading the vector\n");
84: PetscOptionsSetValue("-viewer_binary_mpiio","");
85: }
86: if (isbinary) {
87: PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
88: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
89: PetscViewerBinarySetFlowControl(viewer,2);
90: #if defined(PETSC_HAVE_HDF5)
91: } else if (ishdf5) {
92: PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");
93: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
94: #endif
95: }
96: VecCreate(PETSC_COMM_WORLD,&u);
97: PetscObjectSetName((PetscObject) u,"Test_Vec");
99: if (vstage2) {
100: PetscPrintf(PETSC_COMM_WORLD,"Setting vector sizes...\n");
101: if (size > 1) {
102: if (!rank) {
103: lsize = m/size + size;
104: VecSetSizes(u,lsize,m);
105: } else if (rank == size-1) {
106: lsize = m/size - size;
107: VecSetSizes(u,lsize,m);
108: } else {
109: lsize = m/size;
110: VecSetSizes(u,lsize,m);
111: }
112: } else {
113: VecSetSizes(u,m,m);
114: }
115: }
117: if (vstage3) {
118: PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
119: VecSetType(u, VECMPI);
120: }
121: VecLoad(u,viewer);
122: PetscViewerDestroy(&viewer);
123: PetscLogEventEnd(VECTOR_READ,0,0,0,0);
124: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
126: /* Free data structures */
127: VecDestroy(&u);
128: PetscFinalize();
129: return 0;
130: }