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