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