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