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