xref: /petsc/src/sys/dll/reg.c (revision 140e18c1ab5d6e82cb97419c1b69bc894c9508d2)
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 */
6c6db04a5SJed Brown #include <petscsys.h>           /*I "petscsys.h" I*/
7e5c89e4eSSatish Balay 
8e5c89e4eSSatish Balay #undef __FUNCT__
9*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGetPathAndFunction"
10*140e18c1SBarry 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);
178caf3d72SBarry Smith   work[sizeof(work) - 1] = 0;
18e5c89e4eSSatish Balay   ierr = PetscStrchr(work,':',&lfunction);CHKERRQ(ierr);
19e5c89e4eSSatish Balay   if (lfunction != work && lfunction && lfunction[1] != ':') {
20e5c89e4eSSatish Balay     lfunction[0] = 0;
21e5c89e4eSSatish Balay     ierr = PetscStrallocpy(work,path);CHKERRQ(ierr);
22e5c89e4eSSatish Balay     ierr = PetscStrallocpy(lfunction+1,function);CHKERRQ(ierr);
23e5c89e4eSSatish Balay   } else {
24e5c89e4eSSatish Balay     *path = 0;
25e5c89e4eSSatish Balay     ierr = PetscStrallocpy(name,function);CHKERRQ(ierr);
26e5c89e4eSSatish Balay   }
27e5c89e4eSSatish Balay   PetscFunctionReturn(0);
28e5c89e4eSSatish Balay }
29e5c89e4eSSatish Balay 
303fa76a5bSLisandro Dalcin /*
313fa76a5bSLisandro Dalcin     This is the default list used by PETSc with the PetscDLLibrary register routines
323fa76a5bSLisandro Dalcin */
33d44a1e48SBarry Smith PetscDLLibrary PetscDLLibrariesLoaded = 0;
343fa76a5bSLisandro Dalcin 
35ebd79076SLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
36ebd79076SLisandro Dalcin 
37e5c89e4eSSatish Balay #undef __FUNCT__
38487e5849SBarry Smith #define __FUNCT__ "PetscLoadDynamicLibrary"
397087cfbeSBarry Smith static PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
40487e5849SBarry Smith {
41487e5849SBarry Smith   char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
42487e5849SBarry Smith   PetscErrorCode ierr;
43487e5849SBarry Smith 
44487e5849SBarry Smith   PetscFunctionBegin;
45487e5849SBarry Smith   ierr = PetscStrcpy(libs,"${PETSC_LIB_DIR}/libpetsc");CHKERRQ(ierr);
46487e5849SBarry Smith   ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
47487e5849SBarry Smith   ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
48487e5849SBarry Smith   if (*found) {
49d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
50487e5849SBarry Smith   } else {
51487e5849SBarry Smith     ierr = PetscStrcpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc");CHKERRQ(ierr);
52487e5849SBarry Smith     ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
53487e5849SBarry Smith     ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
54487e5849SBarry Smith     if (*found) {
55d44a1e48SBarry Smith       ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
56487e5849SBarry Smith     }
57487e5849SBarry Smith   }
58487e5849SBarry Smith   PetscFunctionReturn(0);
59487e5849SBarry Smith }
60487e5849SBarry Smith 
613fa76a5bSLisandro Dalcin #endif
623fa76a5bSLisandro Dalcin 
63487e5849SBarry Smith #undef __FUNCT__
64e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize_DynamicLibraries"
65e5c89e4eSSatish Balay /*
66e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
67e5c89e4eSSatish Balay     search path.
68e5c89e4eSSatish Balay */
697087cfbeSBarry Smith PetscErrorCode  PetscInitialize_DynamicLibraries(void)
70e5c89e4eSSatish Balay {
71487e5849SBarry Smith   char           *libname[32];
72e5c89e4eSSatish Balay   PetscErrorCode ierr;
73e5c89e4eSSatish Balay   PetscInt       nmax,i;
743fa76a5bSLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
75ace3abfcSBarry Smith   PetscBool      found;
763fa76a5bSLisandro Dalcin #endif
77e5c89e4eSSatish Balay 
7860154eb2SBarry Smith   PetscFunctionBegin;
79e5c89e4eSSatish Balay   nmax = 32;
80e5c89e4eSSatish Balay   ierr = PetscOptionsGetStringArray(PETSC_NULL,"-dll_prepend",libname,&nmax,PETSC_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 
863fa76a5bSLisandro Dalcin #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
873fa76a5bSLisandro Dalcin   /*
883fa76a5bSLisandro Dalcin       This just initializes the most basic PETSc stuff.
893fa76a5bSLisandro Dalcin 
903fa76a5bSLisandro Dalcin     The classes, from PetscDraw to PetscTS, are initialized the first
913fa76a5bSLisandro Dalcin     time an XXCreate() is called.
923fa76a5bSLisandro Dalcin   */
9360154eb2SBarry Smith   ierr = PetscSysInitializePackage(PETSC_NULL);CHKERRQ(ierr);
943fa76a5bSLisandro Dalcin #else
9560154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
96487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
97e32f2f54SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
9860154eb2SBarry Smith #else
9960154eb2SBarry Smith   ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
10060154eb2SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
101487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
102e32f2f54SBarry 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!");
103487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
104e32f2f54SBarry 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!");
105487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
106e32f2f54SBarry 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!");
10760154eb2SBarry Smith   ierr = PetscLoadDynamicLibrary("characteristic",&found);CHKERRQ(ierr);
10860154eb2SBarry 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!");
109487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
110e32f2f54SBarry 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!");
111487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
112e32f2f54SBarry 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!");
113487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
114e32f2f54SBarry 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!");
115bb84e0fdSBarry Smith #endif
116e5c89e4eSSatish Balay 
117487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("mesh",&found);CHKERRQ(ierr);
118487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("contrib",&found);CHKERRQ(ierr);
1193fa76a5bSLisandro Dalcin #endif
120e5c89e4eSSatish Balay 
121e5c89e4eSSatish Balay   nmax = 32;
122e5c89e4eSSatish Balay   ierr = PetscOptionsGetStringArray(PETSC_NULL,"-dll_append",libname,&nmax,PETSC_NULL);CHKERRQ(ierr);
123e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
124d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
125e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
126e5c89e4eSSatish Balay   }
1273fa76a5bSLisandro Dalcin 
128e5c89e4eSSatish Balay   PetscFunctionReturn(0);
129e5c89e4eSSatish Balay }
130e5c89e4eSSatish Balay 
131e5c89e4eSSatish Balay #undef __FUNCT__
132e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize_DynamicLibraries"
133ebd79076SLisandro Dalcin /*
134ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
135ebd79076SLisandro Dalcin */
136e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void)
137e5c89e4eSSatish Balay {
138ebd79076SLisandro Dalcin   PetscErrorCode ierr;
139ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
140e5c89e4eSSatish Balay 
141ebd79076SLisandro Dalcin   PetscFunctionBegin;
142acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-dll_view",&flg,PETSC_NULL);CHKERRQ(ierr);
143d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
144d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
145d44a1e48SBarry Smith   PetscDLLibrariesLoaded = 0;
146e5c89e4eSSatish Balay   PetscFunctionReturn(0);
147e5c89e4eSSatish Balay }
148e5c89e4eSSatish Balay 
149e4d1774bSDmitry Karpeev 
150e4d1774bSDmitry Karpeev 
151e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
152*140e18c1SBarry Smith struct _n_PetscFunctionList {
153e5c89e4eSSatish Balay   void               (*routine)(void);   /* the routine */
154e5c89e4eSSatish Balay   char               *path;              /* path of link library containing routine */
155e5c89e4eSSatish Balay   char               *name;              /* string to identify routine */
156e5c89e4eSSatish Balay   char               *rname;             /* routine name in dynamic library */
157*140e18c1SBarry Smith   PetscFunctionList  next;               /* next pointer */
158*140e18c1SBarry Smith   PetscFunctionList  next_list;          /* used to maintain list of all lists for freeing */
159e5c89e4eSSatish Balay };
160e5c89e4eSSatish Balay 
161e5c89e4eSSatish Balay /*
162*140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
163e5c89e4eSSatish Balay */
164*140e18c1SBarry Smith static PetscFunctionList   dlallhead = 0;
165e5c89e4eSSatish Balay 
166e5c89e4eSSatish Balay #undef __FUNCT__
167*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListAdd"
168e5c89e4eSSatish Balay /*@C
169*140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
170e5c89e4eSSatish Balay    specified registry.
171e5c89e4eSSatish Balay 
1726d75e210SBarry Smith      Formally Collective on MPI_Comm
173e5c89e4eSSatish Balay 
174e5c89e4eSSatish Balay    Input Parameters:
1756d75e210SBarry Smith +  comm  - the comm where this exists (currently not used)
1766d75e210SBarry Smith .  fl    - pointer registry
177e5c89e4eSSatish Balay .  name  - string to identify routine
178e5c89e4eSSatish Balay .  rname - routine name in dynamic library
179e5c89e4eSSatish Balay -  fnc   - function pointer (optional if using dynamic libraries)
180e5c89e4eSSatish Balay 
181e5c89e4eSSatish Balay    Notes:
182e5c89e4eSSatish Balay    To remove a registered routine, pass in a PETSC_NULL rname and fnc().
183e5c89e4eSSatish Balay 
184e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
185e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
186e5c89e4eSSatish Balay    for that particular component (e.g., SNESRegisterDynamic()) instead of
187*140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
188e5c89e4eSSatish Balay 
189e5c89e4eSSatish Balay    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environmental variable}
190e5c89e4eSSatish Balay   occuring in pathname will be replaced with appropriate values.
191e5c89e4eSSatish Balay 
192e5c89e4eSSatish Balay    Level: developer
193e5c89e4eSSatish Balay 
194*140e18c1SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegisterDynamic(), KSPRegisterDynamic(),
195*140e18c1SBarry Smith           PCRegisterDynamic(), TSRegisterDynamic(), PetscFunctionList
196e5c89e4eSSatish Balay @*/
197*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListAdd(MPI_Comm comm,PetscFunctionList *fl,const char name[],const char rname[],void (*fnc)(void))
198e5c89e4eSSatish Balay {
199*140e18c1SBarry Smith   PetscFunctionList entry,ne;
200e5c89e4eSSatish Balay   PetscErrorCode    ierr;
201e5c89e4eSSatish Balay   char              *fpath,*fname;
202e5c89e4eSSatish Balay 
203e5c89e4eSSatish Balay   PetscFunctionBegin;
204e5c89e4eSSatish Balay   if (!*fl) {
205*140e18c1SBarry Smith     ierr           = PetscNew(struct _n_PetscFunctionList,&entry);CHKERRQ(ierr);
206e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
207*140e18c1SBarry Smith     ierr           = PetscFunctionListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr);
208e5c89e4eSSatish Balay     entry->path    = fpath;
209e5c89e4eSSatish Balay     entry->rname   = fname;
210e5c89e4eSSatish Balay     entry->routine = fnc;
211e5c89e4eSSatish Balay     entry->next    = 0;
212e5c89e4eSSatish Balay     *fl = entry;
213e5c89e4eSSatish Balay 
214e5c89e4eSSatish Balay     /* add this new list to list of all lists */
215e5c89e4eSSatish Balay     if (!dlallhead) {
216e5c89e4eSSatish Balay       dlallhead        = *fl;
217e5c89e4eSSatish Balay       (*fl)->next_list = 0;
218e5c89e4eSSatish Balay     } else {
219e5c89e4eSSatish Balay       ne               = dlallhead;
220e5c89e4eSSatish Balay       dlallhead        = *fl;
221e5c89e4eSSatish Balay       (*fl)->next_list = ne;
222e5c89e4eSSatish Balay     }
223e5c89e4eSSatish Balay   } else {
224e5c89e4eSSatish Balay     /* search list to see if it is already there */
225e5c89e4eSSatish Balay     ne = *fl;
226e5c89e4eSSatish Balay     while (ne) {
227ace3abfcSBarry Smith       PetscBool  founddup;
228e5c89e4eSSatish Balay 
229e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
230e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
231*140e18c1SBarry Smith         ierr = PetscFunctionListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr);
232503cfb0cSBarry Smith         ierr = PetscFree(ne->path);CHKERRQ(ierr);
233503cfb0cSBarry Smith         ierr = PetscFree(ne->rname);CHKERRQ(ierr);
234e5c89e4eSSatish Balay         ne->path    = fpath;
235e5c89e4eSSatish Balay         ne->rname   = fname;
236e5c89e4eSSatish Balay         ne->routine = fnc;
237e5c89e4eSSatish Balay         PetscFunctionReturn(0);
238e5c89e4eSSatish Balay       }
239e5c89e4eSSatish Balay       if (ne->next) ne = ne->next; else break;
240e5c89e4eSSatish Balay     }
241e5c89e4eSSatish Balay     /* create new entry and add to end of list */
242*140e18c1SBarry Smith     ierr           = PetscNew(struct _n_PetscFunctionList,&entry);CHKERRQ(ierr);
243e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
244*140e18c1SBarry Smith     ierr           = PetscFunctionListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr);
245e5c89e4eSSatish Balay     entry->path    = fpath;
246e5c89e4eSSatish Balay     entry->rname   = fname;
247e5c89e4eSSatish Balay     entry->routine = fnc;
248e5c89e4eSSatish Balay     entry->next    = 0;
249e5c89e4eSSatish Balay     ne->next       = entry;
250e5c89e4eSSatish Balay   }
251e5c89e4eSSatish Balay   PetscFunctionReturn(0);
252e5c89e4eSSatish Balay }
253e5c89e4eSSatish Balay 
254e5c89e4eSSatish Balay #undef __FUNCT__
255*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroy"
256e5c89e4eSSatish Balay /*@
257*140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
258e5c89e4eSSatish Balay 
259e5c89e4eSSatish Balay     Input Parameter:
260e5c89e4eSSatish Balay .   fl  - pointer to list
261e5c89e4eSSatish Balay 
262e5c89e4eSSatish Balay     Level: developer
263e5c89e4eSSatish Balay 
264*140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
265e5c89e4eSSatish Balay @*/
266*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
267e5c89e4eSSatish Balay {
268*140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
269e5c89e4eSSatish Balay   PetscErrorCode    ierr;
270e5c89e4eSSatish Balay 
271e5c89e4eSSatish Balay   PetscFunctionBegin;
2721441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
27360154eb2SBarry Smith   if (!dlallhead) PetscFunctionReturn(0);
274e5c89e4eSSatish Balay 
275e5c89e4eSSatish Balay   /*
276e5c89e4eSSatish Balay        Remove this entry from the master DL list (if it is in it)
277e5c89e4eSSatish Balay   */
2781441b1d3SBarry Smith   if (dlallhead == *fl) {
279e5c89e4eSSatish Balay     if (dlallhead->next_list) {
280e5c89e4eSSatish Balay       dlallhead = dlallhead->next_list;
281e5c89e4eSSatish Balay     } else {
282e5c89e4eSSatish Balay       dlallhead = 0;
283e5c89e4eSSatish Balay     }
284e5c89e4eSSatish Balay   } else {
2851441b1d3SBarry Smith     while (tmp->next_list != *fl) {
286e5c89e4eSSatish Balay       tmp = tmp->next_list;
287e5c89e4eSSatish Balay       if (!tmp->next_list) break;
288e5c89e4eSSatish Balay     }
289e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
290e5c89e4eSSatish Balay   }
291e5c89e4eSSatish Balay 
292e5c89e4eSSatish Balay   /* free this list */
2931441b1d3SBarry Smith   entry = *fl;
294e5c89e4eSSatish Balay   while (entry) {
295e5c89e4eSSatish Balay     next = entry->next;
296503cfb0cSBarry Smith     ierr = PetscFree(entry->path);CHKERRQ(ierr);
297e5c89e4eSSatish Balay     ierr = PetscFree(entry->name);CHKERRQ(ierr);
298e5c89e4eSSatish Balay     ierr = PetscFree(entry->rname);CHKERRQ(ierr);
299e5c89e4eSSatish Balay     ierr = PetscFree(entry);CHKERRQ(ierr);
300e5c89e4eSSatish Balay     entry = next;
301e5c89e4eSSatish Balay   }
3021441b1d3SBarry Smith   *fl = 0;
303e5c89e4eSSatish Balay   PetscFunctionReturn(0);
304e5c89e4eSSatish Balay }
305e5c89e4eSSatish Balay 
306e5c89e4eSSatish Balay /*
307e5c89e4eSSatish Balay    Destroys all the function lists that anyone has every registered, such as KSPList, VecList, etc.
308e5c89e4eSSatish Balay */
309e5c89e4eSSatish Balay #undef __FUNCT__
310*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroyAll"
311*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroyAll(void)
312e5c89e4eSSatish Balay {
313*140e18c1SBarry Smith   PetscFunctionList tmp2,tmp1 = dlallhead;
314e5c89e4eSSatish Balay   PetscErrorCode    ierr;
315e5c89e4eSSatish Balay 
316e5c89e4eSSatish Balay   PetscFunctionBegin;
317e5c89e4eSSatish Balay   while (tmp1) {
318e5c89e4eSSatish Balay     tmp2 = tmp1->next_list;
319*140e18c1SBarry Smith     ierr = PetscFunctionListDestroy(&tmp1);CHKERRQ(ierr);
320e5c89e4eSSatish Balay     tmp1 = tmp2;
321e5c89e4eSSatish Balay   }
322e5c89e4eSSatish Balay   dlallhead = 0;
323e5c89e4eSSatish Balay   PetscFunctionReturn(0);
324e5c89e4eSSatish Balay }
325e5c89e4eSSatish Balay 
326e5c89e4eSSatish Balay #undef __FUNCT__
327*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListFind"
328e5c89e4eSSatish Balay /*@C
329*140e18c1SBarry Smith     PetscFunctionListFind - Given a name, finds the matching routine.
330e5c89e4eSSatish Balay 
331e5c89e4eSSatish Balay     Input Parameters:
3321d280d73SBarry Smith +   fl   - pointer to list
3331d280d73SBarry Smith .   comm - processors looking for routine
3344b91b6eaSBarry Smith .   name - name string
3354b91b6eaSBarry Smith -   searchlibraries - if not found in the list then search the dynamic libraries and executable for the symbol
336e5c89e4eSSatish Balay 
337e5c89e4eSSatish Balay     Output Parameters:
338e5c89e4eSSatish Balay .   r - the routine
339e5c89e4eSSatish Balay 
340e5c89e4eSSatish Balay     Level: developer
341e5c89e4eSSatish Balay 
342*140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
343e5c89e4eSSatish Balay @*/
344*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListFind(MPI_Comm comm,PetscFunctionList fl,const char name[],PetscBool searchlibraries,void (**r)(void))
345e5c89e4eSSatish Balay {
346*140e18c1SBarry Smith   PetscFunctionList entry = fl;
347e5c89e4eSSatish Balay   PetscErrorCode    ierr;
348e5c89e4eSSatish Balay   char              *function,*path;
349ace3abfcSBarry Smith   PetscBool         flg,f1,f2,f3;
3500598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
351e5c89e4eSSatish Balay   char              *newpath;
352e5c89e4eSSatish Balay #endif
353e5c89e4eSSatish Balay 
354e5c89e4eSSatish Balay   PetscFunctionBegin;
355e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
356e5c89e4eSSatish Balay 
357e5c89e4eSSatish Balay   *r = 0;
358*140e18c1SBarry Smith   ierr = PetscFunctionListGetPathAndFunction(name,&path,&function);CHKERRQ(ierr);
359e5c89e4eSSatish Balay 
360e5c89e4eSSatish Balay   /*
361e5c89e4eSSatish Balay         If path then append it to search libraries
362e5c89e4eSSatish Balay   */
3630598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
364e5c89e4eSSatish Balay   if (path) {
365d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(comm,&PetscDLLibrariesLoaded,path);CHKERRQ(ierr);
366e5c89e4eSSatish Balay   }
367e5c89e4eSSatish Balay #endif
368e5c89e4eSSatish Balay 
369e5c89e4eSSatish Balay   while (entry) {
370e5c89e4eSSatish Balay     flg = PETSC_FALSE;
371e5c89e4eSSatish Balay     if (path && entry->path) {
372e5c89e4eSSatish Balay       ierr = PetscStrcmp(path,entry->path,&f1);CHKERRQ(ierr);
373e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->rname,&f2);CHKERRQ(ierr);
374e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->name,&f3);CHKERRQ(ierr);
375ace3abfcSBarry Smith       flg =  (PetscBool) ((f1 && f2) || (f1 && f3));
376e5c89e4eSSatish Balay     } else if (!path) {
377e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->name,&f1);CHKERRQ(ierr);
378e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->rname,&f2);CHKERRQ(ierr);
379ace3abfcSBarry Smith       flg =  (PetscBool) (f1 || f2);
380e5c89e4eSSatish Balay     } else {
381e5c89e4eSSatish Balay       ierr = PetscStrcmp(function,entry->name,&flg);CHKERRQ(ierr);
382e5c89e4eSSatish Balay       if (flg) {
383e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
384e5c89e4eSSatish Balay         ierr = PetscStrallocpy(entry->rname,&function);CHKERRQ(ierr);
385e5c89e4eSSatish Balay       } else {
386e5c89e4eSSatish Balay         ierr = PetscStrcmp(function,entry->rname,&flg);CHKERRQ(ierr);
387e5c89e4eSSatish Balay       }
388e5c89e4eSSatish Balay     }
389e5c89e4eSSatish Balay 
390e5c89e4eSSatish Balay     if (flg) {
391e5c89e4eSSatish Balay       if (entry->routine) {
392e5c89e4eSSatish Balay         *r   = entry->routine;
393503cfb0cSBarry Smith         ierr = PetscFree(path);CHKERRQ(ierr);
394e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
395e5c89e4eSSatish Balay         PetscFunctionReturn(0);
396e5c89e4eSSatish Balay       }
3975629bde7SJed Brown       if (!(entry->rname && entry->rname[0])) { /* The entry has been cleared */
3985629bde7SJed Brown         ierr = PetscFree(function);CHKERRQ(ierr);
3995629bde7SJed Brown         PetscFunctionReturn(0);
4005629bde7SJed Brown       }
401e5c89e4eSSatish Balay       if ((path && entry->path && f3) || (!path && f1)) { /* convert name of function (alias) to actual function name */
402e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
403e5c89e4eSSatish Balay         ierr = PetscStrallocpy(entry->rname,&function);CHKERRQ(ierr);
404e5c89e4eSSatish Balay       }
405e5c89e4eSSatish Balay 
406e5c89e4eSSatish Balay       /* it is not yet in memory so load from dynamic library */
4070598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
408e5c89e4eSSatish Balay       newpath = path;
409e5c89e4eSSatish Balay       if (!path) newpath = entry->path;
410d44a1e48SBarry Smith       ierr = PetscDLLibrarySym(comm,&PetscDLLibrariesLoaded,newpath,entry->rname,(void **)r);CHKERRQ(ierr);
411e5c89e4eSSatish Balay       if (*r) {
412e5c89e4eSSatish Balay         entry->routine = *r;
413503cfb0cSBarry Smith         ierr = PetscFree(path);CHKERRQ(ierr);
414e5c89e4eSSatish Balay         ierr = PetscFree(function);CHKERRQ(ierr);
415e5c89e4eSSatish Balay         PetscFunctionReturn(0);
416e5c89e4eSSatish Balay       }
417e5c89e4eSSatish Balay #endif
418e5c89e4eSSatish Balay     }
419e5c89e4eSSatish Balay     entry = entry->next;
420e5c89e4eSSatish Balay   }
421e5c89e4eSSatish Balay 
4220598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
4234b91b6eaSBarry Smith   if (searchlibraries) {
424e5c89e4eSSatish Balay     /* Function never registered; try for it anyway */
425d44a1e48SBarry Smith     ierr = PetscDLLibrarySym(comm,&PetscDLLibrariesLoaded,path,function,(void **)r);CHKERRQ(ierr);
426503cfb0cSBarry Smith     ierr = PetscFree(path);CHKERRQ(ierr);
427e5c89e4eSSatish Balay     if (*r) {
428*140e18c1SBarry Smith       ierr = PetscFunctionListAdd(comm,&fl,name,name,*r);CHKERRQ(ierr);
429e5c89e4eSSatish Balay     }
4304b91b6eaSBarry Smith   }
431e5c89e4eSSatish Balay #endif
432e5c89e4eSSatish Balay   ierr = PetscFree(function);CHKERRQ(ierr);
433e5c89e4eSSatish Balay   PetscFunctionReturn(0);
434e5c89e4eSSatish Balay }
435e5c89e4eSSatish Balay 
436e5c89e4eSSatish Balay #undef __FUNCT__
437*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListView"
438e5c89e4eSSatish Balay /*@
439*140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
440e5c89e4eSSatish Balay 
441e5c89e4eSSatish Balay    Collective over MPI_Comm
442e5c89e4eSSatish Balay 
443e5c89e4eSSatish Balay    Input Parameters:
444e5c89e4eSSatish Balay +  list - the list of functions
445e5c89e4eSSatish Balay -  viewer - currently ignored
446e5c89e4eSSatish Balay 
447e5c89e4eSSatish Balay    Level: developer
448e5c89e4eSSatish Balay 
449*140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionListPrintTypes(), PetscFunctionList
450e5c89e4eSSatish Balay @*/
451*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
452e5c89e4eSSatish Balay {
453e5c89e4eSSatish Balay   PetscErrorCode ierr;
454ace3abfcSBarry Smith   PetscBool      iascii;
455e5c89e4eSSatish Balay 
456e5c89e4eSSatish Balay   PetscFunctionBegin;
457e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
458e5c89e4eSSatish Balay   PetscValidPointer(list,1);
4590700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
460e5c89e4eSSatish Balay 
461251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
462e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
463e5c89e4eSSatish Balay 
464e5c89e4eSSatish Balay   while (list) {
465e5c89e4eSSatish Balay     if (list->path) {
466e5c89e4eSSatish Balay       ierr = PetscViewerASCIIPrintf(viewer," %s %s %s\n",list->path,list->name,list->rname);CHKERRQ(ierr);
467e5c89e4eSSatish Balay     } else {
468e5c89e4eSSatish Balay       ierr = PetscViewerASCIIPrintf(viewer," %s %s\n",list->name,list->rname);CHKERRQ(ierr);
469e5c89e4eSSatish Balay     }
470e5c89e4eSSatish Balay     list = list->next;
471e5c89e4eSSatish Balay   }
472e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
473e5c89e4eSSatish Balay   PetscFunctionReturn(0);
474e5c89e4eSSatish Balay }
475e5c89e4eSSatish Balay 
476e5c89e4eSSatish Balay #undef __FUNCT__
477*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGet"
478065533a5SJed Brown /*@C
479*140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
480e5c89e4eSSatish Balay          by help etc.
481e5c89e4eSSatish Balay 
482e5c89e4eSSatish Balay    Collective over MPI_Comm
483e5c89e4eSSatish Balay 
484e5c89e4eSSatish Balay    Input Parameter:
485e5c89e4eSSatish Balay .  list   - list of types
486e5c89e4eSSatish Balay 
487e5c89e4eSSatish Balay    Output Parameter:
488e5c89e4eSSatish Balay +  array - array of names
489e5c89e4eSSatish Balay -  n - length of array
490e5c89e4eSSatish Balay 
491e5c89e4eSSatish Balay    Notes:
492e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
493e5c89e4eSSatish Balay     not copied so should not be freed.
494e5c89e4eSSatish Balay 
495e5c89e4eSSatish Balay    Level: developer
496e5c89e4eSSatish Balay 
497*140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
498e5c89e4eSSatish Balay @*/
499*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
500e5c89e4eSSatish Balay {
501e5c89e4eSSatish Balay   PetscErrorCode    ierr;
502e5c89e4eSSatish Balay   PetscInt          count = 0;
503*140e18c1SBarry Smith   PetscFunctionList klist = list;
504e5c89e4eSSatish Balay 
505e5c89e4eSSatish Balay   PetscFunctionBegin;
506e5c89e4eSSatish Balay   while (list) {
507e5c89e4eSSatish Balay     list = list->next;
508e5c89e4eSSatish Balay     count++;
509e5c89e4eSSatish Balay   }
510e5c89e4eSSatish Balay   ierr  = PetscMalloc((count+1)*sizeof(char *),array);CHKERRQ(ierr);
511e5c89e4eSSatish Balay   count = 0;
512e5c89e4eSSatish Balay   while (klist) {
513e5c89e4eSSatish Balay     (*array)[count] = klist->name;
514e5c89e4eSSatish Balay     klist = klist->next;
515e5c89e4eSSatish Balay     count++;
516e5c89e4eSSatish Balay   }
517e5c89e4eSSatish Balay   (*array)[count] = 0;
518e5c89e4eSSatish Balay   *n = count+1;
519e5c89e4eSSatish Balay   PetscFunctionReturn(0);
520e5c89e4eSSatish Balay }
521e5c89e4eSSatish Balay 
522e5c89e4eSSatish Balay 
523e5c89e4eSSatish Balay #undef __FUNCT__
524*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListPrintTypes"
525e5c89e4eSSatish Balay /*@C
526*140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
527e5c89e4eSSatish Balay 
528e5c89e4eSSatish Balay    Collective over MPI_Comm
529e5c89e4eSSatish Balay 
530e5c89e4eSSatish Balay    Input Parameters:
531e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
532e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
533e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
534e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
535e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
536e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
5373cc1e11dSBarry Smith .  list   - list of types
5383cc1e11dSBarry Smith -  def - default (current) value
539e5c89e4eSSatish Balay 
540e5c89e4eSSatish Balay    Level: developer
541e5c89e4eSSatish Balay 
542*140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
543e5c89e4eSSatish Balay @*/
544*140e18c1SBarry 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[])
545e5c89e4eSSatish Balay {
546e5c89e4eSSatish Balay   PetscErrorCode ierr;
547e5c89e4eSSatish Balay   PetscInt       count = 0;
548e5c89e4eSSatish Balay   char           p[64];
549e5c89e4eSSatish Balay 
550e5c89e4eSSatish Balay   PetscFunctionBegin;
551da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
552e5c89e4eSSatish Balay 
553e5c89e4eSSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
554e5c89e4eSSatish Balay   if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);}
5553cc1e11dSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr);
556e5c89e4eSSatish Balay 
557e5c89e4eSSatish Balay   while (list) {
558e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
559e5c89e4eSSatish Balay     list = list->next;
560e5c89e4eSSatish Balay     count++;
561e5c89e4eSSatish Balay     if (count == 8) {ierr = PetscFPrintf(comm,fd,"\n     ");CHKERRQ(ierr);}
562e5c89e4eSSatish Balay   }
563e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
564e5c89e4eSSatish Balay   PetscFunctionReturn(0);
565e5c89e4eSSatish Balay }
566e5c89e4eSSatish Balay 
567e5c89e4eSSatish Balay #undef __FUNCT__
568*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDuplicate"
569e5c89e4eSSatish Balay /*@
570*140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
571e5c89e4eSSatish Balay 
572e5c89e4eSSatish Balay     Input Parameters:
573e5c89e4eSSatish Balay .   fl   - pointer to list
574e5c89e4eSSatish Balay 
575e5c89e4eSSatish Balay     Output Parameters:
576e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
577e5c89e4eSSatish Balay 
578e5c89e4eSSatish Balay     Level: developer
579e5c89e4eSSatish Balay 
580*140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
581e5c89e4eSSatish Balay 
582e5c89e4eSSatish Balay @*/
583*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
584e5c89e4eSSatish Balay {
585e5c89e4eSSatish Balay   PetscErrorCode ierr;
586e5c89e4eSSatish Balay   char           path[PETSC_MAX_PATH_LEN];
587e5c89e4eSSatish Balay 
588e5c89e4eSSatish Balay   PetscFunctionBegin;
589e5c89e4eSSatish Balay   while (fl) {
590e5c89e4eSSatish Balay     /* this is silly, rebuild the complete pathname */
591e5c89e4eSSatish Balay     if (fl->path) {
592e5c89e4eSSatish Balay       ierr = PetscStrcpy(path,fl->path);CHKERRQ(ierr);
593e5c89e4eSSatish Balay       ierr = PetscStrcat(path,":");CHKERRQ(ierr);
594e5c89e4eSSatish Balay       ierr = PetscStrcat(path,fl->name);CHKERRQ(ierr);
595e5c89e4eSSatish Balay     } else {
596e5c89e4eSSatish Balay       ierr = PetscStrcpy(path,fl->name);CHKERRQ(ierr);
597e5c89e4eSSatish Balay     }
598*140e18c1SBarry Smith     ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,nl,path,fl->rname,fl->routine);CHKERRQ(ierr);
599e5c89e4eSSatish Balay     fl   = fl->next;
600e5c89e4eSSatish Balay   }
601e5c89e4eSSatish Balay   PetscFunctionReturn(0);
602e5c89e4eSSatish Balay }
603e5c89e4eSSatish Balay 
604e5c89e4eSSatish Balay 
605e5c89e4eSSatish Balay #undef __FUNCT__
606*140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListConcat"
607e5c89e4eSSatish Balay /*
608*140e18c1SBarry Smith     PetscFunctionListConcat - joins name of a libary, and the path where it is located
609e5c89e4eSSatish Balay     into a single string.
610e5c89e4eSSatish Balay 
611e5c89e4eSSatish Balay     Input Parameters:
612e5c89e4eSSatish Balay .   path   - path to the library name.
613e5c89e4eSSatish Balay .   name   - name of the library
614e5c89e4eSSatish Balay 
615e5c89e4eSSatish Balay     Output Parameters:
616e5c89e4eSSatish Balay .   fullname - the name that is the union of the path and the library name,
617e5c89e4eSSatish Balay                delimited by a semicolon, i.e., path:name
618e5c89e4eSSatish Balay 
619e5c89e4eSSatish Balay     Notes:
620e5c89e4eSSatish Balay     If the path is NULL, assumes that the name, specified also includes
621e5c89e4eSSatish Balay     the path as path:name
622e5c89e4eSSatish Balay 
623e5c89e4eSSatish Balay */
624*140e18c1SBarry Smith PetscErrorCode  PetscFunctionListConcat(const char path[],const char name[],char fullname[])
625e5c89e4eSSatish Balay {
626e5c89e4eSSatish Balay   PetscErrorCode ierr;
627e5c89e4eSSatish Balay   PetscFunctionBegin;
628e5c89e4eSSatish Balay   if (path) {
629e5c89e4eSSatish Balay     ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr);
630e5c89e4eSSatish Balay     ierr = PetscStrcat(fullname,":");CHKERRQ(ierr);
631e5c89e4eSSatish Balay     ierr = PetscStrcat(fullname,name);CHKERRQ(ierr);
632e5c89e4eSSatish Balay   } else {
633e5c89e4eSSatish Balay     ierr = PetscStrcpy(fullname,name);CHKERRQ(ierr);
634e5c89e4eSSatish Balay   }
635e5c89e4eSSatish Balay   PetscFunctionReturn(0);
636e5c89e4eSSatish Balay }
637