xref: /petsc/src/sys/dll/reg.c (revision afcb2eb581850588619400ff311bed89835ec555)
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 */
6*afcb2eb5SJed Brown #include <petsc-private/petscimpl.h>           /*I "petscsys.h" I*/
7e5c89e4eSSatish Balay 
8e5c89e4eSSatish Balay #undef __FUNCT__
9140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGetPathAndFunction"
10140e18c1SBarry Smith PetscErrorCode  PetscFunctionListGetPathAndFunction(const char name[],char *path[],char *function[])
11e5c89e4eSSatish Balay {
12e5c89e4eSSatish Balay   PetscErrorCode ierr;
13e5c89e4eSSatish Balay   char           work[PETSC_MAX_PATH_LEN],*lfunction;
14e5c89e4eSSatish Balay 
15e5c89e4eSSatish Balay   PetscFunctionBegin;
168caf3d72SBarry Smith   ierr = PetscStrncpy(work,name,sizeof(work));CHKERRQ(ierr);
17a297a907SKarl Rupp 
188caf3d72SBarry Smith   work[sizeof(work) - 1] = 0;
19a297a907SKarl Rupp 
20e5c89e4eSSatish Balay   ierr = PetscStrchr(work,':',&lfunction);CHKERRQ(ierr);
21e5c89e4eSSatish Balay   if (lfunction != work && lfunction && lfunction[1] != ':') {
22e5c89e4eSSatish Balay     lfunction[0] = 0;
23a297a907SKarl Rupp 
24e5c89e4eSSatish Balay     ierr = PetscStrallocpy(work,path);CHKERRQ(ierr);
25e5c89e4eSSatish Balay     ierr = PetscStrallocpy(lfunction+1,function);CHKERRQ(ierr);
26e5c89e4eSSatish Balay   } else {
27e5c89e4eSSatish Balay     *path = 0;
28e5c89e4eSSatish Balay     ierr  = PetscStrallocpy(name,function);CHKERRQ(ierr);
29e5c89e4eSSatish Balay   }
30e5c89e4eSSatish Balay   PetscFunctionReturn(0);
31e5c89e4eSSatish Balay }
32e5c89e4eSSatish Balay 
333fa76a5bSLisandro Dalcin /*
343fa76a5bSLisandro Dalcin     This is the default list used by PETSc with the PetscDLLibrary register routines
353fa76a5bSLisandro Dalcin */
36d44a1e48SBarry Smith PetscDLLibrary PetscDLLibrariesLoaded = 0;
373fa76a5bSLisandro Dalcin 
38ebd79076SLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
39ebd79076SLisandro Dalcin 
40e5c89e4eSSatish Balay #undef __FUNCT__
41487e5849SBarry Smith #define __FUNCT__ "PetscLoadDynamicLibrary"
427087cfbeSBarry Smith static PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
43487e5849SBarry Smith {
44487e5849SBarry Smith   char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
45487e5849SBarry Smith   PetscErrorCode ierr;
46487e5849SBarry Smith 
47487e5849SBarry Smith   PetscFunctionBegin;
48487e5849SBarry Smith   ierr = PetscStrcpy(libs,"${PETSC_LIB_DIR}/libpetsc");CHKERRQ(ierr);
49487e5849SBarry Smith   ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
50487e5849SBarry Smith   ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
51487e5849SBarry Smith   if (*found) {
52d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
53487e5849SBarry Smith   } else {
54487e5849SBarry Smith     ierr = PetscStrcpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc");CHKERRQ(ierr);
55487e5849SBarry Smith     ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
56487e5849SBarry Smith     ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
57487e5849SBarry Smith     if (*found) {
58d44a1e48SBarry Smith       ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
59487e5849SBarry Smith     }
60487e5849SBarry Smith   }
61487e5849SBarry Smith   PetscFunctionReturn(0);
62487e5849SBarry Smith }
63487e5849SBarry Smith 
643fa76a5bSLisandro Dalcin #endif
653fa76a5bSLisandro Dalcin 
66487e5849SBarry Smith #undef __FUNCT__
67e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize_DynamicLibraries"
68e5c89e4eSSatish Balay /*
69e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
70e5c89e4eSSatish Balay     search path.
71e5c89e4eSSatish Balay */
727087cfbeSBarry Smith PetscErrorCode  PetscInitialize_DynamicLibraries(void)
73e5c89e4eSSatish Balay {
74487e5849SBarry Smith   char           *libname[32];
75e5c89e4eSSatish Balay   PetscErrorCode ierr;
76e5c89e4eSSatish Balay   PetscInt       nmax,i;
773fa76a5bSLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
78ace3abfcSBarry Smith   PetscBool      found;
793fa76a5bSLisandro Dalcin #endif
80e5c89e4eSSatish Balay 
8160154eb2SBarry Smith   PetscFunctionBegin;
82e5c89e4eSSatish Balay   nmax = 32;
830298fd71SBarry Smith   ierr = PetscOptionsGetStringArray(NULL,"-dll_prepend",libname,&nmax,NULL);CHKERRQ(ierr);
84e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
85d44a1e48SBarry Smith     ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
86e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
87e5c89e4eSSatish Balay   }
88e5c89e4eSSatish Balay 
893fa76a5bSLisandro Dalcin #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
903fa76a5bSLisandro Dalcin   /*
913fa76a5bSLisandro Dalcin       This just initializes the most basic PETSc stuff.
923fa76a5bSLisandro Dalcin 
933fa76a5bSLisandro Dalcin     The classes, from PetscDraw to PetscTS, are initialized the first
943fa76a5bSLisandro Dalcin     time an XXCreate() is called.
953fa76a5bSLisandro Dalcin   */
960298fd71SBarry Smith   ierr = PetscSysInitializePackage(NULL);CHKERRQ(ierr);
973fa76a5bSLisandro Dalcin #else
9860154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
99487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
100e32f2f54SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
10160154eb2SBarry Smith #else
10260154eb2SBarry Smith   ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
10360154eb2SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
104487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
105e32f2f54SBarry 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!");
106487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
107e32f2f54SBarry 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!");
108487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
109e32f2f54SBarry 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!");
11060154eb2SBarry Smith   ierr = PetscLoadDynamicLibrary("characteristic",&found);CHKERRQ(ierr);
11160154eb2SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Characteristic dynamic library \n You cannot move the dynamic libraries!");
112487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
113e32f2f54SBarry 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!");
114487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
115e32f2f54SBarry 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!");
116487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
117e32f2f54SBarry 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!");
118bb84e0fdSBarry Smith #endif
119e5c89e4eSSatish Balay 
120487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("mesh",&found);CHKERRQ(ierr);
121487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("contrib",&found);CHKERRQ(ierr);
1223fa76a5bSLisandro Dalcin #endif
123e5c89e4eSSatish Balay 
124e5c89e4eSSatish Balay   nmax = 32;
1250298fd71SBarry Smith   ierr = PetscOptionsGetStringArray(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   }
130e5c89e4eSSatish Balay   PetscFunctionReturn(0);
131e5c89e4eSSatish Balay }
132e5c89e4eSSatish Balay 
133e5c89e4eSSatish Balay #undef __FUNCT__
134e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize_DynamicLibraries"
135ebd79076SLisandro Dalcin /*
136ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
137ebd79076SLisandro Dalcin */
138e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void)
139e5c89e4eSSatish Balay {
140ebd79076SLisandro Dalcin   PetscErrorCode ierr;
141ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
142e5c89e4eSSatish Balay 
143ebd79076SLisandro Dalcin   PetscFunctionBegin;
1440298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
145d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
146d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
147a297a907SKarl Rupp 
148d44a1e48SBarry Smith   PetscDLLibrariesLoaded = 0;
149e5c89e4eSSatish Balay   PetscFunctionReturn(0);
150e5c89e4eSSatish Balay }
151e5c89e4eSSatish Balay 
152e4d1774bSDmitry Karpeev 
153e4d1774bSDmitry Karpeev 
154e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
155140e18c1SBarry Smith struct _n_PetscFunctionList {
156e5c89e4eSSatish Balay   void              (*routine)(void);    /* the routine */
157e5c89e4eSSatish Balay   char              *path;               /* path of link library containing routine */
158e5c89e4eSSatish Balay   char              *name;               /* string to identify routine */
159e5c89e4eSSatish Balay   char              *rname;              /* routine name in dynamic library */
160140e18c1SBarry Smith   PetscFunctionList next;                /* next pointer */
161140e18c1SBarry Smith   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
162e5c89e4eSSatish Balay };
163e5c89e4eSSatish Balay 
164e5c89e4eSSatish Balay /*
165140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
166e5c89e4eSSatish Balay */
167140e18c1SBarry Smith static PetscFunctionList dlallhead = 0;
168e5c89e4eSSatish Balay 
169e5c89e4eSSatish Balay #undef __FUNCT__
170140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListAdd"
171e5c89e4eSSatish Balay /*@C
172140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
173e5c89e4eSSatish Balay    specified registry.
174e5c89e4eSSatish Balay 
1756d75e210SBarry Smith      Formally Collective on MPI_Comm
176e5c89e4eSSatish Balay 
177e5c89e4eSSatish Balay    Input Parameters:
1786d75e210SBarry Smith +  comm  - the comm where this exists (currently not used)
1796d75e210SBarry Smith .  fl    - pointer registry
180e5c89e4eSSatish Balay .  name  - string to identify routine
181e5c89e4eSSatish Balay .  rname - routine name in dynamic library
182e5c89e4eSSatish Balay -  fnc   - function pointer (optional if using dynamic libraries)
183e5c89e4eSSatish Balay 
184e5c89e4eSSatish Balay    Notes:
1850298fd71SBarry Smith    To remove a registered routine, pass in a NULL rname and fnc().
186e5c89e4eSSatish Balay 
187e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
188e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
189e5c89e4eSSatish Balay    for that particular component (e.g., SNESRegisterDynamic()) instead of
190140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
191e5c89e4eSSatish Balay 
192e5c89e4eSSatish Balay    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environmental variable}
193e5c89e4eSSatish Balay   occuring in pathname will be replaced with appropriate values.
194e5c89e4eSSatish Balay 
195e5c89e4eSSatish Balay    Level: developer
196e5c89e4eSSatish Balay 
197140e18c1SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegisterDynamic(), KSPRegisterDynamic(),
198140e18c1SBarry Smith           PCRegisterDynamic(), TSRegisterDynamic(), PetscFunctionList
199e5c89e4eSSatish Balay @*/
200140e18c1SBarry Smith PetscErrorCode  PetscFunctionListAdd(MPI_Comm comm,PetscFunctionList *fl,const char name[],const char rname[],void (*fnc)(void))
201e5c89e4eSSatish Balay {
202140e18c1SBarry Smith   PetscFunctionList entry,ne;
203e5c89e4eSSatish Balay   PetscErrorCode    ierr;
204e5c89e4eSSatish Balay   char              *fpath,*fname;
205e5c89e4eSSatish Balay 
206e5c89e4eSSatish Balay   PetscFunctionBegin;
207e5c89e4eSSatish Balay   if (!*fl) {
208140e18c1SBarry Smith     ierr           = PetscNew(struct _n_PetscFunctionList,&entry);CHKERRQ(ierr);
209e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
210140e18c1SBarry Smith     ierr           = PetscFunctionListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr);
211e5c89e4eSSatish Balay     entry->path    = fpath;
212e5c89e4eSSatish Balay     entry->rname   = fname;
213e5c89e4eSSatish Balay     entry->routine = fnc;
214e5c89e4eSSatish Balay     entry->next    = 0;
215e5c89e4eSSatish Balay     *fl            = entry;
216e5c89e4eSSatish Balay 
217e5c89e4eSSatish Balay     /* add this new list to list of all lists */
218e5c89e4eSSatish Balay     if (!dlallhead) {
219e5c89e4eSSatish Balay       dlallhead        = *fl;
220e5c89e4eSSatish Balay       (*fl)->next_list = 0;
221e5c89e4eSSatish Balay     } else {
222e5c89e4eSSatish Balay       ne               = dlallhead;
223e5c89e4eSSatish Balay       dlallhead        = *fl;
224e5c89e4eSSatish Balay       (*fl)->next_list = ne;
225e5c89e4eSSatish Balay     }
226e5c89e4eSSatish Balay   } else {
227e5c89e4eSSatish Balay     /* search list to see if it is already there */
228e5c89e4eSSatish Balay     ne = *fl;
229e5c89e4eSSatish Balay     while (ne) {
230ace3abfcSBarry Smith       PetscBool founddup;
231e5c89e4eSSatish Balay 
232e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
233e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
234140e18c1SBarry Smith         ierr = PetscFunctionListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr);
235503cfb0cSBarry Smith         ierr = PetscFree(ne->path);CHKERRQ(ierr);
236503cfb0cSBarry Smith         ierr = PetscFree(ne->rname);CHKERRQ(ierr);
237a297a907SKarl Rupp 
238e5c89e4eSSatish Balay         ne->path    = fpath;
239e5c89e4eSSatish Balay         ne->rname   = fname;
240e5c89e4eSSatish Balay         ne->routine = fnc;
241e5c89e4eSSatish Balay         PetscFunctionReturn(0);
242e5c89e4eSSatish Balay       }
243a297a907SKarl Rupp       if (ne->next) ne = ne->next;
244a297a907SKarl Rupp       else break;
245e5c89e4eSSatish Balay     }
246e5c89e4eSSatish Balay     /* create new entry and add to end of list */
247140e18c1SBarry Smith     ierr           = PetscNew(struct _n_PetscFunctionList,&entry);CHKERRQ(ierr);
248e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
249140e18c1SBarry Smith     ierr           = PetscFunctionListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr);
250e5c89e4eSSatish Balay     entry->path    = fpath;
251e5c89e4eSSatish Balay     entry->rname   = fname;
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 
269140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), 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);
27860154eb2SBarry Smith   if (!dlallhead) PetscFunctionReturn(0);
279e5c89e4eSSatish Balay 
280e5c89e4eSSatish Balay   /*
281e5c89e4eSSatish Balay        Remove this entry from the master DL list (if it is in it)
282e5c89e4eSSatish Balay   */
2831441b1d3SBarry Smith   if (dlallhead == *fl) {
284a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
285a297a907SKarl Rupp     else dlallhead = 0;
286e5c89e4eSSatish Balay   } else {
2871441b1d3SBarry Smith     while (tmp->next_list != *fl) {
288e5c89e4eSSatish Balay       tmp = tmp->next_list;
289e5c89e4eSSatish Balay       if (!tmp->next_list) break;
290e5c89e4eSSatish Balay     }
291e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
292e5c89e4eSSatish Balay   }
293e5c89e4eSSatish Balay 
294e5c89e4eSSatish Balay   /* free this list */
2951441b1d3SBarry Smith   entry = *fl;
296e5c89e4eSSatish Balay   while (entry) {
297e5c89e4eSSatish Balay     next  = entry->next;
298503cfb0cSBarry Smith     ierr  = PetscFree(entry->path);CHKERRQ(ierr);
299e5c89e4eSSatish Balay     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
300e5c89e4eSSatish Balay     ierr  = PetscFree(entry->rname);CHKERRQ(ierr);
301e5c89e4eSSatish Balay     ierr  = PetscFree(entry);CHKERRQ(ierr);
302e5c89e4eSSatish Balay     entry = next;
303e5c89e4eSSatish Balay   }
3041441b1d3SBarry Smith   *fl = 0;
305e5c89e4eSSatish Balay   PetscFunctionReturn(0);
306e5c89e4eSSatish Balay }
307e5c89e4eSSatish Balay 
308e5c89e4eSSatish Balay /*
309e5c89e4eSSatish Balay    Destroys all the function lists that anyone has every registered, such as KSPList, VecList, etc.
310e5c89e4eSSatish Balay */
311e5c89e4eSSatish Balay #undef __FUNCT__
312140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroyAll"
313140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroyAll(void)
314e5c89e4eSSatish Balay {
315140e18c1SBarry Smith   PetscFunctionList tmp2,tmp1 = dlallhead;
316e5c89e4eSSatish Balay   PetscErrorCode    ierr;
317e5c89e4eSSatish Balay 
318e5c89e4eSSatish Balay   PetscFunctionBegin;
319e5c89e4eSSatish Balay   while (tmp1) {
320e5c89e4eSSatish Balay     tmp2 = tmp1->next_list;
321140e18c1SBarry Smith     ierr = PetscFunctionListDestroy(&tmp1);CHKERRQ(ierr);
322e5c89e4eSSatish Balay     tmp1 = tmp2;
323e5c89e4eSSatish Balay   }
324e5c89e4eSSatish Balay   dlallhead = 0;
325e5c89e4eSSatish Balay   PetscFunctionReturn(0);
326e5c89e4eSSatish Balay }
327e5c89e4eSSatish Balay 
328e5c89e4eSSatish Balay #undef __FUNCT__
329140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListFind"
330e5c89e4eSSatish Balay /*@C
331140e18c1SBarry Smith     PetscFunctionListFind - Given a name, finds the matching routine.
332e5c89e4eSSatish Balay 
333e5c89e4eSSatish Balay     Input Parameters:
3341d280d73SBarry Smith +   fl   - pointer to list
3351d280d73SBarry Smith .   comm - processors looking for routine
3364b91b6eaSBarry Smith .   name - name string
3374b91b6eaSBarry Smith -   searchlibraries - if not found in the list then search the dynamic libraries and executable for the symbol
338e5c89e4eSSatish Balay 
339e5c89e4eSSatish Balay     Output Parameters:
340e5c89e4eSSatish Balay .   r - the routine
341e5c89e4eSSatish Balay 
342e5c89e4eSSatish Balay     Level: developer
343e5c89e4eSSatish Balay 
344140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
345e5c89e4eSSatish Balay @*/
346140e18c1SBarry Smith PetscErrorCode  PetscFunctionListFind(MPI_Comm comm,PetscFunctionList fl,const char name[],PetscBool searchlibraries,void (**r)(void))
347e5c89e4eSSatish Balay {
348140e18c1SBarry Smith   PetscFunctionList entry = fl;
349e5c89e4eSSatish Balay   PetscErrorCode    ierr;
350e5c89e4eSSatish Balay   char              *function,*path;
351ace3abfcSBarry Smith   PetscBool         flg,f1,f2,f3;
3520598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
353e5c89e4eSSatish Balay   char              *newpath;
354e5c89e4eSSatish Balay #endif
355e5c89e4eSSatish Balay 
356e5c89e4eSSatish Balay   PetscFunctionBegin;
357e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
358e5c89e4eSSatish Balay 
359e5c89e4eSSatish Balay   *r = 0;
360140e18c1SBarry Smith   ierr = PetscFunctionListGetPathAndFunction(name,&path,&function);CHKERRQ(ierr);
361e5c89e4eSSatish Balay 
362e5c89e4eSSatish Balay   /*
363e5c89e4eSSatish Balay         If path then append it to search libraries
364e5c89e4eSSatish Balay   */
3650598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
366e5c89e4eSSatish Balay   if (path) {
367d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(comm,&PetscDLLibrariesLoaded,path);CHKERRQ(ierr);
368e5c89e4eSSatish Balay   }
369e5c89e4eSSatish Balay #endif
370e5c89e4eSSatish Balay 
371e5c89e4eSSatish Balay   while (entry) {
372e5c89e4eSSatish Balay     flg = PETSC_FALSE;
373e5c89e4eSSatish Balay     if (path && entry->path) {
374e5c89e4eSSatish Balay       ierr = PetscStrcmp(path,entry->path,&f1);CHKERRQ(ierr);
375e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->rname,&f2);CHKERRQ(ierr);
376e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->name,&f3);CHKERRQ(ierr);
377ace3abfcSBarry Smith       flg  =  (PetscBool) ((f1 && f2) || (f1 && f3));
378e5c89e4eSSatish Balay     } else if (!path) {
379e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->name,&f1);CHKERRQ(ierr);
380e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->rname,&f2);CHKERRQ(ierr);
381ace3abfcSBarry Smith       flg  =  (PetscBool) (f1 || f2);
382e5c89e4eSSatish Balay     } else {
383e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->name,&flg);CHKERRQ(ierr);
384e5c89e4eSSatish Balay       if (flg) {
385e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
386e5c89e4eSSatish Balay         ierr = PetscStrallocpy(entry->rname,&function);CHKERRQ(ierr);
387e5c89e4eSSatish Balay       } else {
388e5c89e4eSSatish Balay         ierr = PetscStrcmp(function,entry->rname,&flg);CHKERRQ(ierr);
389e5c89e4eSSatish Balay       }
390e5c89e4eSSatish Balay     }
391e5c89e4eSSatish Balay 
392e5c89e4eSSatish Balay     if (flg) {
393e5c89e4eSSatish Balay       if (entry->routine) {
394e5c89e4eSSatish Balay         *r   = entry->routine;
395503cfb0cSBarry Smith         ierr = PetscFree(path);CHKERRQ(ierr);
396e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
397e5c89e4eSSatish Balay         PetscFunctionReturn(0);
398e5c89e4eSSatish Balay       }
3995629bde7SJed Brown       if (!(entry->rname && entry->rname[0])) { /* The entry has been cleared */
4005629bde7SJed Brown         ierr = PetscFree(function);CHKERRQ(ierr);
4015629bde7SJed Brown         PetscFunctionReturn(0);
4025629bde7SJed Brown       }
403e5c89e4eSSatish Balay       if ((path && entry->path && f3) || (!path && f1)) { /* convert name of function (alias) to actual function name */
404e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
405e5c89e4eSSatish Balay         ierr = PetscStrallocpy(entry->rname,&function);CHKERRQ(ierr);
406e5c89e4eSSatish Balay       }
407e5c89e4eSSatish Balay 
408e5c89e4eSSatish Balay       /* it is not yet in memory so load from dynamic library */
4090598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
410e5c89e4eSSatish Balay       newpath = path;
411e5c89e4eSSatish Balay       if (!path) newpath = entry->path;
412d44a1e48SBarry Smith       ierr = PetscDLLibrarySym(comm,&PetscDLLibrariesLoaded,newpath,entry->rname,(void**)r);CHKERRQ(ierr);
413e5c89e4eSSatish Balay       if (*r) {
414e5c89e4eSSatish Balay         entry->routine = *r;
415a297a907SKarl Rupp 
416503cfb0cSBarry Smith         ierr = PetscFree(path);CHKERRQ(ierr);
417e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
418e5c89e4eSSatish Balay         PetscFunctionReturn(0);
419e5c89e4eSSatish Balay       }
420e5c89e4eSSatish Balay #endif
421e5c89e4eSSatish Balay     }
422e5c89e4eSSatish Balay     entry = entry->next;
423e5c89e4eSSatish Balay   }
424e5c89e4eSSatish Balay 
4250598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
4264b91b6eaSBarry Smith   if (searchlibraries) {
427e5c89e4eSSatish Balay     /* Function never registered; try for it anyway */
428d44a1e48SBarry Smith     ierr = PetscDLLibrarySym(comm,&PetscDLLibrariesLoaded,path,function,(void**)r);CHKERRQ(ierr);
429503cfb0cSBarry Smith     ierr = PetscFree(path);CHKERRQ(ierr);
430e5c89e4eSSatish Balay     if (*r) {
431140e18c1SBarry Smith       ierr = PetscFunctionListAdd(comm,&fl,name,name,*r);CHKERRQ(ierr);
432e5c89e4eSSatish Balay     }
4334b91b6eaSBarry Smith   }
434e5c89e4eSSatish Balay #endif
435e5c89e4eSSatish Balay   ierr = PetscFree(function);CHKERRQ(ierr);
436e5c89e4eSSatish Balay   PetscFunctionReturn(0);
437e5c89e4eSSatish Balay }
438e5c89e4eSSatish Balay 
439e5c89e4eSSatish Balay #undef __FUNCT__
440140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListView"
441e5c89e4eSSatish Balay /*@
442140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
443e5c89e4eSSatish Balay 
444e5c89e4eSSatish Balay    Collective over MPI_Comm
445e5c89e4eSSatish Balay 
446e5c89e4eSSatish Balay    Input Parameters:
447e5c89e4eSSatish Balay +  list - the list of functions
448e5c89e4eSSatish Balay -  viewer - currently ignored
449e5c89e4eSSatish Balay 
450e5c89e4eSSatish Balay    Level: developer
451e5c89e4eSSatish Balay 
452140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionListPrintTypes(), PetscFunctionList
453e5c89e4eSSatish Balay @*/
454140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
455e5c89e4eSSatish Balay {
456e5c89e4eSSatish Balay   PetscErrorCode ierr;
457ace3abfcSBarry Smith   PetscBool      iascii;
458e5c89e4eSSatish Balay 
459e5c89e4eSSatish Balay   PetscFunctionBegin;
460e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
461e5c89e4eSSatish Balay   PetscValidPointer(list,1);
4620700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
463e5c89e4eSSatish Balay 
464251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
465e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
466e5c89e4eSSatish Balay 
467e5c89e4eSSatish Balay   while (list) {
468e5c89e4eSSatish Balay     if (list->path) {
469e5c89e4eSSatish Balay       ierr = PetscViewerASCIIPrintf(viewer," %s %s %s\n",list->path,list->name,list->rname);CHKERRQ(ierr);
470e5c89e4eSSatish Balay     } else {
471e5c89e4eSSatish Balay       ierr = PetscViewerASCIIPrintf(viewer," %s %s\n",list->name,list->rname);CHKERRQ(ierr);
472e5c89e4eSSatish Balay     }
473e5c89e4eSSatish Balay     list = list->next;
474e5c89e4eSSatish Balay   }
475e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
476e5c89e4eSSatish Balay   PetscFunctionReturn(0);
477e5c89e4eSSatish Balay }
478e5c89e4eSSatish Balay 
479e5c89e4eSSatish Balay #undef __FUNCT__
480140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGet"
481065533a5SJed Brown /*@C
482140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
483e5c89e4eSSatish Balay          by help etc.
484e5c89e4eSSatish Balay 
485e5c89e4eSSatish Balay    Collective over MPI_Comm
486e5c89e4eSSatish Balay 
487e5c89e4eSSatish Balay    Input Parameter:
488e5c89e4eSSatish Balay .  list   - list of types
489e5c89e4eSSatish Balay 
490e5c89e4eSSatish Balay    Output Parameter:
491e5c89e4eSSatish Balay +  array - array of names
492e5c89e4eSSatish Balay -  n - length of array
493e5c89e4eSSatish Balay 
494e5c89e4eSSatish Balay    Notes:
495e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
496e5c89e4eSSatish Balay     not copied so should not be freed.
497e5c89e4eSSatish Balay 
498e5c89e4eSSatish Balay    Level: developer
499e5c89e4eSSatish Balay 
500140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
501e5c89e4eSSatish Balay @*/
502140e18c1SBarry Smith PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
503e5c89e4eSSatish Balay {
504e5c89e4eSSatish Balay   PetscErrorCode    ierr;
505e5c89e4eSSatish Balay   PetscInt          count = 0;
506140e18c1SBarry Smith   PetscFunctionList klist = list;
507e5c89e4eSSatish Balay 
508e5c89e4eSSatish Balay   PetscFunctionBegin;
509e5c89e4eSSatish Balay   while (list) {
510e5c89e4eSSatish Balay     list = list->next;
511e5c89e4eSSatish Balay     count++;
512e5c89e4eSSatish Balay   }
513e5c89e4eSSatish Balay   ierr  = PetscMalloc((count+1)*sizeof(char*),array);CHKERRQ(ierr);
514e5c89e4eSSatish Balay   count = 0;
515e5c89e4eSSatish Balay   while (klist) {
516e5c89e4eSSatish Balay     (*array)[count] = klist->name;
517e5c89e4eSSatish Balay     klist           = klist->next;
518e5c89e4eSSatish Balay     count++;
519e5c89e4eSSatish Balay   }
520e5c89e4eSSatish Balay   (*array)[count] = 0;
521e5c89e4eSSatish Balay   *n              = count+1;
522e5c89e4eSSatish Balay   PetscFunctionReturn(0);
523e5c89e4eSSatish Balay }
524e5c89e4eSSatish Balay 
525e5c89e4eSSatish Balay 
526e5c89e4eSSatish Balay #undef __FUNCT__
527140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListPrintTypes"
528e5c89e4eSSatish Balay /*@C
529140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
530e5c89e4eSSatish Balay 
531e5c89e4eSSatish Balay    Collective over MPI_Comm
532e5c89e4eSSatish Balay 
533e5c89e4eSSatish Balay    Input Parameters:
534e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
535e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
536e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
537e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
538e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
539e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
5403cc1e11dSBarry Smith .  list   - list of types
5413cc1e11dSBarry Smith -  def - default (current) value
542e5c89e4eSSatish Balay 
543e5c89e4eSSatish Balay    Level: developer
544e5c89e4eSSatish Balay 
545140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
546e5c89e4eSSatish Balay @*/
547140e18c1SBarry 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[])
548e5c89e4eSSatish Balay {
549e5c89e4eSSatish Balay   PetscErrorCode ierr;
550e5c89e4eSSatish Balay   PetscInt       count = 0;
551e5c89e4eSSatish Balay   char           p[64];
552e5c89e4eSSatish Balay 
553e5c89e4eSSatish Balay   PetscFunctionBegin;
554da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
555e5c89e4eSSatish Balay 
556e5c89e4eSSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
557e5c89e4eSSatish Balay   if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);}
5583cc1e11dSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr);
559e5c89e4eSSatish Balay 
560e5c89e4eSSatish Balay   while (list) {
561e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
562e5c89e4eSSatish Balay     list = list->next;
563e5c89e4eSSatish Balay     count++;
564e5c89e4eSSatish Balay     if (count == 8) {ierr = PetscFPrintf(comm,fd,"\n     ");CHKERRQ(ierr);}
565e5c89e4eSSatish Balay   }
566e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
567e5c89e4eSSatish Balay   PetscFunctionReturn(0);
568e5c89e4eSSatish Balay }
569e5c89e4eSSatish Balay 
570e5c89e4eSSatish Balay #undef __FUNCT__
571140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDuplicate"
572e5c89e4eSSatish Balay /*@
573140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
574e5c89e4eSSatish Balay 
575e5c89e4eSSatish Balay     Input Parameters:
576e5c89e4eSSatish Balay .   fl   - pointer to list
577e5c89e4eSSatish Balay 
578e5c89e4eSSatish Balay     Output Parameters:
579e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
580e5c89e4eSSatish Balay 
581e5c89e4eSSatish Balay     Level: developer
582e5c89e4eSSatish Balay 
583140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
584e5c89e4eSSatish Balay 
585e5c89e4eSSatish Balay @*/
586140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
587e5c89e4eSSatish Balay {
588e5c89e4eSSatish Balay   PetscErrorCode ierr;
589e5c89e4eSSatish Balay   char           path[PETSC_MAX_PATH_LEN];
590e5c89e4eSSatish Balay 
591e5c89e4eSSatish Balay   PetscFunctionBegin;
592e5c89e4eSSatish Balay   while (fl) {
593e5c89e4eSSatish Balay     /* this is silly, rebuild the complete pathname */
594e5c89e4eSSatish Balay     if (fl->path) {
595e5c89e4eSSatish Balay       ierr = PetscStrcpy(path,fl->path);CHKERRQ(ierr);
596e5c89e4eSSatish Balay       ierr = PetscStrcat(path,":");CHKERRQ(ierr);
597e5c89e4eSSatish Balay       ierr = PetscStrcat(path,fl->name);CHKERRQ(ierr);
598e5c89e4eSSatish Balay     } else {
599e5c89e4eSSatish Balay       ierr = PetscStrcpy(path,fl->name);CHKERRQ(ierr);
600e5c89e4eSSatish Balay     }
601140e18c1SBarry Smith     ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,nl,path,fl->rname,fl->routine);CHKERRQ(ierr);
602e5c89e4eSSatish Balay     fl   = fl->next;
603e5c89e4eSSatish Balay   }
604e5c89e4eSSatish Balay   PetscFunctionReturn(0);
605e5c89e4eSSatish Balay }
606e5c89e4eSSatish Balay 
607e5c89e4eSSatish Balay 
608e5c89e4eSSatish Balay #undef __FUNCT__
609140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListConcat"
610e5c89e4eSSatish Balay /*
611140e18c1SBarry Smith     PetscFunctionListConcat - joins name of a libary, and the path where it is located
612e5c89e4eSSatish Balay     into a single string.
613e5c89e4eSSatish Balay 
614e5c89e4eSSatish Balay     Input Parameters:
615e5c89e4eSSatish Balay .   path   - path to the library name.
616e5c89e4eSSatish Balay .   name   - name of the library
617e5c89e4eSSatish Balay 
618e5c89e4eSSatish Balay     Output Parameters:
619e5c89e4eSSatish Balay .   fullname - the name that is the union of the path and the library name,
620e5c89e4eSSatish Balay                delimited by a semicolon, i.e., path:name
621e5c89e4eSSatish Balay 
622e5c89e4eSSatish Balay     Notes:
623e5c89e4eSSatish Balay     If the path is NULL, assumes that the name, specified also includes
624e5c89e4eSSatish Balay     the path as path:name
625e5c89e4eSSatish Balay 
626e5c89e4eSSatish Balay */
627140e18c1SBarry Smith PetscErrorCode  PetscFunctionListConcat(const char path[],const char name[],char fullname[])
628e5c89e4eSSatish Balay {
629e5c89e4eSSatish Balay   PetscErrorCode ierr;
6305fd66863SKarl Rupp 
631e5c89e4eSSatish Balay   PetscFunctionBegin;
632e5c89e4eSSatish Balay   if (path) {
633e5c89e4eSSatish Balay     ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr);
634e5c89e4eSSatish Balay     ierr = PetscStrcat(fullname,":");CHKERRQ(ierr);
635e5c89e4eSSatish Balay     ierr = PetscStrcat(fullname,name);CHKERRQ(ierr);
636e5c89e4eSSatish Balay   } else {
637e5c89e4eSSatish Balay     ierr = PetscStrcpy(fullname,name);CHKERRQ(ierr);
638e5c89e4eSSatish Balay   }
639e5c89e4eSSatish Balay   PetscFunctionReturn(0);
640e5c89e4eSSatish Balay }
641