Actual source code: ex100.cu
2: static char help[] = "Tests I/O of vectors for different data formats (binary,HDF5)\n\n";
4: #include <petscvec.h>
5: #include <petscdevice.h>
6: #include <petscviewerhdf5.h>
8: /* Note: Most applications would not read and write a vector within
9: the same program. This example is intended only to demonstrate
10: both input and output and is written for use with either 1,2,or 4 processors. */
12: int main(int argc,char **args)
13: {
14: PetscMPIInt rank,size;
15: PetscInt i,m = 20,low,high,ldim,iglobal,lsize;
16: PetscScalar v;
17: Vec u;
18: PetscViewer viewer;
19: PetscBool vstage2,vstage3,mpiio_use,isbinary = PETSC_FALSE;
20: VecType vectype;
21: #if defined(PETSC_HAVE_HDF5)
22: PetscBool ishdf5 = PETSC_FALSE;
23: #endif
24: #if defined(PETSC_HAVE_ADIOS)
25: PetscBool isadios = PETSC_FALSE;
26: #endif
27: PetscScalar const *values;
29: PetscInitialize(&argc,&args,(char*)0,help);
30: {
31: PetscDeviceContext dctx; /* unused, only there to force initialization of device */
33: PetscDeviceContextGetCurrentContext(&dctx);
34: }
36: mpiio_use = vstage2 = vstage3 = PETSC_FALSE;
38: PetscOptionsGetBool(NULL,NULL,"-binary",&isbinary,NULL);
39: #if defined(PETSC_HAVE_HDF5)
40: PetscOptionsGetBool(NULL,NULL,"-hdf5",&ishdf5,NULL);
41: #endif
42: #if defined(PETSC_HAVE_ADIOS)
43: PetscOptionsGetBool(NULL,NULL,"-adios",&isadios,NULL);
44: #endif
45: PetscOptionsGetBool(NULL,NULL,"-mpiio",&mpiio_use,NULL);
46: PetscOptionsGetBool(NULL,NULL,"-sizes_set",&vstage2,NULL);
47: PetscOptionsGetBool(NULL,NULL,"-type_set",&vstage3,NULL);
49: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
50: MPI_Comm_size(PETSC_COMM_WORLD,&size);
51: PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
53: /* PART 1: Generate vector, then write it in the given data format */
55: /* Generate vector */
56: VecCreate(PETSC_COMM_WORLD,&u);
57: VecSetType(u, VECCUDA);
58: PetscObjectSetName((PetscObject)u, "Test_Vec");
59: VecSetSizes(u,PETSC_DECIDE,m);
60: VecSetFromOptions(u);
61: VecGetOwnershipRange(u,&low,&high);
62: VecGetLocalSize(u,&ldim);
63: for (i=0; i<ldim; i++) {
64: iglobal = i + low;
65: v = (PetscScalar)(i + low);
66: VecSetValues(u,1,&iglobal,&v,INSERT_VALUES);
67: }
68: VecAssemblyBegin(u);
69: VecAssemblyEnd(u);
70: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
72: if (isbinary) {
73: PetscPrintf(PETSC_COMM_WORLD,"writing vector in binary to vector.dat ...\n");
74: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
75: #if defined(PETSC_HAVE_HDF5)
76: } else if (ishdf5) {
77: PetscPrintf(PETSC_COMM_WORLD,"writing vector in hdf5 to vector.dat ...\n");
78: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
79: #endif
80: #if defined(PETSC_HAVE_ADIOS)
81: } else if (isadios) {
82: PetscPrintf(PETSC_COMM_WORLD,"writing vector in adios to vector.dat ...\n");
83: PetscViewerADIOSOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_WRITE,&viewer);
84: #endif
85: } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No data format specified, run with one of -binary -hdf5 -adios options");
86: VecView(u,viewer);
87: PetscViewerDestroy(&viewer);
88: VecDestroy(&u);
90: /* PART 2: Read in vector in binary format */
91: /* Read new vector in binary format */
92: if (mpiio_use) {
93: PetscPrintf(PETSC_COMM_WORLD,"Using MPI IO for reading the vector\n");
94: PetscOptionsSetValue(NULL,"-viewer_binary_mpiio","");
95: }
96: if (isbinary) {
97: PetscPrintf(PETSC_COMM_WORLD,"reading vector in binary from vector.dat ...\n");
98: PetscViewerBinaryOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
99: PetscViewerBinarySetFlowControl(viewer,2);
100: #if defined(PETSC_HAVE_HDF5)
101: } else if (ishdf5) {
102: PetscPrintf(PETSC_COMM_WORLD,"reading vector in hdf5 from vector.dat ...\n");
103: PetscViewerHDF5Open(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
104: #endif
105: #if defined(PETSC_HAVE_ADIOS)
106: } else if (isadios) {
107: PetscPrintf(PETSC_COMM_WORLD,"reading vector in adios from vector.dat ...\n");
108: PetscViewerADIOSOpen(PETSC_COMM_WORLD,"vector.dat",FILE_MODE_READ,&viewer);
109: #endif
110: }
111: VecCreate(PETSC_COMM_WORLD,&u);
112: PetscObjectSetName((PetscObject) u,"Test_Vec");
113: if (vstage2) {
114: PetscPrintf(PETSC_COMM_WORLD,"Setting vector sizes...\n");
115: if (size > 1) {
116: if (rank == 0) {
117: lsize = m/size + size;
118: VecSetSizes(u,lsize,m);
119: } else if (rank == size-1) {
120: lsize = PetscMax(m/size - size,0);
121: VecSetSizes(u,lsize,m);
122: } else {
123: lsize = m/size;
124: VecSetSizes(u,lsize,m);
125: }
126: } else {
127: VecSetSizes(u,m,m);
128: }
129: }
131: PetscPrintf(PETSC_COMM_WORLD,"Setting vector type...\n");
132: VecSetType(u, VECCUDA);
133: VecGetType(u, &vectype);
134: PetscPrintf(PETSC_COMM_WORLD, "Before load, vectype is : %s\n", (char*)vectype);
135: VecLoad(u,viewer);
136: VecGetType(u, &vectype);
137: PetscPrintf(PETSC_COMM_WORLD, "After load, vectype is : %s\n", (char*)vectype);
138: PetscViewerDestroy(&viewer);
139: VecView(u,PETSC_VIEWER_STDOUT_WORLD);
140: VecGetArrayRead(u,&values);
141: VecGetLocalSize(u,&ldim);
142: VecGetOwnershipRange(u,&low,NULL);
143: for (i=0; i<ldim; i++) {
145: }
146: VecRestoreArrayRead(u,&values);
148: /* Free data structures */
149: VecDestroy(&u);
150: PetscFinalize();
151: return 0;
152: }
154: /*TEST
156: build:
157: requires: cuda
159: test:
160: nsize: 2
161: args: -binary
163: test:
164: suffix: 2
165: nsize: 3
166: args: -binary
168: test:
169: suffix: 3
170: nsize: 5
171: args: -binary
173: test:
174: suffix: 4
175: requires: hdf5
176: nsize: 2
177: args: -hdf5
179: test:
180: suffix: 5
181: nsize: 4
182: args: -binary -sizes_set
184: test:
185: suffix: 6
186: requires: hdf5
187: nsize: 4
188: args: -hdf5 -sizes_set
190: TEST*/