xref: /petsc/src/dm/impls/da/dagtol.c (revision db609ea75e24d1bf22e421c6099bd64ada4af056)
1 
2 /*
3   Code for manipulating distributed regular arrays in parallel.
4 */
5 
6 #include <petsc-private/daimpl.h>    /*I   "petscdmda.h"   I*/
7 
8 #undef __FUNCT__
9 #define __FUNCT__ "DMGlobalToLocalBegin_DA"
10 PetscErrorCode  DMGlobalToLocalBegin_DA(DM da,Vec g,InsertMode mode,Vec l)
11 {
12   PetscErrorCode ierr;
13   DM_DA          *dd = (DM_DA*)da->data;
14 
15   PetscFunctionBegin;
16   PetscValidHeaderSpecific(da,DM_CLASSID,1);
17   PetscValidHeaderSpecific(g,VEC_CLASSID,2);
18   PetscValidHeaderSpecific(l,VEC_CLASSID,4);
19   ierr = VecScatterBegin(dd->gtol,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
20   PetscFunctionReturn(0);
21 }
22 
23 
24 #undef __FUNCT__
25 #define __FUNCT__ "DMGlobalToLocalEnd_DA"
26 PetscErrorCode  DMGlobalToLocalEnd_DA(DM da,Vec g,InsertMode mode,Vec l)
27 {
28   PetscErrorCode ierr;
29   DM_DA          *dd = (DM_DA*)da->data;
30 
31   PetscFunctionBegin;
32   PetscValidHeaderSpecific(da,DM_CLASSID,1);
33   PetscValidHeaderSpecific(g,VEC_CLASSID,2);
34   PetscValidHeaderSpecific(l,VEC_CLASSID,4);
35   ierr = VecScatterEnd(dd->gtol,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
36   PetscFunctionReturn(0);
37 }
38 
39 #undef __FUNCT__
40 #define __FUNCT__ "DMLocalToGlobalBegin_DA"
41 PetscErrorCode  DMLocalToGlobalBegin_DA(DM da,Vec l,InsertMode mode,Vec g)
42 {
43   PetscErrorCode ierr;
44   DM_DA          *dd = (DM_DA*)da->data;
45 
46   PetscFunctionBegin;
47   PetscValidHeaderSpecific(da,DM_CLASSID,1);
48   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
49   PetscValidHeaderSpecific(g,VEC_CLASSID,3);
50   if (mode == ADD_VALUES) {
51     ierr = VecScatterBegin(dd->gtol,l,g,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
52   } else if (mode == INSERT_VALUES) {
53     ierr = VecScatterBegin(dd->ltog,l,g,mode,SCATTER_FORWARD);CHKERRQ(ierr);
54   } else SETERRQ(((PetscObject)da)->comm,PETSC_ERR_SUP,"Not yet implemented");
55   PetscFunctionReturn(0);
56 }
57 
58 #undef __FUNCT__
59 #define __FUNCT__ "DMLocalToGlobalEnd_DA"
60 PetscErrorCode  DMLocalToGlobalEnd_DA(DM da,Vec l,InsertMode mode,Vec g)
61 {
62   PetscErrorCode ierr;
63   DM_DA          *dd = (DM_DA*)da->data;
64 
65   PetscFunctionBegin;
66   PetscValidHeaderSpecific(da,DM_CLASSID,1);
67   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
68   PetscValidHeaderSpecific(g,VEC_CLASSID,3);
69   if (mode == ADD_VALUES) {
70     ierr = VecScatterEnd(dd->gtol,l,g,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
71   } else if (mode == INSERT_VALUES) {
72     ierr = VecScatterEnd(dd->ltog,l,g,mode,SCATTER_FORWARD);CHKERRQ(ierr);
73   } else SETERRQ(((PetscObject)da)->comm,PETSC_ERR_SUP,"Not yet implemented");
74   PetscFunctionReturn(0);
75 }
76 
77 extern PetscErrorCode DMDAGetNatural_Private(DM,PetscInt*,IS*);
78 #undef __FUNCT__
79 #define __FUNCT__ "DMDAGlobalToNatural_Create"
80 /*
81    DMDAGlobalToNatural_Create - Create the global to natural scatter object
82 
83    Collective on DMDA
84 
85    Input Parameter:
86 .  da - the distributed array context
87 
88    Level: developer
89 
90    Notes: This is an internal routine called by DMDAGlobalToNatural() to
91      create the scatter context.
92 
93 .keywords: distributed array, global to local, begin
94 
95 .seealso: DMDAGlobalToNaturalBegin(), DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
96           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
97 */
98 PetscErrorCode DMDAGlobalToNatural_Create(DM da)
99 {
100   PetscErrorCode ierr;
101   PetscInt       m,start,Nlocal;
102   IS             from,to;
103   Vec            global;
104   DM_DA          *dd = (DM_DA*)da->data;
105 
106   PetscFunctionBegin;
107   PetscValidHeaderSpecific(da,DM_CLASSID,1);
108   if (!dd->natural) {
109     SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ORDER,"Natural layout vector not yet created; cannot scatter into it");
110   }
111 
112   /* create the scatter context */
113   ierr = VecGetLocalSize(dd->natural,&m);CHKERRQ(ierr);
114   ierr = VecGetOwnershipRange(dd->natural,&start,PETSC_NULL);CHKERRQ(ierr);
115 
116   ierr = DMDAGetNatural_Private(da,&Nlocal,&to);CHKERRQ(ierr);
117   if (Nlocal != m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error: Nlocal %D local vector size %D",Nlocal,m);
118   ierr = ISCreateStride(((PetscObject)da)->comm,m,start,1,&from);CHKERRQ(ierr);
119   ierr = VecCreateMPIWithArray(((PetscObject)da)->comm,dd->w,dd->Nlocal,PETSC_DETERMINE,0,&global);
120   ierr = VecScatterCreate(global,from,dd->natural,to,&dd->gton);CHKERRQ(ierr);
121   ierr = VecDestroy(&global);CHKERRQ(ierr);
122   ierr = ISDestroy(&from);CHKERRQ(ierr);
123   ierr = ISDestroy(&to);CHKERRQ(ierr);
124   PetscFunctionReturn(0);
125 }
126 
127 #undef __FUNCT__
128 #define __FUNCT__ "DMDAGlobalToNaturalBegin"
129 /*@
130    DMDAGlobalToNaturalBegin - Maps values from the global vector to a global vector
131    in the "natural" grid ordering. Must be followed by
132    DMDAGlobalToNaturalEnd() to complete the exchange.
133 
134    Neighbor-wise Collective on DMDA
135 
136    Input Parameters:
137 +  da - the distributed array context
138 .  g - the global vector
139 -  mode - one of INSERT_VALUES or ADD_VALUES
140 
141    Output Parameter:
142 .  l  - the natural ordering values
143 
144    Level: advanced
145 
146    Notes:
147    The global and natrual vectors used here need not be the same as those
148    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
149    must have the same parallel data layout; they could, for example, be
150    obtained with VecDuplicate() from the DMDA originating vectors.
151 
152    You must call DMDACreateNaturalVector() before using this routine
153 
154 .keywords: distributed array, global to local, begin
155 
156 .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
157           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
158 
159 @*/
160 PetscErrorCode  DMDAGlobalToNaturalBegin(DM da,Vec g,InsertMode mode,Vec l)
161 {
162   PetscErrorCode ierr;
163   DM_DA          *dd = (DM_DA*)da->data;
164 
165   PetscFunctionBegin;
166   PetscValidHeaderSpecific(da,DM_CLASSID,1);
167   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
168   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
169   if (!dd->gton) {
170     /* create the scatter context */
171     ierr = DMDAGlobalToNatural_Create(da);CHKERRQ(ierr);
172   }
173   ierr = VecScatterBegin(dd->gton,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
174   PetscFunctionReturn(0);
175 }
176 
177 #undef __FUNCT__
178 #define __FUNCT__ "DMDAGlobalToNaturalEnd"
179 /*@
180    DMDAGlobalToNaturalEnd - Maps values from the global vector to a global vector
181    in the natural ordering. Must be preceeded by DMDAGlobalToNaturalBegin().
182 
183    Neighbor-wise Collective on DMDA
184 
185    Input Parameters:
186 +  da - the distributed array context
187 .  g - the global vector
188 -  mode - one of INSERT_VALUES or ADD_VALUES
189 
190    Output Parameter:
191 .  l  - the global values in the natural ordering
192 
193    Level: advanced
194 
195    Notes:
196    The global and local vectors used here need not be the same as those
197    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
198    must have the same parallel data layout; they could, for example, be
199    obtained with VecDuplicate() from the DMDA originating vectors.
200 
201 .keywords: distributed array, global to local, end
202 
203 .seealso: DMDAGlobalToNaturalBegin(), DMLocalToGlobalBegin(), DMDACreate2d(),
204           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
205 
206 @*/
207 PetscErrorCode  DMDAGlobalToNaturalEnd(DM da,Vec g,InsertMode mode,Vec l)
208 {
209   PetscErrorCode ierr;
210   DM_DA          *dd = (DM_DA*)da->data;
211 
212   PetscFunctionBegin;
213   PetscValidHeaderSpecific(da,DM_CLASSID,1);
214   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
215   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
216   ierr = VecScatterEnd(dd->gton,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
217   PetscFunctionReturn(0);
218 }
219 
220 #undef __FUNCT__
221 #define __FUNCT__ "DMDANaturalToGlobalBegin"
222 /*@
223    DMDANaturalToGlobalBegin - Maps values from a global vector in the "natural" ordering
224    to a global vector in the PETSc DMDA grid ordering. Must be followed by
225    DMDANaturalToGlobalEnd() to complete the exchange.
226 
227    Neighbor-wise Collective on DMDA
228 
229    Input Parameters:
230 +  da - the distributed array context
231 .  g - the global vector in a natural ordering
232 -  mode - one of INSERT_VALUES or ADD_VALUES
233 
234    Output Parameter:
235 .  l  - the values in the DMDA ordering
236 
237    Level: advanced
238 
239    Notes:
240    The global and natural vectors used here need not be the same as those
241    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
242    must have the same parallel data layout; they could, for example, be
243    obtained with VecDuplicate() from the DMDA originating vectors.
244 
245 .keywords: distributed array, global to local, begin
246 
247 .seealso: DMDAGlobalToNaturalEnd(), DMDAGlobalToNaturalBegin(), DMLocalToGlobalBegin(), DMDACreate2d(),
248           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
249 
250 @*/
251 PetscErrorCode  DMDANaturalToGlobalBegin(DM da,Vec g,InsertMode mode,Vec l)
252 {
253   PetscErrorCode ierr;
254   DM_DA          *dd = (DM_DA*)da->data;
255 
256   PetscFunctionBegin;
257   PetscValidHeaderSpecific(da,DM_CLASSID,1);
258   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
259   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
260   if (!dd->gton) {
261     /* create the scatter context */
262     ierr = DMDAGlobalToNatural_Create(da);CHKERRQ(ierr);
263   }
264   ierr = VecScatterBegin(dd->gton,g,l,mode,SCATTER_REVERSE);CHKERRQ(ierr);
265   PetscFunctionReturn(0);
266 }
267 
268 #undef __FUNCT__
269 #define __FUNCT__ "DMDANaturalToGlobalEnd"
270 /*@
271    DMDANaturalToGlobalEnd - Maps values from the natural ordering global vector
272    to a global vector in the PETSc DMDA ordering. Must be preceeded by DMDANaturalToGlobalBegin().
273 
274    Neighbor-wise Collective on DMDA
275 
276    Input Parameters:
277 +  da - the distributed array context
278 .  g - the global vector in a natural ordering
279 -  mode - one of INSERT_VALUES or ADD_VALUES
280 
281    Output Parameter:
282 .  l  - the global values in the PETSc DMDA ordering
283 
284    Level: intermediate
285 
286    Notes:
287    The global and local vectors used here need not be the same as those
288    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
289    must have the same parallel data layout; they could, for example, be
290    obtained with VecDuplicate() from the DMDA originating vectors.
291 
292 .keywords: distributed array, global to local, end
293 
294 .seealso: DMDAGlobalToNaturalBegin(), DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
295           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
296 
297 @*/
298 PetscErrorCode  DMDANaturalToGlobalEnd(DM da,Vec g,InsertMode mode,Vec l)
299 {
300   PetscErrorCode ierr;
301   DM_DA          *dd = (DM_DA*)da->data;
302 
303   PetscFunctionBegin;
304   PetscValidHeaderSpecific(da,DM_CLASSID,1);
305   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
306   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
307   ierr = VecScatterEnd(dd->gton,g,l,mode,SCATTER_REVERSE);CHKERRQ(ierr);
308   PetscFunctionReturn(0);
309 }
310 
311