xref: /petsc/src/vec/is/ao/interface/ao.c (revision bfcb38ea38335faa6e7f8d97f6bc6ce9aa2a1dd1)
1 
2 /*
3    Defines the abstract operations on AO (application orderings)
4 */
5 #include <../src/vec/is/ao/aoimpl.h>      /*I "petscao.h" I*/
6 
7 /* Logging support */
8 PetscClassId  AO_CLASSID;
9 PetscLogEvent AO_PetscToApplication, AO_ApplicationToPetsc;
10 
11 /*@C
12    AOView - Displays an application ordering.
13 
14    Collective on AO and PetscViewer
15 
16    Input Parameters:
17 +  ao - the application ordering context
18 -  viewer - viewer used for display
19 
20    Level: intermediate
21 
22     Options Database Key:
23 .   -ao_view - calls AOView() at end of AOCreate()
24 
25    Note:
26    The available visualization contexts include
27 +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
28 -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
29          output where only the first processor opens
30          the file.  All other processors send their
31          data to the first processor to print.
32 
33    The user can open an alternative visualization context with
34    PetscViewerASCIIOpen() - output to a specified file.
35 
36 .seealso: PetscViewerASCIIOpen()
37 @*/
38 PetscErrorCode  AOView(AO ao,PetscViewer viewer)
39 {
40   PetscErrorCode ierr;
41 
42   PetscFunctionBegin;
43   PetscValidHeaderSpecific(ao,AO_CLASSID,1);
44   if (!viewer) {
45     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ao),&viewer);CHKERRQ(ierr);
46   }
47   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
48 
49   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ao,viewer);CHKERRQ(ierr);
50   ierr = (*ao->ops->view)(ao,viewer);CHKERRQ(ierr);
51   PetscFunctionReturn(0);
52 }
53 
54 /*@
55    AODestroy - Destroys an application ordering.
56 
57    Collective on AO
58 
59    Input Parameters:
60 .  ao - the application ordering context
61 
62    Level: beginner
63 
64 .seealso: AOCreate()
65 @*/
66 PetscErrorCode  AODestroy(AO *ao)
67 {
68   PetscErrorCode ierr;
69 
70   PetscFunctionBegin;
71   if (!*ao) PetscFunctionReturn(0);
72   PetscValidHeaderSpecific((*ao),AO_CLASSID,1);
73   if (--((PetscObject)(*ao))->refct > 0) {*ao = 0; PetscFunctionReturn(0);}
74   /* if memory was published with SAWs then destroy it */
75   ierr = PetscObjectSAWsViewOff((PetscObject)*ao);CHKERRQ(ierr);
76   ierr = ISDestroy(&(*ao)->isapp);CHKERRQ(ierr);
77   ierr = ISDestroy(&(*ao)->ispetsc);CHKERRQ(ierr);
78   /* destroy the internal part */
79   if ((*ao)->ops->destroy) {
80     ierr = (*(*ao)->ops->destroy)(*ao);CHKERRQ(ierr);
81   }
82   ierr = PetscHeaderDestroy(ao);CHKERRQ(ierr);
83   PetscFunctionReturn(0);
84 }
85 
86 
87 #include <../src/vec/is/is/impls/general/general.h>
88 /* ---------------------------------------------------------------------*/
89 
90 PETSC_INTERN PetscErrorCode ISSetUp_General(IS);
91 
92 /*@
93    AOPetscToApplicationIS - Maps an index set in the PETSc ordering to
94    the application-defined ordering.
95 
96    Collective on AO and IS
97 
98    Input Parameters:
99 +  ao - the application ordering context
100 -  is - the index set; this is replaced with its mapped values
101 
102    Output Parameter:
103 .  is - the mapped index set
104 
105    Level: intermediate
106 
107    Notes:
108    The index set cannot be of type stride or block
109 
110    Any integers in ia[] that are negative are left unchanged. This
111          allows one to convert, for example, neighbor lists that use negative
112          entries to indicate nonexistent neighbors due to boundary conditions
113          etc.
114 
115 .seealso: AOCreateBasic(), AOView(),AOApplicationToPetsc(),
116           AOApplicationToPetscIS(),AOPetscToApplication()
117 @*/
118 PetscErrorCode  AOPetscToApplicationIS(AO ao,IS is)
119 {
120   PetscErrorCode ierr;
121   PetscInt       n;
122   PetscInt       *ia;
123 
124   PetscFunctionBegin;
125   PetscValidHeaderSpecific(ao,AO_CLASSID,1);
126   PetscValidHeaderSpecific(is,IS_CLASSID,2);
127   ierr = ISToGeneral(is);CHKERRQ(ierr);
128   /* we cheat because we know the is is general and that we can change the indices */
129   ierr = ISGetIndices(is,(const PetscInt**)&ia);CHKERRQ(ierr);
130   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
131   ierr = (*ao->ops->petsctoapplication)(ao,n,ia);CHKERRQ(ierr);
132   ierr = ISRestoreIndices(is,(const PetscInt**)&ia);CHKERRQ(ierr);
133   /* updated cached values (sorted, min, max, etc.)*/
134   ierr = ISSetUp_General(is);CHKERRQ(ierr);
135   PetscFunctionReturn(0);
136 }
137 
138 /*@
139    AOApplicationToPetscIS - Maps an index set in the application-defined
140    ordering to the PETSc ordering.
141 
142    Collective on AO and IS
143 
144    Input Parameters:
145 +  ao - the application ordering context
146 -  is - the index set; this is replaced with its mapped values
147 
148    Output Parameter:
149 .  is - the mapped index set
150 
151    Level: beginner
152 
153    Note:
154    The index set cannot be of type stride or block
155 
156    Any integers in ia[] that are negative are left unchanged. This
157    allows one to convert, for example, neighbor lists that use negative
158    entries to indicate nonexistent neighbors due to boundary conditions, etc.
159 
160 .seealso: AOCreateBasic(), AOView(), AOPetscToApplication(),
161           AOPetscToApplicationIS(), AOApplicationToPetsc()
162 @*/
163 PetscErrorCode  AOApplicationToPetscIS(AO ao,IS is)
164 {
165   PetscErrorCode ierr;
166   PetscInt       n,*ia;
167 
168   PetscFunctionBegin;
169   PetscValidHeaderSpecific(ao,AO_CLASSID,1);
170   PetscValidHeaderSpecific(is,IS_CLASSID,2);
171   ierr = ISToGeneral(is);CHKERRQ(ierr);
172   /* we cheat because we know the is is general and that we can change the indices */
173   ierr = ISGetIndices(is,(const PetscInt**)&ia);CHKERRQ(ierr);
174   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
175   ierr = (*ao->ops->applicationtopetsc)(ao,n,ia);CHKERRQ(ierr);
176   ierr = ISRestoreIndices(is,(const PetscInt**)&ia);CHKERRQ(ierr);
177   /* updated cached values (sorted, min, max, etc.)*/
178   ierr = ISSetUp_General(is);CHKERRQ(ierr);
179   PetscFunctionReturn(0);
180 }
181 
182 /*@
183    AOPetscToApplication - Maps a set of integers in the PETSc ordering to
184    the application-defined ordering.
185 
186    Collective on AO
187 
188    Input Parameters:
189 +  ao - the application ordering context
190 .  n - the number of integers
191 -  ia - the integers; these are replaced with their mapped value
192 
193    Output Parameter:
194 .   ia - the mapped integers
195 
196    Level: beginner
197 
198    Note:
199    Any integers in ia[] that are negative are left unchanged. This
200    allows one to convert, for example, neighbor lists that use negative
201    entries to indicate nonexistent neighbors due to boundary conditions, etc.
202 
203    Integers that are out of range are mapped to -1
204 
205 .seealso: AOCreateBasic(), AOView(),AOApplicationToPetsc(),
206           AOPetscToApplicationIS(), AOApplicationToPetsc()
207 @*/
208 PetscErrorCode  AOPetscToApplication(AO ao,PetscInt n,PetscInt ia[])
209 {
210   PetscErrorCode ierr;
211 
212   PetscFunctionBegin;
213   PetscValidHeaderSpecific(ao,AO_CLASSID,1);
214   if (n) PetscValidIntPointer(ia,3);
215   ierr = (*ao->ops->petsctoapplication)(ao,n,ia);CHKERRQ(ierr);
216   PetscFunctionReturn(0);
217 }
218 
219 /*@
220    AOApplicationToPetsc - Maps a set of integers in the application-defined
221    ordering to the PETSc ordering.
222 
223    Collective on AO
224 
225    Input Parameters:
226 +  ao - the application ordering context
227 .  n - the number of integers
228 -  ia - the integers; these are replaced with their mapped value
229 
230    Output Parameter:
231 .   ia - the mapped integers
232 
233    Level: beginner
234 
235    Note:
236    Any integers in ia[] that are negative are left unchanged. This
237    allows one to convert, for example, neighbor lists that use negative
238    entries to indicate nonexistent neighbors due to boundary conditions, etc.
239 
240    Integers that are out of range are mapped to -1
241 
242 .seealso: AOCreateBasic(), AOView(), AOPetscToApplication(),
243           AOPetscToApplicationIS(), AOApplicationToPetsc()
244 @*/
245 PetscErrorCode  AOApplicationToPetsc(AO ao,PetscInt n,PetscInt ia[])
246 {
247   PetscErrorCode ierr;
248 
249   PetscFunctionBegin;
250   PetscValidHeaderSpecific(ao,AO_CLASSID,1);
251   if (n) PetscValidIntPointer(ia,3);
252   ierr = (*ao->ops->applicationtopetsc)(ao,n,ia);CHKERRQ(ierr);
253   PetscFunctionReturn(0);
254 }
255 
256 /*@
257   AOPetscToApplicationPermuteInt - Permutes an array of blocks of integers
258   in the PETSc ordering to the application-defined ordering.
259 
260   Collective on AO
261 
262   Input Parameters:
263 + ao    - The application ordering context
264 . block - The block size
265 - array - The integer array
266 
267   Output Parameter:
268 . array - The permuted array
269 
270   Note: The length of the array should be block*N, where N is length
271   provided to the AOCreate*() method that created the AO.
272 
273   The permutation takes array[i_pet] --> array[i_app], where i_app is
274   the index of 'i' in the application ordering and i_pet is the index
275   of 'i' in the petsc ordering.
276 
277   Level: beginner
278 
279 .seealso: AOCreateBasic(), AOView(), AOApplicationToPetsc(), AOPetscToApplicationIS()
280 @*/
281 PetscErrorCode  AOPetscToApplicationPermuteInt(AO ao, PetscInt block, PetscInt array[])
282 {
283   PetscErrorCode ierr;
284 
285   PetscFunctionBegin;
286   PetscValidHeaderSpecific(ao, AO_CLASSID,1);
287   PetscValidIntPointer(array,3);
288   ierr = (*ao->ops->petsctoapplicationpermuteint)(ao, block, array);CHKERRQ(ierr);
289   PetscFunctionReturn(0);
290 }
291 
292 /*@
293   AOApplicationToPetscPermuteInt - Permutes an array of blocks of integers
294   in the application-defined ordering to the PETSc ordering.
295 
296   Collective on AO
297 
298   Input Parameters:
299 + ao    - The application ordering context
300 . block - The block size
301 - array - The integer array
302 
303   Output Parameter:
304 . array - The permuted array
305 
306   Note: The length of the array should be block*N, where N is length
307   provided to the AOCreate*() method that created the AO.
308 
309   The permutation takes array[i_app] --> array[i_pet], where i_app is
310   the index of 'i' in the application ordering and i_pet is the index
311   of 'i' in the petsc ordering.
312 
313   Level: beginner
314 
315 .seealso: AOCreateBasic(), AOView(), AOPetscToApplicationIS(), AOApplicationToPetsc()
316 @*/
317 PetscErrorCode  AOApplicationToPetscPermuteInt(AO ao, PetscInt block, PetscInt array[])
318 {
319   PetscErrorCode ierr;
320 
321   PetscFunctionBegin;
322   PetscValidHeaderSpecific(ao, AO_CLASSID,1);
323   PetscValidIntPointer(array,3);
324   ierr = (*ao->ops->applicationtopetscpermuteint)(ao, block, array);CHKERRQ(ierr);
325   PetscFunctionReturn(0);
326 }
327 
328 /*@
329   AOPetscToApplicationPermuteReal - Permutes an array of blocks of reals
330   in the PETSc ordering to the application-defined ordering.
331 
332   Collective on AO
333 
334   Input Parameters:
335 + ao    - The application ordering context
336 . block - The block size
337 - array - The integer array
338 
339   Output Parameter:
340 . array - The permuted array
341 
342   Note: The length of the array should be block*N, where N is length
343   provided to the AOCreate*() method that created the AO.
344 
345   The permutation takes array[i_pet] --> array[i_app], where i_app is
346   the index of 'i' in the application ordering and i_pet is the index
347   of 'i' in the petsc ordering.
348 
349   Level: beginner
350 
351 .seealso: AOCreateBasic(), AOView(), AOApplicationToPetsc(), AOPetscToApplicationIS()
352 @*/
353 PetscErrorCode  AOPetscToApplicationPermuteReal(AO ao, PetscInt block, PetscReal array[])
354 {
355   PetscErrorCode ierr;
356 
357   PetscFunctionBegin;
358   PetscValidHeaderSpecific(ao, AO_CLASSID,1);
359   PetscValidIntPointer(array,3);
360   ierr = (*ao->ops->petsctoapplicationpermutereal)(ao, block, array);CHKERRQ(ierr);
361   PetscFunctionReturn(0);
362 }
363 
364 /*@
365   AOApplicationToPetscPermuteReal - Permutes an array of blocks of reals
366   in the application-defined ordering to the PETSc ordering.
367 
368   Collective on AO
369 
370   Input Parameters:
371 + ao    - The application ordering context
372 . block - The block size
373 - array - The integer array
374 
375   Output Parameter:
376 . array - The permuted array
377 
378   Note: The length of the array should be block*N, where N is length
379   provided to the AOCreate*() method that created the AO.
380 
381   The permutation takes array[i_app] --> array[i_pet], where i_app is
382   the index of 'i' in the application ordering and i_pet is the index
383   of 'i' in the petsc ordering.
384 
385   Level: beginner
386 
387 .seealso: AOCreateBasic(), AOView(),AOApplicationToPetsc(), AOPetscToApplicationIS()
388 @*/
389 PetscErrorCode  AOApplicationToPetscPermuteReal(AO ao, PetscInt block, PetscReal array[])
390 {
391   PetscErrorCode ierr;
392 
393   PetscFunctionBegin;
394   PetscValidHeaderSpecific(ao, AO_CLASSID,1);
395   PetscValidIntPointer(array,3);
396   ierr = (*ao->ops->applicationtopetscpermutereal)(ao, block, array);CHKERRQ(ierr);
397   PetscFunctionReturn(0);
398 }
399 
400 /*@
401     AOSetFromOptions - Sets AO options from the options database.
402 
403    Collective on AO
404 
405    Input Parameter:
406 .  ao - the application ordering
407 
408    Level: beginner
409 
410 .seealso: AOCreate(), AOSetType(), AODestroy(), AOPetscToApplication(), AOApplicationToPetsc()
411 @*/
412 PetscErrorCode AOSetFromOptions(AO ao)
413 {
414   PetscErrorCode ierr;
415   char           type[256];
416   const char     *def=AOBASIC;
417   PetscBool      flg;
418 
419   PetscFunctionBegin;
420   PetscValidHeaderSpecific(ao,AO_CLASSID,1);
421 
422   ierr = PetscObjectOptionsBegin((PetscObject)ao);CHKERRQ(ierr);
423   ierr = PetscOptionsFList("-ao_type","AO type","AOSetType",AOList,def,type,256,&flg);CHKERRQ(ierr);
424   if (flg) {
425     ierr = AOSetType(ao,type);CHKERRQ(ierr);
426   } else if (!((PetscObject)ao)->type_name) {
427     ierr = AOSetType(ao,def);CHKERRQ(ierr);
428   }
429   ierr = PetscOptionsEnd();CHKERRQ(ierr);
430   PetscFunctionReturn(0);
431 }
432 
433 /*@
434    AOSetIS - Sets the IS associated with the application ordering.
435 
436    Collective on MPI_Comm
437 
438    Input Parameters:
439 +  ao - the application ordering
440 .  isapp -  index set that defines an ordering
441 -  ispetsc - index set that defines another ordering (may be NULL to use the
442              natural ordering)
443 
444    Notes:
445    The index sets isapp and ispetsc are used only for creation of ao.
446 
447    This routine increases the reference count of isapp and ispetsc so you may/should destroy these arguments after this call if you no longer need them
448 
449    Level: beginner
450 
451 .seealso: AOCreate(), AODestroy(), AOPetscToApplication(), AOApplicationToPetsc()
452 @*/
453 PetscErrorCode AOSetIS(AO ao,IS isapp,IS ispetsc)
454 {
455   PetscErrorCode ierr;
456 
457   PetscFunctionBegin;
458   if (ispetsc) {
459     PetscInt napp,npetsc;
460     ierr = ISGetLocalSize(isapp,&napp);CHKERRQ(ierr);
461     ierr = ISGetLocalSize(ispetsc,&npetsc);CHKERRQ(ierr);
462     if (napp != npetsc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"napp %D != npetsc %D. Local IS lengths must match",napp,npetsc);
463   }
464   if (isapp) {ierr = PetscObjectReference((PetscObject)isapp);CHKERRQ(ierr);}
465   if (ispetsc) {ierr = PetscObjectReference((PetscObject)ispetsc);CHKERRQ(ierr);}
466   ierr = ISDestroy(&ao->isapp);CHKERRQ(ierr);
467   ierr = ISDestroy(&ao->ispetsc);CHKERRQ(ierr);
468   ao->isapp   = isapp;
469   ao->ispetsc = ispetsc;
470   PetscFunctionReturn(0);
471 }
472 
473 /*@
474    AOCreate - Creates an application ordering.
475 
476    Collective on MPI_Comm
477 
478    Input Parameters:
479 .  comm - MPI communicator that is to share AO
480 
481    Output Parameter:
482 .  ao - the new application ordering
483 
484    Options Database Key:
485 +   -ao_type <aotype> - create ao with particular format
486 -   -ao_view - call AOView() at the conclusion of AOCreate()
487 
488    Level: beginner
489 
490 .seealso: AOSetIS(), AODestroy(), AOPetscToApplication(), AOApplicationToPetsc()
491 @*/
492 PetscErrorCode  AOCreate(MPI_Comm comm,AO *ao)
493 {
494   PetscErrorCode ierr;
495   AO             aonew;
496 
497   PetscFunctionBegin;
498   PetscValidPointer(ao,2);
499   *ao = NULL;
500   ierr = AOInitializePackage();CHKERRQ(ierr);
501 
502   ierr = PetscHeaderCreate(aonew,AO_CLASSID,"AO","Application Ordering","AO",comm,AODestroy,AOView);CHKERRQ(ierr);
503   *ao  = aonew;
504   PetscFunctionReturn(0);
505 }
506