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