Actual source code: ex10.c
petsc-3.3-p7 2013-05-11
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>
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 and is written for use with either 1,2,or 4 processors. */
12: int main(int argc,char **args)
13: {
15: PetscMPIInt rank,size;
16: PetscInt i,m = 20,low,high,ldim,iglobal,lsize;
17: PetscScalar v;
18: Vec u;
19: PetscViewer viewer;
20: PetscBool vstage2,vstage3,mpiio_use,isbinary,ishdf5;
21: #if defined(PETSC_USE_LOG)
22: PetscLogEvent VECTOR_GENERATE,VECTOR_READ;
23: #endif
25: PetscInitialize(&argc,&args,(char *)0,help);
26: isbinary = ishdf5 = PETSC_FALSE;
27: mpiio_use = vstage2 = vstage3 = PETSC_FALSE;
28: PetscOptionsGetBool(PETSC_NULL,"-binary",&isbinary,PETSC_NULL);
29: PetscOptionsGetBool(PETSC_NULL,"-hdf5",&ishdf5,PETSC_NULL);
30: PetscOptionsGetBool(PETSC_NULL,"-mpiio",&mpiio_use,PETSC_NULL);
31: PetscOptionsGetBool(PETSC_NULL,"-sizes_set",&vstage2,PETSC_NULL);
32: PetscOptionsGetBool(PETSC_NULL,"-type_set",&vstage3,PETSC_NULL);
34: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
35: MPI_Comm_size(PETSC_COMM_WORLD,&size);
36: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
38: /* PART 1: Generate vector, then write it in the given data format */
40: PetscLogEventRegister("Generate Vector",VEC_CLASSID,&VECTOR_GENERATE);
41: PetscLogEventBegin(VECTOR_GENERATE,0,0,0,0);
42: /* Generate vector */
43: VecCreate(PETSC_COMM_WORLD,&u);
44: PetscObjectSetName((PetscObject)u, "Test_Vec");
45: VecSetSizes(u,PETSC_DECIDE,m);
46: VecSetFromOptions(u);
47: VecGetOwnershipRange(u,&low,&high);
48: VecGetLocalSize(u,&ldim);
49: for (i=0; i<ldim; i++) {
50: iglobal = i + low;
51: v = (PetscScalar)(i + 100*rank);
52: VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
53: }
54: VecAssemblyBegin(u);
55: VecAssemblyEnd(u);
56: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
58: if (isbinary) {
59: PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
60: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
61: #if defined(PETSC_HAVE_HDF5)
62: } else if (ishdf5) {
63: PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");
64: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
65: #endif
66: } else {
67: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No data format specified, run with either -binary or -hdf5 option\n");
68: }
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: }
106: else if (rank == size-1) {
107: lsize = m/size - size;
108: VecSetSizes(u,lsize,m);
109: }
110: else {
111: lsize = m/size;
112: VecSetSizes(u,lsize,m);
113: }
114: } else {
115: VecSetSizes(u,m,m);
116: }
117: }
119: if (vstage3) {
120: PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
121: VecSetType(u, VECMPI);
122: }
123: VecLoad(u,viewer);
124: PetscViewerDestroy(&viewer);
125: PetscLogEventEnd(VECTOR_READ,0,0,0,0);
126: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
128: /* Free data structures */
129: VecDestroy(&u);
130: PetscFinalize();
131: return 0;
132: }