xref: /petsc/src/sys/dll/reg.c (revision 60da17ec39af736fa870f8b3e3b0a4b951a26ca0)
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 
16*60da17ecSBarry 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   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 
39*60da17ecSBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY) && !(defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES))
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);
53*60da17ecSBarry Smith PETSC_EXTERN PetscErrorCode TaoInitializePackage(void);
54*60da17ecSBarry Smith #endif
55*60da17ecSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
5622b6d1caSBarry Smith static MPI_Comm PETSC_COMM_WORLD_INNER = 0,PETSC_COMM_SELF_INNER = 0;
57acff04ddSBarry Smith #endif
58acff04ddSBarry Smith 
59e5c89e4eSSatish Balay /*
60e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
61e5c89e4eSSatish Balay     search path.
62e5c89e4eSSatish Balay */
6395c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void)
64e5c89e4eSSatish Balay {
65487e5849SBarry Smith   char           *libname[32];
66e5c89e4eSSatish Balay   PetscErrorCode ierr;
67e5c89e4eSSatish Balay   PetscInt       nmax,i;
68*60da17ecSBarry Smith   PetscBool      preload = PETSC_FALSE;
69540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
70540e20f2SPierre Jolivet   PetscBool      PetscInitialized = PetscInitializeCalled;
71540e20f2SPierre Jolivet #endif
72e5c89e4eSSatish Balay 
7360154eb2SBarry Smith   PetscFunctionBegin;
74*60da17ecSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
75*60da17ecSBarry Smith   /* These must be all initialized here because it is not safe for individual threads to call these initialize routines */
76*60da17ecSBarry Smith   preload = PETSC_TRUE;
77*60da17ecSBarry Smith #endif
78*60da17ecSBarry Smith 
79e5c89e4eSSatish Balay   nmax = 32;
80c5929fdfSBarry Smith   ierr = PetscOptionsGetStringArray(NULL,NULL,"-dll_prepend",libname,&nmax,NULL);CHKERRQ(ierr);
81e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
82d44a1e48SBarry Smith     ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
83e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
84e5c89e4eSSatish Balay   }
85e5c89e4eSSatish Balay 
86*60da17ecSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-library_preload",&preload,NULL);CHKERRQ(ierr);
87*60da17ecSBarry Smith   if (!preload) {
88607a6623SBarry Smith     ierr = PetscSysInitializePackage();CHKERRQ(ierr);
89*60da17ecSBarry Smith   } else {
90*60da17ecSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
91aa2d57e9SJed Brown     PetscBool found;
9260154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
93487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
94e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
9560154eb2SBarry Smith #else
9660154eb2SBarry Smith     ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
9760154eb2SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
98487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
99e32f2f54SBarry 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!");
100487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
101e32f2f54SBarry 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!");
102487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
103e32f2f54SBarry 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!");
104487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
105e32f2f54SBarry 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!");
106487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
107e32f2f54SBarry 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!");
108487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
109e32f2f54SBarry 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!");
110*60da17ecSBarry Smith     ierr = PetscLoadDynamicLibrary("tao",&found);CHKERRQ(ierr);
111*60da17ecSBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate Tao dynamic library \n You cannot move the dynamic libraries!");
112bb84e0fdSBarry Smith #endif
113*60da17ecSBarry Smith #else /* defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) */
114*60da17ecSBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
1153020f62cSBarry Smith   ierr = AOInitializePackage();CHKERRQ(ierr);
1163020f62cSBarry Smith   ierr = PetscSFInitializePackage();CHKERRQ(ierr);
1177da51e29SSatish Balay #if !defined(PETSC_USE_COMPLEX)
1183020f62cSBarry Smith   ierr = CharacteristicInitializePackage();CHKERRQ(ierr);
1197da51e29SSatish Balay #endif
1203020f62cSBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
1213020f62cSBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
1223020f62cSBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1233020f62cSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
1243020f62cSBarry Smith   ierr = PCInitializePackage();CHKERRQ(ierr);
1253020f62cSBarry Smith   ierr = KSPInitializePackage();CHKERRQ(ierr);
1263020f62cSBarry Smith   ierr = SNESInitializePackage();CHKERRQ(ierr);
1273020f62cSBarry Smith   ierr = TSInitializePackage();CHKERRQ(ierr);
128*60da17ecSBarry Smith   ierr = TaoInitializePackage();CHKERRQ(ierr);
129*60da17ecSBarry Smith #else
130*60da17ecSBarry Smith   SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Cannot use -library_preload with multiple static PETSc libraries");
131*60da17ecSBarry Smith #endif
132*60da17ecSBarry Smith #endif /* defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) */
133*60da17ecSBarry Smith   }
134*60da17ecSBarry Smith 
135*60da17ecSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) && defined(PETSC_HAVE_BAMG)
136*60da17ecSBarry Smith   {
137*60da17ecSBarry Smith     PetscBool found;
138*60da17ecSBarry Smith     ierr = PetscLoadDynamicLibrary("bamg",&found);CHKERRQ(ierr);
139*60da17ecSBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc BAMG dynamic library \n You cannot move the dynamic libraries!");
140*60da17ecSBarry Smith   }
141*60da17ecSBarry Smith #endif
142*60da17ecSBarry Smith 
143*60da17ecSBarry Smith   nmax = 32;
144*60da17ecSBarry Smith   ierr = PetscOptionsGetStringArray(NULL,NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr);
145*60da17ecSBarry Smith   for (i=0; i<nmax; i++) {
146*60da17ecSBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
147*60da17ecSBarry Smith     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
148*60da17ecSBarry Smith   }
149*60da17ecSBarry Smith 
150*60da17ecSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
151acff04ddSBarry Smith   ierr = PetscCommDuplicate(PETSC_COMM_SELF,&PETSC_COMM_SELF_INNER,NULL);CHKERRQ(ierr);
152acff04ddSBarry Smith   ierr = PetscCommDuplicate(PETSC_COMM_WORLD,&PETSC_COMM_WORLD_INNER,NULL);CHKERRQ(ierr);
153acff04ddSBarry Smith #endif
154540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
155540e20f2SPierre Jolivet   /* in Fortran, PetscInitializeCalled is set to PETSC_TRUE before PetscInitialize_DynamicLibraries() */
156540e20f2SPierre Jolivet   /* in C, it is not the case, but the value is forced to PETSC_TRUE so that PetscRegisterFinalize() is called */
157540e20f2SPierre Jolivet   PetscInitializeCalled = PETSC_TRUE;
158540e20f2SPierre Jolivet   ierr = PetscElementalInitializePackage();CHKERRQ(ierr);
159540e20f2SPierre Jolivet   PetscInitializeCalled = PetscInitialized;
160540e20f2SPierre Jolivet #endif
161e5c89e4eSSatish Balay   PetscFunctionReturn(0);
162e5c89e4eSSatish Balay }
163e5c89e4eSSatish Balay 
164ebd79076SLisandro Dalcin /*
165ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
166ebd79076SLisandro Dalcin */
16795c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void)
168e5c89e4eSSatish Balay {
169ebd79076SLisandro Dalcin   PetscErrorCode ierr;
170ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
171e5c89e4eSSatish Balay 
172ebd79076SLisandro Dalcin   PetscFunctionBegin;
173c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
174d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
175d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
176a297a907SKarl Rupp 
177acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY)
178acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_SELF_INNER);CHKERRQ(ierr);
179acff04ddSBarry Smith   ierr = PetscCommDestroy(&PETSC_COMM_WORLD_INNER);CHKERRQ(ierr);
180acff04ddSBarry Smith #endif
181acff04ddSBarry Smith 
18202c9f0b5SLisandro Dalcin   PetscDLLibrariesLoaded = NULL;
183e5c89e4eSSatish Balay   PetscFunctionReturn(0);
184e5c89e4eSSatish Balay }
185e5c89e4eSSatish Balay 
186e4d1774bSDmitry Karpeev 
187e4d1774bSDmitry Karpeev 
188e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
189140e18c1SBarry Smith struct _n_PetscFunctionList {
190e5c89e4eSSatish Balay   void              (*routine)(void);    /* the routine */
191e5c89e4eSSatish Balay   char              *name;               /* string to identify routine */
192140e18c1SBarry Smith   PetscFunctionList next;                /* next pointer */
193140e18c1SBarry Smith   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
194e5c89e4eSSatish Balay };
195e5c89e4eSSatish Balay 
196e5c89e4eSSatish Balay /*
197140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
198e5c89e4eSSatish Balay */
19902c9f0b5SLisandro Dalcin static PetscFunctionList dlallhead = NULL;
200e5c89e4eSSatish Balay 
201a240a19fSJed Brown /*MC
202140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
203e5c89e4eSSatish Balay    specified registry.
204e5c89e4eSSatish Balay 
205a240a19fSJed Brown    Synopsis:
206aaa7dc30SBarry Smith    #include <petscsys.h>
207b9fb364aSBarry Smith    PetscErrorCode PetscFunctionListAdd(PetscFunctionList *flist,const char name[],void (*fptr)(void))
208a240a19fSJed Brown 
209d4349b43SBarry Smith    Not Collective
210e5c89e4eSSatish Balay 
211e5c89e4eSSatish Balay    Input Parameters:
212b9fb364aSBarry Smith +  flist - pointer to function list object
213e5c89e4eSSatish Balay .  name - string to identify routine
214a240a19fSJed Brown -  fptr - function pointer
215e5c89e4eSSatish Balay 
216e5c89e4eSSatish Balay    Notes:
217a240a19fSJed Brown    To remove a registered routine, pass in a NULL fptr.
218e5c89e4eSSatish Balay 
219e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
220e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
2211c84c290SBarry Smith    for that particular component (e.g., SNESRegister()) instead of
222140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
223e5c89e4eSSatish Balay 
224e5c89e4eSSatish Balay     Level: developer
225e5c89e4eSSatish Balay 
2261c84c290SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(),
227a240a19fSJed Brown           PCRegister(), TSRegister(), PetscFunctionList, PetscObjectComposeFunction()
228a240a19fSJed Brown M*/
229a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl,const char name[],void (*fnc)(void))
230e5c89e4eSSatish Balay {
231140e18c1SBarry Smith   PetscFunctionList entry,ne;
232e5c89e4eSSatish Balay   PetscErrorCode    ierr;
233e5c89e4eSSatish Balay 
234e5c89e4eSSatish Balay   PetscFunctionBegin;
235e5c89e4eSSatish Balay   if (!*fl) {
236b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
237e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
238e5c89e4eSSatish Balay     entry->routine = fnc;
23902c9f0b5SLisandro Dalcin     entry->next    = NULL;
240e5c89e4eSSatish Balay     *fl            = entry;
241e5c89e4eSSatish Balay 
24276bd3646SJed Brown     if (PetscDefined(USE_DEBUG)) {
243e5c89e4eSSatish Balay       /* add this new list to list of all lists */
244e5c89e4eSSatish Balay       if (!dlallhead) {
245e5c89e4eSSatish Balay         dlallhead        = *fl;
24602c9f0b5SLisandro Dalcin         (*fl)->next_list = NULL;
247e5c89e4eSSatish Balay       } else {
248e5c89e4eSSatish Balay         ne               = dlallhead;
249e5c89e4eSSatish Balay         dlallhead        = *fl;
250e5c89e4eSSatish Balay         (*fl)->next_list = ne;
251e5c89e4eSSatish Balay       }
25276bd3646SJed Brown     }
253a8a6328fSBarry Smith 
254e5c89e4eSSatish Balay   } else {
255e5c89e4eSSatish Balay     /* search list to see if it is already there */
256e5c89e4eSSatish Balay     ne = *fl;
257e5c89e4eSSatish Balay     while (ne) {
258ace3abfcSBarry Smith       PetscBool founddup;
259e5c89e4eSSatish Balay 
260e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
261e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
262e5c89e4eSSatish Balay         ne->routine = fnc;
263e5c89e4eSSatish Balay         PetscFunctionReturn(0);
264e5c89e4eSSatish Balay       }
265a297a907SKarl Rupp       if (ne->next) ne = ne->next;
266a297a907SKarl Rupp       else break;
267e5c89e4eSSatish Balay     }
268e5c89e4eSSatish Balay     /* create new entry and add to end of list */
269b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
270e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
271e5c89e4eSSatish Balay     entry->routine = fnc;
27202c9f0b5SLisandro Dalcin     entry->next    = NULL;
273e5c89e4eSSatish Balay     ne->next       = entry;
274e5c89e4eSSatish Balay   }
275e5c89e4eSSatish Balay   PetscFunctionReturn(0);
276e5c89e4eSSatish Balay }
277e5c89e4eSSatish Balay 
278e5c89e4eSSatish Balay /*@
279140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
280e5c89e4eSSatish Balay 
281e5c89e4eSSatish Balay     Input Parameter:
282e5c89e4eSSatish Balay .   fl  - pointer to list
283e5c89e4eSSatish Balay 
284e5c89e4eSSatish Balay     Level: developer
285e5c89e4eSSatish Balay 
286a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
287e5c89e4eSSatish Balay @*/
288140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
289e5c89e4eSSatish Balay {
290140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
291e5c89e4eSSatish Balay   PetscErrorCode    ierr;
292e5c89e4eSSatish Balay 
293e5c89e4eSSatish Balay   PetscFunctionBegin;
2941441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
295e5c89e4eSSatish Balay 
296e5c89e4eSSatish Balay   /*
2979dddd249SSatish Balay        Remove this entry from the main DL list (if it is in it)
298e5c89e4eSSatish Balay   */
2991441b1d3SBarry Smith   if (dlallhead == *fl) {
300a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
30137e93019SBarry Smith     else dlallhead = NULL;
30237e93019SBarry Smith   } else if (tmp) {
3031441b1d3SBarry Smith     while (tmp->next_list != *fl) {
304e5c89e4eSSatish Balay       tmp = tmp->next_list;
305e5c89e4eSSatish Balay       if (!tmp->next_list) break;
306e5c89e4eSSatish Balay     }
307e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
308e5c89e4eSSatish Balay   }
309e5c89e4eSSatish Balay 
310e5c89e4eSSatish Balay   /* free this list */
3111441b1d3SBarry Smith   entry = *fl;
312e5c89e4eSSatish Balay   while (entry) {
313e5c89e4eSSatish Balay     next  = entry->next;
314e5c89e4eSSatish Balay     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
315e5c89e4eSSatish Balay     ierr  = PetscFree(entry);CHKERRQ(ierr);
316e5c89e4eSSatish Balay     entry = next;
317e5c89e4eSSatish Balay   }
31802c9f0b5SLisandro Dalcin   *fl = NULL;
319e5c89e4eSSatish Balay   PetscFunctionReturn(0);
320e5c89e4eSSatish Balay }
321e5c89e4eSSatish Balay 
322e5c89e4eSSatish Balay /*
32337e93019SBarry Smith    Print any PetscFunctionLists that have not be destroyed
324e5c89e4eSSatish Balay */
32537e93019SBarry Smith PetscErrorCode  PetscFunctionListPrintAll(void)
326e5c89e4eSSatish Balay {
32737e93019SBarry Smith   PetscFunctionList tmp = dlallhead;
328e5c89e4eSSatish Balay   PetscErrorCode    ierr;
329e5c89e4eSSatish Balay 
330e5c89e4eSSatish Balay   PetscFunctionBegin;
33137e93019SBarry Smith   if (tmp) {
33237e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"The following PetscFunctionLists were not destroyed\n");CHKERRQ(ierr);
333e5c89e4eSSatish Balay   }
33437e93019SBarry Smith   while (tmp) {
33537e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"%s \n",tmp->name);CHKERRQ(ierr);
33637e93019SBarry Smith     tmp = tmp->next_list;
33737e93019SBarry Smith   }
338e5c89e4eSSatish Balay   PetscFunctionReturn(0);
339e5c89e4eSSatish Balay }
340e5c89e4eSSatish Balay 
3411c9cd337SJed Brown /*MC
3421c9cd337SJed Brown     PetscFunctionListFind - Find function registered under given name
3431c9cd337SJed Brown 
3441c9cd337SJed Brown     Synopsis:
345aaa7dc30SBarry Smith     #include <petscsys.h>
3461c9cd337SJed Brown     PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void))
347e5c89e4eSSatish Balay 
348e5c89e4eSSatish Balay     Input Parameters:
3491c9cd337SJed Brown +   flist   - pointer to list
3501c9cd337SJed Brown -   name - name registered for the function
351e5c89e4eSSatish Balay 
352e5c89e4eSSatish Balay     Output Parameters:
3531c9cd337SJed Brown .   fptr - the function pointer if name was found, else NULL
354e5c89e4eSSatish Balay 
355e5c89e4eSSatish Balay     Level: developer
356e5c89e4eSSatish Balay 
3571c9cd337SJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList, PetscObjectQueryFunction()
3581c9cd337SJed Brown M*/
3591c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void))
360e5c89e4eSSatish Balay {
361140e18c1SBarry Smith   PetscFunctionList entry = fl;
362e5c89e4eSSatish Balay   PetscErrorCode    ierr;
363bdf89e91SBarry Smith   PetscBool         flg;
364e5c89e4eSSatish Balay 
365e5c89e4eSSatish Balay   PetscFunctionBegin;
366e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
367e5c89e4eSSatish Balay 
36802c9f0b5SLisandro Dalcin   *r = NULL;
369e5c89e4eSSatish Balay   while (entry) {
370bdf89e91SBarry Smith     ierr = PetscStrcmp(name,entry->name,&flg);CHKERRQ(ierr);
371bdf89e91SBarry Smith     if (flg) {
372e5c89e4eSSatish Balay       *r   = entry->routine;
373e5c89e4eSSatish Balay       PetscFunctionReturn(0);
374e5c89e4eSSatish Balay     }
375e5c89e4eSSatish Balay     entry = entry->next;
376e5c89e4eSSatish Balay   }
377e5c89e4eSSatish Balay   PetscFunctionReturn(0);
378e5c89e4eSSatish Balay }
379e5c89e4eSSatish Balay 
380e5c89e4eSSatish Balay /*@
381140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
382e5c89e4eSSatish Balay 
383e5c89e4eSSatish Balay    Collective over MPI_Comm
384e5c89e4eSSatish Balay 
385e5c89e4eSSatish Balay    Input Parameters:
386e5c89e4eSSatish Balay +  list - the list of functions
387e5c89e4eSSatish Balay -  viewer - currently ignored
388e5c89e4eSSatish Balay 
389e5c89e4eSSatish Balay    Level: developer
390e5c89e4eSSatish Balay 
391a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionListPrintTypes(), PetscFunctionList
392e5c89e4eSSatish Balay @*/
393140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
394e5c89e4eSSatish Balay {
395e5c89e4eSSatish Balay   PetscErrorCode ierr;
396ace3abfcSBarry Smith   PetscBool      iascii;
397e5c89e4eSSatish Balay 
398e5c89e4eSSatish Balay   PetscFunctionBegin;
399e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
400e5c89e4eSSatish Balay   PetscValidPointer(list,1);
4010700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
402e5c89e4eSSatish Balay 
403251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
404e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
405e5c89e4eSSatish Balay 
406e5c89e4eSSatish Balay   while (list) {
407bdf89e91SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %s\n",list->name);CHKERRQ(ierr);
408e5c89e4eSSatish Balay     list = list->next;
409e5c89e4eSSatish Balay   }
410e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
411e5c89e4eSSatish Balay   PetscFunctionReturn(0);
412e5c89e4eSSatish Balay }
413e5c89e4eSSatish Balay 
414065533a5SJed Brown /*@C
415140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
416e5c89e4eSSatish Balay          by help etc.
417e5c89e4eSSatish Balay 
418d4349b43SBarry Smith    Not Collective
419e5c89e4eSSatish Balay 
420e5c89e4eSSatish Balay    Input Parameter:
421e5c89e4eSSatish Balay .  list   - list of types
422e5c89e4eSSatish Balay 
423e5c89e4eSSatish Balay    Output Parameter:
424e5c89e4eSSatish Balay +  array - array of names
425e5c89e4eSSatish Balay -  n - length of array
426e5c89e4eSSatish Balay 
427e5c89e4eSSatish Balay    Notes:
428e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
429e5c89e4eSSatish Balay     not copied so should not be freed.
430e5c89e4eSSatish Balay 
431e5c89e4eSSatish Balay    Level: developer
432e5c89e4eSSatish Balay 
433a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
434e5c89e4eSSatish Balay @*/
435ca1b3570SJed Brown PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
436e5c89e4eSSatish Balay {
437e5c89e4eSSatish Balay   PetscErrorCode    ierr;
438e5c89e4eSSatish Balay   PetscInt          count = 0;
439140e18c1SBarry Smith   PetscFunctionList klist = list;
440e5c89e4eSSatish Balay 
441e5c89e4eSSatish Balay   PetscFunctionBegin;
442e5c89e4eSSatish Balay   while (list) {
443e5c89e4eSSatish Balay     list = list->next;
444e5c89e4eSSatish Balay     count++;
445e5c89e4eSSatish Balay   }
446ca1b3570SJed Brown   ierr  = PetscMalloc1(count+1,(char***)array);CHKERRQ(ierr);
447e5c89e4eSSatish Balay   count = 0;
448e5c89e4eSSatish Balay   while (klist) {
449e5c89e4eSSatish Balay     (*array)[count] = klist->name;
450e5c89e4eSSatish Balay     klist           = klist->next;
451e5c89e4eSSatish Balay     count++;
452e5c89e4eSSatish Balay   }
45302c9f0b5SLisandro Dalcin   (*array)[count] = NULL;
454e5c89e4eSSatish Balay   *n              = count+1;
455e5c89e4eSSatish Balay   PetscFunctionReturn(0);
456e5c89e4eSSatish Balay }
457e5c89e4eSSatish Balay 
458e5c89e4eSSatish Balay 
459e5c89e4eSSatish Balay /*@C
460140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
461e5c89e4eSSatish Balay 
462e5c89e4eSSatish Balay    Collective over MPI_Comm
463e5c89e4eSSatish Balay 
464e5c89e4eSSatish Balay    Input Parameters:
465e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
466e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
467e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
468e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
469e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
470e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
4713cc1e11dSBarry Smith .  list   - list of types
47244ef3d73SBarry Smith .  def - default (current) value
47344ef3d73SBarry Smith -  newv - new value
474e5c89e4eSSatish Balay 
475e5c89e4eSSatish Balay    Level: developer
476e5c89e4eSSatish Balay 
477a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
478e5c89e4eSSatish Balay @*/
47944ef3d73SBarry 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[])
480e5c89e4eSSatish Balay {
481e5c89e4eSSatish Balay   PetscErrorCode ierr;
482e5c89e4eSSatish Balay   char           p[64];
483e5c89e4eSSatish Balay 
484e5c89e4eSSatish Balay   PetscFunctionBegin;
485da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
486e5c89e4eSSatish Balay 
487a126751eSBarry Smith   ierr = PetscStrncpy(p,"-",sizeof(p));CHKERRQ(ierr);
488a126751eSBarry Smith   if (prefix) {ierr = PetscStrlcat(p,prefix,sizeof(p));CHKERRQ(ierr);}
4890e4c290aSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <now %s : formerly %s>: %s (one of)",p,name+1,newv,def,text);CHKERRQ(ierr);
490e5c89e4eSSatish Balay 
491e5c89e4eSSatish Balay   while (list) {
492e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
493e5c89e4eSSatish Balay     list = list->next;
494e5c89e4eSSatish Balay   }
495e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
496e5c89e4eSSatish Balay   PetscFunctionReturn(0);
497e5c89e4eSSatish Balay }
498e5c89e4eSSatish Balay 
499e5c89e4eSSatish Balay /*@
500140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
501e5c89e4eSSatish Balay 
502e5c89e4eSSatish Balay     Input Parameters:
503e5c89e4eSSatish Balay .   fl   - pointer to list
504e5c89e4eSSatish Balay 
505e5c89e4eSSatish Balay     Output Parameters:
506e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
507e5c89e4eSSatish Balay 
508e5c89e4eSSatish Balay     Level: developer
509e5c89e4eSSatish Balay 
510140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
511e5c89e4eSSatish Balay 
512e5c89e4eSSatish Balay @*/
513140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
514e5c89e4eSSatish Balay {
515e5c89e4eSSatish Balay   PetscErrorCode ierr;
516e5c89e4eSSatish Balay 
517e5c89e4eSSatish Balay   PetscFunctionBegin;
518e5c89e4eSSatish Balay   while (fl) {
519bdf89e91SBarry Smith     ierr = PetscFunctionListAdd(nl,fl->name,fl->routine);CHKERRQ(ierr);
520e5c89e4eSSatish Balay     fl   = fl->next;
521e5c89e4eSSatish Balay   }
522e5c89e4eSSatish Balay   PetscFunctionReturn(0);
523e5c89e4eSSatish Balay }
524