Actual source code: ex10.c
petsc-3.7.5 2017-01-01
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,NULL,"-binary",&isbinary,NULL);
31: PetscOptionsGetBool(NULL,NULL,"-hdf5",&ishdf5,NULL);
32: PetscOptionsGetBool(NULL,NULL,"-mpiio",&mpiio_use,NULL);
33: PetscOptionsGetBool(NULL,NULL,"-sizes_set",&vstage2,NULL);
34: PetscOptionsGetBool(NULL,NULL,"-type_set",&vstage3,NULL);
36: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
37: MPI_Comm_size(PETSC_COMM_WORLD,&size);
38: PetscOptionsGetInt(NULL,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);
74: PetscLogEventEnd(VECTOR_GENERATE,0,0,0,0);
76: /* PART 2: Read in vector in binary format */
78: /* Read new vector in binary format */
79: PetscLogEventRegister("Read Vector",VEC_CLASSID,&VECTOR_READ);
80: PetscLogEventBegin(VECTOR_READ,0,0,0,0);
81: if (mpiio_use) {
82: PetscPrintf(PETSC_COMM_WORLD,"Using MPI IO for reading the vector\n");
83: PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");
84: }
85: if (isbinary) {
86: PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
87: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
88: PetscViewerBinarySetFlowControl(viewer,2);
89: #if defined(PETSC_HAVE_HDF5)
90: } else if (ishdf5) {
91: PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");
92: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
93: #endif
94: }
95: VecCreate(PETSC_COMM_WORLD,&u);
96: PetscObjectSetName((PetscObject) u,"Test_Vec");
98: if (vstage2) {
99: PetscPrintf(PETSC_COMM_WORLD,"Setting vector sizes...\n");
100: if (size > 1) {
101: if (!rank) {
102: lsize = m/size + size;
103: VecSetSizes(u,lsize,m);
104: } else if (rank == size-1) {
105: lsize = m/size - size;
106: VecSetSizes(u,lsize,m);
107: } else {
108: lsize = m/size;
109: VecSetSizes(u,lsize,m);
110: }
111: } else {
112: VecSetSizes(u,m,m);
113: }
114: }
116: if (vstage3) {
117: PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
118: VecSetType(u, VECMPI);
119: }
120: VecLoad(u,viewer);
121: PetscViewerDestroy(&viewer);
122: PetscLogEventEnd(VECTOR_READ,0,0,0,0);
123: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
125: /* Free data structures */
126: VecDestroy(&u);
127: PetscFinalize();
128: return 0;
129: }