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