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