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