xref: /petsc/src/dm/impls/da/dagtol.c (revision 47c6ae997ffd1b2afd66b6474dff5950ae8613d1)
1 #define PETSCDM_DLL
2 
3 /*
4   Code for manipulating distributed regular arrays in parallel.
5 */
6 
7 #include "private/daimpl.h"    /*I   "petscda.h"   I*/
8 
9 #undef __FUNCT__
10 #define __FUNCT__ "DAGlobalToLocalBegin"
11 /*@
12    DAGlobalToLocalBegin - Maps values from the global vector to the local
13    patch; the ghost points are included. Must be followed by
14    DAGlobalToLocalEnd() to complete the exchange.
15 
16    Neighbor-wise Collective on DA
17 
18    Input Parameters:
19 +  da - the distributed array context
20 .  g - the global vector
21 -  mode - one of INSERT_VALUES or ADD_VALUES
22 
23    Output Parameter:
24 .  l  - the local values
25 
26    Level: beginner
27 
28    Notes:
29    The global and local vectors used here need not be the same as those
30    obtained from DACreateGlobalVector() and DACreateLocalVector(), BUT they
31    must have the same parallel data layout; they could, for example, be
32    obtained with VecDuplicate() from the DA originating vectors.
33 
34 .keywords: distributed array, global to local, begin
35 
36 .seealso: DAGlobalToLocalEnd(), DALocalToGlobal(), DACreate2d(),
37           DALocalToLocalBegin(), DALocalToLocalEnd(),
38           DALocalToGlobalBegin(), DALocalToGlobalEnd()
39 
40 
41 @*/
42 PetscErrorCode PETSCDM_DLLEXPORT DAGlobalToLocalBegin(DA da,Vec g,InsertMode mode,Vec l)
43 {
44   PetscErrorCode ierr;
45   DM_DA          *dd = (DM_DA*)da->data;
46 
47   PetscFunctionBegin;
48   PetscValidHeaderSpecific(da,DM_CLASSID,1);
49   PetscValidHeaderSpecific(g,VEC_CLASSID,2);
50   PetscValidHeaderSpecific(l,VEC_CLASSID,4);
51   ierr = VecScatterBegin(dd->gtol,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
52   PetscFunctionReturn(0);
53 }
54 
55 #undef __FUNCT__
56 #define __FUNCT__ "DALocalToGlobalBegin"
57 /*@
58    DALocalToGlobalBegin - Adds values from the local (ghosted) vector
59    into the global (nonghosted) vector.
60 
61    Neighbor-wise Collective on DA
62 
63    Input Parameters:
64 +  da - the distributed array context
65 -  l  - the local values
66 
67    Output Parameter:
68 .  g - the global vector
69 
70    Level: beginner
71 
72    Notes:
73    Use DALocalToGlobal() to discard the ghost point values
74 
75    The global and local vectors used here need not be the same as those
76    obtained from DACreateGlobalVector() and DACreateLocalVector(), BUT they
77    must have the same parallel data layout; they could, for example, be
78    obtained with VecDuplicate() from the DA originating vectors.
79 
80 .keywords: distributed array, global to local, begin
81 
82 .seealso: DAGlobalToLocalEnd(), DALocalToGlobal(), DACreate2d(),
83           DALocalToLocalBegin(), DALocalToLocalEnd(), DALocalToGlobalEnd()
84 
85 @*/
86 PetscErrorCode PETSCDM_DLLEXPORT DALocalToGlobalBegin(DA da,Vec l,Vec g)
87 {
88   PetscErrorCode ierr;
89   DM_DA          *dd = (DM_DA*)da->data;
90 
91   PetscFunctionBegin;
92   PetscValidHeaderSpecific(da,DM_CLASSID,1);
93   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
94   PetscValidHeaderSpecific(g,VEC_CLASSID,3);
95   ierr = VecScatterBegin(dd->gtol,l,g,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
96   PetscFunctionReturn(0);
97 }
98 
99 #undef __FUNCT__
100 #define __FUNCT__ "DALocalToGlobalEnd"
101 /*@
102    DALocalToGlobalEnd - Adds values from the local (ghosted) vector
103    into the global (nonghosted) vector.
104 
105    Neighbor-wise Collective on DA
106 
107    Input Parameters:
108 +  da - the distributed array context
109 -  l  - the local values
110 
111    Output Parameter:
112 .  g - the global vector
113 
114    Level: beginner
115 
116    Notes:
117    Use DALocalToGlobal() to discard the ghost point values
118 
119    The global and local vectors used here need not be the same as those
120    obtained from DACreateGlobalVector() and DACreateLocalVector(), BUT they
121    must have the same parallel data layout; they could, for example, be
122    obtained with VecDuplicate() from the DA originating vectors.
123 
124 .keywords: distributed array, global to local, begin
125 
126 .seealso: DAGlobalToLocalEnd(), DALocalToGlobal(), DACreate2d(),
127           DALocalToLocalBegin(), DALocalToLocalEnd(), DALocalToGlobalBegin()
128 
129 @*/
130 PetscErrorCode PETSCDM_DLLEXPORT DALocalToGlobalEnd(DA da,Vec l,Vec g)
131 {
132   PetscErrorCode ierr;
133   DM_DA          *dd = (DM_DA*)da->data;
134 
135   PetscFunctionBegin;
136   PetscValidHeaderSpecific(da,DM_CLASSID,1);
137   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
138   PetscValidHeaderSpecific(g,VEC_CLASSID,3);
139   ierr = VecScatterEnd(dd->gtol,l,g,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
140   PetscFunctionReturn(0);
141 }
142 
143 #undef __FUNCT__
144 #define __FUNCT__ "DAGlobalToLocalEnd"
145 /*@
146    DAGlobalToLocalEnd - Maps values from the global vector to the local
147    patch; the ghost points are included. Must be preceeded by
148    DAGlobalToLocalBegin().
149 
150    Neighbor-wise Collective on DA
151 
152    Input Parameters:
153 +  da - the distributed array context
154 .  g - the global vector
155 -  mode - one of INSERT_VALUES or ADD_VALUES
156 
157    Output Parameter:
158 .  l  - the local values
159 
160    Level: beginner
161 
162    Notes:
163    The global and local vectors used here need not be the same as those
164    obtained from DACreateGlobalVector() and DACreateLocalVector(), BUT they
165    must have the same parallel data layout; they could, for example, be
166    obtained with VecDuplicate() from the DA originating vectors.
167 
168 .keywords: distributed array, global to local, end
169 
170 .seealso: DAGlobalToLocalBegin(), DALocalToGlobal(), DACreate2d(),
171      DALocalToLocalBegin(), DALocalToLocalEnd(), DALocalToGlobalBegin(), DALocalToGlobalEnd()
172 @*/
173 PetscErrorCode PETSCDM_DLLEXPORT DAGlobalToLocalEnd(DA da,Vec g,InsertMode mode,Vec l)
174 {
175   PetscErrorCode ierr;
176   DM_DA          *dd = (DM_DA*)da->data;
177 
178   PetscFunctionBegin;
179   PetscValidHeaderSpecific(da,DM_CLASSID,1);
180   PetscValidHeaderSpecific(g,VEC_CLASSID,2);
181   PetscValidHeaderSpecific(l,VEC_CLASSID,4);
182   ierr = VecScatterEnd(dd->gtol,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
183   PetscFunctionReturn(0);
184 }
185 
186 EXTERN PetscErrorCode DAGetNatural_Private(DA,PetscInt*,IS*);
187 #undef __FUNCT__
188 #define __FUNCT__ "DAGlobalToNatural_Create"
189 /*
190    DAGlobalToNatural_Create - Create the global to natural scatter object
191 
192    Collective on DA
193 
194    Input Parameter:
195 .  da - the distributed array context
196 
197    Level: developer
198 
199    Notes: This is an internal routine called by DAGlobalToNatural() to
200      create the scatter context.
201 
202 .keywords: distributed array, global to local, begin
203 
204 .seealso: DAGlobalToNaturalBegin(), DAGlobalToNaturalEnd(), DALocalToGlobal(), DACreate2d(),
205           DAGlobalToLocalBegin(), DAGlobalToLocalEnd(), DACreateNaturalVector()
206 */
207 PetscErrorCode DAGlobalToNatural_Create(DA da)
208 {
209   PetscErrorCode ierr;
210   PetscInt       m,start,Nlocal;
211   IS             from,to;
212   Vec            global;
213   DM_DA          *dd = (DM_DA*)da->data;
214 
215   PetscFunctionBegin;
216   PetscValidHeaderSpecific(da,DM_CLASSID,1);
217   if (!dd->natural) {
218     SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ORDER,"Natural layout vector not yet created; cannot scatter into it");
219   }
220 
221   /* create the scatter context */
222   ierr = VecGetLocalSize(dd->natural,&m);CHKERRQ(ierr);
223   ierr = VecGetOwnershipRange(dd->natural,&start,PETSC_NULL);CHKERRQ(ierr);
224 
225   ierr = DAGetNatural_Private(da,&Nlocal,&to);CHKERRQ(ierr);
226   if (Nlocal != m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error: Nlocal %D local vector size %D",Nlocal,m);
227   ierr = ISCreateStride(((PetscObject)da)->comm,m,start,1,&from);CHKERRQ(ierr);
228   ierr = VecCreateMPIWithArray(((PetscObject)da)->comm,dd->Nlocal,PETSC_DETERMINE,0,&global);
229   ierr = VecSetBlockSize(global,dd->w);CHKERRQ(ierr);
230   ierr = VecScatterCreate(global,from,dd->natural,to,&dd->gton);CHKERRQ(ierr);
231   ierr = VecDestroy(global);CHKERRQ(ierr);
232   ierr = ISDestroy(from);CHKERRQ(ierr);
233   ierr = ISDestroy(to);CHKERRQ(ierr);
234   PetscFunctionReturn(0);
235 }
236 
237 #undef __FUNCT__
238 #define __FUNCT__ "DAGlobalToNaturalBegin"
239 /*@
240    DAGlobalToNaturalBegin - Maps values from the global vector to a global vector
241    in the "natural" grid ordering. Must be followed by
242    DAGlobalToNaturalEnd() to complete the exchange.
243 
244    Neighbor-wise Collective on DA
245 
246    Input Parameters:
247 +  da - the distributed array context
248 .  g - the global vector
249 -  mode - one of INSERT_VALUES or ADD_VALUES
250 
251    Output Parameter:
252 .  l  - the natural ordering values
253 
254    Level: advanced
255 
256    Notes:
257    The global and natrual vectors used here need not be the same as those
258    obtained from DACreateGlobalVector() and DACreateNaturalVector(), BUT they
259    must have the same parallel data layout; they could, for example, be
260    obtained with VecDuplicate() from the DA originating vectors.
261 
262    You must call DACreateNaturalVector() before using this routine
263 
264 .keywords: distributed array, global to local, begin
265 
266 .seealso: DAGlobalToNaturalEnd(), DALocalToGlobal(), DACreate2d(),
267           DAGlobalToLocalBegin(), DAGlobalToLocalEnd(), DACreateNaturalVector(),
268           DALocalToGlobalBegin(), DALocalToGlobalEnd()
269 @*/
270 PetscErrorCode PETSCDM_DLLEXPORT DAGlobalToNaturalBegin(DA da,Vec g,InsertMode mode,Vec l)
271 {
272   PetscErrorCode ierr;
273   DM_DA          *dd = (DM_DA*)da->data;
274 
275   PetscFunctionBegin;
276   PetscValidHeaderSpecific(da,DM_CLASSID,1);
277   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
278   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
279   if (!dd->gton) {
280     /* create the scatter context */
281     ierr = DAGlobalToNatural_Create(da);CHKERRQ(ierr);
282   }
283   ierr = VecScatterBegin(dd->gton,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
284   PetscFunctionReturn(0);
285 }
286 
287 #undef __FUNCT__
288 #define __FUNCT__ "DAGlobalToNaturalEnd"
289 /*@
290    DAGlobalToNaturalEnd - Maps values from the global vector to a global vector
291    in the natural ordering. Must be preceeded by DAGlobalToNaturalBegin().
292 
293    Neighbor-wise Collective on DA
294 
295    Input Parameters:
296 +  da - the distributed array context
297 .  g - the global vector
298 -  mode - one of INSERT_VALUES or ADD_VALUES
299 
300    Output Parameter:
301 .  l  - the global values in the natural ordering
302 
303    Level: advanced
304 
305    Notes:
306    The global and local vectors used here need not be the same as those
307    obtained from DACreateGlobalVector() and DACreateNaturalVector(), BUT they
308    must have the same parallel data layout; they could, for example, be
309    obtained with VecDuplicate() from the DA originating vectors.
310 
311 .keywords: distributed array, global to local, end
312 
313 .seealso: DAGlobalToNaturalBegin(), DALocalToGlobal(), DACreate2d(),
314           DAGlobalToLocalBegin(), DAGlobalToLocalEnd(), DACreateNaturalVector(),
315           DALocalToGlobalBegin(), DALocalToGlobalEnd()
316 @*/
317 PetscErrorCode PETSCDM_DLLEXPORT DAGlobalToNaturalEnd(DA da,Vec g,InsertMode mode,Vec l)
318 {
319   PetscErrorCode ierr;
320   DM_DA          *dd = (DM_DA*)da->data;
321 
322   PetscFunctionBegin;
323   PetscValidHeaderSpecific(da,DM_CLASSID,1);
324   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
325   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
326   ierr = VecScatterEnd(dd->gton,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
327   PetscFunctionReturn(0);
328 }
329 
330 #undef __FUNCT__
331 #define __FUNCT__ "DANaturalToGlobalBegin"
332 /*@
333    DANaturalToGlobalBegin - Maps values from a global vector in the "natural" ordering
334    to a global vector in the PETSc DA grid ordering. Must be followed by
335    DANaturalToGlobalEnd() to complete the exchange.
336 
337    Neighbor-wise Collective on DA
338 
339    Input Parameters:
340 +  da - the distributed array context
341 .  g - the global vector in a natural ordering
342 -  mode - one of INSERT_VALUES or ADD_VALUES
343 
344    Output Parameter:
345 .  l  - the values in the DA ordering
346 
347    Level: advanced
348 
349    Notes:
350    The global and natural vectors used here need not be the same as those
351    obtained from DACreateGlobalVector() and DACreateNaturalVector(), BUT they
352    must have the same parallel data layout; they could, for example, be
353    obtained with VecDuplicate() from the DA originating vectors.
354 
355 .keywords: distributed array, global to local, begin
356 
357 .seealso: DAGlobalToNaturalEnd(), DAGlobalToNaturalBegin(), DALocalToGlobal(), DACreate2d(),
358           DAGlobalToLocalBegin(), DAGlobalToLocalEnd(), DACreateNaturalVector(),
359           DALocalToGlobalBegin(), DALocalToGlobalEnd()
360 
361 @*/
362 PetscErrorCode PETSCDM_DLLEXPORT DANaturalToGlobalBegin(DA da,Vec g,InsertMode mode,Vec l)
363 {
364   PetscErrorCode ierr;
365   DM_DA          *dd = (DM_DA*)da->data;
366 
367   PetscFunctionBegin;
368   PetscValidHeaderSpecific(da,DM_CLASSID,1);
369   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
370   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
371   if (!dd->gton) {
372     /* create the scatter context */
373     ierr = DAGlobalToNatural_Create(da);CHKERRQ(ierr);
374   }
375   ierr = VecScatterBegin(dd->gton,g,l,mode,SCATTER_REVERSE);CHKERRQ(ierr);
376   PetscFunctionReturn(0);
377 }
378 
379 #undef __FUNCT__
380 #define __FUNCT__ "DANaturalToGlobalEnd"
381 /*@
382    DANaturalToGlobalEnd - Maps values from the natural ordering global vector
383    to a global vector in the PETSc DA ordering. Must be preceeded by DANaturalToGlobalBegin().
384 
385    Neighbor-wise Collective on DA
386 
387    Input Parameters:
388 +  da - the distributed array context
389 .  g - the global vector in a natural ordering
390 -  mode - one of INSERT_VALUES or ADD_VALUES
391 
392    Output Parameter:
393 .  l  - the global values in the PETSc DA ordering
394 
395    Level: intermediate
396 
397    Notes:
398    The global and local vectors used here need not be the same as those
399    obtained from DACreateGlobalVector() and DACreateNaturalVector(), BUT they
400    must have the same parallel data layout; they could, for example, be
401    obtained with VecDuplicate() from the DA originating vectors.
402 
403 .keywords: distributed array, global to local, end
404 
405 .seealso: DAGlobalToNaturalBegin(), DAGlobalToNaturalEnd(), DALocalToGlobal(), DACreate2d(),
406           DAGlobalToLocalBegin(), DAGlobalToLocalEnd(), DACreateNaturalVector(),
407           DALocalToGlobalBegin(), DALocalToGlobalEnd()
408 
409 @*/
410 PetscErrorCode PETSCDM_DLLEXPORT DANaturalToGlobalEnd(DA da,Vec g,InsertMode mode,Vec l)
411 {
412   PetscErrorCode ierr;
413   DM_DA          *dd = (DM_DA*)da->data;
414 
415   PetscFunctionBegin;
416   PetscValidHeaderSpecific(da,DM_CLASSID,1);
417   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
418   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
419   ierr = VecScatterEnd(dd->gton,g,l,mode,SCATTER_REVERSE);CHKERRQ(ierr);
420   PetscFunctionReturn(0);
421 }
422 
423