xref: /petsc/src/sys/dll/reg.c (revision dc21ef1b90327b63d953fd6bfd4ffd391dec03f5)
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 */
12d44a1e48SBarry Smith PetscDLLibrary PetscDLLibrariesLoaded = 0;
133fa76a5bSLisandro Dalcin 
14cbd104e6SBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
15ebd79076SLisandro Dalcin 
16*dc21ef1bSBarry 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;
22487e5849SBarry Smith   ierr = PetscStrcpy(libs,"${PETSC_LIB_DIR}/libpetsc");CHKERRQ(ierr);
23487e5849SBarry Smith   ierr = PetscStrcat(libs,name);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 {
28487e5849SBarry Smith     ierr = PetscStrcpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc");CHKERRQ(ierr);
29487e5849SBarry Smith     ierr = PetscStrcat(libs,name);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 }
37487e5849SBarry Smith 
383fa76a5bSLisandro Dalcin #endif
393fa76a5bSLisandro Dalcin 
40acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
4122b6d1caSBarry Smith extern PetscErrorCode AOInitializePackage(void);
4222b6d1caSBarry Smith extern PetscErrorCode PetscSFInitializePackage(void);
437da51e29SSatish Balay #if !defined(PETSC_USE_COMPLEX)
4422b6d1caSBarry Smith extern PetscErrorCode CharacteristicInitializePackage(void);
457da51e29SSatish Balay #endif
4622b6d1caSBarry Smith extern PetscErrorCode ISInitializePackage(void);
4722b6d1caSBarry Smith extern PetscErrorCode VecInitializePackage(void);
4822b6d1caSBarry Smith extern PetscErrorCode MatInitializePackage(void);
4922b6d1caSBarry Smith extern PetscErrorCode DMInitializePackage(void);
5022b6d1caSBarry Smith extern PetscErrorCode PCInitializePackage(void);
5122b6d1caSBarry Smith extern PetscErrorCode KSPInitializePackage(void);
5222b6d1caSBarry Smith extern PetscErrorCode SNESInitializePackage(void);
5322b6d1caSBarry Smith extern PetscErrorCode TSInitializePackage(void);
5422b6d1caSBarry Smith static MPI_Comm PETSC_COMM_WORLD_INNER = 0,PETSC_COMM_SELF_INNER = 0;
55acff04ddSBarry Smith #endif
56acff04ddSBarry Smith 
57e5c89e4eSSatish Balay /*
58e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
59e5c89e4eSSatish Balay     search path.
60e5c89e4eSSatish Balay */
617087cfbeSBarry Smith PetscErrorCode  PetscInitialize_DynamicLibraries(void)
62e5c89e4eSSatish Balay {
63487e5849SBarry Smith   char           *libname[32];
64e5c89e4eSSatish Balay   PetscErrorCode ierr;
65e5c89e4eSSatish Balay   PetscInt       nmax,i;
66*dc21ef1bSBarry Smith #if defined(PETSC_USE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
67aa2d57e9SJed Brown   PetscBool      preload;
683fa76a5bSLisandro Dalcin #endif
69e5c89e4eSSatish Balay 
7060154eb2SBarry Smith   PetscFunctionBegin;
71e5c89e4eSSatish Balay   nmax = 32;
72c5929fdfSBarry Smith   ierr = PetscOptionsGetStringArray(NULL,NULL,"-dll_prepend",libname,&nmax,NULL);CHKERRQ(ierr);
73e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
74d44a1e48SBarry Smith     ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
75e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
76e5c89e4eSSatish Balay   }
77e5c89e4eSSatish Balay 
78*dc21ef1bSBarry Smith #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) || !defined(PETSC_USE_SHARED_LIBRARIES)
793fa76a5bSLisandro Dalcin   /*
803fa76a5bSLisandro Dalcin       This just initializes the most basic PETSc stuff.
813fa76a5bSLisandro Dalcin 
823fa76a5bSLisandro Dalcin     The classes, from PetscDraw to PetscTS, are initialized the first
833fa76a5bSLisandro Dalcin     time an XXCreate() is called.
843fa76a5bSLisandro Dalcin   */
85607a6623SBarry Smith   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
863fa76a5bSLisandro Dalcin #else
87aa2d57e9SJed Brown   preload = PETSC_FALSE;
88c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-dynamic_library_preload",&preload,NULL);CHKERRQ(ierr);
89aa2d57e9SJed Brown   if (preload) {
90aa2d57e9SJed Brown     PetscBool found;
9160154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
92487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
93e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
9460154eb2SBarry Smith #else
9560154eb2SBarry Smith     ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
9660154eb2SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
97487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
98e32f2f54SBarry 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!");
99487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
100e32f2f54SBarry 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!");
101487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
102e32f2f54SBarry 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!");
103487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
104e32f2f54SBarry 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!");
105487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
106e32f2f54SBarry 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!");
107487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
108e32f2f54SBarry 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!");
109bb84e0fdSBarry Smith #endif
110aa2d57e9SJed Brown   }
1113fa76a5bSLisandro Dalcin #endif
112e5c89e4eSSatish Balay 
113e5c89e4eSSatish Balay   nmax = 32;
114c5929fdfSBarry Smith   ierr = PetscOptionsGetStringArray(NULL,NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr);
115e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
116d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
117e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
118e5c89e4eSSatish Balay   }
1193020f62cSBarry Smith 
120acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
12122b6d1caSBarry Smith   /* These must be done here because it is not safe for individual threads to call these initialize routines */
1223020f62cSBarry Smith   ierr = AOInitializePackage();CHKERRQ(ierr);
1233020f62cSBarry Smith   ierr = PetscSFInitializePackage();CHKERRQ(ierr);
1247da51e29SSatish Balay #if !defined(PETSC_USE_COMPLEX)
1253020f62cSBarry Smith   ierr = CharacteristicInitializePackage();CHKERRQ(ierr);
1267da51e29SSatish Balay #endif
1273020f62cSBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
1283020f62cSBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
1293020f62cSBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1303020f62cSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
1313020f62cSBarry Smith   ierr = PCInitializePackage();CHKERRQ(ierr);
1323020f62cSBarry Smith   ierr = KSPInitializePackage();CHKERRQ(ierr);
1333020f62cSBarry Smith   ierr = SNESInitializePackage();CHKERRQ(ierr);
1343020f62cSBarry Smith   ierr = TSInitializePackage();CHKERRQ(ierr);
135acff04ddSBarry Smith   ierr = PetscCommDuplicate(PETSC_COMM_SELF,&PETSC_COMM_SELF_INNER,NULL);CHKERRQ(ierr);
136acff04ddSBarry Smith   ierr = PetscCommDuplicate(PETSC_COMM_WORLD,&PETSC_COMM_WORLD_INNER,NULL);CHKERRQ(ierr);
137acff04ddSBarry Smith #endif
138e5c89e4eSSatish Balay   PetscFunctionReturn(0);
139e5c89e4eSSatish Balay }
140e5c89e4eSSatish Balay 
141ebd79076SLisandro Dalcin /*
142ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
143ebd79076SLisandro Dalcin */
144e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void)
145e5c89e4eSSatish Balay {
146ebd79076SLisandro Dalcin   PetscErrorCode ierr;
147ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
148e5c89e4eSSatish Balay 
149ebd79076SLisandro Dalcin   PetscFunctionBegin;
150c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
151d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
152d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
153a297a907SKarl Rupp 
154acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
155acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_SELF_INNER);CHKERRQ(ierr);
156acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_WORLD_INNER);CHKERRQ(ierr);
157acff04ddSBarry Smith #endif
158acff04ddSBarry Smith 
159d44a1e48SBarry Smith   PetscDLLibrariesLoaded = 0;
160e5c89e4eSSatish Balay   PetscFunctionReturn(0);
161e5c89e4eSSatish Balay }
162e5c89e4eSSatish Balay 
163e4d1774bSDmitry Karpeev 
164e4d1774bSDmitry Karpeev 
165e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
166140e18c1SBarry Smith struct _n_PetscFunctionList {
167e5c89e4eSSatish Balay   void              (*routine)(void);    /* the routine */
168e5c89e4eSSatish Balay   char              *name;               /* string to identify routine */
169140e18c1SBarry Smith   PetscFunctionList next;                /* next pointer */
170140e18c1SBarry Smith   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
171e5c89e4eSSatish Balay };
172e5c89e4eSSatish Balay 
173e5c89e4eSSatish Balay /*
174140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
175e5c89e4eSSatish Balay */
176140e18c1SBarry Smith static PetscFunctionList dlallhead = 0;
177e5c89e4eSSatish Balay 
178a240a19fSJed Brown /*MC
179140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
180e5c89e4eSSatish Balay    specified registry.
181e5c89e4eSSatish Balay 
182a240a19fSJed Brown    Synopsis:
183aaa7dc30SBarry Smith    #include <petscsys.h>
184b9fb364aSBarry Smith    PetscErrorCode PetscFunctionListAdd(PetscFunctionList *flist,const char name[],void (*fptr)(void))
185a240a19fSJed Brown 
186d4349b43SBarry Smith    Not Collective
187e5c89e4eSSatish Balay 
188e5c89e4eSSatish Balay    Input Parameters:
189b9fb364aSBarry Smith +  flist - pointer to function list object
190e5c89e4eSSatish Balay .  name - string to identify routine
191a240a19fSJed Brown -  fptr - function pointer
192e5c89e4eSSatish Balay 
193e5c89e4eSSatish Balay    Notes:
194a240a19fSJed Brown    To remove a registered routine, pass in a NULL fptr.
195e5c89e4eSSatish Balay 
196e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
197e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
1981c84c290SBarry Smith    for that particular component (e.g., SNESRegister()) instead of
199140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
200e5c89e4eSSatish Balay 
201e5c89e4eSSatish Balay     Level: developer
202e5c89e4eSSatish Balay 
2031c84c290SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(),
204a240a19fSJed Brown           PCRegister(), TSRegister(), PetscFunctionList, PetscObjectComposeFunction()
205a240a19fSJed Brown M*/
206a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl,const char name[],void (*fnc)(void))
207e5c89e4eSSatish Balay {
208140e18c1SBarry Smith   PetscFunctionList entry,ne;
209e5c89e4eSSatish Balay   PetscErrorCode    ierr;
210e5c89e4eSSatish Balay 
211e5c89e4eSSatish Balay   PetscFunctionBegin;
212e5c89e4eSSatish Balay   if (!*fl) {
213b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
214e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
215e5c89e4eSSatish Balay     entry->routine = fnc;
216e5c89e4eSSatish Balay     entry->next    = 0;
217e5c89e4eSSatish Balay     *fl            = entry;
218e5c89e4eSSatish Balay 
219a2553e36SBarry Smith #if defined(PETSC_USE_DEBUG)
220e5c89e4eSSatish Balay     /* add this new list to list of all lists */
221e5c89e4eSSatish Balay     if (!dlallhead) {
222e5c89e4eSSatish Balay       dlallhead        = *fl;
223e5c89e4eSSatish Balay       (*fl)->next_list = 0;
224e5c89e4eSSatish Balay     } else {
225e5c89e4eSSatish Balay       ne               = dlallhead;
226e5c89e4eSSatish Balay       dlallhead        = *fl;
227e5c89e4eSSatish Balay       (*fl)->next_list = ne;
228e5c89e4eSSatish Balay     }
229a8a6328fSBarry Smith #endif
230a8a6328fSBarry Smith 
231e5c89e4eSSatish Balay   } else {
232e5c89e4eSSatish Balay     /* search list to see if it is already there */
233e5c89e4eSSatish Balay     ne = *fl;
234e5c89e4eSSatish Balay     while (ne) {
235ace3abfcSBarry Smith       PetscBool founddup;
236e5c89e4eSSatish Balay 
237e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
238e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
239e5c89e4eSSatish Balay         ne->routine = fnc;
240e5c89e4eSSatish Balay         PetscFunctionReturn(0);
241e5c89e4eSSatish Balay       }
242a297a907SKarl Rupp       if (ne->next) ne = ne->next;
243a297a907SKarl Rupp       else break;
244e5c89e4eSSatish Balay     }
245e5c89e4eSSatish Balay     /* create new entry and add to end of list */
246b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
247e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
248e5c89e4eSSatish Balay     entry->routine = fnc;
249e5c89e4eSSatish Balay     entry->next    = 0;
250e5c89e4eSSatish Balay     ne->next       = entry;
251e5c89e4eSSatish Balay   }
252e5c89e4eSSatish Balay   PetscFunctionReturn(0);
253e5c89e4eSSatish Balay }
254e5c89e4eSSatish Balay 
255e5c89e4eSSatish Balay /*@
256140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
257e5c89e4eSSatish Balay 
258e5c89e4eSSatish Balay     Input Parameter:
259e5c89e4eSSatish Balay .   fl  - pointer to list
260e5c89e4eSSatish Balay 
261e5c89e4eSSatish Balay     Level: developer
262e5c89e4eSSatish Balay 
263a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
264e5c89e4eSSatish Balay @*/
265140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
266e5c89e4eSSatish Balay {
267140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
268e5c89e4eSSatish Balay   PetscErrorCode    ierr;
269e5c89e4eSSatish Balay 
270e5c89e4eSSatish Balay   PetscFunctionBegin;
2711441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
272e5c89e4eSSatish Balay 
273e5c89e4eSSatish Balay   /*
274e5c89e4eSSatish Balay        Remove this entry from the master DL list (if it is in it)
275e5c89e4eSSatish Balay   */
2761441b1d3SBarry Smith   if (dlallhead == *fl) {
277a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
27837e93019SBarry Smith     else dlallhead = NULL;
27937e93019SBarry Smith   } else if (tmp) {
2801441b1d3SBarry Smith     while (tmp->next_list != *fl) {
281e5c89e4eSSatish Balay       tmp = tmp->next_list;
282e5c89e4eSSatish Balay       if (!tmp->next_list) break;
283e5c89e4eSSatish Balay     }
284e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
285e5c89e4eSSatish Balay   }
286e5c89e4eSSatish Balay 
287e5c89e4eSSatish Balay   /* free this list */
2881441b1d3SBarry Smith   entry = *fl;
289e5c89e4eSSatish Balay   while (entry) {
290e5c89e4eSSatish Balay     next  = entry->next;
291e5c89e4eSSatish Balay     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
292e5c89e4eSSatish Balay     ierr  = PetscFree(entry);CHKERRQ(ierr);
293e5c89e4eSSatish Balay     entry = next;
294e5c89e4eSSatish Balay   }
2951441b1d3SBarry Smith   *fl = 0;
296e5c89e4eSSatish Balay   PetscFunctionReturn(0);
297e5c89e4eSSatish Balay }
298e5c89e4eSSatish Balay 
299e5c89e4eSSatish Balay /*
30037e93019SBarry Smith    Print any PetscFunctionLists that have not be destroyed
301e5c89e4eSSatish Balay */
30237e93019SBarry Smith PetscErrorCode  PetscFunctionListPrintAll(void)
303e5c89e4eSSatish Balay {
30437e93019SBarry Smith   PetscFunctionList tmp = dlallhead;
305e5c89e4eSSatish Balay   PetscErrorCode    ierr;
306e5c89e4eSSatish Balay 
307e5c89e4eSSatish Balay   PetscFunctionBegin;
30837e93019SBarry Smith   if (tmp) {
30937e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"The following PetscFunctionLists were not destroyed\n");CHKERRQ(ierr);
310e5c89e4eSSatish Balay   }
31137e93019SBarry Smith   while (tmp) {
31237e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"%s \n",tmp->name);CHKERRQ(ierr);
31337e93019SBarry Smith     tmp = tmp->next_list;
31437e93019SBarry Smith   }
315e5c89e4eSSatish Balay   PetscFunctionReturn(0);
316e5c89e4eSSatish Balay }
317e5c89e4eSSatish Balay 
3181c9cd337SJed Brown /*MC
3191c9cd337SJed Brown     PetscFunctionListFind - Find function registered under given name
3201c9cd337SJed Brown 
3211c9cd337SJed Brown     Synopsis:
322aaa7dc30SBarry Smith     #include <petscsys.h>
3231c9cd337SJed Brown     PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void))
324e5c89e4eSSatish Balay 
325e5c89e4eSSatish Balay     Input Parameters:
3261c9cd337SJed Brown +   flist   - pointer to list
3271c9cd337SJed Brown -   name - name registered for the function
328e5c89e4eSSatish Balay 
329e5c89e4eSSatish Balay     Output Parameters:
3301c9cd337SJed Brown .   fptr - the function pointer if name was found, else NULL
331e5c89e4eSSatish Balay 
332e5c89e4eSSatish Balay     Level: developer
333e5c89e4eSSatish Balay 
3341c9cd337SJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList, PetscObjectQueryFunction()
3351c9cd337SJed Brown M*/
3361c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void))
337e5c89e4eSSatish Balay {
338140e18c1SBarry Smith   PetscFunctionList entry = fl;
339e5c89e4eSSatish Balay   PetscErrorCode    ierr;
340bdf89e91SBarry Smith   PetscBool         flg;
341e5c89e4eSSatish Balay 
342e5c89e4eSSatish Balay   PetscFunctionBegin;
343e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
344e5c89e4eSSatish Balay 
345e5c89e4eSSatish Balay   *r = 0;
346e5c89e4eSSatish Balay   while (entry) {
347bdf89e91SBarry Smith     ierr = PetscStrcmp(name,entry->name,&flg);CHKERRQ(ierr);
348bdf89e91SBarry Smith     if (flg) {
349e5c89e4eSSatish Balay       *r   = entry->routine;
350e5c89e4eSSatish Balay       PetscFunctionReturn(0);
351e5c89e4eSSatish Balay     }
352e5c89e4eSSatish Balay     entry = entry->next;
353e5c89e4eSSatish Balay   }
354e5c89e4eSSatish Balay   PetscFunctionReturn(0);
355e5c89e4eSSatish Balay }
356e5c89e4eSSatish Balay 
357e5c89e4eSSatish Balay /*@
358140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
359e5c89e4eSSatish Balay 
360e5c89e4eSSatish Balay    Collective over MPI_Comm
361e5c89e4eSSatish Balay 
362e5c89e4eSSatish Balay    Input Parameters:
363e5c89e4eSSatish Balay +  list - the list of functions
364e5c89e4eSSatish Balay -  viewer - currently ignored
365e5c89e4eSSatish Balay 
366e5c89e4eSSatish Balay    Level: developer
367e5c89e4eSSatish Balay 
368a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionListPrintTypes(), PetscFunctionList
369e5c89e4eSSatish Balay @*/
370140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
371e5c89e4eSSatish Balay {
372e5c89e4eSSatish Balay   PetscErrorCode ierr;
373ace3abfcSBarry Smith   PetscBool      iascii;
374e5c89e4eSSatish Balay 
375e5c89e4eSSatish Balay   PetscFunctionBegin;
376e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
377e5c89e4eSSatish Balay   PetscValidPointer(list,1);
3780700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
379e5c89e4eSSatish Balay 
380251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
381e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
382e5c89e4eSSatish Balay 
383e5c89e4eSSatish Balay   while (list) {
384bdf89e91SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %s\n",list->name);CHKERRQ(ierr);
385e5c89e4eSSatish Balay     list = list->next;
386e5c89e4eSSatish Balay   }
387e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
388e5c89e4eSSatish Balay   PetscFunctionReturn(0);
389e5c89e4eSSatish Balay }
390e5c89e4eSSatish Balay 
391065533a5SJed Brown /*@C
392140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
393e5c89e4eSSatish Balay          by help etc.
394e5c89e4eSSatish Balay 
395d4349b43SBarry Smith    Not Collective
396e5c89e4eSSatish Balay 
397e5c89e4eSSatish Balay    Input Parameter:
398e5c89e4eSSatish Balay .  list   - list of types
399e5c89e4eSSatish Balay 
400e5c89e4eSSatish Balay    Output Parameter:
401e5c89e4eSSatish Balay +  array - array of names
402e5c89e4eSSatish Balay -  n - length of array
403e5c89e4eSSatish Balay 
404e5c89e4eSSatish Balay    Notes:
405e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
406e5c89e4eSSatish Balay     not copied so should not be freed.
407e5c89e4eSSatish Balay 
408e5c89e4eSSatish Balay    Level: developer
409e5c89e4eSSatish Balay 
410a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
411e5c89e4eSSatish Balay @*/
412ca1b3570SJed Brown PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
413e5c89e4eSSatish Balay {
414e5c89e4eSSatish Balay   PetscErrorCode    ierr;
415e5c89e4eSSatish Balay   PetscInt          count = 0;
416140e18c1SBarry Smith   PetscFunctionList klist = list;
417e5c89e4eSSatish Balay 
418e5c89e4eSSatish Balay   PetscFunctionBegin;
419e5c89e4eSSatish Balay   while (list) {
420e5c89e4eSSatish Balay     list = list->next;
421e5c89e4eSSatish Balay     count++;
422e5c89e4eSSatish Balay   }
423ca1b3570SJed Brown   ierr  = PetscMalloc1(count+1,(char***)array);CHKERRQ(ierr);
424e5c89e4eSSatish Balay   count = 0;
425e5c89e4eSSatish Balay   while (klist) {
426e5c89e4eSSatish Balay     (*array)[count] = klist->name;
427e5c89e4eSSatish Balay     klist           = klist->next;
428e5c89e4eSSatish Balay     count++;
429e5c89e4eSSatish Balay   }
430e5c89e4eSSatish Balay   (*array)[count] = 0;
431e5c89e4eSSatish Balay   *n              = count+1;
432e5c89e4eSSatish Balay   PetscFunctionReturn(0);
433e5c89e4eSSatish Balay }
434e5c89e4eSSatish Balay 
435e5c89e4eSSatish Balay 
436e5c89e4eSSatish Balay /*@C
437140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
438e5c89e4eSSatish Balay 
439e5c89e4eSSatish Balay    Collective over MPI_Comm
440e5c89e4eSSatish Balay 
441e5c89e4eSSatish Balay    Input Parameters:
442e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
443e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
444e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
445e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
446e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
447e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
4483cc1e11dSBarry Smith .  list   - list of types
4493cc1e11dSBarry Smith -  def - default (current) value
450e5c89e4eSSatish Balay 
451e5c89e4eSSatish Balay    Level: developer
452e5c89e4eSSatish Balay 
453a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
454e5c89e4eSSatish Balay @*/
455140e18c1SBarry 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[])
456e5c89e4eSSatish Balay {
457e5c89e4eSSatish Balay   PetscErrorCode ierr;
458e5c89e4eSSatish Balay   char           p[64];
459e5c89e4eSSatish Balay 
460e5c89e4eSSatish Balay   PetscFunctionBegin;
461da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
462e5c89e4eSSatish Balay 
463e5c89e4eSSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
464e5c89e4eSSatish Balay   if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);}
4653cc1e11dSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr);
466e5c89e4eSSatish Balay 
467e5c89e4eSSatish Balay   while (list) {
468e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
469e5c89e4eSSatish Balay     list = list->next;
470e5c89e4eSSatish Balay   }
471e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
472e5c89e4eSSatish Balay   PetscFunctionReturn(0);
473e5c89e4eSSatish Balay }
474e5c89e4eSSatish Balay 
475e5c89e4eSSatish Balay /*@
476140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
477e5c89e4eSSatish Balay 
478e5c89e4eSSatish Balay     Input Parameters:
479e5c89e4eSSatish Balay .   fl   - pointer to list
480e5c89e4eSSatish Balay 
481e5c89e4eSSatish Balay     Output Parameters:
482e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
483e5c89e4eSSatish Balay 
484e5c89e4eSSatish Balay     Level: developer
485e5c89e4eSSatish Balay 
486140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
487e5c89e4eSSatish Balay 
488e5c89e4eSSatish Balay @*/
489140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
490e5c89e4eSSatish Balay {
491e5c89e4eSSatish Balay   PetscErrorCode ierr;
492e5c89e4eSSatish Balay 
493e5c89e4eSSatish Balay   PetscFunctionBegin;
494e5c89e4eSSatish Balay   while (fl) {
495bdf89e91SBarry Smith     ierr = PetscFunctionListAdd(nl,fl->name,fl->routine);CHKERRQ(ierr);
496e5c89e4eSSatish Balay     fl   = fl->next;
497e5c89e4eSSatish Balay   }
498e5c89e4eSSatish Balay   PetscFunctionReturn(0);
499e5c89e4eSSatish Balay }
500e5c89e4eSSatish Balay 
501