xref: /petsc/src/sys/dll/reg.c (revision 2e956fe4fc852fabc23b437482e1fb7b77fddb0d)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay     Provides a general mechanism to allow one to register new routines in
4e5c89e4eSSatish Balay     dynamic libraries for many of the PETSc objects (including, e.g., KSP and PC).
5e5c89e4eSSatish Balay */
6af0996ceSBarry Smith #include <petsc/private/petscimpl.h>           /*I "petscsys.h" I*/
7665c2dedSJed Brown #include <petscviewer.h>
8e5c89e4eSSatish Balay 
93fa76a5bSLisandro Dalcin /*
103fa76a5bSLisandro Dalcin     This is the default list used by PETSc with the PetscDLLibrary register routines
113fa76a5bSLisandro Dalcin */
1202c9f0b5SLisandro Dalcin PetscDLLibrary PetscDLLibrariesLoaded = NULL;
133fa76a5bSLisandro Dalcin 
14cbd104e6SBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
15ebd79076SLisandro Dalcin 
1660da17ecSBarry Smith static PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
17487e5849SBarry Smith {
18487e5849SBarry Smith   char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
19487e5849SBarry Smith 
20487e5849SBarry Smith   PetscFunctionBegin;
219566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(libs,"${PETSC_LIB_DIR}/libpetsc",sizeof(libs)));
229566063dSJacob Faibussowitsch   PetscCall(PetscStrlcat(libs,name,sizeof(libs)));
239566063dSJacob Faibussowitsch   PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found));
24487e5849SBarry Smith   if (*found) {
259566063dSJacob Faibussowitsch     PetscCall(PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib));
26487e5849SBarry Smith   } else {
279566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc",sizeof(libs)));
289566063dSJacob Faibussowitsch     PetscCall(PetscStrlcat(libs,name,sizeof(libs)));
299566063dSJacob Faibussowitsch     PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found));
30487e5849SBarry Smith     if (*found) {
319566063dSJacob Faibussowitsch       PetscCall(PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib));
32487e5849SBarry Smith     }
33487e5849SBarry Smith   }
34487e5849SBarry Smith   PetscFunctionReturn(0);
35487e5849SBarry Smith }
363fa76a5bSLisandro Dalcin #endif
373fa76a5bSLisandro Dalcin 
3860da17ecSBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY) && !(defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES))
3995c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode AOInitializePackage(void);
4095c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscSFInitializePackage(void);
417da51e29SSatish Balay #if !defined(PETSC_USE_COMPLEX)
4295c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode CharacteristicInitializePackage(void);
437da51e29SSatish Balay #endif
4495c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode ISInitializePackage(void);
4595c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode VecInitializePackage(void);
4695c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode MatInitializePackage(void);
4795c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode DMInitializePackage(void);
4895c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PCInitializePackage(void);
4995c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode KSPInitializePackage(void);
5095c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode SNESInitializePackage(void);
5195c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode TSInitializePackage(void);
5260da17ecSBarry Smith PETSC_EXTERN PetscErrorCode TaoInitializePackage(void);
5360da17ecSBarry Smith #endif
5460da17ecSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
5522b6d1caSBarry Smith static MPI_Comm PETSC_COMM_WORLD_INNER = 0,PETSC_COMM_SELF_INNER = 0;
56acff04ddSBarry Smith #endif
57acff04ddSBarry Smith 
58e5c89e4eSSatish Balay /*
59e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
60e5c89e4eSSatish Balay     search path.
61e5c89e4eSSatish Balay */
6295c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void)
63e5c89e4eSSatish Balay {
64487e5849SBarry Smith   char           *libname[32];
65e5c89e4eSSatish Balay   PetscInt       nmax,i;
6660da17ecSBarry Smith   PetscBool      preload = PETSC_FALSE;
67540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
68540e20f2SPierre Jolivet   PetscBool      PetscInitialized = PetscInitializeCalled;
69540e20f2SPierre Jolivet #endif
70e5c89e4eSSatish Balay 
7160154eb2SBarry Smith   PetscFunctionBegin;
7260da17ecSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
7360da17ecSBarry Smith   /* These must be all initialized here because it is not safe for individual threads to call these initialize routines */
7460da17ecSBarry Smith   preload = PETSC_TRUE;
7560da17ecSBarry Smith #endif
7660da17ecSBarry Smith 
77e5c89e4eSSatish Balay   nmax = 32;
789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetStringArray(NULL,NULL,"-dll_prepend",libname,&nmax,NULL));
79e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
809566063dSJacob Faibussowitsch     PetscCall(PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]));
819566063dSJacob Faibussowitsch     PetscCall(PetscFree(libname[i]));
82e5c89e4eSSatish Balay   }
83e5c89e4eSSatish Balay 
849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL,NULL,"-library_preload",&preload,NULL));
8560da17ecSBarry Smith   if (!preload) {
869566063dSJacob Faibussowitsch     PetscCall(PetscSysInitializePackage());
8760da17ecSBarry Smith   } else {
8860da17ecSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
89aa2d57e9SJed Brown     PetscBool found;
9060154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
919566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("",&found));
9228b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
9360154eb2SBarry Smith #else
949566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("sys",&found));
9528b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
969566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("vec",&found));
9728b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Vec dynamic library \n You cannot move the dynamic libraries!");
989566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("mat",&found));
9928b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Mat dynamic library \n You cannot move the dynamic libraries!");
1009566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("dm",&found));
10128b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc DM dynamic library \n You cannot move the dynamic libraries!");
1029566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("ksp",&found));
10328b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc KSP dynamic library \n You cannot move the dynamic libraries!");
1049566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("snes",&found));
10528b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc SNES dynamic library \n You cannot move the dynamic libraries!");
1069566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("ts",&found));
10728b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc TS dynamic library \n You cannot move the dynamic libraries!");
1089566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("tao",&found));
10928b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate Tao dynamic library \n You cannot move the dynamic libraries!");
110bb84e0fdSBarry Smith #endif
11160da17ecSBarry Smith #else /* defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) */
11260da17ecSBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
1139566063dSJacob Faibussowitsch   PetscCall(AOInitializePackage());
1149566063dSJacob Faibussowitsch   PetscCall(PetscSFInitializePackage());
1157da51e29SSatish Balay #if !defined(PETSC_USE_COMPLEX)
1169566063dSJacob Faibussowitsch   PetscCall(CharacteristicInitializePackage());
1177da51e29SSatish Balay #endif
1189566063dSJacob Faibussowitsch   PetscCall(ISInitializePackage());
1199566063dSJacob Faibussowitsch   PetscCall(VecInitializePackage());
1209566063dSJacob Faibussowitsch   PetscCall(MatInitializePackage());
1219566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
1229566063dSJacob Faibussowitsch   PetscCall(PCInitializePackage());
1239566063dSJacob Faibussowitsch   PetscCall(KSPInitializePackage());
1249566063dSJacob Faibussowitsch   PetscCall(SNESInitializePackage());
1259566063dSJacob Faibussowitsch   PetscCall(TSInitializePackage());
1269566063dSJacob Faibussowitsch   PetscCall(TaoInitializePackage());
12760da17ecSBarry Smith #else
12860da17ecSBarry Smith   SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Cannot use -library_preload with multiple static PETSc libraries");
12960da17ecSBarry Smith #endif
13060da17ecSBarry Smith #endif /* defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) */
13160da17ecSBarry Smith   }
13260da17ecSBarry Smith 
13360da17ecSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) && defined(PETSC_HAVE_BAMG)
13460da17ecSBarry Smith   {
13560da17ecSBarry Smith     PetscBool found;
1369566063dSJacob Faibussowitsch     PetscCall(PetscLoadDynamicLibrary("bamg",&found));
13728b400f6SJacob Faibussowitsch     PetscCheck(found,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc BAMG dynamic library \n You cannot move the dynamic libraries!");
13860da17ecSBarry Smith   }
13960da17ecSBarry Smith #endif
14060da17ecSBarry Smith 
14160da17ecSBarry Smith   nmax = 32;
1429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetStringArray(NULL,NULL,"-dll_append",libname,&nmax,NULL));
14360da17ecSBarry Smith   for (i=0; i<nmax; i++) {
1449566063dSJacob Faibussowitsch     PetscCall(PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]));
1459566063dSJacob Faibussowitsch     PetscCall(PetscFree(libname[i]));
14660da17ecSBarry Smith   }
14760da17ecSBarry Smith 
14860da17ecSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
1499566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(PETSC_COMM_SELF,&PETSC_COMM_SELF_INNER,NULL));
1509566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(PETSC_COMM_WORLD,&PETSC_COMM_WORLD_INNER,NULL));
151acff04ddSBarry Smith #endif
152540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
153540e20f2SPierre Jolivet   /* in Fortran, PetscInitializeCalled is set to PETSC_TRUE before PetscInitialize_DynamicLibraries() */
154540e20f2SPierre Jolivet   /* in C, it is not the case, but the value is forced to PETSC_TRUE so that PetscRegisterFinalize() is called */
155540e20f2SPierre Jolivet   PetscInitializeCalled = PETSC_TRUE;
1569566063dSJacob Faibussowitsch   PetscCall(PetscElementalInitializePackage());
157540e20f2SPierre Jolivet   PetscInitializeCalled = PetscInitialized;
158540e20f2SPierre Jolivet #endif
159e5c89e4eSSatish Balay   PetscFunctionReturn(0);
160e5c89e4eSSatish Balay }
161e5c89e4eSSatish Balay 
162ebd79076SLisandro Dalcin /*
163ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
164ebd79076SLisandro Dalcin */
16595c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void)
166e5c89e4eSSatish Balay {
167ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
168e5c89e4eSSatish Balay 
169ebd79076SLisandro Dalcin   PetscFunctionBegin;
1709566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL,NULL,"-dll_view",&flg,NULL));
1719566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscDLLibraryPrintPath(PetscDLLibrariesLoaded));
1729566063dSJacob Faibussowitsch   PetscCall(PetscDLLibraryClose(PetscDLLibrariesLoaded));
173a297a907SKarl Rupp 
174acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
1759566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&PETSC_COMM_SELF_INNER));
1769566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&PETSC_COMM_WORLD_INNER));
177acff04ddSBarry Smith #endif
178acff04ddSBarry Smith 
17902c9f0b5SLisandro Dalcin   PetscDLLibrariesLoaded = NULL;
180e5c89e4eSSatish Balay   PetscFunctionReturn(0);
181e5c89e4eSSatish Balay }
182e5c89e4eSSatish Balay 
183e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
184140e18c1SBarry Smith struct _n_PetscFunctionList {
185e5c89e4eSSatish Balay   void              (*routine)(void);    /* the routine */
186e5c89e4eSSatish Balay   char              *name;               /* string to identify routine */
187140e18c1SBarry Smith   PetscFunctionList next;                /* next pointer */
188140e18c1SBarry Smith   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
189e5c89e4eSSatish Balay };
190e5c89e4eSSatish Balay 
191e5c89e4eSSatish Balay /*
192140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
193e5c89e4eSSatish Balay */
19402c9f0b5SLisandro Dalcin static PetscFunctionList dlallhead = NULL;
195e5c89e4eSSatish Balay 
196a240a19fSJed Brown /*MC
197140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
198e5c89e4eSSatish Balay    specified registry.
199e5c89e4eSSatish Balay 
200a240a19fSJed Brown    Synopsis:
201aaa7dc30SBarry Smith    #include <petscsys.h>
202b9fb364aSBarry Smith    PetscErrorCode PetscFunctionListAdd(PetscFunctionList *flist,const char name[],void (*fptr)(void))
203a240a19fSJed Brown 
204d4349b43SBarry Smith    Not Collective
205e5c89e4eSSatish Balay 
206e5c89e4eSSatish Balay    Input Parameters:
207b9fb364aSBarry Smith +  flist - pointer to function list object
208e5c89e4eSSatish Balay .  name - string to identify routine
209a240a19fSJed Brown -  fptr - function pointer
210e5c89e4eSSatish Balay 
211e5c89e4eSSatish Balay    Notes:
212a240a19fSJed Brown    To remove a registered routine, pass in a NULL fptr.
213e5c89e4eSSatish Balay 
214e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
215e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
2161c84c290SBarry Smith    for that particular component (e.g., SNESRegister()) instead of
217140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
218e5c89e4eSSatish Balay 
219e5c89e4eSSatish Balay     Level: developer
220e5c89e4eSSatish Balay 
221db781477SPatrick Sanan .seealso: `PetscFunctionListDestroy()`, `SNESRegister()`, `KSPRegister()`,
222db781477SPatrick Sanan           `PCRegister()`, `TSRegister()`, `PetscFunctionList`, `PetscObjectComposeFunction()`
223a240a19fSJed Brown M*/
224a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl,const char name[],void (*fnc)(void))
225e5c89e4eSSatish Balay {
226140e18c1SBarry Smith   PetscFunctionList entry,ne;
227e5c89e4eSSatish Balay 
228e5c89e4eSSatish Balay   PetscFunctionBegin;
229e5c89e4eSSatish Balay   if (!*fl) {
2309566063dSJacob Faibussowitsch     PetscCall(PetscNew(&entry));
2319566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name,&entry->name));
232e5c89e4eSSatish Balay     entry->routine = fnc;
23302c9f0b5SLisandro Dalcin     entry->next    = NULL;
234e5c89e4eSSatish Balay     *fl            = entry;
235e5c89e4eSSatish Balay 
23676bd3646SJed Brown     if (PetscDefined(USE_DEBUG)) {
237e5c89e4eSSatish Balay       /* add this new list to list of all lists */
238e5c89e4eSSatish Balay       if (!dlallhead) {
239e5c89e4eSSatish Balay         dlallhead        = *fl;
24002c9f0b5SLisandro Dalcin         (*fl)->next_list = NULL;
241e5c89e4eSSatish Balay       } else {
242e5c89e4eSSatish Balay         ne               = dlallhead;
243e5c89e4eSSatish Balay         dlallhead        = *fl;
244e5c89e4eSSatish Balay         (*fl)->next_list = ne;
245e5c89e4eSSatish Balay       }
24676bd3646SJed Brown     }
247a8a6328fSBarry Smith 
248e5c89e4eSSatish Balay   } else {
249e5c89e4eSSatish Balay     /* search list to see if it is already there */
250e5c89e4eSSatish Balay     ne = *fl;
251e5c89e4eSSatish Balay     while (ne) {
252ace3abfcSBarry Smith       PetscBool founddup;
253e5c89e4eSSatish Balay 
2549566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(ne->name,name,&founddup));
255e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
256e5c89e4eSSatish Balay         ne->routine = fnc;
257e5c89e4eSSatish Balay         PetscFunctionReturn(0);
258e5c89e4eSSatish Balay       }
259a297a907SKarl Rupp       if (ne->next) ne = ne->next;
260a297a907SKarl Rupp       else break;
261e5c89e4eSSatish Balay     }
262e5c89e4eSSatish Balay     /* create new entry and add to end of list */
2639566063dSJacob Faibussowitsch     PetscCall(PetscNew(&entry));
2649566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name,&entry->name));
265e5c89e4eSSatish Balay     entry->routine = fnc;
26602c9f0b5SLisandro Dalcin     entry->next    = NULL;
267e5c89e4eSSatish Balay     ne->next       = entry;
268e5c89e4eSSatish Balay   }
269e5c89e4eSSatish Balay   PetscFunctionReturn(0);
270e5c89e4eSSatish Balay }
271e5c89e4eSSatish Balay 
272e5c89e4eSSatish Balay /*@
273140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
274e5c89e4eSSatish Balay 
275e5c89e4eSSatish Balay     Input Parameter:
276e5c89e4eSSatish Balay .   fl  - pointer to list
277e5c89e4eSSatish Balay 
278e5c89e4eSSatish Balay     Level: developer
279e5c89e4eSSatish Balay 
280db781477SPatrick Sanan .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`
281e5c89e4eSSatish Balay @*/
282140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
283e5c89e4eSSatish Balay {
284140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
285e5c89e4eSSatish Balay 
286e5c89e4eSSatish Balay   PetscFunctionBegin;
2871441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
288e5c89e4eSSatish Balay 
289e5c89e4eSSatish Balay   /*
2909dddd249SSatish Balay        Remove this entry from the main DL list (if it is in it)
291e5c89e4eSSatish Balay   */
2921441b1d3SBarry Smith   if (dlallhead == *fl) {
293a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
29437e93019SBarry Smith     else dlallhead = NULL;
29537e93019SBarry Smith   } else if (tmp) {
2961441b1d3SBarry Smith     while (tmp->next_list != *fl) {
297e5c89e4eSSatish Balay       tmp = tmp->next_list;
298e5c89e4eSSatish Balay       if (!tmp->next_list) break;
299e5c89e4eSSatish Balay     }
300e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
301e5c89e4eSSatish Balay   }
302e5c89e4eSSatish Balay 
303e5c89e4eSSatish Balay   /* free this list */
3041441b1d3SBarry Smith   entry = *fl;
305e5c89e4eSSatish Balay   while (entry) {
306e5c89e4eSSatish Balay     next  = entry->next;
3079566063dSJacob Faibussowitsch     PetscCall(PetscFree(entry->name));
3089566063dSJacob Faibussowitsch     PetscCall(PetscFree(entry));
309e5c89e4eSSatish Balay     entry = next;
310e5c89e4eSSatish Balay   }
31102c9f0b5SLisandro Dalcin   *fl = NULL;
312e5c89e4eSSatish Balay   PetscFunctionReturn(0);
313e5c89e4eSSatish Balay }
314e5c89e4eSSatish Balay 
315e5c89e4eSSatish Balay /*
316*2e956fe4SStefano Zampini    Print registered PetscFunctionLists
317e5c89e4eSSatish Balay */
31837e93019SBarry Smith PetscErrorCode  PetscFunctionListPrintAll(void)
319e5c89e4eSSatish Balay {
32037e93019SBarry Smith   PetscFunctionList tmp = dlallhead;
321e5c89e4eSSatish Balay 
322e5c89e4eSSatish Balay   PetscFunctionBegin;
323*2e956fe4SStefano Zampini   if (tmp) PetscCall(PetscPrintf(PETSC_COMM_SELF,"[%d] Registered PetscFunctionLists\n",PetscGlobalRank));
32437e93019SBarry Smith   while (tmp) {
325*2e956fe4SStefano Zampini     PetscCall(PetscPrintf(PETSC_COMM_SELF,"[%d]   %s\n",PetscGlobalRank,tmp->name));
32637e93019SBarry Smith     tmp = tmp->next_list;
32737e93019SBarry Smith   }
328e5c89e4eSSatish Balay   PetscFunctionReturn(0);
329e5c89e4eSSatish Balay }
330e5c89e4eSSatish Balay 
3311c9cd337SJed Brown /*MC
332*2e956fe4SStefano Zampini     PetscFunctionListNonEmpty - Print composed names for non null function pointers
333*2e956fe4SStefano Zampini 
334*2e956fe4SStefano Zampini     Input Parameter:
335*2e956fe4SStefano Zampini .   flist   - pointer to list
336*2e956fe4SStefano Zampini 
337*2e956fe4SStefano Zampini     Level: developer
338*2e956fe4SStefano Zampini 
339*2e956fe4SStefano Zampini .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`, `PetscObjectQueryFunction()`
340*2e956fe4SStefano Zampini M*/
341*2e956fe4SStefano Zampini PetscErrorCode  PetscFunctionListPrintNonEmpty(PetscFunctionList fl)
342*2e956fe4SStefano Zampini {
343*2e956fe4SStefano Zampini   PetscFunctionBegin;
344*2e956fe4SStefano Zampini   while (fl) {
345*2e956fe4SStefano Zampini     PetscFunctionList next  = fl->next;
346*2e956fe4SStefano Zampini     if (fl->routine) PetscCall(PetscPrintf(PETSC_COMM_SELF,"[%d] function name: %s\n",PetscGlobalRank,fl->name));
347*2e956fe4SStefano Zampini     fl = next;
348*2e956fe4SStefano Zampini   }
349*2e956fe4SStefano Zampini   PetscFunctionReturn(0);
350*2e956fe4SStefano Zampini }
351*2e956fe4SStefano Zampini 
352*2e956fe4SStefano Zampini /*MC
3531c9cd337SJed Brown     PetscFunctionListFind - Find function registered under given name
3541c9cd337SJed Brown 
3551c9cd337SJed Brown     Synopsis:
356aaa7dc30SBarry Smith     #include <petscsys.h>
3571c9cd337SJed Brown     PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void))
358e5c89e4eSSatish Balay 
359e5c89e4eSSatish Balay     Input Parameters:
3601c9cd337SJed Brown +   flist   - pointer to list
3611c9cd337SJed Brown -   name - name registered for the function
362e5c89e4eSSatish Balay 
363e5c89e4eSSatish Balay     Output Parameters:
3641c9cd337SJed Brown .   fptr - the function pointer if name was found, else NULL
365e5c89e4eSSatish Balay 
366e5c89e4eSSatish Balay     Level: developer
367e5c89e4eSSatish Balay 
368db781477SPatrick Sanan .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`, `PetscObjectQueryFunction()`
3691c9cd337SJed Brown M*/
3701c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void))
371e5c89e4eSSatish Balay {
372140e18c1SBarry Smith   PetscFunctionList entry = fl;
373bdf89e91SBarry Smith   PetscBool         flg;
374e5c89e4eSSatish Balay 
375e5c89e4eSSatish Balay   PetscFunctionBegin;
37628b400f6SJacob Faibussowitsch   PetscCheck(name,PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
377e5c89e4eSSatish Balay 
37802c9f0b5SLisandro Dalcin   *r = NULL;
379e5c89e4eSSatish Balay   while (entry) {
3809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name,entry->name,&flg));
381bdf89e91SBarry Smith     if (flg) {
382e5c89e4eSSatish Balay       *r   = entry->routine;
383e5c89e4eSSatish Balay       PetscFunctionReturn(0);
384e5c89e4eSSatish Balay     }
385e5c89e4eSSatish Balay     entry = entry->next;
386e5c89e4eSSatish Balay   }
387e5c89e4eSSatish Balay   PetscFunctionReturn(0);
388e5c89e4eSSatish Balay }
389e5c89e4eSSatish Balay 
390e5c89e4eSSatish Balay /*@
391140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
392e5c89e4eSSatish Balay 
393e5c89e4eSSatish Balay    Collective over MPI_Comm
394e5c89e4eSSatish Balay 
395e5c89e4eSSatish Balay    Input Parameters:
396e5c89e4eSSatish Balay +  list - the list of functions
397e5c89e4eSSatish Balay -  viewer - currently ignored
398e5c89e4eSSatish Balay 
399e5c89e4eSSatish Balay    Level: developer
400e5c89e4eSSatish Balay 
401db781477SPatrick Sanan .seealso: `PetscFunctionListAdd()`, `PetscFunctionListPrintTypes()`, `PetscFunctionList`
402e5c89e4eSSatish Balay @*/
403140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
404e5c89e4eSSatish Balay {
405ace3abfcSBarry Smith   PetscBool      iascii;
406e5c89e4eSSatish Balay 
407e5c89e4eSSatish Balay   PetscFunctionBegin;
408e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
409e5c89e4eSSatish Balay   PetscValidPointer(list,1);
4100700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
411e5c89e4eSSatish Balay 
4129566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii));
41328b400f6SJacob Faibussowitsch   PetscCheck(iascii,PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
414e5c89e4eSSatish Balay 
415e5c89e4eSSatish Balay   while (list) {
4169566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer," %s\n",list->name));
417e5c89e4eSSatish Balay     list = list->next;
418e5c89e4eSSatish Balay   }
4199566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer,"\n"));
420e5c89e4eSSatish Balay   PetscFunctionReturn(0);
421e5c89e4eSSatish Balay }
422e5c89e4eSSatish Balay 
423065533a5SJed Brown /*@C
424140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
425e5c89e4eSSatish Balay          by help etc.
426e5c89e4eSSatish Balay 
427d4349b43SBarry Smith    Not Collective
428e5c89e4eSSatish Balay 
429e5c89e4eSSatish Balay    Input Parameter:
430e5c89e4eSSatish Balay .  list   - list of types
431e5c89e4eSSatish Balay 
432d8d19677SJose E. Roman    Output Parameters:
433e5c89e4eSSatish Balay +  array - array of names
434e5c89e4eSSatish Balay -  n - length of array
435e5c89e4eSSatish Balay 
436e5c89e4eSSatish Balay    Notes:
437e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
438e5c89e4eSSatish Balay     not copied so should not be freed.
439e5c89e4eSSatish Balay 
440e5c89e4eSSatish Balay    Level: developer
441e5c89e4eSSatish Balay 
442db781477SPatrick Sanan .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`
443e5c89e4eSSatish Balay @*/
444ca1b3570SJed Brown PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
445e5c89e4eSSatish Balay {
446e5c89e4eSSatish Balay   PetscInt          count = 0;
447140e18c1SBarry Smith   PetscFunctionList klist = list;
448e5c89e4eSSatish Balay 
449e5c89e4eSSatish Balay   PetscFunctionBegin;
450e5c89e4eSSatish Balay   while (list) {
451e5c89e4eSSatish Balay     list = list->next;
452e5c89e4eSSatish Balay     count++;
453e5c89e4eSSatish Balay   }
4549566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(count+1,(char***)array));
455e5c89e4eSSatish Balay   count = 0;
456e5c89e4eSSatish Balay   while (klist) {
457e5c89e4eSSatish Balay     (*array)[count] = klist->name;
458e5c89e4eSSatish Balay     klist           = klist->next;
459e5c89e4eSSatish Balay     count++;
460e5c89e4eSSatish Balay   }
46102c9f0b5SLisandro Dalcin   (*array)[count] = NULL;
462e5c89e4eSSatish Balay   *n              = count+1;
463e5c89e4eSSatish Balay   PetscFunctionReturn(0);
464e5c89e4eSSatish Balay }
465e5c89e4eSSatish Balay 
466e5c89e4eSSatish Balay /*@C
467140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
468e5c89e4eSSatish Balay 
469e5c89e4eSSatish Balay    Collective over MPI_Comm
470e5c89e4eSSatish Balay 
471e5c89e4eSSatish Balay    Input Parameters:
472e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
473e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
474e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
475e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
476e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
477e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
4783cc1e11dSBarry Smith .  list   - list of types
47944ef3d73SBarry Smith .  def - default (current) value
48044ef3d73SBarry Smith -  newv - new value
481e5c89e4eSSatish Balay 
482e5c89e4eSSatish Balay    Level: developer
483e5c89e4eSSatish Balay 
484db781477SPatrick Sanan .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`
485e5c89e4eSSatish Balay @*/
48644ef3d73SBarry Smith PetscErrorCode  PetscFunctionListPrintTypes(MPI_Comm comm,FILE *fd,const char prefix[],const char name[],const char text[],const char man[],PetscFunctionList list,const char def[],const char newv[])
487e5c89e4eSSatish Balay {
488e5c89e4eSSatish Balay   char           p[64];
489e5c89e4eSSatish Balay 
490e5c89e4eSSatish Balay   PetscFunctionBegin;
491da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
492e5c89e4eSSatish Balay 
4939566063dSJacob Faibussowitsch   PetscCall(PetscStrncpy(p,"-",sizeof(p)));
4949566063dSJacob Faibussowitsch   if (prefix) PetscCall(PetscStrlcat(p,prefix,sizeof(p)));
4959566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm,fd,"  %s%s <now %s : formerly %s>: %s (one of)",p,name+1,newv,def,text));
496e5c89e4eSSatish Balay 
497e5c89e4eSSatish Balay   while (list) {
4989566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm,fd," %s",list->name));
499e5c89e4eSSatish Balay     list = list->next;
500e5c89e4eSSatish Balay   }
5019566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm,fd," (%s)\n",man));
502e5c89e4eSSatish Balay   PetscFunctionReturn(0);
503e5c89e4eSSatish Balay }
504e5c89e4eSSatish Balay 
505e5c89e4eSSatish Balay /*@
506140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
507e5c89e4eSSatish Balay 
508e5c89e4eSSatish Balay     Input Parameters:
509e5c89e4eSSatish Balay .   fl   - pointer to list
510e5c89e4eSSatish Balay 
511e5c89e4eSSatish Balay     Output Parameters:
512e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
513e5c89e4eSSatish Balay 
514e5c89e4eSSatish Balay     Level: developer
515e5c89e4eSSatish Balay 
516db781477SPatrick Sanan .seealso: `PetscFunctionList`, `PetscFunctionListAdd()`, `PetscFlistDestroy()`
517e5c89e4eSSatish Balay 
518e5c89e4eSSatish Balay @*/
519140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
520e5c89e4eSSatish Balay {
521e5c89e4eSSatish Balay   PetscFunctionBegin;
522e5c89e4eSSatish Balay   while (fl) {
5239566063dSJacob Faibussowitsch     PetscCall(PetscFunctionListAdd(nl,fl->name,fl->routine));
524e5c89e4eSSatish Balay     fl   = fl->next;
525e5c89e4eSSatish Balay   }
526e5c89e4eSSatish Balay   PetscFunctionReturn(0);
527e5c89e4eSSatish Balay }
528