Actual source code: da1.c
1: #define PETSCDM_DLL
2: /*
3: Code for manipulating distributed regular 1d arrays in parallel.
4: This file was created by Peter Mell 6/30/95
5: */
7: #include src/dm/da/daimpl.h
9: const char *DAPeriodicTypes[] = {"NONPERIODIC","XPERIODIC","YPERIODIC","XYPERIODIC",
10: "XYZPERIODIC","XZPERIODIC","YZPERIODIC","ZPERIODIC","XYZGHOSTED","DAPeriodicType","DA_",0};
14: PetscErrorCode DAView_1d(DA da,PetscViewer viewer)
15: {
17: PetscMPIInt rank;
18: PetscTruth iascii,isdraw;
21: MPI_Comm_rank(((PetscObject)da)->comm,&rank);
23: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
24: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
25: if (iascii) {
26: PetscViewerASCIISynchronizedPrintf(viewer,"Processor [%d] M %D m %D w %D s %D\n",rank,da->M,
27: da->m,da->w,da->s);
28: PetscViewerASCIISynchronizedPrintf(viewer,"X range of indices: %D %D\n",da->xs,da->xe);
29: PetscViewerFlush(viewer);
30: } else if (isdraw) {
31: PetscDraw draw;
32: double ymin = -1,ymax = 1,xmin = -1,xmax = da->M,x;
33: PetscInt base;
34: char node[10];
35: PetscTruth isnull;
37: PetscViewerDrawGetDraw(viewer,0,&draw);
38: PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
40: PetscDrawSetCoordinates(draw,xmin,ymin,xmax,ymax);
41: PetscDrawSynchronizedClear(draw);
43: /* first processor draws all node lines */
44: if (!rank) {
45: PetscInt xmin_tmp;
46: ymin = 0.0; ymax = 0.3;
47:
48: /* ADIC doesn't like doubles in a for loop */
49: for (xmin_tmp =0; xmin_tmp < da->M; xmin_tmp++) {
50: PetscDrawLine(draw,(double)xmin_tmp,ymin,(double)xmin_tmp,ymax,PETSC_DRAW_BLACK);
51: }
53: xmin = 0.0; xmax = da->M - 1;
54: PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_BLACK);
55: PetscDrawLine(draw,xmin,ymax,xmax,ymax,PETSC_DRAW_BLACK);
56: }
58: PetscDrawSynchronizedFlush(draw);
59: PetscDrawPause(draw);
61: /* draw my box */
62: ymin = 0; ymax = 0.3; xmin = da->xs / da->w; xmax = (da->xe / da->w) - 1;
63: PetscDrawLine(draw,xmin,ymin,xmax,ymin,PETSC_DRAW_RED);
64: PetscDrawLine(draw,xmin,ymin,xmin,ymax,PETSC_DRAW_RED);
65: PetscDrawLine(draw,xmin,ymax,xmax,ymax,PETSC_DRAW_RED);
66: PetscDrawLine(draw,xmax,ymin,xmax,ymax,PETSC_DRAW_RED);
68: /* Put in index numbers */
69: base = da->base / da->w;
70: for (x=xmin; x<=xmax; x++) {
71: sprintf(node,"%d",(int)base++);
72: PetscDrawString(draw,x,ymin,PETSC_DRAW_RED,node);
73: }
75: PetscDrawSynchronizedFlush(draw);
76: PetscDrawPause(draw);
77: } else {
78: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for DA 1d",((PetscObject)viewer)->type_name);
79: }
80: return(0);
81: }
83: #if 0
84: EXTERN PetscErrorCode DAPublish_Petsc(PetscObject);
85: #endif
89: /*@C
90: DACreate1d - Creates an object that will manage the communication of one-dimensional
91: regular array data that is distributed across some processors.
93: Collective on MPI_Comm
95: Input Parameters:
96: + comm - MPI communicator
97: . wrap - type of periodicity should the array have, if any. Use
98: either DA_NONPERIODIC or DA_XPERIODIC
99: . M - global dimension of the array (use -M to indicate that it may be set to a different value
100: from the command line with -da_grid_x <M>)
101: . dof - number of degrees of freedom per node
102: . s - stencil width
103: - lc - array containing number of nodes in the X direction on each processor,
104: or PETSC_NULL. If non-null, must be of length as m.
106: Output Parameter:
107: . inra - the resulting distributed array object
109: Options Database Key:
110: + -da_view - Calls DAView() at the conclusion of DACreate1d()
111: . -da_grid_x <nx> - number of grid points in x direction; can set if M < 0
112: - -da_refine_x - refinement factor
114: Level: beginner
116: Notes:
117: The array data itself is NOT stored in the DA, it is stored in Vec objects;
118: The appropriate vector objects can be obtained with calls to DACreateGlobalVector()
119: and DACreateLocalVector() and calls to VecDuplicate() if more are needed.
122: .keywords: distributed array, create, one-dimensional
124: .seealso: DADestroy(), DAView(), DACreate2d(), DACreate3d(), DAGlobalToLocalBegin(), DASetRefinementFactor(),
125: DAGlobalToLocalEnd(), DALocalToGlobal(), DALocalToLocalBegin(), DALocalToLocalEnd(), DAGetRefinementFactor(),
126: DAGetInfo(), DACreateGlobalVector(), DACreateLocalVector(), DACreateNaturalVector(), DALoad(), DAView(), DAGetOwnershipRange()
128: @*/
129: PetscErrorCode DACreate1d(MPI_Comm comm,DAPeriodicType wrap,PetscInt M,PetscInt dof,PetscInt s,PetscInt *lc,DA *inra)
130: {
132: PetscMPIInt rank,size;
133: PetscInt i,*idx,nn,left,refine_x = 2,tM = M,xs,xe,x,Xs,Xe,start,end,m;
134: PetscTruth flg1,flg2;
135: DA da;
136: Vec local,global;
137: VecScatter ltog,gtol;
138: IS to,from;
142: *inra = 0;
143: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
144: DMInitializePackage(PETSC_NULL);
145: #endif
147: if (dof < 1) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Must have 1 or more degrees of freedom per node: %D",dof);
148: if (s < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Stencil width cannot be negative: %D",s);
150: PetscOptionsBegin(comm,PETSC_NULL,"1d DA Options","DA");
151: if (M < 0) {
152: tM = -M;
153: PetscOptionsInt("-da_grid_x","Number of grid points in x direction","DACreate1d",tM,&tM,PETSC_NULL);
154: }
155: PetscOptionsInt("-da_refine_x","Refinement ratio in x direction","DASetRefinementFactor",refine_x,&refine_x,PETSC_NULL);
156: PetscOptionsEnd();
157: M = tM;
159: PetscHeaderCreate(da,_p_DA,struct _DAOps,DA_COOKIE,0,"DA",comm,DADestroy,DAView);
160: da->ops->globaltolocalbegin = DAGlobalToLocalBegin;
161: da->ops->globaltolocalend = DAGlobalToLocalEnd;
162: da->ops->localtoglobal = DALocalToGlobal;
163: da->ops->createglobalvector = DACreateGlobalVector;
164: da->ops->getinterpolation = DAGetInterpolation;
165: da->ops->getcoloring = DAGetColoring;
166: da->ops->getmatrix = DAGetMatrix;
167: da->ops->refine = DARefine;
168: da->ops->coarsen = DACoarsen;
169: da->ops->getaggregates = DAGetAggregates;
170: da->dim = 1;
171: da->interptype = DA_Q1;
172: da->refine_x = refine_x;
173: PetscMalloc(dof*sizeof(char*),&da->fieldname);
174: PetscMemzero(da->fieldname,dof*sizeof(char*));
175: MPI_Comm_size(comm,&size);
176: MPI_Comm_rank(comm,&rank);
178: m = size;
180: if (M < m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"More processors than data points! %D %D",m,M);
181: if ((M-1) < s) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Array is too small for stencil! %D %D",M-1,s);
183: /*
184: Determine locally owned region
185: xs is the first local node number, x is the number of local nodes
186: */
187: if (!lc) {
188: PetscOptionsHasName(PETSC_NULL,"-da_partition_blockcomm",&flg1);
189: PetscOptionsHasName(PETSC_NULL,"-da_partition_nodes_at_end",&flg2);
190: if (flg1) { /* Block Comm type Distribution */
191: xs = rank*M/m;
192: x = (rank + 1)*M/m - xs;
193: } else if (flg2) { /* The odd nodes are evenly distributed across last nodes */
194: x = (M + rank)/m;
195: if (M/m == x) { xs = rank*x; }
196: else { xs = rank*(x-1) + (M+rank)%(x*m); }
197: } else { /* The odd nodes are evenly distributed across the first k nodes */
198: /* Regular PETSc Distribution */
199: x = M/m + ((M % m) > rank);
200: if (rank >= (M % m)) {xs = (rank * (PetscInt)(M/m) + M % m);}
201: else {xs = rank * (PetscInt)(M/m) + rank;}
202: }
203: } else {
204: x = lc[rank];
205: xs = 0;
206: for (i=0; i<rank; i++) {
207: xs += lc[i];
208: }
209: /* verify that data user provided is consistent */
210: left = xs;
211: for (i=rank; i<size; i++) {
212: left += lc[i];
213: }
214: if (left != M) {
215: SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Sum of lc across processors not equal to M %D %D",left,M);
216: }
217: }
219: /* From now on x,s,xs,xe,Xs,Xe are the exact location in the array */
220: x *= dof;
221: s *= dof; /* NOTE: here change s to be absolute stencil distance */
222: xs *= dof;
223: xe = xs + x;
225: /* determine ghost region */
226: if (wrap == DA_XPERIODIC || wrap == DA_XYZGHOSTED) {
227: Xs = xs - s;
228: Xe = xe + s;
229: } else {
230: if ((xs-s) >= 0) Xs = xs-s; else Xs = 0;
231: if ((xe+s) <= M*dof) Xe = xe+s; else Xe = M*dof;
232: }
234: /* allocate the base parallel and sequential vectors */
235: da->Nlocal = x;
236: VecCreateMPIWithArray(comm,da->Nlocal,PETSC_DECIDE,0,&global);
237: VecSetBlockSize(global,dof);
238: da->nlocal = (Xe-Xs);
239: VecCreateSeqWithArray(PETSC_COMM_SELF,da->nlocal,0,&local);
240: VecSetBlockSize(local,dof);
241:
242: /* Create Local to Global Vector Scatter Context */
243: /* local to global inserts non-ghost point region into global */
244: VecGetOwnershipRange(global,&start,&end);
245: ISCreateStride(comm,x,start,1,&to);
246: ISCreateStride(comm,x,xs-Xs,1,&from);
247: VecScatterCreate(local,from,global,to,<og);
248: PetscLogObjectParent(da,to);
249: PetscLogObjectParent(da,from);
250: PetscLogObjectParent(da,ltog);
251: ISDestroy(from);
252: ISDestroy(to);
254: /* Create Global to Local Vector Scatter Context */
255: /* global to local must retrieve ghost points */
256: if (wrap == DA_XYZGHOSTED) {
257: if (size == 1) {
258: ISCreateStride(comm,(xe-xs),s,1,&to);
259: } else if (!rank) {
260: ISCreateStride(comm,(Xe-xs),s,1,&to);
261: } else if (rank == size-1) {
262: ISCreateStride(comm,(xe-Xs),0,1,&to);
263: } else {
264: ISCreateStride(comm,(Xe-Xs),0,1,&to);
265: }
266: } else {
267: ISCreateStride(comm,(Xe-Xs),0,1,&to);
268: }
269:
270: PetscMalloc((x+2*s)*sizeof(PetscInt),&idx);
271: PetscLogObjectMemory(da,(x+2*s)*sizeof(PetscInt));
273: nn = 0;
274: if (wrap == DA_XPERIODIC) { /* Handle all cases with wrap first */
276: for (i=0; i<s; i++) { /* Left ghost points */
277: if ((xs-s+i)>=0) { idx[nn++] = xs-s+i;}
278: else { idx[nn++] = M*dof+(xs-s+i);}
279: }
281: for (i=0; i<x; i++) { idx [nn++] = xs + i;} /* Non-ghost points */
282:
283: for (i=0; i<s; i++) { /* Right ghost points */
284: if ((xe+i)<M*dof) { idx [nn++] = xe+i; }
285: else { idx [nn++] = (xe+i) - M*dof;}
286: }
287: } else if (wrap == DA_XYZGHOSTED) {
289: if (s <= xs) {for (i=0; i<s; i++) {idx[nn++] = xs - s + i;}}
291: for (i=0; i<x; i++) { idx [nn++] = xs + i;}
292:
293: if ((xe+s)<=M*dof) {for (i=0; i<s; i++) {idx[nn++]=xe+i;}}
295: } else { /* Now do all cases with no wrapping */
297: if (s <= xs) {for (i=0; i<s; i++) {idx[nn++] = xs - s + i;}}
298: else {for (i=0; i<xs; i++) {idx[nn++] = i;}}
300: for (i=0; i<x; i++) { idx [nn++] = xs + i;}
301:
302: if ((xe+s)<=M*dof) {for (i=0; i<s; i++) {idx[nn++]=xe+i;}}
303: else {for (i=xe; i<(M*dof); i++) {idx[nn++]=i;}}
304: }
306: ISCreateGeneral(comm,nn,idx,&from);
307: VecScatterCreate(global,from,local,to,>ol);
308: PetscLogObjectParent(da,to);
309: PetscLogObjectParent(da,from);
310: PetscLogObjectParent(da,gtol);
311: ISDestroy(to);
312: ISDestroy(from);
313: VecDestroy(local);
314: VecDestroy(global);
316: da->M = M; da->N = 1; da->m = m; da->n = 1;
317: da->xs = xs; da->xe = xe; da->ys = 0; da->ye = 1; da->zs = 0; da->ze = 1;
318: da->Xs = Xs; da->Xe = Xe; da->Ys = 0; da->Ye = 1; da->Zs = 0; da->Ze = 1;
319: da->P = 1; da->p = 1; da->w = dof; da->s = s/dof;
321: da->gtol = gtol;
322: da->ltog = ltog;
323: da->base = xs;
324: da->ops->view = DAView_1d;
325: da->wrap = wrap;
326: da->stencil_type = DA_STENCIL_STAR;
328: /*
329: Set the local to global ordering in the global vector, this allows use
330: of VecSetValuesLocal().
331: */
332: if (wrap == DA_XYZGHOSTED) {
333: PetscInt *tmpidx;
334: if (size == 1) {
335: PetscMalloc((nn+2*s)*sizeof(PetscInt),&tmpidx);
336: for (i=0; i<s; i++) tmpidx[i] = -1;
337: PetscMemcpy(tmpidx+s,idx,nn*sizeof(PetscInt));
338: for (i=nn+s; i<nn+2*s; i++) tmpidx[i] = -1;
339: PetscFree(idx);
340: idx = tmpidx;
341: nn += 2*s;
342: } else if (!rank) { /* must preprend -1 marker for ghost location that have no global value */
343: PetscMalloc((nn+s)*sizeof(PetscInt),&tmpidx);
344: for (i=0; i<s; i++) tmpidx[i] = -1;
345: PetscMemcpy(tmpidx+s,idx,nn*sizeof(PetscInt));
346: PetscFree(idx);
347: idx = tmpidx;
348: nn += s;
349: } else if (rank == size-1) { /* must postpend -1 marker for ghost location that have no global value */
350: PetscMalloc((nn+s)*sizeof(PetscInt),&tmpidx);
351: PetscMemcpy(tmpidx,idx,nn*sizeof(PetscInt));
352: for (i=nn; i<nn+s; i++) tmpidx[i] = -1;
353: PetscFree(idx);
354: idx = tmpidx;
355: nn += s;
356: }
357: }
358: ISLocalToGlobalMappingCreateNC(comm,nn,idx,&da->ltogmap);
359: ISLocalToGlobalMappingBlock(da->ltogmap,da->w,&da->ltogmapb);
360: PetscLogObjectParent(da,da->ltogmap);
362: da->idx = idx;
363: da->Nl = nn;
365: da->ltol = PETSC_NULL;
366: da->ao = PETSC_NULL;
368: DAView_Private(da);
369: *inra = da;
370: PetscPublishAll(da);
371: return(0);
372: }
376: /*
377: Processes command line options to determine if/how a DA
378: is to be viewed. Called by DACreateXX()
379: */
380: PetscErrorCode DAView_Private(DA da)
381: {
383: PetscTruth flg1;
384: PetscViewer view;
387: PetscOptionsBegin(((PetscObject)da)->comm,((PetscObject)da)->prefix,"Distributed array (DA) options","DA");
388: PetscOptionsTruth("-da_view","Print information about the DA's distribution","DAView",PETSC_FALSE,&flg1,PETSC_NULL);
389: if (flg1) {
390: PetscViewerASCIIGetStdout(((PetscObject)da)->comm,&view);
391: DAView(da,view);
392: }
393: PetscOptionsTruth("-da_view_draw","Draw how the DA is distributed","DAView",PETSC_FALSE,&flg1,PETSC_NULL);
394: if (flg1) {DAView(da,PETSC_VIEWER_DRAW_(((PetscObject)da)->comm));}
395: PetscOptionsEnd();
396: return(0);
397: }