xref: /petsc/src/sys/dll/reg.c (revision 9dddd24924da2034e9ad37bd0330bf8579e05078)
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 
16dc21ef1bSBarry Smith PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
17487e5849SBarry Smith {
18487e5849SBarry Smith   char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
19487e5849SBarry Smith   PetscErrorCode ierr;
20487e5849SBarry Smith 
21487e5849SBarry Smith   PetscFunctionBegin;
22a126751eSBarry Smith   ierr = PetscStrncpy(libs,"${PETSC_LIB_DIR}/libpetsc",sizeof(libs));CHKERRQ(ierr);
23a126751eSBarry Smith   ierr = PetscStrlcat(libs,name,sizeof(libs));CHKERRQ(ierr);
24487e5849SBarry Smith   ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
25487e5849SBarry Smith   if (*found) {
26d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
27487e5849SBarry Smith   } else {
28a126751eSBarry Smith     ierr = PetscStrncpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc",sizeof(libs));CHKERRQ(ierr);
29a126751eSBarry Smith     ierr = PetscStrlcat(libs,name,sizeof(libs));CHKERRQ(ierr);
30487e5849SBarry Smith     ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
31487e5849SBarry Smith     if (*found) {
32d44a1e48SBarry Smith       ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
33487e5849SBarry Smith     }
34487e5849SBarry Smith   }
35487e5849SBarry Smith   PetscFunctionReturn(0);
36487e5849SBarry Smith }
373fa76a5bSLisandro Dalcin #endif
383fa76a5bSLisandro Dalcin 
39acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
4095c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode AOInitializePackage(void);
4195c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscSFInitializePackage(void);
427da51e29SSatish Balay #if !defined(PETSC_USE_COMPLEX)
4395c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode CharacteristicInitializePackage(void);
447da51e29SSatish Balay #endif
4595c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode ISInitializePackage(void);
4695c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode VecInitializePackage(void);
4795c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode MatInitializePackage(void);
4895c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode DMInitializePackage(void);
4995c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PCInitializePackage(void);
5095c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode KSPInitializePackage(void);
5195c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode SNESInitializePackage(void);
5295c0884eSLisandro Dalcin PETSC_EXTERN PetscErrorCode TSInitializePackage(void);
5322b6d1caSBarry Smith static MPI_Comm PETSC_COMM_WORLD_INNER = 0,PETSC_COMM_SELF_INNER = 0;
54acff04ddSBarry Smith #endif
55acff04ddSBarry Smith 
56e5c89e4eSSatish Balay /*
57e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
58e5c89e4eSSatish Balay     search path.
59e5c89e4eSSatish Balay */
6095c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void)
61e5c89e4eSSatish Balay {
62487e5849SBarry Smith   char           *libname[32];
63e5c89e4eSSatish Balay   PetscErrorCode ierr;
64e5c89e4eSSatish Balay   PetscInt       nmax,i;
65dc21ef1bSBarry Smith #if defined(PETSC_USE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
66aa2d57e9SJed Brown   PetscBool      preload;
673fa76a5bSLisandro Dalcin #endif
68540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
69540e20f2SPierre Jolivet   PetscBool      PetscInitialized = PetscInitializeCalled;
70540e20f2SPierre Jolivet #endif
71e5c89e4eSSatish Balay 
7260154eb2SBarry Smith   PetscFunctionBegin;
73e5c89e4eSSatish Balay   nmax = 32;
74c5929fdfSBarry Smith   ierr = PetscOptionsGetStringArray(NULL,NULL,"-dll_prepend",libname,&nmax,NULL);CHKERRQ(ierr);
75e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
76d44a1e48SBarry Smith     ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
77e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
78e5c89e4eSSatish Balay   }
79e5c89e4eSSatish Balay 
80dc21ef1bSBarry Smith #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) || !defined(PETSC_USE_SHARED_LIBRARIES)
813fa76a5bSLisandro Dalcin   /*
823fa76a5bSLisandro Dalcin       This just initializes the most basic PETSc stuff.
833fa76a5bSLisandro Dalcin 
843fa76a5bSLisandro Dalcin     The classes, from PetscDraw to PetscTS, are initialized the first
853fa76a5bSLisandro Dalcin     time an XXCreate() is called.
863fa76a5bSLisandro Dalcin   */
87607a6623SBarry Smith   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
883fa76a5bSLisandro Dalcin #else
89aa2d57e9SJed Brown   preload = PETSC_FALSE;
90c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-dynamic_library_preload",&preload,NULL);CHKERRQ(ierr);
91aa2d57e9SJed Brown   if (preload) {
92aa2d57e9SJed Brown     PetscBool found;
9360154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
94487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
95e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
9660154eb2SBarry Smith #else
9760154eb2SBarry Smith     ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
9860154eb2SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
99487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
100e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Vec dynamic library \n You cannot move the dynamic libraries!");
101487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
102e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Mat dynamic library \n You cannot move the dynamic libraries!");
103487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
104e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc DM dynamic library \n You cannot move the dynamic libraries!");
105487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
106e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc KSP dynamic library \n You cannot move the dynamic libraries!");
107487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
108e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc SNES dynamic library \n You cannot move the dynamic libraries!");
109487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
110e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc TS dynamic library \n You cannot move the dynamic libraries!");
111bb84e0fdSBarry Smith #endif
112aa2d57e9SJed Brown   }
1133fa76a5bSLisandro Dalcin #endif
1147f90d108SMatthew G. Knepley #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
1157f90d108SMatthew G. Knepley #if defined(PETSC_HAVE_BAMG)
1167f90d108SMatthew G. Knepley   {
1177f90d108SMatthew G. Knepley     PetscBool found;
1187f90d108SMatthew G. Knepley     ierr = PetscLoadDynamicLibrary("bamg",&found);CHKERRQ(ierr);
1197f90d108SMatthew G. Knepley     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc BAMG dynamic library \n You cannot move the dynamic libraries!");
1207f90d108SMatthew G. Knepley   }
1217f90d108SMatthew G. Knepley #endif
1227f90d108SMatthew G. Knepley #endif
123e5c89e4eSSatish Balay 
124e5c89e4eSSatish Balay   nmax = 32;
125c5929fdfSBarry Smith   ierr = PetscOptionsGetStringArray(NULL,NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr);
126e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
127d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
128e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
129e5c89e4eSSatish Balay   }
1303020f62cSBarry Smith 
131acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
13222b6d1caSBarry Smith   /* These must be done here because it is not safe for individual threads to call these initialize routines */
1333020f62cSBarry Smith   ierr = AOInitializePackage();CHKERRQ(ierr);
1343020f62cSBarry Smith   ierr = PetscSFInitializePackage();CHKERRQ(ierr);
1357da51e29SSatish Balay #if !defined(PETSC_USE_COMPLEX)
1363020f62cSBarry Smith   ierr = CharacteristicInitializePackage();CHKERRQ(ierr);
1377da51e29SSatish Balay #endif
1383020f62cSBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
1393020f62cSBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
1403020f62cSBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1413020f62cSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
1423020f62cSBarry Smith   ierr = PCInitializePackage();CHKERRQ(ierr);
1433020f62cSBarry Smith   ierr = KSPInitializePackage();CHKERRQ(ierr);
1443020f62cSBarry Smith   ierr = SNESInitializePackage();CHKERRQ(ierr);
1453020f62cSBarry Smith   ierr = TSInitializePackage();CHKERRQ(ierr);
146acff04ddSBarry Smith   ierr = PetscCommDuplicate(PETSC_COMM_SELF,&PETSC_COMM_SELF_INNER,NULL);CHKERRQ(ierr);
147acff04ddSBarry Smith   ierr = PetscCommDuplicate(PETSC_COMM_WORLD,&PETSC_COMM_WORLD_INNER,NULL);CHKERRQ(ierr);
148acff04ddSBarry Smith #endif
149540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
150540e20f2SPierre Jolivet   /* in Fortran, PetscInitializeCalled is set to PETSC_TRUE before PetscInitialize_DynamicLibraries() */
151540e20f2SPierre Jolivet   /* in C, it is not the case, but the value is forced to PETSC_TRUE so that PetscRegisterFinalize() is called */
152540e20f2SPierre Jolivet   PetscInitializeCalled = PETSC_TRUE;
153540e20f2SPierre Jolivet   ierr = PetscElementalInitializePackage();CHKERRQ(ierr);
154540e20f2SPierre Jolivet   PetscInitializeCalled = PetscInitialized;
155540e20f2SPierre Jolivet #endif
156e5c89e4eSSatish Balay   PetscFunctionReturn(0);
157e5c89e4eSSatish Balay }
158e5c89e4eSSatish Balay 
159ebd79076SLisandro Dalcin /*
160ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
161ebd79076SLisandro Dalcin */
16295c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void)
163e5c89e4eSSatish Balay {
164ebd79076SLisandro Dalcin   PetscErrorCode ierr;
165ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
166e5c89e4eSSatish Balay 
167ebd79076SLisandro Dalcin   PetscFunctionBegin;
168c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
169d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
170d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
171a297a907SKarl Rupp 
172acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
173acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_SELF_INNER);CHKERRQ(ierr);
174acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_WORLD_INNER);CHKERRQ(ierr);
175acff04ddSBarry Smith #endif
176acff04ddSBarry Smith 
17702c9f0b5SLisandro Dalcin   PetscDLLibrariesLoaded = NULL;
178e5c89e4eSSatish Balay   PetscFunctionReturn(0);
179e5c89e4eSSatish Balay }
180e5c89e4eSSatish Balay 
181e4d1774bSDmitry Karpeev 
182e4d1774bSDmitry Karpeev 
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 
2211c84c290SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(),
222a240a19fSJed Brown           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   PetscErrorCode    ierr;
228e5c89e4eSSatish Balay 
229e5c89e4eSSatish Balay   PetscFunctionBegin;
230e5c89e4eSSatish Balay   if (!*fl) {
231b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
232e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
233e5c89e4eSSatish Balay     entry->routine = fnc;
23402c9f0b5SLisandro Dalcin     entry->next    = NULL;
235e5c89e4eSSatish Balay     *fl            = entry;
236e5c89e4eSSatish Balay 
23776bd3646SJed Brown     if (PetscDefined(USE_DEBUG)) {
238e5c89e4eSSatish Balay       /* add this new list to list of all lists */
239e5c89e4eSSatish Balay       if (!dlallhead) {
240e5c89e4eSSatish Balay         dlallhead        = *fl;
24102c9f0b5SLisandro Dalcin         (*fl)->next_list = NULL;
242e5c89e4eSSatish Balay       } else {
243e5c89e4eSSatish Balay         ne               = dlallhead;
244e5c89e4eSSatish Balay         dlallhead        = *fl;
245e5c89e4eSSatish Balay         (*fl)->next_list = ne;
246e5c89e4eSSatish Balay       }
24776bd3646SJed Brown     }
248a8a6328fSBarry Smith 
249e5c89e4eSSatish Balay   } else {
250e5c89e4eSSatish Balay     /* search list to see if it is already there */
251e5c89e4eSSatish Balay     ne = *fl;
252e5c89e4eSSatish Balay     while (ne) {
253ace3abfcSBarry Smith       PetscBool founddup;
254e5c89e4eSSatish Balay 
255e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
256e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
257e5c89e4eSSatish Balay         ne->routine = fnc;
258e5c89e4eSSatish Balay         PetscFunctionReturn(0);
259e5c89e4eSSatish Balay       }
260a297a907SKarl Rupp       if (ne->next) ne = ne->next;
261a297a907SKarl Rupp       else break;
262e5c89e4eSSatish Balay     }
263e5c89e4eSSatish Balay     /* create new entry and add to end of list */
264b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
265e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
266e5c89e4eSSatish Balay     entry->routine = fnc;
26702c9f0b5SLisandro Dalcin     entry->next    = NULL;
268e5c89e4eSSatish Balay     ne->next       = entry;
269e5c89e4eSSatish Balay   }
270e5c89e4eSSatish Balay   PetscFunctionReturn(0);
271e5c89e4eSSatish Balay }
272e5c89e4eSSatish Balay 
273e5c89e4eSSatish Balay /*@
274140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
275e5c89e4eSSatish Balay 
276e5c89e4eSSatish Balay     Input Parameter:
277e5c89e4eSSatish Balay .   fl  - pointer to list
278e5c89e4eSSatish Balay 
279e5c89e4eSSatish Balay     Level: developer
280e5c89e4eSSatish Balay 
281a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
282e5c89e4eSSatish Balay @*/
283140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
284e5c89e4eSSatish Balay {
285140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
286e5c89e4eSSatish Balay   PetscErrorCode    ierr;
287e5c89e4eSSatish Balay 
288e5c89e4eSSatish Balay   PetscFunctionBegin;
2891441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
290e5c89e4eSSatish Balay 
291e5c89e4eSSatish Balay   /*
292*9dddd249SSatish Balay        Remove this entry from the main DL list (if it is in it)
293e5c89e4eSSatish Balay   */
2941441b1d3SBarry Smith   if (dlallhead == *fl) {
295a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
29637e93019SBarry Smith     else dlallhead = NULL;
29737e93019SBarry Smith   } else if (tmp) {
2981441b1d3SBarry Smith     while (tmp->next_list != *fl) {
299e5c89e4eSSatish Balay       tmp = tmp->next_list;
300e5c89e4eSSatish Balay       if (!tmp->next_list) break;
301e5c89e4eSSatish Balay     }
302e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
303e5c89e4eSSatish Balay   }
304e5c89e4eSSatish Balay 
305e5c89e4eSSatish Balay   /* free this list */
3061441b1d3SBarry Smith   entry = *fl;
307e5c89e4eSSatish Balay   while (entry) {
308e5c89e4eSSatish Balay     next  = entry->next;
309e5c89e4eSSatish Balay     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
310e5c89e4eSSatish Balay     ierr  = PetscFree(entry);CHKERRQ(ierr);
311e5c89e4eSSatish Balay     entry = next;
312e5c89e4eSSatish Balay   }
31302c9f0b5SLisandro Dalcin   *fl = NULL;
314e5c89e4eSSatish Balay   PetscFunctionReturn(0);
315e5c89e4eSSatish Balay }
316e5c89e4eSSatish Balay 
317e5c89e4eSSatish Balay /*
31837e93019SBarry Smith    Print any PetscFunctionLists that have not be destroyed
319e5c89e4eSSatish Balay */
32037e93019SBarry Smith PetscErrorCode  PetscFunctionListPrintAll(void)
321e5c89e4eSSatish Balay {
32237e93019SBarry Smith   PetscFunctionList tmp = dlallhead;
323e5c89e4eSSatish Balay   PetscErrorCode    ierr;
324e5c89e4eSSatish Balay 
325e5c89e4eSSatish Balay   PetscFunctionBegin;
32637e93019SBarry Smith   if (tmp) {
32737e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"The following PetscFunctionLists were not destroyed\n");CHKERRQ(ierr);
328e5c89e4eSSatish Balay   }
32937e93019SBarry Smith   while (tmp) {
33037e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"%s \n",tmp->name);CHKERRQ(ierr);
33137e93019SBarry Smith     tmp = tmp->next_list;
33237e93019SBarry Smith   }
333e5c89e4eSSatish Balay   PetscFunctionReturn(0);
334e5c89e4eSSatish Balay }
335e5c89e4eSSatish Balay 
3361c9cd337SJed Brown /*MC
3371c9cd337SJed Brown     PetscFunctionListFind - Find function registered under given name
3381c9cd337SJed Brown 
3391c9cd337SJed Brown     Synopsis:
340aaa7dc30SBarry Smith     #include <petscsys.h>
3411c9cd337SJed Brown     PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void))
342e5c89e4eSSatish Balay 
343e5c89e4eSSatish Balay     Input Parameters:
3441c9cd337SJed Brown +   flist   - pointer to list
3451c9cd337SJed Brown -   name - name registered for the function
346e5c89e4eSSatish Balay 
347e5c89e4eSSatish Balay     Output Parameters:
3481c9cd337SJed Brown .   fptr - the function pointer if name was found, else NULL
349e5c89e4eSSatish Balay 
350e5c89e4eSSatish Balay     Level: developer
351e5c89e4eSSatish Balay 
3521c9cd337SJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList, PetscObjectQueryFunction()
3531c9cd337SJed Brown M*/
3541c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void))
355e5c89e4eSSatish Balay {
356140e18c1SBarry Smith   PetscFunctionList entry = fl;
357e5c89e4eSSatish Balay   PetscErrorCode    ierr;
358bdf89e91SBarry Smith   PetscBool         flg;
359e5c89e4eSSatish Balay 
360e5c89e4eSSatish Balay   PetscFunctionBegin;
361e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
362e5c89e4eSSatish Balay 
36302c9f0b5SLisandro Dalcin   *r = NULL;
364e5c89e4eSSatish Balay   while (entry) {
365bdf89e91SBarry Smith     ierr = PetscStrcmp(name,entry->name,&flg);CHKERRQ(ierr);
366bdf89e91SBarry Smith     if (flg) {
367e5c89e4eSSatish Balay       *r   = entry->routine;
368e5c89e4eSSatish Balay       PetscFunctionReturn(0);
369e5c89e4eSSatish Balay     }
370e5c89e4eSSatish Balay     entry = entry->next;
371e5c89e4eSSatish Balay   }
372e5c89e4eSSatish Balay   PetscFunctionReturn(0);
373e5c89e4eSSatish Balay }
374e5c89e4eSSatish Balay 
375e5c89e4eSSatish Balay /*@
376140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
377e5c89e4eSSatish Balay 
378e5c89e4eSSatish Balay    Collective over MPI_Comm
379e5c89e4eSSatish Balay 
380e5c89e4eSSatish Balay    Input Parameters:
381e5c89e4eSSatish Balay +  list - the list of functions
382e5c89e4eSSatish Balay -  viewer - currently ignored
383e5c89e4eSSatish Balay 
384e5c89e4eSSatish Balay    Level: developer
385e5c89e4eSSatish Balay 
386a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionListPrintTypes(), PetscFunctionList
387e5c89e4eSSatish Balay @*/
388140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
389e5c89e4eSSatish Balay {
390e5c89e4eSSatish Balay   PetscErrorCode ierr;
391ace3abfcSBarry Smith   PetscBool      iascii;
392e5c89e4eSSatish Balay 
393e5c89e4eSSatish Balay   PetscFunctionBegin;
394e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
395e5c89e4eSSatish Balay   PetscValidPointer(list,1);
3960700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
397e5c89e4eSSatish Balay 
398251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
399e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
400e5c89e4eSSatish Balay 
401e5c89e4eSSatish Balay   while (list) {
402bdf89e91SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %s\n",list->name);CHKERRQ(ierr);
403e5c89e4eSSatish Balay     list = list->next;
404e5c89e4eSSatish Balay   }
405e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
406e5c89e4eSSatish Balay   PetscFunctionReturn(0);
407e5c89e4eSSatish Balay }
408e5c89e4eSSatish Balay 
409065533a5SJed Brown /*@C
410140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
411e5c89e4eSSatish Balay          by help etc.
412e5c89e4eSSatish Balay 
413d4349b43SBarry Smith    Not Collective
414e5c89e4eSSatish Balay 
415e5c89e4eSSatish Balay    Input Parameter:
416e5c89e4eSSatish Balay .  list   - list of types
417e5c89e4eSSatish Balay 
418e5c89e4eSSatish Balay    Output Parameter:
419e5c89e4eSSatish Balay +  array - array of names
420e5c89e4eSSatish Balay -  n - length of array
421e5c89e4eSSatish Balay 
422e5c89e4eSSatish Balay    Notes:
423e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
424e5c89e4eSSatish Balay     not copied so should not be freed.
425e5c89e4eSSatish Balay 
426e5c89e4eSSatish Balay    Level: developer
427e5c89e4eSSatish Balay 
428a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
429e5c89e4eSSatish Balay @*/
430ca1b3570SJed Brown PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
431e5c89e4eSSatish Balay {
432e5c89e4eSSatish Balay   PetscErrorCode    ierr;
433e5c89e4eSSatish Balay   PetscInt          count = 0;
434140e18c1SBarry Smith   PetscFunctionList klist = list;
435e5c89e4eSSatish Balay 
436e5c89e4eSSatish Balay   PetscFunctionBegin;
437e5c89e4eSSatish Balay   while (list) {
438e5c89e4eSSatish Balay     list = list->next;
439e5c89e4eSSatish Balay     count++;
440e5c89e4eSSatish Balay   }
441ca1b3570SJed Brown   ierr  = PetscMalloc1(count+1,(char***)array);CHKERRQ(ierr);
442e5c89e4eSSatish Balay   count = 0;
443e5c89e4eSSatish Balay   while (klist) {
444e5c89e4eSSatish Balay     (*array)[count] = klist->name;
445e5c89e4eSSatish Balay     klist           = klist->next;
446e5c89e4eSSatish Balay     count++;
447e5c89e4eSSatish Balay   }
44802c9f0b5SLisandro Dalcin   (*array)[count] = NULL;
449e5c89e4eSSatish Balay   *n              = count+1;
450e5c89e4eSSatish Balay   PetscFunctionReturn(0);
451e5c89e4eSSatish Balay }
452e5c89e4eSSatish Balay 
453e5c89e4eSSatish Balay 
454e5c89e4eSSatish Balay /*@C
455140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
456e5c89e4eSSatish Balay 
457e5c89e4eSSatish Balay    Collective over MPI_Comm
458e5c89e4eSSatish Balay 
459e5c89e4eSSatish Balay    Input Parameters:
460e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
461e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
462e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
463e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
464e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
465e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
4663cc1e11dSBarry Smith .  list   - list of types
46744ef3d73SBarry Smith .  def - default (current) value
46844ef3d73SBarry Smith -  newv - new value
469e5c89e4eSSatish Balay 
470e5c89e4eSSatish Balay    Level: developer
471e5c89e4eSSatish Balay 
472a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
473e5c89e4eSSatish Balay @*/
47444ef3d73SBarry 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[])
475e5c89e4eSSatish Balay {
476e5c89e4eSSatish Balay   PetscErrorCode ierr;
477e5c89e4eSSatish Balay   char           p[64];
478e5c89e4eSSatish Balay 
479e5c89e4eSSatish Balay   PetscFunctionBegin;
480da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
481e5c89e4eSSatish Balay 
482a126751eSBarry Smith   ierr = PetscStrncpy(p,"-",sizeof(p));CHKERRQ(ierr);
483a126751eSBarry Smith   if (prefix) {ierr = PetscStrlcat(p,prefix,sizeof(p));CHKERRQ(ierr);}
4840e4c290aSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <now %s : formerly %s>: %s (one of)",p,name+1,newv,def,text);CHKERRQ(ierr);
485e5c89e4eSSatish Balay 
486e5c89e4eSSatish Balay   while (list) {
487e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
488e5c89e4eSSatish Balay     list = list->next;
489e5c89e4eSSatish Balay   }
490e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
491e5c89e4eSSatish Balay   PetscFunctionReturn(0);
492e5c89e4eSSatish Balay }
493e5c89e4eSSatish Balay 
494e5c89e4eSSatish Balay /*@
495140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
496e5c89e4eSSatish Balay 
497e5c89e4eSSatish Balay     Input Parameters:
498e5c89e4eSSatish Balay .   fl   - pointer to list
499e5c89e4eSSatish Balay 
500e5c89e4eSSatish Balay     Output Parameters:
501e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
502e5c89e4eSSatish Balay 
503e5c89e4eSSatish Balay     Level: developer
504e5c89e4eSSatish Balay 
505140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
506e5c89e4eSSatish Balay 
507e5c89e4eSSatish Balay @*/
508140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
509e5c89e4eSSatish Balay {
510e5c89e4eSSatish Balay   PetscErrorCode ierr;
511e5c89e4eSSatish Balay 
512e5c89e4eSSatish Balay   PetscFunctionBegin;
513e5c89e4eSSatish Balay   while (fl) {
514bdf89e91SBarry Smith     ierr = PetscFunctionListAdd(nl,fl->name,fl->routine);CHKERRQ(ierr);
515e5c89e4eSSatish Balay     fl   = fl->next;
516e5c89e4eSSatish Balay   }
517e5c89e4eSSatish Balay   PetscFunctionReturn(0);
518e5c89e4eSSatish Balay }
519