Actual source code: gr2.c
2: /*
3: Plots vectors obtained with DMDACreate2d()
4: */
6: #include <private/daimpl.h> /*I "petscdmda.h" I*/
7: #include <private/vecimpl.h>
9: /*
10: The data that is passed into the graphics callback
11: */
12: typedef struct {
13: PetscInt m,n,step,k;
14: PetscReal min,max,scale;
15: PetscScalar *xy,*v;
16: PetscBool showgrid;
17: } ZoomCtx;
19: /*
20: This does the drawing for one particular field
21: in one particular set of coordinates. It is a callback
22: called from PetscDrawZoom()
23: */
26: PetscErrorCode VecView_MPI_Draw_DA2d_Zoom(PetscDraw draw,void *ctx)
27: {
28: ZoomCtx *zctx = (ZoomCtx*)ctx;
30: PetscInt m,n,i,j,k,step,id,c1,c2,c3,c4;
31: PetscReal s,min,x1,x2,x3,x4,y_1,y2,y3,y4;
32: PetscScalar *v,*xy;
35: m = zctx->m;
36: n = zctx->n;
37: step = zctx->step;
38: k = zctx->k;
39: v = zctx->v;
40: xy = zctx->xy;
41: s = zctx->scale;
42: min = zctx->min;
43:
44: /* PetscDraw the contour plot patch */
45: for (j=0; j<n-1; j++) {
46: for (i=0; i<m-1; i++) {
47: #if !defined(PETSC_USE_COMPLEX)
48: id = i+j*m; x1 = xy[2*id];y_1 = xy[2*id+1];c1 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
49: id = i+j*m+1; x2 = xy[2*id];y2 = y_1; c2 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
50: id = i+j*m+1+m;x3 = x2; y3 = xy[2*id+1];c3 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
51: id = i+j*m+m; x4 = x1; y4 = y3; c4 = (int)(PETSC_DRAW_BASIC_COLORS+s*(v[k+step*id]-min));
52: #else
53: id = i+j*m; x1 = PetscRealPart(xy[2*id]);y_1 = PetscRealPart(xy[2*id+1]);c1 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
54: id = i+j*m+1; x2 = PetscRealPart(xy[2*id]);y2 = y_1; c2 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
55: id = i+j*m+1+m;x3 = x2; y3 = PetscRealPart(xy[2*id+1]);c3 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
56: id = i+j*m+m; x4 = x1; y4 = y3; c4 = (int)(PETSC_DRAW_BASIC_COLORS+s*(PetscRealPart(v[k+step*id])-min));
57: #endif
58: PetscDrawTriangle(draw,x1,y_1,x2,y2,x3,y3,c1,c2,c3);
59: PetscDrawTriangle(draw,x1,y_1,x3,y3,x4,y4,c1,c3,c4);
60: if (zctx->showgrid) {
61: PetscDrawLine(draw,x1,y_1,x2,y2,PETSC_DRAW_BLACK);
62: PetscDrawLine(draw,x2,y2,x3,y3,PETSC_DRAW_BLACK);
63: PetscDrawLine(draw,x3,y3,x4,y4,PETSC_DRAW_BLACK);
64: PetscDrawLine(draw,x4,y4,x1,y_1,PETSC_DRAW_BLACK);
65: }
66: }
67: }
68: return(0);
69: }
73: PetscErrorCode VecView_MPI_Draw_DA2d(Vec xin,PetscViewer viewer)
74: {
75: DM da,dac,dag;
76: PetscErrorCode ierr;
77: PetscMPIInt rank;
78: PetscInt N,s,M,w;
79: const PetscInt *lx,*ly;
80: PetscReal coors[4],ymin,ymax,xmin,xmax;
81: PetscDraw draw,popup;
82: PetscBool isnull,useports = PETSC_FALSE;
83: MPI_Comm comm;
84: Vec xlocal,xcoor,xcoorl;
85: DMDABoundaryType bx,by;
86: DMDAStencilType st;
87: ZoomCtx zctx;
88: PetscDrawViewPorts *ports = PETSC_NULL;
89: PetscViewerFormat format;
90: PetscInt *displayfields;
91: PetscInt ndisplayfields,i,nbounds;
92: PetscBool flg;
93: const PetscReal *bounds;
96: zctx.showgrid = PETSC_FALSE;
97: PetscViewerDrawGetDraw(viewer,0,&draw);
98: PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
99: PetscViewerDrawGetBounds(viewer,&nbounds,&bounds);
101: PetscObjectQuery((PetscObject)xin,"DM",(PetscObject*)&da);
102: if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
104: PetscObjectGetComm((PetscObject)xin,&comm);
105: MPI_Comm_rank(comm,&rank);
107: DMDAGetInfo(da,0,&M,&N,0,&zctx.m,&zctx.n,0,&w,&s,&bx,&by,0,&st);
108: DMDAGetOwnershipRanges(da,&lx,&ly,PETSC_NULL);
110: /*
111: Obtain a sequential vector that is going to contain the local values plus ONE layer of
112: ghosted values to draw the graphics from. We also need its corresponding DMDA (dac) that will
113: update the local values pluse ONE layer of ghost values.
114: */
115: PetscObjectQuery((PetscObject)da,"GraphicsGhosted",(PetscObject*)&xlocal);
116: if (!xlocal) {
117: if (bx != DMDA_BOUNDARY_NONE || by != DMDA_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) {
118: /*
119: if original da is not of stencil width one, or periodic or not a box stencil then
120: create a special DMDA to handle one level of ghost points for graphics
121: */
122: DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,zctx.m,zctx.n,w,1,lx,ly,&dac);
123: PetscInfo(da,"Creating auxilary DMDA for managing graphics ghost points\n");
124: } else {
125: /* otherwise we can use the da we already have */
126: dac = da;
127: }
128: /* create local vector for holding ghosted values used in graphics */
129: DMCreateLocalVector(dac,&xlocal);
130: if (dac != da) {
131: /* don't keep any public reference of this DMDA, is is only available through xlocal */
132: PetscObjectDereference((PetscObject)dac);
133: } else {
134: /* remove association between xlocal and da, because below we compose in the opposite
135: direction and if we left this connect we'd get a loop, so the objects could
136: never be destroyed */
137: PetscObjectRemoveReference((PetscObject)xlocal,"DM");
138: }
139: PetscObjectCompose((PetscObject)da,"GraphicsGhosted",(PetscObject)xlocal);
140: PetscObjectDereference((PetscObject)xlocal);
141: } else {
142: if (bx != DMDA_BOUNDARY_NONE || by != DMDA_BOUNDARY_NONE || s != 1 || st != DMDA_STENCIL_BOX) {
143: PetscObjectQuery((PetscObject)xlocal,"DM",(PetscObject*)&dac);
144: } else {
145: dac = da;
146: }
147: }
149: /*
150: Get local (ghosted) values of vector
151: */
152: DMGlobalToLocalBegin(dac,xin,INSERT_VALUES,xlocal);
153: DMGlobalToLocalEnd(dac,xin,INSERT_VALUES,xlocal);
154: VecGetArray(xlocal,&zctx.v);
156: /* get coordinates of nodes */
157: DMDAGetCoordinates(da,&xcoor);
158: if (!xcoor) {
159: DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,0.0);
160: DMDAGetCoordinates(da,&xcoor);
161: }
163: /*
164: Determine the min and max coordinates in plot
165: */
166: VecStrideMin(xcoor,0,PETSC_NULL,&xmin);
167: VecStrideMax(xcoor,0,PETSC_NULL,&xmax);
168: VecStrideMin(xcoor,1,PETSC_NULL,&ymin);
169: VecStrideMax(xcoor,1,PETSC_NULL,&ymax);
170: coors[0] = xmin - .05*(xmax- xmin); coors[2] = xmax + .05*(xmax - xmin);
171: coors[1] = ymin - .05*(ymax- ymin); coors[3] = ymax + .05*(ymax - ymin);
172: PetscInfo4(da,"Preparing DMDA 2d contour plot coordinates %G %G %G %G\n",coors[0],coors[1],coors[2],coors[3]);
174: /*
175: get local ghosted version of coordinates
176: */
177: PetscObjectQuery((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject*)&xcoorl);
178: if (!xcoorl) {
179: /* create DMDA to get local version of graphics */
180: DMDACreate2d(comm,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,zctx.m,zctx.n,2,1,lx,ly,&dag);
181: PetscInfo(dag,"Creating auxilary DMDA for managing graphics coordinates ghost points\n");
182: DMCreateLocalVector(dag,&xcoorl);
183: PetscObjectCompose((PetscObject)da,"GraphicsCoordinateGhosted",(PetscObject)xcoorl);
184: PetscObjectDereference((PetscObject)dag);
185: PetscObjectDereference((PetscObject)xcoorl);
186: } else {
187: PetscObjectQuery((PetscObject)xcoorl,"DM",(PetscObject*)&dag);
188: }
189: DMGlobalToLocalBegin(dag,xcoor,INSERT_VALUES,xcoorl);
190: DMGlobalToLocalEnd(dag,xcoor,INSERT_VALUES,xcoorl);
191: VecGetArray(xcoorl,&zctx.xy);
192:
193: /*
194: Get information about size of area each processor must do graphics for
195: */
196: DMDAGetInfo(dac,0,&M,&N,0,0,0,0,&zctx.step,0,&bx,&by,0,0);
197: DMDAGetGhostCorners(dac,0,0,0,&zctx.m,&zctx.n,0);
199: PetscOptionsGetBool(PETSC_NULL,"-draw_contour_grid",&zctx.showgrid,PETSC_NULL);
200: PetscMalloc(zctx.step*sizeof(PetscInt),&displayfields);
201: for (i=0; i<zctx.step; i++) displayfields[i] = i;
202: ndisplayfields = zctx.step;
203: PetscOptionsGetIntArray(PETSC_NULL,"-draw_fields",displayfields,&ndisplayfields,&flg);
204: if (!flg) ndisplayfields = zctx.step;
206: PetscViewerGetFormat(viewer,&format);
207: PetscOptionsGetBool(PETSC_NULL,"-draw_ports",&useports,PETSC_NULL);
208: if (useports || format == PETSC_VIEWER_DRAW_PORTS){
209: PetscDrawSynchronizedClear(draw);
210: PetscDrawViewPortsCreate(draw,ndisplayfields,&ports);
211: }
213: /*
214: Loop over each field; drawing each in a different window
215: */
216: for (i=0; i<ndisplayfields; i++) {
217: zctx.k = displayfields[i];
218: if (useports) {
219: PetscDrawViewPortsSet(ports,i);
220: } else {
221: PetscViewerDrawGetDraw(viewer,i,&draw);
222: PetscDrawSynchronizedClear(draw);
223: }
225: /*
226: Determine the min and max color in plot
227: */
228: VecStrideMin(xin,zctx.k,PETSC_NULL,&zctx.min);
229: VecStrideMax(xin,zctx.k,PETSC_NULL,&zctx.max);
230: if (zctx.k < nbounds) {
231: zctx.min = PetscMin(zctx.min,bounds[2*zctx.k]);
232: zctx.max = PetscMax(zctx.max,bounds[2*zctx.k+1]);
233: }
234: if (zctx.min == zctx.max) {
235: zctx.min -= 1.e-12;
236: zctx.max += 1.e-12;
237: }
239: if (!rank) {
240: const char *title;
242: DMDAGetFieldName(da,zctx.k,&title);
243: if (title) {
244: PetscDrawSetTitle(draw,title);
245: }
246: }
247: PetscDrawSetCoordinates(draw,coors[0],coors[1],coors[2],coors[3]);
248: PetscInfo2(da,"DMDA 2d contour plot min %G max %G\n",zctx.min,zctx.max);
250: PetscDrawGetPopup(draw,&popup);
251: if (popup) {PetscDrawScalePopup(popup,zctx.min,zctx.max);}
253: zctx.scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/(zctx.max - zctx.min);
255: PetscDrawZoom(draw,VecView_MPI_Draw_DA2d_Zoom,&zctx);
256: }
257: PetscFree(displayfields);
258: PetscDrawViewPortsDestroy(ports);
260: VecRestoreArray(xcoorl,&zctx.xy);
261: VecRestoreArray(xlocal,&zctx.v);
262: return(0);
263: }
266: #if defined(PETSC_HAVE_HDF5)
269: PetscErrorCode VecView_MPI_HDF5_DA(Vec xin,PetscViewer viewer)
270: {
272: DM dm;
273: DM_DA *da;
274: hsize_t dim,dims[5];
275: hsize_t count[5];
276: hsize_t offset[5];
277: PetscInt cnt = 0;
278: PetscScalar *x;
279: const char *vecname;
280: hid_t filespace; /* file dataspace identifier */
281: hid_t plist_id; /* property list identifier */
282: hid_t dset_id; /* dataset identifier */
283: hid_t memspace; /* memory dataspace identifier */
284: hid_t file_id;
285: herr_t status;
288: PetscViewerHDF5GetFileId(viewer, &file_id);
289: PetscObjectQuery((PetscObject)xin,"DM",(PetscObject*)&dm);
290: if (!dm) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
291: da = (DM_DA*)dm->data;
293: /* Create the dataspace for the dataset */
294: dim = PetscHDF5IntCast(da->dim + ((da->w == 1) ? 0 : 1));
295: if (da->dim == 3) dims[cnt++] = PetscHDF5IntCast(da->P);
296: if (da->dim > 1) dims[cnt++] = PetscHDF5IntCast(da->N);
297: dims[cnt++] = PetscHDF5IntCast(da->M);
298: if (da->w > 1) dims[cnt++] = PetscHDF5IntCast(da->w);
299: #if defined(PETSC_USE_COMPLEX)
300: dim++;
301: dims[cnt++] = 2;
302: #endif
303: filespace = H5Screate_simple(dim, dims, NULL);
304: if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
306: /* Create the dataset with default properties and close filespace */
307: PetscObjectGetName((PetscObject)xin,&vecname);
308: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
309: dset_id = H5Dcreate2(file_id, vecname, H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
310: #else
311: dset_id = H5Dcreate(file_id, vecname, H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT);
312: #endif
313: if (dset_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dcreate2()");
314: status = H5Sclose(filespace);CHKERRQ(status);
316: /* Each process defines a dataset and writes it to the hyperslab in the file */
317: cnt = 0;
318: if (da->dim == 3) offset[cnt++] = PetscHDF5IntCast(da->zs);
319: if (da->dim > 1) offset[cnt++] = PetscHDF5IntCast(da->ys);
320: offset[cnt++] = PetscHDF5IntCast(da->xs/da->w);
321: if (da->w > 1) offset[cnt++] = 0;
322: #if defined(PETSC_USE_COMPLEX)
323: offset[cnt++] = 0;
324: #endif
325: cnt = 0;
326: if (da->dim == 3) count[cnt++] = PetscHDF5IntCast(da->ze - da->zs);
327: if (da->dim > 1) count[cnt++] = PetscHDF5IntCast(da->ye - da->ys);
328: count[cnt++] = PetscHDF5IntCast((da->xe - da->xs)/da->w);
329: if (da->w > 1) count[cnt++] = PetscHDF5IntCast(da->w);
330: #if defined(PETSC_USE_COMPLEX)
331: count[cnt++] = 2;
332: #endif
333: memspace = H5Screate_simple(dim, count, NULL);
334: if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
337: filespace = H5Dget_space(dset_id);
338: if (filespace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dget_space()");
339: status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
341: /* Create property list for collective dataset write */
342: plist_id = H5Pcreate(H5P_DATASET_XFER);
343: if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
344: #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
345: status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
346: #endif
347: /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
349: VecGetArray(xin, &x);
350: status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
351: status = H5Fflush(file_id, H5F_SCOPE_GLOBAL);CHKERRQ(status);
352: VecRestoreArray(xin, &x);
354: /* Close/release resources */
355: status = H5Pclose(plist_id);CHKERRQ(status);
356: status = H5Sclose(filespace);CHKERRQ(status);
357: status = H5Sclose(memspace);CHKERRQ(status);
358: status = H5Dclose(dset_id);CHKERRQ(status);
359: PetscInfo1(xin,"Wrote Vec object with name %s\n",vecname);
360: return(0);
361: }
362: #endif
366: #if defined(PETSC_HAVE_MPIIO)
369: static PetscErrorCode DMDAArrayMPIIO(DM da,PetscViewer viewer,Vec xin,PetscBool write)
370: {
371: PetscErrorCode ierr;
372: MPI_File mfdes;
373: PetscMPIInt gsizes[4],lsizes[4],lstarts[4],asiz,dof;
374: MPI_Datatype view;
375: const PetscScalar *array;
376: MPI_Offset off;
377: MPI_Aint ub,ul;
378: PetscInt type,rows,vecrows,tr[2];
379: DM_DA *dd = (DM_DA*)da->data;
382: VecGetSize(xin,&vecrows);
383: if (!write) {
384: /* Read vector header. */
385: PetscViewerBinaryRead(viewer,tr,2,PETSC_INT);
386: type = tr[0];
387: rows = tr[1];
388: if (type != VEC_FILE_CLASSID) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ARG_WRONG,"Not vector next in file");
389: if (rows != vecrows) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ARG_SIZ,"Vector in file not same size as DMDA vector");
390: } else {
391: tr[0] = VEC_FILE_CLASSID;
392: tr[1] = vecrows;
393: PetscViewerBinaryWrite(viewer,tr,2,PETSC_INT,PETSC_TRUE);
394: }
396: dof = PetscMPIIntCast(dd->w);
397: gsizes[0] = dof; gsizes[1] = PetscMPIIntCast(dd->M); gsizes[2] = PetscMPIIntCast(dd->N); gsizes[3] = PetscMPIIntCast(dd->P);
398: lsizes[0] = dof;lsizes[1] = PetscMPIIntCast((dd->xe-dd->xs)/dof); lsizes[2] = PetscMPIIntCast(dd->ye-dd->ys); lsizes[3] = PetscMPIIntCast(dd->ze-dd->zs);
399: lstarts[0] = 0; lstarts[1] = PetscMPIIntCast(dd->xs/dof); lstarts[2] = PetscMPIIntCast(dd->ys); lstarts[3] = PetscMPIIntCast(dd->zs);
400: MPI_Type_create_subarray(dd->dim+1,gsizes,lsizes,lstarts,MPI_ORDER_FORTRAN,MPIU_SCALAR,&view);
401: MPI_Type_commit(&view);
402:
403: PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);
404: PetscViewerBinaryGetMPIIOOffset(viewer,&off);
405: MPI_File_set_view(mfdes,off,MPIU_SCALAR,view,(char *)"native",MPI_INFO_NULL);
406: VecGetArrayRead(xin,&array);
407: asiz = lsizes[1]*(lsizes[2] > 0 ? lsizes[2] : 1)*(lsizes[3] > 0 ? lsizes[3] : 1)*dof;
408: if (write) {
409: MPIU_File_write_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);
410: } else {
411: MPIU_File_read_all(mfdes,(PetscScalar*)array,asiz,MPIU_SCALAR,MPI_STATUS_IGNORE);
412: }
413: MPI_Type_get_extent(view,&ul,&ub);
414: PetscViewerBinaryAddMPIIOOffset(viewer,ub);
415: VecRestoreArrayRead(xin,&array);
416: MPI_Type_free(&view);
417: return(0);
418: }
419: #endif
424: PetscErrorCode VecView_MPI_DA(Vec xin,PetscViewer viewer)
425: {
426: DM da;
428: PetscInt dim;
429: Vec natural;
430: PetscBool isdraw;
431: #if defined(PETSC_HAVE_HDF5)
432: PetscBool ishdf5;
433: #endif
434: const char *prefix,*name;
437: PetscObjectQuery((PetscObject)xin,"DM",(PetscObject*)&da);
438: if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
439: PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
440: #if defined(PETSC_HAVE_HDF5)
441: PetscTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
442: #endif
443: if (isdraw) {
444: DMDAGetInfo(da,&dim,0,0,0,0,0,0,0,0,0,0,0,0);
445: if (dim == 1) {
446: VecView_MPI_Draw_DA1d(xin,viewer);
447: } else if (dim == 2) {
448: VecView_MPI_Draw_DA2d(xin,viewer);
449: } else {
450: SETERRQ1(((PetscObject)da)->comm,PETSC_ERR_SUP,"Cannot graphically view vector associated with this dimensional DMDA %D",dim);
451: }
452: #if defined(PETSC_HAVE_HDF5)
453: } else if (ishdf5) {
454: VecView_MPI_HDF5_DA(xin,viewer);
455: #endif
456: } else {
457: #if defined(PETSC_HAVE_MPIIO)
458: PetscBool isbinary,isMPIIO;
460: PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
461: if (isbinary) {
462: PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);
463: if (isMPIIO) {
464: DMDAArrayMPIIO(da,viewer,xin,PETSC_TRUE);
465: return(0);
466: }
467: }
468: #endif
469:
470: /* call viewer on natural ordering */
471: PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);
472: DMDACreateNaturalVector(da,&natural);
473: PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);
474: DMDAGlobalToNaturalBegin(da,xin,INSERT_VALUES,natural);
475: DMDAGlobalToNaturalEnd(da,xin,INSERT_VALUES,natural);
476: PetscObjectGetName((PetscObject)xin,&name);
477: PetscObjectSetName((PetscObject)natural,name);
478: VecView(natural,viewer);
479: VecDestroy(&natural);
480: }
481: return(0);
482: }
485: #if defined(PETSC_HAVE_HDF5)
488: PetscErrorCode VecLoad_HDF5_DA(Vec xin, PetscViewer viewer)
489: {
490: DM da;
492: hsize_t dim;
493: hsize_t count[5];
494: hsize_t offset[5];
495: PetscInt cnt = 0;
496: PetscScalar *x;
497: const char *vecname;
498: hid_t filespace; /* file dataspace identifier */
499: hid_t plist_id; /* property list identifier */
500: hid_t dset_id; /* dataset identifier */
501: hid_t memspace; /* memory dataspace identifier */
502: hid_t file_id;
503: herr_t status;
504: DM_DA *dd;
507: PetscViewerHDF5GetFileId(viewer, &file_id);
508: PetscObjectQuery((PetscObject)xin,"DM",(PetscObject*)&da);
509: dd = (DM_DA*)da->data;
511: /* Create the dataspace for the dataset */
512: dim = PetscHDF5IntCast(dd->dim + ((dd->w == 1) ? 0 : 1));
513: #if defined(PETSC_USE_COMPLEX)
514: dim++;
515: #endif
517: /* Create the dataset with default properties and close filespace */
518: PetscObjectGetName((PetscObject)xin,&vecname);
519: #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800)
520: dset_id = H5Dopen2(file_id, vecname, H5P_DEFAULT);
521: #else
522: dset_id = H5Dopen(file_id, vecname);
523: #endif
524: if (dset_id == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Dopen2() with Vec named %s",vecname);
525: filespace = H5Dget_space(dset_id);
527: /* Each process defines a dataset and reads it from the hyperslab in the file */
528: cnt = 0;
529: if (dd->dim == 3) offset[cnt++] = PetscHDF5IntCast(dd->zs);
530: if (dd->dim > 1) offset[cnt++] = PetscHDF5IntCast(dd->ys);
531: offset[cnt++] = PetscHDF5IntCast(dd->xs/dd->w);
532: if (dd->w > 1) offset[cnt++] = 0;
533: #if defined(PETSC_USE_COMPLEX)
534: offset[cnt++] = 0;
535: #endif
536: cnt = 0;
537: if (dd->dim == 3) count[cnt++] = PetscHDF5IntCast(dd->ze - dd->zs);
538: if (dd->dim > 1) count[cnt++] = PetscHDF5IntCast(dd->ye - dd->ys);
539: count[cnt++] = PetscHDF5IntCast((dd->xe - dd->xs)/dd->w);
540: if (dd->w > 1) count[cnt++] = PetscHDF5IntCast(dd->w);
541: #if defined(PETSC_USE_COMPLEX)
542: count[cnt++] = 2;
543: #endif
544: memspace = H5Screate_simple(dim, count, NULL);
545: if (memspace == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Screate_simple()");
547: status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);CHKERRQ(status);
549: /* Create property list for collective dataset write */
550: plist_id = H5Pcreate(H5P_DATASET_XFER);
551: if (plist_id == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot H5Pcreate()");
552: #if defined(PETSC_HAVE_H5PSET_FAPL_MPIO)
553: status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);CHKERRQ(status);
554: #endif
555: /* To write dataset independently use H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT) */
557: VecGetArray(xin, &x);
558: status = H5Dread(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace, plist_id, x);CHKERRQ(status);
559: VecRestoreArray(xin, &x);
561: /* Close/release resources */
562: status = H5Pclose(plist_id);CHKERRQ(status);
563: status = H5Sclose(filespace);CHKERRQ(status);
564: status = H5Sclose(memspace);CHKERRQ(status);
565: status = H5Dclose(dset_id);CHKERRQ(status);
566: return(0);
567: }
568: #endif
572: PetscErrorCode VecLoad_Binary_DA(Vec xin, PetscViewer viewer)
573: {
574: DM da;
576: Vec natural;
577: const char *prefix;
578: PetscInt bs;
579: PetscBool flag;
580: DM_DA *dd;
581: #if defined(PETSC_HAVE_MPIIO)
582: PetscBool isMPIIO;
583: #endif
586: PetscObjectQuery((PetscObject)xin,"DM",(PetscObject*)&da);
587: dd = (DM_DA*)da->data;
588: #if defined(PETSC_HAVE_MPIIO)
589: PetscViewerBinaryGetMPIIO(viewer,&isMPIIO);
590: if (isMPIIO) {
591: DMDAArrayMPIIO(da,viewer,xin,PETSC_FALSE);
592: return(0);
593: }
594: #endif
596: PetscObjectGetOptionsPrefix((PetscObject)xin,&prefix);
597: DMDACreateNaturalVector(da,&natural);
598: PetscObjectSetName((PetscObject)natural,((PetscObject)xin)->name);
599: PetscObjectSetOptionsPrefix((PetscObject)natural,prefix);
600: VecLoad_Binary(natural,viewer);
601: DMDANaturalToGlobalBegin(da,natural,INSERT_VALUES,xin);
602: DMDANaturalToGlobalEnd(da,natural,INSERT_VALUES,xin);
603: VecDestroy(&natural);
604: PetscInfo(xin,"Loading vector from natural ordering into DMDA\n");
605: PetscOptionsGetInt(((PetscObject)xin)->prefix,"-vecload_block_size",&bs,&flag);
606: if (flag && bs != dd->w) {
607: PetscInfo2(xin,"Block size in file %D not equal to DMDA's dof %D\n",bs,dd->w);
608: }
609: return(0);
610: }
615: PetscErrorCode VecLoad_Default_DA(Vec xin, PetscViewer viewer)
616: {
618: DM da;
619: PetscBool isbinary;
620: #if defined(PETSC_HAVE_HDF5)
621: PetscBool ishdf5;
622: #endif
625: PetscObjectQuery((PetscObject)xin,"DM",(PetscObject*)&da);
626: if (!da) SETERRQ(((PetscObject)xin)->comm,PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
628: #if defined(PETSC_HAVE_HDF5)
629: PetscTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);
630: #endif
631: PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
633: if (isbinary) {
634: VecLoad_Binary_DA(xin,viewer);
635: #if defined(PETSC_HAVE_HDF5)
636: } else if (ishdf5) {
637: VecLoad_HDF5_DA(xin,viewer);
638: #endif
639: } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for vector loading", ((PetscObject)viewer)->type_name);
640: return(0);
641: }