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