xref: /petsc/src/sys/dll/reg.c (revision a2553e36dc2fd274f393967e9f281a789169e3b3)
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 
14aa2d57e9SJed Brown #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
15ebd79076SLisandro Dalcin 
16e5c89e4eSSatish Balay #undef __FUNCT__
17487e5849SBarry Smith #define __FUNCT__ "PetscLoadDynamicLibrary"
187087cfbeSBarry Smith static PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
19487e5849SBarry Smith {
20487e5849SBarry Smith   char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
21487e5849SBarry Smith   PetscErrorCode ierr;
22487e5849SBarry Smith 
23487e5849SBarry Smith   PetscFunctionBegin;
24487e5849SBarry Smith   ierr = PetscStrcpy(libs,"${PETSC_LIB_DIR}/libpetsc");CHKERRQ(ierr);
25487e5849SBarry Smith   ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
26487e5849SBarry Smith   ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
27487e5849SBarry Smith   if (*found) {
28d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
29487e5849SBarry Smith   } else {
30487e5849SBarry Smith     ierr = PetscStrcpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc");CHKERRQ(ierr);
31487e5849SBarry Smith     ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
32487e5849SBarry Smith     ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
33487e5849SBarry Smith     if (*found) {
34d44a1e48SBarry Smith       ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
35487e5849SBarry Smith     }
36487e5849SBarry Smith   }
37487e5849SBarry Smith   PetscFunctionReturn(0);
38487e5849SBarry Smith }
39487e5849SBarry Smith 
403fa76a5bSLisandro Dalcin #endif
413fa76a5bSLisandro Dalcin 
42acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
4322b6d1caSBarry Smith extern PetscErrorCode AOInitializePackage(void);
4422b6d1caSBarry Smith extern PetscErrorCode PetscSFInitializePackage(void);
4522b6d1caSBarry Smith extern PetscErrorCode CharacteristicInitializePackage(void);
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 
57487e5849SBarry Smith #undef __FUNCT__
58e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize_DynamicLibraries"
59e5c89e4eSSatish Balay /*
60e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
61e5c89e4eSSatish Balay     search path.
62e5c89e4eSSatish Balay */
637087cfbeSBarry Smith PetscErrorCode  PetscInitialize_DynamicLibraries(void)
64e5c89e4eSSatish Balay {
65487e5849SBarry Smith   char           *libname[32];
66e5c89e4eSSatish Balay   PetscErrorCode ierr;
67e5c89e4eSSatish Balay   PetscInt       nmax,i;
68aa2d57e9SJed Brown #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
69aa2d57e9SJed Brown   PetscBool      preload;
703fa76a5bSLisandro Dalcin #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 
80aa2d57e9SJed Brown #if !defined(PETSC_HAVE_DYNAMIC_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
114e5c89e4eSSatish Balay 
115e5c89e4eSSatish Balay   nmax = 32;
116c5929fdfSBarry Smith   ierr = PetscOptionsGetStringArray(NULL,NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr);
117e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
118d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
119e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
120e5c89e4eSSatish Balay   }
1213020f62cSBarry Smith 
122acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
12322b6d1caSBarry Smith   /* These must be done here because it is not safe for individual threads to call these initialize routines */
1243020f62cSBarry Smith   ierr = AOInitializePackage();CHKERRQ(ierr);
1253020f62cSBarry Smith   ierr = PetscSFInitializePackage();CHKERRQ(ierr);
1263020f62cSBarry Smith   ierr = CharacteristicInitializePackage();CHKERRQ(ierr);
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 
141e5c89e4eSSatish Balay #undef __FUNCT__
142e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize_DynamicLibraries"
143ebd79076SLisandro Dalcin /*
144ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
145ebd79076SLisandro Dalcin */
146e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void)
147e5c89e4eSSatish Balay {
148ebd79076SLisandro Dalcin   PetscErrorCode ierr;
149ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
150e5c89e4eSSatish Balay 
151ebd79076SLisandro Dalcin   PetscFunctionBegin;
152c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
153d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
154d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
155a297a907SKarl Rupp 
156acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
157acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_SELF_INNER);CHKERRQ(ierr);
158acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_WORLD_INNER);CHKERRQ(ierr);
159acff04ddSBarry Smith #endif
160acff04ddSBarry Smith 
161d44a1e48SBarry Smith   PetscDLLibrariesLoaded = 0;
162e5c89e4eSSatish Balay   PetscFunctionReturn(0);
163e5c89e4eSSatish Balay }
164e5c89e4eSSatish Balay 
165e4d1774bSDmitry Karpeev 
166e4d1774bSDmitry Karpeev 
167e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
168140e18c1SBarry Smith struct _n_PetscFunctionList {
169e5c89e4eSSatish Balay   void              (*routine)(void);    /* the routine */
170e5c89e4eSSatish Balay   char              *name;               /* string to identify routine */
171140e18c1SBarry Smith   PetscFunctionList next;                /* next pointer */
172140e18c1SBarry Smith   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
173e5c89e4eSSatish Balay };
174e5c89e4eSSatish Balay 
175e5c89e4eSSatish Balay /*
176140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
177e5c89e4eSSatish Balay */
178140e18c1SBarry Smith static PetscFunctionList dlallhead = 0;
179e5c89e4eSSatish Balay 
180a240a19fSJed Brown /*MC
181140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
182e5c89e4eSSatish Balay    specified registry.
183e5c89e4eSSatish Balay 
184a240a19fSJed Brown    Synopsis:
185aaa7dc30SBarry Smith    #include <petscsys.h>
186b9fb364aSBarry Smith    PetscErrorCode PetscFunctionListAdd(PetscFunctionList *flist,const char name[],void (*fptr)(void))
187a240a19fSJed Brown 
188d4349b43SBarry Smith    Not Collective
189e5c89e4eSSatish Balay 
190e5c89e4eSSatish Balay    Input Parameters:
191b9fb364aSBarry Smith +  flist - pointer to function list object
192e5c89e4eSSatish Balay .  name - string to identify routine
193a240a19fSJed Brown -  fptr - function pointer
194e5c89e4eSSatish Balay 
195e5c89e4eSSatish Balay    Notes:
196a240a19fSJed Brown    To remove a registered routine, pass in a NULL fptr.
197e5c89e4eSSatish Balay 
198e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
199e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
2001c84c290SBarry Smith    for that particular component (e.g., SNESRegister()) instead of
201140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
202e5c89e4eSSatish Balay 
203e5c89e4eSSatish Balay     Level: developer
204e5c89e4eSSatish Balay 
2051c84c290SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(),
206a240a19fSJed Brown           PCRegister(), TSRegister(), PetscFunctionList, PetscObjectComposeFunction()
207a240a19fSJed Brown M*/
208a240a19fSJed Brown #undef __FUNCT__
209a240a19fSJed Brown #define __FUNCT__ "PetscFunctionListAdd_Private"
210a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl,const char name[],void (*fnc)(void))
211e5c89e4eSSatish Balay {
212140e18c1SBarry Smith   PetscFunctionList entry,ne;
213e5c89e4eSSatish Balay   PetscErrorCode    ierr;
214e5c89e4eSSatish Balay 
215e5c89e4eSSatish Balay   PetscFunctionBegin;
216e5c89e4eSSatish Balay   if (!*fl) {
217b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
218e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
219e5c89e4eSSatish Balay     entry->routine = fnc;
220e5c89e4eSSatish Balay     entry->next    = 0;
221e5c89e4eSSatish Balay     *fl            = entry;
222e5c89e4eSSatish Balay 
223*a2553e36SBarry Smith #if defined(PETSC_USE_DEBUG)
224e5c89e4eSSatish Balay     /* add this new list to list of all lists */
225e5c89e4eSSatish Balay     if (!dlallhead) {
226e5c89e4eSSatish Balay       dlallhead        = *fl;
227e5c89e4eSSatish Balay       (*fl)->next_list = 0;
228e5c89e4eSSatish Balay     } else {
229e5c89e4eSSatish Balay       ne               = dlallhead;
230e5c89e4eSSatish Balay       dlallhead        = *fl;
231e5c89e4eSSatish Balay       (*fl)->next_list = ne;
232e5c89e4eSSatish Balay     }
233a8a6328fSBarry Smith #endif
234a8a6328fSBarry Smith 
235e5c89e4eSSatish Balay   } else {
236e5c89e4eSSatish Balay     /* search list to see if it is already there */
237e5c89e4eSSatish Balay     ne = *fl;
238e5c89e4eSSatish Balay     while (ne) {
239ace3abfcSBarry Smith       PetscBool founddup;
240e5c89e4eSSatish Balay 
241e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
242e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
243e5c89e4eSSatish Balay         ne->routine = fnc;
244e5c89e4eSSatish Balay         PetscFunctionReturn(0);
245e5c89e4eSSatish Balay       }
246a297a907SKarl Rupp       if (ne->next) ne = ne->next;
247a297a907SKarl Rupp       else break;
248e5c89e4eSSatish Balay     }
249e5c89e4eSSatish Balay     /* create new entry and add to end of list */
250b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
251e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
252e5c89e4eSSatish Balay     entry->routine = fnc;
253e5c89e4eSSatish Balay     entry->next    = 0;
254e5c89e4eSSatish Balay     ne->next       = entry;
255e5c89e4eSSatish Balay   }
256e5c89e4eSSatish Balay   PetscFunctionReturn(0);
257e5c89e4eSSatish Balay }
258e5c89e4eSSatish Balay 
259e5c89e4eSSatish Balay #undef __FUNCT__
260140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroy"
261e5c89e4eSSatish Balay /*@
262140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
263e5c89e4eSSatish Balay 
264e5c89e4eSSatish Balay     Input Parameter:
265e5c89e4eSSatish Balay .   fl  - pointer to list
266e5c89e4eSSatish Balay 
267e5c89e4eSSatish Balay     Level: developer
268e5c89e4eSSatish Balay 
269a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
270e5c89e4eSSatish Balay @*/
271140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
272e5c89e4eSSatish Balay {
273140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
274e5c89e4eSSatish Balay   PetscErrorCode    ierr;
275e5c89e4eSSatish Balay 
276e5c89e4eSSatish Balay   PetscFunctionBegin;
2771441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
278e5c89e4eSSatish Balay 
279e5c89e4eSSatish Balay   /*
280e5c89e4eSSatish Balay        Remove this entry from the master DL list (if it is in it)
281e5c89e4eSSatish Balay   */
2821441b1d3SBarry Smith   if (dlallhead == *fl) {
283a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
28437e93019SBarry Smith     else dlallhead = NULL;
28537e93019SBarry Smith   } else if (tmp) {
2861441b1d3SBarry Smith     while (tmp->next_list != *fl) {
287e5c89e4eSSatish Balay       tmp = tmp->next_list;
288e5c89e4eSSatish Balay       if (!tmp->next_list) break;
289e5c89e4eSSatish Balay     }
290e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
291e5c89e4eSSatish Balay   }
292e5c89e4eSSatish Balay 
293e5c89e4eSSatish Balay   /* free this list */
2941441b1d3SBarry Smith   entry = *fl;
295e5c89e4eSSatish Balay   while (entry) {
296e5c89e4eSSatish Balay     next  = entry->next;
297e5c89e4eSSatish Balay     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
298e5c89e4eSSatish Balay     ierr  = PetscFree(entry);CHKERRQ(ierr);
299e5c89e4eSSatish Balay     entry = next;
300e5c89e4eSSatish Balay   }
3011441b1d3SBarry Smith   *fl = 0;
302e5c89e4eSSatish Balay   PetscFunctionReturn(0);
303e5c89e4eSSatish Balay }
304e5c89e4eSSatish Balay 
305e5c89e4eSSatish Balay /*
30637e93019SBarry Smith    Print any PetscFunctionLists that have not be destroyed
307e5c89e4eSSatish Balay */
308e5c89e4eSSatish Balay #undef __FUNCT__
30937e93019SBarry Smith #define __FUNCT__ "PetscFunctionListPrintAll"
31037e93019SBarry Smith PetscErrorCode  PetscFunctionListPrintAll(void)
311e5c89e4eSSatish Balay {
31237e93019SBarry Smith   PetscFunctionList tmp = dlallhead;
313e5c89e4eSSatish Balay   PetscErrorCode    ierr;
314e5c89e4eSSatish Balay 
315e5c89e4eSSatish Balay   PetscFunctionBegin;
31637e93019SBarry Smith   if (tmp) {
31737e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"The following PetscFunctionLists were not destroyed\n");CHKERRQ(ierr);
318e5c89e4eSSatish Balay   }
31937e93019SBarry Smith   while (tmp) {
32037e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"%s \n",tmp->name);CHKERRQ(ierr);
32137e93019SBarry Smith     tmp = tmp->next_list;
32237e93019SBarry Smith   }
323e5c89e4eSSatish Balay   PetscFunctionReturn(0);
324e5c89e4eSSatish Balay }
325e5c89e4eSSatish Balay 
3261c9cd337SJed Brown /*MC
3271c9cd337SJed Brown     PetscFunctionListFind - Find function registered under given name
3281c9cd337SJed Brown 
3291c9cd337SJed Brown     Synopsis:
330aaa7dc30SBarry Smith     #include <petscsys.h>
3311c9cd337SJed Brown     PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void))
332e5c89e4eSSatish Balay 
333e5c89e4eSSatish Balay     Input Parameters:
3341c9cd337SJed Brown +   flist   - pointer to list
3351c9cd337SJed Brown -   name - name registered for the function
336e5c89e4eSSatish Balay 
337e5c89e4eSSatish Balay     Output Parameters:
3381c9cd337SJed Brown .   fptr - the function pointer if name was found, else NULL
339e5c89e4eSSatish Balay 
340e5c89e4eSSatish Balay     Level: developer
341e5c89e4eSSatish Balay 
3421c9cd337SJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList, PetscObjectQueryFunction()
3431c9cd337SJed Brown M*/
3441c9cd337SJed Brown #undef __FUNCT__
3451c9cd337SJed Brown #define __FUNCT__ "PetscFunctionListFind_Private"
3461c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void))
347e5c89e4eSSatish Balay {
348140e18c1SBarry Smith   PetscFunctionList entry = fl;
349e5c89e4eSSatish Balay   PetscErrorCode    ierr;
350bdf89e91SBarry Smith   PetscBool         flg;
351e5c89e4eSSatish Balay 
352e5c89e4eSSatish Balay   PetscFunctionBegin;
353e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
354e5c89e4eSSatish Balay 
355e5c89e4eSSatish Balay   *r = 0;
356e5c89e4eSSatish Balay   while (entry) {
357bdf89e91SBarry Smith     ierr = PetscStrcmp(name,entry->name,&flg);CHKERRQ(ierr);
358bdf89e91SBarry Smith     if (flg) {
359e5c89e4eSSatish Balay       *r   = entry->routine;
360e5c89e4eSSatish Balay       PetscFunctionReturn(0);
361e5c89e4eSSatish Balay     }
362e5c89e4eSSatish Balay     entry = entry->next;
363e5c89e4eSSatish Balay   }
364e5c89e4eSSatish Balay   PetscFunctionReturn(0);
365e5c89e4eSSatish Balay }
366e5c89e4eSSatish Balay 
367e5c89e4eSSatish Balay #undef __FUNCT__
368140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListView"
369e5c89e4eSSatish Balay /*@
370140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
371e5c89e4eSSatish Balay 
372e5c89e4eSSatish Balay    Collective over MPI_Comm
373e5c89e4eSSatish Balay 
374e5c89e4eSSatish Balay    Input Parameters:
375e5c89e4eSSatish Balay +  list - the list of functions
376e5c89e4eSSatish Balay -  viewer - currently ignored
377e5c89e4eSSatish Balay 
378e5c89e4eSSatish Balay    Level: developer
379e5c89e4eSSatish Balay 
380a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionListPrintTypes(), PetscFunctionList
381e5c89e4eSSatish Balay @*/
382140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
383e5c89e4eSSatish Balay {
384e5c89e4eSSatish Balay   PetscErrorCode ierr;
385ace3abfcSBarry Smith   PetscBool      iascii;
386e5c89e4eSSatish Balay 
387e5c89e4eSSatish Balay   PetscFunctionBegin;
388e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
389e5c89e4eSSatish Balay   PetscValidPointer(list,1);
3900700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
391e5c89e4eSSatish Balay 
392251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
393e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
394e5c89e4eSSatish Balay 
395e5c89e4eSSatish Balay   while (list) {
396bdf89e91SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %s\n",list->name);CHKERRQ(ierr);
397e5c89e4eSSatish Balay     list = list->next;
398e5c89e4eSSatish Balay   }
399e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
400e5c89e4eSSatish Balay   PetscFunctionReturn(0);
401e5c89e4eSSatish Balay }
402e5c89e4eSSatish Balay 
403e5c89e4eSSatish Balay #undef __FUNCT__
404140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGet"
405065533a5SJed Brown /*@C
406140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
407e5c89e4eSSatish Balay          by help etc.
408e5c89e4eSSatish Balay 
409d4349b43SBarry Smith    Not Collective
410e5c89e4eSSatish Balay 
411e5c89e4eSSatish Balay    Input Parameter:
412e5c89e4eSSatish Balay .  list   - list of types
413e5c89e4eSSatish Balay 
414e5c89e4eSSatish Balay    Output Parameter:
415e5c89e4eSSatish Balay +  array - array of names
416e5c89e4eSSatish Balay -  n - length of array
417e5c89e4eSSatish Balay 
418e5c89e4eSSatish Balay    Notes:
419e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
420e5c89e4eSSatish Balay     not copied so should not be freed.
421e5c89e4eSSatish Balay 
422e5c89e4eSSatish Balay    Level: developer
423e5c89e4eSSatish Balay 
424a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
425e5c89e4eSSatish Balay @*/
426140e18c1SBarry Smith PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
427e5c89e4eSSatish Balay {
428e5c89e4eSSatish Balay   PetscErrorCode    ierr;
429e5c89e4eSSatish Balay   PetscInt          count = 0;
430140e18c1SBarry Smith   PetscFunctionList klist = list;
431e5c89e4eSSatish Balay 
432e5c89e4eSSatish Balay   PetscFunctionBegin;
433e5c89e4eSSatish Balay   while (list) {
434e5c89e4eSSatish Balay     list = list->next;
435e5c89e4eSSatish Balay     count++;
436e5c89e4eSSatish Balay   }
437854ce69bSBarry Smith   ierr  = PetscMalloc1(count+1,array);CHKERRQ(ierr);
438e5c89e4eSSatish Balay   count = 0;
439e5c89e4eSSatish Balay   while (klist) {
440e5c89e4eSSatish Balay     (*array)[count] = klist->name;
441e5c89e4eSSatish Balay     klist           = klist->next;
442e5c89e4eSSatish Balay     count++;
443e5c89e4eSSatish Balay   }
444e5c89e4eSSatish Balay   (*array)[count] = 0;
445e5c89e4eSSatish Balay   *n              = count+1;
446e5c89e4eSSatish Balay   PetscFunctionReturn(0);
447e5c89e4eSSatish Balay }
448e5c89e4eSSatish Balay 
449e5c89e4eSSatish Balay 
450e5c89e4eSSatish Balay #undef __FUNCT__
451140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListPrintTypes"
452e5c89e4eSSatish Balay /*@C
453140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
454e5c89e4eSSatish Balay 
455e5c89e4eSSatish Balay    Collective over MPI_Comm
456e5c89e4eSSatish Balay 
457e5c89e4eSSatish Balay    Input Parameters:
458e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
459e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
460e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
461e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
462e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
463e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
4643cc1e11dSBarry Smith .  list   - list of types
4653cc1e11dSBarry Smith -  def - default (current) value
466e5c89e4eSSatish Balay 
467e5c89e4eSSatish Balay    Level: developer
468e5c89e4eSSatish Balay 
469a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
470e5c89e4eSSatish Balay @*/
471140e18c1SBarry 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[])
472e5c89e4eSSatish Balay {
473e5c89e4eSSatish Balay   PetscErrorCode ierr;
474e5c89e4eSSatish Balay   char           p[64];
475e5c89e4eSSatish Balay 
476e5c89e4eSSatish Balay   PetscFunctionBegin;
477da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
478e5c89e4eSSatish Balay 
479e5c89e4eSSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
480e5c89e4eSSatish Balay   if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);}
4813cc1e11dSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr);
482e5c89e4eSSatish Balay 
483e5c89e4eSSatish Balay   while (list) {
484e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
485e5c89e4eSSatish Balay     list = list->next;
486e5c89e4eSSatish Balay   }
487e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
488e5c89e4eSSatish Balay   PetscFunctionReturn(0);
489e5c89e4eSSatish Balay }
490e5c89e4eSSatish Balay 
491e5c89e4eSSatish Balay #undef __FUNCT__
492140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDuplicate"
493e5c89e4eSSatish Balay /*@
494140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
495e5c89e4eSSatish Balay 
496e5c89e4eSSatish Balay     Input Parameters:
497e5c89e4eSSatish Balay .   fl   - pointer to list
498e5c89e4eSSatish Balay 
499e5c89e4eSSatish Balay     Output Parameters:
500e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
501e5c89e4eSSatish Balay 
502e5c89e4eSSatish Balay     Level: developer
503e5c89e4eSSatish Balay 
504140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
505e5c89e4eSSatish Balay 
506e5c89e4eSSatish Balay @*/
507140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
508e5c89e4eSSatish Balay {
509e5c89e4eSSatish Balay   PetscErrorCode ierr;
510e5c89e4eSSatish Balay 
511e5c89e4eSSatish Balay   PetscFunctionBegin;
512e5c89e4eSSatish Balay   while (fl) {
513bdf89e91SBarry Smith     ierr = PetscFunctionListAdd(nl,fl->name,fl->routine);CHKERRQ(ierr);
514e5c89e4eSSatish Balay     fl   = fl->next;
515e5c89e4eSSatish Balay   }
516e5c89e4eSSatish Balay   PetscFunctionReturn(0);
517e5c89e4eSSatish Balay }
518e5c89e4eSSatish Balay 
519