xref: /petsc/src/vec/is/ao/interface/aoreg.c (revision 8a7d4057d9226490dba4e1a062f54f84e7d90861)
1 #include <../src/vec/is/ao/aoimpl.h> /*I "petscao.h"  I*/
2 
3 static PetscBool AOPackageInitialized = PETSC_FALSE;
4 static PetscBool AORegisterAllCalled  = PETSC_FALSE;
5 
6 /*@C
7   AOFinalizePackage - This function finalizes everything in the `AO` package. It is called
8   from `PetscFinalize()`.
9 
10   Level: developer
11 
12 .seealso: `AOInitializePackage()`, `PetscInitialize()`
13 @*/
AOFinalizePackage(void)14 PetscErrorCode AOFinalizePackage(void)
15 {
16   PetscFunctionBegin;
17   PetscCall(PetscFunctionListDestroy(&AOList));
18   AOPackageInitialized = PETSC_FALSE;
19   AORegisterAllCalled  = PETSC_FALSE;
20   PetscFunctionReturn(PETSC_SUCCESS);
21 }
22 
23 /*@C
24   AOInitializePackage - This function initializes everything in the `AO` package. It is called
25   from `PetscDLLibraryRegister_petscvec()` when using dynamic libraries, and on the first call to `AOCreate()`
26   when using static or shared libraries.
27 
28   Level: developer
29 
30 .seealso: `AOFinalizePackage()`, `PetscInitialize()`
31 @*/
AOInitializePackage(void)32 PetscErrorCode AOInitializePackage(void)
33 {
34   char      logList[256];
35   PetscBool opt, pkg;
36 
37   PetscFunctionBegin;
38   if (AOPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
39   AOPackageInitialized = PETSC_TRUE;
40   /* Register Classes */
41   PetscCall(PetscClassIdRegister("Application Order", &AO_CLASSID));
42   /* Register Constructors */
43   PetscCall(AORegisterAll());
44   /* Register Events */
45   PetscCall(PetscLogEventRegister("AOPetscToApplication", AO_CLASSID, &AO_PetscToApplication));
46   PetscCall(PetscLogEventRegister("AOApplicationToPetsc", AO_CLASSID, &AO_ApplicationToPetsc));
47   /* Process Info */
48   {
49     PetscClassId classids[1];
50 
51     classids[0] = AO_CLASSID;
52     PetscCall(PetscInfoProcessClass("ao", 1, classids));
53   }
54   /* Process summary exclusions */
55   PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
56   if (opt) {
57     PetscCall(PetscStrInList("ao", logList, ',', &pkg));
58     if (pkg) PetscCall(PetscLogEventExcludeClass(AO_CLASSID));
59   }
60   /* Register package finalizer */
61   PetscCall(PetscRegisterFinalize(AOFinalizePackage));
62   PetscFunctionReturn(PETSC_SUCCESS);
63 }
64 
65 /*@
66   AOSetType - Builds an application ordering for a particular `AOType`
67 
68   Collective
69 
70   Input Parameters:
71 + ao     - The `AO` object
72 - method - The name of the AO type
73 
74   Options Database Key:
75 . -ao_type <type> - Sets the `AO` type; use -help for a list of available types
76 
77   Level: intermediate
78 
79   Notes:
80   See "petsc/include/petscao.h" for available AO types (for instance, `AOBASIC` and `AOMEMORYSCALABLE`).
81 
82   `AO` are usually created via the convenience routines such as `AOCreateBasic()` or `AOCreateMemoryScalable()`
83 
84 .seealso: `AO`, `AOType`, `AOCreateBasic()`, `AOCreateMemoryScalable()`, `AOGetType()`, `AOCreate()`
85 @*/
AOSetType(AO ao,AOType method)86 PetscErrorCode AOSetType(AO ao, AOType method)
87 {
88   PetscErrorCode (*r)(AO);
89   PetscBool match;
90 
91   PetscFunctionBegin;
92   PetscValidHeaderSpecific(ao, AO_CLASSID, 1);
93   PetscCall(PetscObjectTypeCompare((PetscObject)ao, method, &match));
94   if (match) PetscFunctionReturn(PETSC_SUCCESS);
95 
96   PetscCall(AORegisterAll());
97   PetscCall(PetscFunctionListFind(AOList, method, &r));
98   PetscCheck(r, PetscObjectComm((PetscObject)ao), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown AO type: %s", method);
99   PetscTryTypeMethod(ao, destroy);
100   ao->ops->destroy = NULL;
101 
102   PetscCall((*r)(ao));
103   PetscFunctionReturn(PETSC_SUCCESS);
104 }
105 
106 /*@
107   AOGetType - Gets the `AO` type name (as a string) from the AO.
108 
109   Not Collective
110 
111   Input Parameter:
112 . ao - The vector
113 
114   Output Parameter:
115 . type - The `AO` type name
116 
117   Level: intermediate
118 
119 .seealso: `AO`, `AOType`, `AOSetType()`, `AOCreate()`
120 @*/
AOGetType(AO ao,AOType * type)121 PetscErrorCode AOGetType(AO ao, AOType *type)
122 {
123   PetscFunctionBegin;
124   PetscValidHeaderSpecific(ao, AO_CLASSID, 1);
125   PetscAssertPointer(type, 2);
126   PetscCall(AORegisterAll());
127   *type = ((PetscObject)ao)->type_name;
128   PetscFunctionReturn(PETSC_SUCCESS);
129 }
130 
131 PetscFunctionList AOList = NULL;
132 
133 /*@C
134   AORegister - Register  an application ordering method
135 
136   Not Collective, No Fortran Support
137 
138   Input Parameters:
139 + sname    - the name (`AOType`) of the `AO` scheme
140 - function - the create routine for the application ordering method
141 
142   Level: advanced
143 
144 .seealso: `AO`, `AOType`, `AOCreate()`, `AORegisterAll()`, `AOBASIC`, `AOADVANCED`, `AOMAPPING`, `AOMEMORYSCALABLE`
145 @*/
AORegister(const char sname[],PetscErrorCode (* function)(AO))146 PetscErrorCode AORegister(const char sname[], PetscErrorCode (*function)(AO))
147 {
148   PetscFunctionBegin;
149   PetscCall(AOInitializePackage());
150   PetscCall(PetscFunctionListAdd(&AOList, sname, function));
151   PetscFunctionReturn(PETSC_SUCCESS);
152 }
153 
154 PETSC_INTERN PetscErrorCode AOCreate_Basic(AO ao);
155 PETSC_INTERN PetscErrorCode AOCreate_MemoryScalable(AO ao);
156 
157 /*@C
158   AORegisterAll - Registers all of the application ordering components in the `AO` package.
159 
160   Not Collective
161 
162   Level: advanced
163 
164 .seealso: `AO`, `AOType`, `AORegister()`, `AORegisterDestroy()`
165 @*/
AORegisterAll(void)166 PetscErrorCode AORegisterAll(void)
167 {
168   PetscFunctionBegin;
169   if (AORegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
170   AORegisterAllCalled = PETSC_TRUE;
171 
172   PetscCall(AORegister(AOBASIC, AOCreate_Basic));
173   PetscCall(AORegister(AOMEMORYSCALABLE, AOCreate_MemoryScalable));
174   PetscFunctionReturn(PETSC_SUCCESS);
175 }
176