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