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