xref: /petsc/src/dm/impls/da/dagtol.c (revision 2205254efee3a00a594e5e2a3a70f74dcb40bc03)
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) SETERRQ(((PetscObject)da)->comm,PETSC_ERR_ORDER,"Natural layout vector not yet created; cannot scatter into it");
109 
110   /* create the scatter context */
111   ierr = VecGetLocalSize(dd->natural,&m);CHKERRQ(ierr);
112   ierr = VecGetOwnershipRange(dd->natural,&start,PETSC_NULL);CHKERRQ(ierr);
113 
114   ierr = DMDAGetNatural_Private(da,&Nlocal,&to);CHKERRQ(ierr);
115   if (Nlocal != m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error: Nlocal %D local vector size %D",Nlocal,m);
116   ierr = ISCreateStride(((PetscObject)da)->comm,m,start,1,&from);CHKERRQ(ierr);
117   ierr = VecCreateMPIWithArray(((PetscObject)da)->comm,dd->w,dd->Nlocal,PETSC_DETERMINE,0,&global);CHKERRQ(ierr);
118   ierr = VecScatterCreate(global,from,dd->natural,to,&dd->gton);CHKERRQ(ierr);
119   ierr = VecDestroy(&global);CHKERRQ(ierr);
120   ierr = ISDestroy(&from);CHKERRQ(ierr);
121   ierr = ISDestroy(&to);CHKERRQ(ierr);
122   PetscFunctionReturn(0);
123 }
124 
125 #undef __FUNCT__
126 #define __FUNCT__ "DMDAGlobalToNaturalBegin"
127 /*@
128    DMDAGlobalToNaturalBegin - Maps values from the global vector to a global vector
129    in the "natural" grid ordering. Must be followed by
130    DMDAGlobalToNaturalEnd() to complete the exchange.
131 
132    Neighbor-wise Collective on DMDA
133 
134    Input Parameters:
135 +  da - the distributed array context
136 .  g - the global vector
137 -  mode - one of INSERT_VALUES or ADD_VALUES
138 
139    Output Parameter:
140 .  l  - the natural ordering values
141 
142    Level: advanced
143 
144    Notes:
145    The global and natrual vectors used here need not be the same as those
146    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
147    must have the same parallel data layout; they could, for example, be
148    obtained with VecDuplicate() from the DMDA originating vectors.
149 
150    You must call DMDACreateNaturalVector() before using this routine
151 
152 .keywords: distributed array, global to local, begin
153 
154 .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
155           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
156 
157 @*/
158 PetscErrorCode  DMDAGlobalToNaturalBegin(DM da,Vec g,InsertMode mode,Vec l)
159 {
160   PetscErrorCode ierr;
161   DM_DA          *dd = (DM_DA*)da->data;
162 
163   PetscFunctionBegin;
164   PetscValidHeaderSpecific(da,DM_CLASSID,1);
165   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
166   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
167   if (!dd->gton) {
168     /* create the scatter context */
169     ierr = DMDAGlobalToNatural_Create(da);CHKERRQ(ierr);
170   }
171   ierr = VecScatterBegin(dd->gton,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
172   PetscFunctionReturn(0);
173 }
174 
175 #undef __FUNCT__
176 #define __FUNCT__ "DMDAGlobalToNaturalEnd"
177 /*@
178    DMDAGlobalToNaturalEnd - Maps values from the global vector to a global vector
179    in the natural ordering. Must be preceeded by DMDAGlobalToNaturalBegin().
180 
181    Neighbor-wise Collective on DMDA
182 
183    Input Parameters:
184 +  da - the distributed array context
185 .  g - the global vector
186 -  mode - one of INSERT_VALUES or ADD_VALUES
187 
188    Output Parameter:
189 .  l  - the global values in the natural ordering
190 
191    Level: advanced
192 
193    Notes:
194    The global and local vectors used here need not be the same as those
195    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
196    must have the same parallel data layout; they could, for example, be
197    obtained with VecDuplicate() from the DMDA originating vectors.
198 
199 .keywords: distributed array, global to local, end
200 
201 .seealso: DMDAGlobalToNaturalBegin(), DMLocalToGlobalBegin(), DMDACreate2d(),
202           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
203 
204 @*/
205 PetscErrorCode  DMDAGlobalToNaturalEnd(DM da,Vec g,InsertMode mode,Vec l)
206 {
207   PetscErrorCode ierr;
208   DM_DA          *dd = (DM_DA*)da->data;
209 
210   PetscFunctionBegin;
211   PetscValidHeaderSpecific(da,DM_CLASSID,1);
212   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
213   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
214   ierr = VecScatterEnd(dd->gton,g,l,mode,SCATTER_FORWARD);CHKERRQ(ierr);
215   PetscFunctionReturn(0);
216 }
217 
218 #undef __FUNCT__
219 #define __FUNCT__ "DMDANaturalToGlobalBegin"
220 /*@
221    DMDANaturalToGlobalBegin - Maps values from a global vector in the "natural" ordering
222    to a global vector in the PETSc DMDA grid ordering. Must be followed by
223    DMDANaturalToGlobalEnd() to complete the exchange.
224 
225    Neighbor-wise Collective on DMDA
226 
227    Input Parameters:
228 +  da - the distributed array context
229 .  g - the global vector in a natural ordering
230 -  mode - one of INSERT_VALUES or ADD_VALUES
231 
232    Output Parameter:
233 .  l  - the values in the DMDA ordering
234 
235    Level: advanced
236 
237    Notes:
238    The global and natural vectors used here need not be the same as those
239    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
240    must have the same parallel data layout; they could, for example, be
241    obtained with VecDuplicate() from the DMDA originating vectors.
242 
243 .keywords: distributed array, global to local, begin
244 
245 .seealso: DMDAGlobalToNaturalEnd(), DMDAGlobalToNaturalBegin(), DMLocalToGlobalBegin(), DMDACreate2d(),
246           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
247 
248 @*/
249 PetscErrorCode  DMDANaturalToGlobalBegin(DM da,Vec g,InsertMode mode,Vec l)
250 {
251   PetscErrorCode ierr;
252   DM_DA          *dd = (DM_DA*)da->data;
253 
254   PetscFunctionBegin;
255   PetscValidHeaderSpecific(da,DM_CLASSID,1);
256   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
257   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
258   if (!dd->gton) {
259     /* create the scatter context */
260     ierr = DMDAGlobalToNatural_Create(da);CHKERRQ(ierr);
261   }
262   ierr = VecScatterBegin(dd->gton,g,l,mode,SCATTER_REVERSE);CHKERRQ(ierr);
263   PetscFunctionReturn(0);
264 }
265 
266 #undef __FUNCT__
267 #define __FUNCT__ "DMDANaturalToGlobalEnd"
268 /*@
269    DMDANaturalToGlobalEnd - Maps values from the natural ordering global vector
270    to a global vector in the PETSc DMDA ordering. Must be preceeded by DMDANaturalToGlobalBegin().
271 
272    Neighbor-wise Collective on DMDA
273 
274    Input Parameters:
275 +  da - the distributed array context
276 .  g - the global vector in a natural ordering
277 -  mode - one of INSERT_VALUES or ADD_VALUES
278 
279    Output Parameter:
280 .  l  - the global values in the PETSc DMDA ordering
281 
282    Level: intermediate
283 
284    Notes:
285    The global and local vectors used here need not be the same as those
286    obtained from DMCreateGlobalVector() and DMDACreateNaturalVector(), BUT they
287    must have the same parallel data layout; they could, for example, be
288    obtained with VecDuplicate() from the DMDA originating vectors.
289 
290 .keywords: distributed array, global to local, end
291 
292 .seealso: DMDAGlobalToNaturalBegin(), DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
293           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
294 
295 @*/
296 PetscErrorCode  DMDANaturalToGlobalEnd(DM da,Vec g,InsertMode mode,Vec l)
297 {
298   PetscErrorCode ierr;
299   DM_DA          *dd = (DM_DA*)da->data;
300 
301   PetscFunctionBegin;
302   PetscValidHeaderSpecific(da,DM_CLASSID,1);
303   PetscValidHeaderSpecific(l,VEC_CLASSID,2);
304   PetscValidHeaderSpecific(g,VEC_CLASSID,4);
305   ierr = VecScatterEnd(dd->gton,g,l,mode,SCATTER_REVERSE);CHKERRQ(ierr);
306   PetscFunctionReturn(0);
307 }
308 
309