Actual source code: dasub.c
petsc-3.5.4 2015-05-23
2: /*
3: Code for manipulating distributed regular arrays in parallel.
4: */
6: #include <petsc-private/dmdaimpl.h> /*I "petscdmda.h" I*/
10: /*@C
11: DMDAGetLogicalCoordinate - Returns a the i,j,k logical coordinate for the closest mesh point to a x,y,z point in the coordinates of the DMDA
13: Collective on DMDA
15: Input Parameters:
16: + da - the distributed array
17: - x,y,z - the physical coordinates
19: Output Parameters:
20: + II, JJ, KK - the logical coordinate (-1 on processes that do not contain that point)
21: - X, Y, Z, - (optional) the coordinates of the located grid point
23: Level: advanced
25: Notes:
26: All processors that share the DMDA must call this with the same coordinate value
28: .keywords: distributed array, get, processor subset
29: @*/
30: PetscErrorCode DMDAGetLogicalCoordinate(DM da,PetscScalar x,PetscScalar y,PetscScalar z,PetscInt *II,PetscInt *JJ,PetscInt *KK,PetscScalar *X,PetscScalar *Y,PetscScalar *Z)
31: {
32: DM_DA *dd = (DM_DA*)da->data;
34: Vec coors;
35: DM dacoors;
36: DMDACoor2d **c;
37: PetscInt i,j,xs,xm,ys,ym;
38: PetscReal d,D = PETSC_MAX_REAL,Dv;
39: PetscMPIInt rank,root;
42: if (dd->dim == 1) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot get point from 1d DMDA");
43: if (dd->dim == 3) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot get point from 3d DMDA");
45: *II = -1;
46: *JJ = -1;
48: DMGetCoordinateDM(da,&dacoors);
49: DMDAGetCorners(dacoors,&xs,&ys,NULL,&xm,&ym,NULL);
50: DMGetCoordinates(da,&coors);
51: DMDAVecGetArray(dacoors,coors,&c);
52: for (j=ys; j<ys+ym; j++) {
53: for (i=xs; i<xs+xm; i++) {
54: d = PetscSqrtReal(PetscRealPart( (c[j][i].x - x)*(c[j][i].x - x) + (c[j][i].y - y)*(c[j][i].y - y) ));
55: if (d < D) {
56: D = d;
57: *II = i;
58: *JJ = j;
59: }
60: }
61: }
62: MPI_Allreduce(&D,&Dv,1,MPIU_REAL,MPI_MIN,PetscObjectComm((PetscObject)da));
63: if (D != Dv) {
64: *II = -1;
65: *JJ = -1;
66: rank = 0;
67: } else {
68: *X = c[*JJ][*II].x;
69: *Y = c[*JJ][*II].y;
70: MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank);
71: rank++;
72: }
73: MPI_Allreduce(&rank,&root,1,MPI_INT,MPI_SUM,PetscObjectComm((PetscObject)da));
74: root--;
75: MPI_Bcast(X,1,MPIU_SCALAR,root,PetscObjectComm((PetscObject)da));
76: MPI_Bcast(Y,1,MPIU_SCALAR,root,PetscObjectComm((PetscObject)da));
77: DMDAVecRestoreArray(dacoors,coors,&c);
78: return(0);
79: }
83: /*@C
84: DMDAGetRay - Returns a vector on process zero that contains a row or column of the values in a DMDA vector
86: Collective on DMDA
88: Input Parameters:
89: + da - the distributed array
90: . vec - the vector
91: . dir - Cartesian direction, either DMDA_X, DMDA_Y, or DMDA_Z
92: - gp - global grid point number in this direction
94: Output Parameters:
95: + newvec - the new vector that can hold the values (size zero on all processes except process 0)
96: - scatter - the VecScatter that will map from the original vector to the slice
98: Level: advanced
100: Notes:
101: All processors that share the DMDA must call this with the same gp value
103: .keywords: distributed array, get, processor subset
104: @*/
105: PetscErrorCode DMDAGetRay(DM da,DMDADirection dir,PetscInt gp,Vec *newvec,VecScatter *scatter)
106: {
107: PetscMPIInt rank;
108: DM_DA *dd = (DM_DA*)da->data;
110: IS is;
111: AO ao;
112: Vec vec;
113: PetscInt *indices,i,j;
116: if (dd->dim == 3) SETERRQ(PetscObjectComm((PetscObject) da), PETSC_ERR_SUP, "Cannot get slice from 3d DMDA");
117: MPI_Comm_rank(PetscObjectComm((PetscObject) da), &rank);
118: DMDAGetAO(da, &ao);
119: if (!rank) {
120: if (dd->dim == 1) {
121: if (dir == DMDA_X) {
122: PetscMalloc1(dd->w, &indices);
123: indices[0] = dd->w*gp;
124: for (i = 1; i < dd->w; ++i) indices[i] = indices[i-1] + 1;
125: AOApplicationToPetsc(ao, dd->w, indices);
126: VecCreate(PETSC_COMM_SELF, newvec);
127: VecSetBlockSize(*newvec, dd->w);
128: VecSetSizes(*newvec, dd->w, PETSC_DETERMINE);
129: VecSetType(*newvec, VECSEQ);
130: ISCreateGeneral(PETSC_COMM_SELF, dd->w, indices, PETSC_OWN_POINTER, &is);
131: } else if (dir == DMDA_Y) SETERRQ(PetscObjectComm((PetscObject) da), PETSC_ERR_SUP, "Cannot get Y slice from 1d DMDA");
132: else SETERRQ(PetscObjectComm((PetscObject) da), PETSC_ERR_ARG_OUTOFRANGE, "Unknown DMDADirection");
133: } else {
134: if (dir == DMDA_Y) {
135: PetscMalloc1(dd->w*dd->M,&indices);
136: indices[0] = gp*dd->M*dd->w;
137: for (i=1; i<dd->M*dd->w; i++) indices[i] = indices[i-1] + 1;
139: AOApplicationToPetsc(ao,dd->M*dd->w,indices);
140: VecCreate(PETSC_COMM_SELF,newvec);
141: VecSetBlockSize(*newvec,dd->w);
142: VecSetSizes(*newvec,dd->M*dd->w,PETSC_DETERMINE);
143: VecSetType(*newvec,VECSEQ);
144: ISCreateGeneral(PETSC_COMM_SELF,dd->w*dd->M,indices,PETSC_OWN_POINTER,&is);
145: } else if (dir == DMDA_X) {
146: PetscMalloc1(dd->w*dd->N,&indices);
147: indices[0] = dd->w*gp;
148: for (j=1; j<dd->w; j++) indices[j] = indices[j-1] + 1;
149: for (i=1; i<dd->N; i++) {
150: indices[i*dd->w] = indices[i*dd->w-1] + dd->w*dd->M - dd->w + 1;
151: for (j=1; j<dd->w; j++) indices[i*dd->w + j] = indices[i*dd->w + j - 1] + 1;
152: }
153: AOApplicationToPetsc(ao,dd->w*dd->N,indices);
154: VecCreate(PETSC_COMM_SELF,newvec);
155: VecSetBlockSize(*newvec,dd->w);
156: VecSetSizes(*newvec,dd->N*dd->w,PETSC_DETERMINE);
157: VecSetType(*newvec,VECSEQ);
158: ISCreateGeneral(PETSC_COMM_SELF,dd->w*dd->N,indices,PETSC_OWN_POINTER,&is);
159: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown DMDADirection");
160: }
161: } else {
162: VecCreateSeq(PETSC_COMM_SELF, 0, newvec);
163: ISCreateGeneral(PETSC_COMM_SELF, 0, 0, PETSC_COPY_VALUES, &is);
164: }
165: DMGetGlobalVector(da, &vec);
166: VecScatterCreate(vec, is, *newvec, NULL, scatter);
167: DMRestoreGlobalVector(da, &vec);
168: ISDestroy(&is);
169: return(0);
170: }
174: /*@C
175: DMDAGetProcessorSubset - Returns a communicator consisting only of the
176: processors in a DMDA that own a particular global x, y, or z grid point
177: (corresponding to a logical plane in a 3D grid or a line in a 2D grid).
179: Collective on DMDA
181: Input Parameters:
182: + da - the distributed array
183: . dir - Cartesian direction, either DMDA_X, DMDA_Y, or DMDA_Z
184: - gp - global grid point number in this direction
186: Output Parameters:
187: . comm - new communicator
189: Level: advanced
191: Notes:
192: All processors that share the DMDA must call this with the same gp value
194: This routine is particularly useful to compute boundary conditions
195: or other application-specific calculations that require manipulating
196: sets of data throughout a logical plane of grid points.
198: .keywords: distributed array, get, processor subset
199: @*/
200: PetscErrorCode DMDAGetProcessorSubset(DM da,DMDADirection dir,PetscInt gp,MPI_Comm *comm)
201: {
202: MPI_Group group,subgroup;
204: PetscInt i,ict,flag,*owners,xs,xm,ys,ym,zs,zm;
205: PetscMPIInt size,*ranks = NULL;
206: DM_DA *dd = (DM_DA*)da->data;
210: flag = 0;
211: DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);
212: MPI_Comm_size(PetscObjectComm((PetscObject)da),&size);
213: if (dir == DMDA_Z) {
214: if (dd->dim < 3) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_OUTOFRANGE,"DMDA_Z invalid for DMDA dim < 3");
215: if (gp < 0 || gp > dd->P) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"invalid grid point");
216: if (gp >= zs && gp < zs+zm) flag = 1;
217: } else if (dir == DMDA_Y) {
218: if (dd->dim == 1) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_OUTOFRANGE,"DMDA_Y invalid for DMDA dim = 1");
219: if (gp < 0 || gp > dd->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"invalid grid point");
220: if (gp >= ys && gp < ys+ym) flag = 1;
221: } else if (dir == DMDA_X) {
222: if (gp < 0 || gp > dd->M) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"invalid grid point");
223: if (gp >= xs && gp < xs+xm) flag = 1;
224: } else SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_ARG_OUTOFRANGE,"Invalid direction");
226: PetscMalloc2(size,&owners,size,&ranks);
227: MPI_Allgather(&flag,1,MPIU_INT,owners,1,MPIU_INT,PetscObjectComm((PetscObject)da));
228: ict = 0;
229: PetscInfo2(da,"DMDAGetProcessorSubset: dim=%D, direction=%d, procs: ",dd->dim,(int)dir);
230: for (i=0; i<size; i++) {
231: if (owners[i]) {
232: ranks[ict] = i; ict++;
233: PetscInfo1(da,"%D ",i);
234: }
235: }
236: PetscInfo(da,"\n");
237: MPI_Comm_group(PetscObjectComm((PetscObject)da),&group);
238: MPI_Group_incl(group,ict,ranks,&subgroup);
239: MPI_Comm_create(PetscObjectComm((PetscObject)da),subgroup,comm);
240: MPI_Group_free(&subgroup);
241: MPI_Group_free(&group);
242: PetscFree2(owners,ranks);
243: return(0);
244: }
248: /*@C
249: DMDAGetProcessorSubsets - Returns communicators consisting only of the
250: processors in a DMDA adjacent in a particular dimension,
251: corresponding to a logical plane in a 3D grid or a line in a 2D grid.
253: Collective on DMDA
255: Input Parameters:
256: + da - the distributed array
257: - dir - Cartesian direction, either DMDA_X, DMDA_Y, or DMDA_Z
259: Output Parameters:
260: . subcomm - new communicator
262: Level: advanced
264: Notes:
265: This routine is useful for distributing one-dimensional data in a tensor product grid.
267: .keywords: distributed array, get, processor subset
268: @*/
269: PetscErrorCode DMDAGetProcessorSubsets(DM da, DMDADirection dir, MPI_Comm *subcomm)
270: {
271: MPI_Comm comm;
272: MPI_Group group, subgroup;
273: PetscInt subgroupSize = 0;
274: PetscInt *firstPoints;
275: PetscMPIInt size, *subgroupRanks = NULL;
276: PetscInt xs, xm, ys, ym, zs, zm, firstPoint, p;
278: DM_DA *dd = (DM_DA*)da->data;
282: PetscObjectGetComm((PetscObject)da,&comm);
283: DMDAGetCorners(da, &xs, &ys, &zs, &xm, &ym, &zm);
284: MPI_Comm_size(comm, &size);
285: if (dir == DMDA_Z) {
286: if (dd->dim < 3) SETERRQ(comm,PETSC_ERR_ARG_OUTOFRANGE,"DMDA_Z invalid for DMDA dim < 3");
287: firstPoint = zs;
288: } else if (dir == DMDA_Y) {
289: if (dd->dim == 1) SETERRQ(comm,PETSC_ERR_ARG_OUTOFRANGE,"DMDA_Y invalid for DMDA dim = 1");
290: firstPoint = ys;
291: } else if (dir == DMDA_X) {
292: firstPoint = xs;
293: } else SETERRQ(comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid direction");
295: PetscMalloc2(size, &firstPoints, size, &subgroupRanks);
296: MPI_Allgather(&firstPoint, 1, MPIU_INT, firstPoints, 1, MPIU_INT, comm);
297: PetscInfo2(da,"DMDAGetProcessorSubset: dim=%D, direction=%d, procs: ",dd->dim,(int)dir);
298: for (p = 0; p < size; ++p) {
299: if (firstPoints[p] == firstPoint) {
300: subgroupRanks[subgroupSize++] = p;
301: PetscInfo1(da, "%D ", p);
302: }
303: }
304: PetscInfo(da, "\n");
305: MPI_Comm_group(comm, &group);
306: MPI_Group_incl(group, subgroupSize, subgroupRanks, &subgroup);
307: MPI_Comm_create(comm, subgroup, subcomm);
308: MPI_Group_free(&subgroup);
309: MPI_Group_free(&group);
310: PetscFree2(firstPoints, subgroupRanks);
311: return(0);
312: }