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