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