xref: /petsc/src/sys/dll/reg.c (revision d4349b43d0d4ff02ef13c3807514f83c985fc1e9)
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 */
6afcb2eb5SJed Brown #include <petsc-private/petscimpl.h>           /*I "petscsys.h" I*/
7665c2dedSJed Brown #include <petscviewer.h>
8e5c89e4eSSatish Balay 
93fa76a5bSLisandro Dalcin /*
103fa76a5bSLisandro Dalcin     This is the default list used by PETSc with the PetscDLLibrary register routines
113fa76a5bSLisandro Dalcin */
12d44a1e48SBarry Smith PetscDLLibrary PetscDLLibrariesLoaded = 0;
133fa76a5bSLisandro Dalcin 
14ebd79076SLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
15ebd79076SLisandro Dalcin 
16e5c89e4eSSatish Balay #undef __FUNCT__
17487e5849SBarry Smith #define __FUNCT__ "PetscLoadDynamicLibrary"
187087cfbeSBarry Smith static PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
19487e5849SBarry Smith {
20487e5849SBarry Smith   char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
21487e5849SBarry Smith   PetscErrorCode ierr;
22487e5849SBarry Smith 
23487e5849SBarry Smith   PetscFunctionBegin;
24487e5849SBarry Smith   ierr = PetscStrcpy(libs,"${PETSC_LIB_DIR}/libpetsc");CHKERRQ(ierr);
25487e5849SBarry Smith   ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
26487e5849SBarry Smith   ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
27487e5849SBarry Smith   if (*found) {
28d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
29487e5849SBarry Smith   } else {
30487e5849SBarry Smith     ierr = PetscStrcpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc");CHKERRQ(ierr);
31487e5849SBarry Smith     ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
32487e5849SBarry Smith     ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
33487e5849SBarry Smith     if (*found) {
34d44a1e48SBarry Smith       ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
35487e5849SBarry Smith     }
36487e5849SBarry Smith   }
37487e5849SBarry Smith   PetscFunctionReturn(0);
38487e5849SBarry Smith }
39487e5849SBarry Smith 
403fa76a5bSLisandro Dalcin #endif
413fa76a5bSLisandro Dalcin 
42487e5849SBarry Smith #undef __FUNCT__
43e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize_DynamicLibraries"
44e5c89e4eSSatish Balay /*
45e5c89e4eSSatish Balay     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
46e5c89e4eSSatish Balay     search path.
47e5c89e4eSSatish Balay */
487087cfbeSBarry Smith PetscErrorCode  PetscInitialize_DynamicLibraries(void)
49e5c89e4eSSatish Balay {
50487e5849SBarry Smith   char           *libname[32];
51e5c89e4eSSatish Balay   PetscErrorCode ierr;
52e5c89e4eSSatish Balay   PetscInt       nmax,i;
533fa76a5bSLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
54ace3abfcSBarry Smith   PetscBool      found;
553fa76a5bSLisandro Dalcin #endif
56e5c89e4eSSatish Balay 
5760154eb2SBarry Smith   PetscFunctionBegin;
58e5c89e4eSSatish Balay   nmax = 32;
590298fd71SBarry Smith   ierr = PetscOptionsGetStringArray(NULL,"-dll_prepend",libname,&nmax,NULL);CHKERRQ(ierr);
60e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
61d44a1e48SBarry Smith     ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
62e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
63e5c89e4eSSatish Balay   }
64e5c89e4eSSatish Balay 
653fa76a5bSLisandro Dalcin #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
663fa76a5bSLisandro Dalcin   /*
673fa76a5bSLisandro Dalcin       This just initializes the most basic PETSc stuff.
683fa76a5bSLisandro Dalcin 
693fa76a5bSLisandro Dalcin     The classes, from PetscDraw to PetscTS, are initialized the first
703fa76a5bSLisandro Dalcin     time an XXCreate() is called.
713fa76a5bSLisandro Dalcin   */
72607a6623SBarry Smith   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
733fa76a5bSLisandro Dalcin #else
7460154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
75487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
76e32f2f54SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
7760154eb2SBarry Smith #else
7860154eb2SBarry Smith   ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
7960154eb2SBarry Smith   if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
80487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
81e32f2f54SBarry 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!");
82487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
83e32f2f54SBarry 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!");
84487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
85e32f2f54SBarry 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!");
86487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
87e32f2f54SBarry 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!");
88487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
89e32f2f54SBarry 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!");
90487e5849SBarry Smith   ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
91e32f2f54SBarry 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!");
92bb84e0fdSBarry Smith #endif
933fa76a5bSLisandro Dalcin #endif
94e5c89e4eSSatish Balay 
95e5c89e4eSSatish Balay   nmax = 32;
960298fd71SBarry Smith   ierr = PetscOptionsGetStringArray(NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr);
97e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
98d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
99e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
100e5c89e4eSSatish Balay   }
101e5c89e4eSSatish Balay   PetscFunctionReturn(0);
102e5c89e4eSSatish Balay }
103e5c89e4eSSatish Balay 
104e5c89e4eSSatish Balay #undef __FUNCT__
105e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize_DynamicLibraries"
106ebd79076SLisandro Dalcin /*
107ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
108ebd79076SLisandro Dalcin */
109e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void)
110e5c89e4eSSatish Balay {
111ebd79076SLisandro Dalcin   PetscErrorCode ierr;
112ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
113e5c89e4eSSatish Balay 
114ebd79076SLisandro Dalcin   PetscFunctionBegin;
1150298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
116d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
117d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
118a297a907SKarl Rupp 
119d44a1e48SBarry Smith   PetscDLLibrariesLoaded = 0;
120e5c89e4eSSatish Balay   PetscFunctionReturn(0);
121e5c89e4eSSatish Balay }
122e5c89e4eSSatish Balay 
123e4d1774bSDmitry Karpeev 
124e4d1774bSDmitry Karpeev 
125e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
126140e18c1SBarry Smith struct _n_PetscFunctionList {
127e5c89e4eSSatish Balay   void              (*routine)(void);    /* the routine */
128e5c89e4eSSatish Balay   char              *name;               /* string to identify routine */
129*d4349b43SBarry Smith   char              *rname;              /* string version of routine name */
130140e18c1SBarry Smith   PetscFunctionList next;                /* next pointer */
131140e18c1SBarry Smith   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
132e5c89e4eSSatish Balay };
133e5c89e4eSSatish Balay 
134e5c89e4eSSatish Balay /*
135140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
136e5c89e4eSSatish Balay */
137140e18c1SBarry Smith static PetscFunctionList dlallhead = 0;
138e5c89e4eSSatish Balay 
139e5c89e4eSSatish Balay #undef __FUNCT__
140140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListAdd"
141e5c89e4eSSatish Balay /*@C
142140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
143e5c89e4eSSatish Balay    specified registry.
144e5c89e4eSSatish Balay 
145*d4349b43SBarry Smith      Not Collective
146e5c89e4eSSatish Balay 
147e5c89e4eSSatish Balay    Input Parameters:
148*d4349b43SBarry Smith +  fl    - pointer registry
149e5c89e4eSSatish Balay .  name  - string to identify routine
150e5c89e4eSSatish Balay .  rname - routine name in dynamic library
151e5c89e4eSSatish Balay -  fnc   - function pointer (optional if using dynamic libraries)
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay    Notes:
1540298fd71SBarry Smith    To remove a registered routine, pass in a NULL rname and fnc().
155e5c89e4eSSatish Balay 
156e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
157e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
1581c84c290SBarry Smith    for that particular component (e.g., SNESRegister()) instead of
159140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
160e5c89e4eSSatish Balay 
161e5c89e4eSSatish Balay     Level: developer
162e5c89e4eSSatish Balay 
1631c84c290SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(),
1641c84c290SBarry Smith           PCRegister(), TSRegister(), PetscFunctionList
165e5c89e4eSSatish Balay @*/
166*d4349b43SBarry Smith PetscErrorCode  PetscFunctionListAdd(PetscFunctionList *fl,const char name[],const char rname[],void (*fnc)(void))
167e5c89e4eSSatish Balay {
168140e18c1SBarry Smith   PetscFunctionList entry,ne;
169e5c89e4eSSatish Balay   PetscErrorCode    ierr;
170e5c89e4eSSatish Balay 
171e5c89e4eSSatish Balay   PetscFunctionBegin;
172e5c89e4eSSatish Balay   if (!*fl) {
173140e18c1SBarry Smith     ierr           = PetscNew(struct _n_PetscFunctionList,&entry);CHKERRQ(ierr);
174e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
175*d4349b43SBarry Smith     ierr           = PetscStrallocpy(rname,&entry->rname);CHKERRQ(ierr);
176e5c89e4eSSatish Balay     entry->routine = fnc;
177e5c89e4eSSatish Balay     entry->next    = 0;
178e5c89e4eSSatish Balay     *fl            = entry;
179e5c89e4eSSatish Balay 
180e5c89e4eSSatish Balay     /* add this new list to list of all lists */
181e5c89e4eSSatish Balay     if (!dlallhead) {
182e5c89e4eSSatish Balay       dlallhead        = *fl;
183e5c89e4eSSatish Balay       (*fl)->next_list = 0;
184e5c89e4eSSatish Balay     } else {
185e5c89e4eSSatish Balay       ne               = dlallhead;
186e5c89e4eSSatish Balay       dlallhead        = *fl;
187e5c89e4eSSatish Balay       (*fl)->next_list = ne;
188e5c89e4eSSatish Balay     }
189e5c89e4eSSatish Balay   } else {
190e5c89e4eSSatish Balay     /* search list to see if it is already there */
191e5c89e4eSSatish Balay     ne = *fl;
192e5c89e4eSSatish Balay     while (ne) {
193ace3abfcSBarry Smith       PetscBool founddup;
194e5c89e4eSSatish Balay 
195e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
196e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
197503cfb0cSBarry Smith         ierr        = PetscFree(ne->rname);CHKERRQ(ierr);
198*d4349b43SBarry Smith         ierr        = PetscStrallocpy(rname,&ne->rname);CHKERRQ(ierr);
199e5c89e4eSSatish Balay         ne->routine = fnc;
200e5c89e4eSSatish Balay         PetscFunctionReturn(0);
201e5c89e4eSSatish Balay       }
202a297a907SKarl Rupp       if (ne->next) ne = ne->next;
203a297a907SKarl Rupp       else break;
204e5c89e4eSSatish Balay     }
205e5c89e4eSSatish Balay     /* create new entry and add to end of list */
206140e18c1SBarry Smith     ierr           = PetscNew(struct _n_PetscFunctionList,&entry);CHKERRQ(ierr);
207e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
208*d4349b43SBarry Smith     ierr           = PetscStrallocpy(rname,&entry->rname);CHKERRQ(ierr);
209e5c89e4eSSatish Balay     entry->routine = fnc;
210e5c89e4eSSatish Balay     entry->next    = 0;
211e5c89e4eSSatish Balay     ne->next       = entry;
212e5c89e4eSSatish Balay   }
213e5c89e4eSSatish Balay   PetscFunctionReturn(0);
214e5c89e4eSSatish Balay }
215e5c89e4eSSatish Balay 
216e5c89e4eSSatish Balay #undef __FUNCT__
217140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroy"
218e5c89e4eSSatish Balay /*@
219140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
220e5c89e4eSSatish Balay 
221e5c89e4eSSatish Balay     Input Parameter:
222e5c89e4eSSatish Balay .   fl  - pointer to list
223e5c89e4eSSatish Balay 
224e5c89e4eSSatish Balay     Level: developer
225e5c89e4eSSatish Balay 
226140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
227e5c89e4eSSatish Balay @*/
228140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
229e5c89e4eSSatish Balay {
230140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
231e5c89e4eSSatish Balay   PetscErrorCode    ierr;
232e5c89e4eSSatish Balay 
233e5c89e4eSSatish Balay   PetscFunctionBegin;
2341441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
23560154eb2SBarry Smith   if (!dlallhead) PetscFunctionReturn(0);
236e5c89e4eSSatish Balay 
237e5c89e4eSSatish Balay   /*
238e5c89e4eSSatish Balay        Remove this entry from the master DL list (if it is in it)
239e5c89e4eSSatish Balay   */
2401441b1d3SBarry Smith   if (dlallhead == *fl) {
241a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
242a297a907SKarl Rupp     else dlallhead = 0;
243e5c89e4eSSatish Balay   } else {
2441441b1d3SBarry Smith     while (tmp->next_list != *fl) {
245e5c89e4eSSatish Balay       tmp = tmp->next_list;
246e5c89e4eSSatish Balay       if (!tmp->next_list) break;
247e5c89e4eSSatish Balay     }
248e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
249e5c89e4eSSatish Balay   }
250e5c89e4eSSatish Balay 
251e5c89e4eSSatish Balay   /* free this list */
2521441b1d3SBarry Smith   entry = *fl;
253e5c89e4eSSatish Balay   while (entry) {
254e5c89e4eSSatish Balay     next  = entry->next;
255e5c89e4eSSatish Balay     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
256e5c89e4eSSatish Balay     ierr  = PetscFree(entry->rname);CHKERRQ(ierr);
257e5c89e4eSSatish Balay     ierr  = PetscFree(entry);CHKERRQ(ierr);
258e5c89e4eSSatish Balay     entry = next;
259e5c89e4eSSatish Balay   }
2601441b1d3SBarry Smith   *fl = 0;
261e5c89e4eSSatish Balay   PetscFunctionReturn(0);
262e5c89e4eSSatish Balay }
263e5c89e4eSSatish Balay 
264e5c89e4eSSatish Balay /*
265e5c89e4eSSatish Balay    Destroys all the function lists that anyone has every registered, such as KSPList, VecList, etc.
266e5c89e4eSSatish Balay */
267e5c89e4eSSatish Balay #undef __FUNCT__
268140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroyAll"
269140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroyAll(void)
270e5c89e4eSSatish Balay {
271140e18c1SBarry Smith   PetscFunctionList tmp2,tmp1 = dlallhead;
272e5c89e4eSSatish Balay   PetscErrorCode    ierr;
273e5c89e4eSSatish Balay 
274e5c89e4eSSatish Balay   PetscFunctionBegin;
275e5c89e4eSSatish Balay   while (tmp1) {
276e5c89e4eSSatish Balay     tmp2 = tmp1->next_list;
277140e18c1SBarry Smith     ierr = PetscFunctionListDestroy(&tmp1);CHKERRQ(ierr);
278e5c89e4eSSatish Balay     tmp1 = tmp2;
279e5c89e4eSSatish Balay   }
280e5c89e4eSSatish Balay   dlallhead = 0;
281e5c89e4eSSatish Balay   PetscFunctionReturn(0);
282e5c89e4eSSatish Balay }
283e5c89e4eSSatish Balay 
284e5c89e4eSSatish Balay #undef __FUNCT__
285140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListFind"
286e5c89e4eSSatish Balay /*@C
287*d4349b43SBarry Smith     PetscFunctionListFind - Given a name registered to a function
288e5c89e4eSSatish Balay 
289e5c89e4eSSatish Balay     Input Parameters:
2901d280d73SBarry Smith +   fl   - pointer to list
291*d4349b43SBarry Smith -   name - either the name registered for the function or the name of the function
292e5c89e4eSSatish Balay 
293e5c89e4eSSatish Balay     Output Parameters:
294*d4349b43SBarry Smith .   r - the function pointer if name was found else NULL
295e5c89e4eSSatish Balay 
296e5c89e4eSSatish Balay     Level: developer
297e5c89e4eSSatish Balay 
298140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
299e5c89e4eSSatish Balay @*/
300*d4349b43SBarry Smith PetscErrorCode  PetscFunctionListFind(PetscFunctionList fl,const char name[],void (**r)(void))
301e5c89e4eSSatish Balay {
302140e18c1SBarry Smith   PetscFunctionList entry = fl;
303e5c89e4eSSatish Balay   PetscErrorCode    ierr;
304*d4349b43SBarry Smith   PetscBool         flg1,flg2;
305e5c89e4eSSatish Balay 
306e5c89e4eSSatish Balay   PetscFunctionBegin;
307e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
308e5c89e4eSSatish Balay 
309e5c89e4eSSatish Balay   *r = 0;
310e5c89e4eSSatish Balay   while (entry) {
311*d4349b43SBarry Smith     ierr = PetscStrcmp(name,entry->name,&flg1);CHKERRQ(ierr);
312*d4349b43SBarry Smith     ierr = PetscStrcmp(name,entry->rname,&flg2);CHKERRQ(ierr);
313*d4349b43SBarry Smith     if (flg1 || flg2) {
314e5c89e4eSSatish Balay       *r   = entry->routine;
315e5c89e4eSSatish Balay       PetscFunctionReturn(0);
316e5c89e4eSSatish Balay     }
317e5c89e4eSSatish Balay     entry = entry->next;
318e5c89e4eSSatish Balay   }
319e5c89e4eSSatish Balay   PetscFunctionReturn(0);
320e5c89e4eSSatish Balay }
321e5c89e4eSSatish Balay 
322e5c89e4eSSatish Balay #undef __FUNCT__
323140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListView"
324e5c89e4eSSatish Balay /*@
325140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
326e5c89e4eSSatish Balay 
327e5c89e4eSSatish Balay    Collective over MPI_Comm
328e5c89e4eSSatish Balay 
329e5c89e4eSSatish Balay    Input Parameters:
330e5c89e4eSSatish Balay +  list - the list of functions
331e5c89e4eSSatish Balay -  viewer - currently ignored
332e5c89e4eSSatish Balay 
333e5c89e4eSSatish Balay    Level: developer
334e5c89e4eSSatish Balay 
335140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionListPrintTypes(), PetscFunctionList
336e5c89e4eSSatish Balay @*/
337140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
338e5c89e4eSSatish Balay {
339e5c89e4eSSatish Balay   PetscErrorCode ierr;
340ace3abfcSBarry Smith   PetscBool      iascii;
341e5c89e4eSSatish Balay 
342e5c89e4eSSatish Balay   PetscFunctionBegin;
343e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
344e5c89e4eSSatish Balay   PetscValidPointer(list,1);
3450700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
346e5c89e4eSSatish Balay 
347251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
348e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
349e5c89e4eSSatish Balay 
350e5c89e4eSSatish Balay   while (list) {
351e5c89e4eSSatish Balay     ierr = PetscViewerASCIIPrintf(viewer," %s %s\n",list->name,list->rname);CHKERRQ(ierr);
352e5c89e4eSSatish Balay     list = list->next;
353e5c89e4eSSatish Balay   }
354e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
355e5c89e4eSSatish Balay   PetscFunctionReturn(0);
356e5c89e4eSSatish Balay }
357e5c89e4eSSatish Balay 
358e5c89e4eSSatish Balay #undef __FUNCT__
359140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGet"
360065533a5SJed Brown /*@C
361140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
362e5c89e4eSSatish Balay          by help etc.
363e5c89e4eSSatish Balay 
364*d4349b43SBarry Smith    Not Collective
365e5c89e4eSSatish Balay 
366e5c89e4eSSatish Balay    Input Parameter:
367e5c89e4eSSatish Balay .  list   - list of types
368e5c89e4eSSatish Balay 
369e5c89e4eSSatish Balay    Output Parameter:
370e5c89e4eSSatish Balay +  array - array of names
371e5c89e4eSSatish Balay -  n - length of array
372e5c89e4eSSatish Balay 
373e5c89e4eSSatish Balay    Notes:
374e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
375e5c89e4eSSatish Balay     not copied so should not be freed.
376e5c89e4eSSatish Balay 
377e5c89e4eSSatish Balay    Level: developer
378e5c89e4eSSatish Balay 
379140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
380e5c89e4eSSatish Balay @*/
381140e18c1SBarry Smith PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
382e5c89e4eSSatish Balay {
383e5c89e4eSSatish Balay   PetscErrorCode    ierr;
384e5c89e4eSSatish Balay   PetscInt          count = 0;
385140e18c1SBarry Smith   PetscFunctionList klist = list;
386e5c89e4eSSatish Balay 
387e5c89e4eSSatish Balay   PetscFunctionBegin;
388e5c89e4eSSatish Balay   while (list) {
389e5c89e4eSSatish Balay     list = list->next;
390e5c89e4eSSatish Balay     count++;
391e5c89e4eSSatish Balay   }
392e5c89e4eSSatish Balay   ierr  = PetscMalloc((count+1)*sizeof(char*),array);CHKERRQ(ierr);
393e5c89e4eSSatish Balay   count = 0;
394e5c89e4eSSatish Balay   while (klist) {
395e5c89e4eSSatish Balay     (*array)[count] = klist->name;
396e5c89e4eSSatish Balay     klist           = klist->next;
397e5c89e4eSSatish Balay     count++;
398e5c89e4eSSatish Balay   }
399e5c89e4eSSatish Balay   (*array)[count] = 0;
400e5c89e4eSSatish Balay   *n              = count+1;
401e5c89e4eSSatish Balay   PetscFunctionReturn(0);
402e5c89e4eSSatish Balay }
403e5c89e4eSSatish Balay 
404e5c89e4eSSatish Balay 
405e5c89e4eSSatish Balay #undef __FUNCT__
406140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListPrintTypes"
407e5c89e4eSSatish Balay /*@C
408140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
409e5c89e4eSSatish Balay 
410e5c89e4eSSatish Balay    Collective over MPI_Comm
411e5c89e4eSSatish Balay 
412e5c89e4eSSatish Balay    Input Parameters:
413e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
414e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
415e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
416e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
417e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
418e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
4193cc1e11dSBarry Smith .  list   - list of types
4203cc1e11dSBarry Smith -  def - default (current) value
421e5c89e4eSSatish Balay 
422e5c89e4eSSatish Balay    Level: developer
423e5c89e4eSSatish Balay 
424140e18c1SBarry Smith .seealso: PetscFunctionListAddDynamic(), PetscFunctionList
425e5c89e4eSSatish Balay @*/
426140e18c1SBarry 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[])
427e5c89e4eSSatish Balay {
428e5c89e4eSSatish Balay   PetscErrorCode ierr;
429e5c89e4eSSatish Balay   PetscInt       count = 0;
430e5c89e4eSSatish Balay   char           p[64];
431e5c89e4eSSatish Balay 
432e5c89e4eSSatish Balay   PetscFunctionBegin;
433da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
434e5c89e4eSSatish Balay 
435e5c89e4eSSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
436e5c89e4eSSatish Balay   if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);}
4373cc1e11dSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr);
438e5c89e4eSSatish Balay 
439e5c89e4eSSatish Balay   while (list) {
440e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
441e5c89e4eSSatish Balay     list = list->next;
442e5c89e4eSSatish Balay     count++;
443e5c89e4eSSatish Balay     if (count == 8) {ierr = PetscFPrintf(comm,fd,"\n     ");CHKERRQ(ierr);}
444e5c89e4eSSatish Balay   }
445e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
446e5c89e4eSSatish Balay   PetscFunctionReturn(0);
447e5c89e4eSSatish Balay }
448e5c89e4eSSatish Balay 
449e5c89e4eSSatish Balay #undef __FUNCT__
450140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDuplicate"
451e5c89e4eSSatish Balay /*@
452140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
453e5c89e4eSSatish Balay 
454e5c89e4eSSatish Balay     Input Parameters:
455e5c89e4eSSatish Balay .   fl   - pointer to list
456e5c89e4eSSatish Balay 
457e5c89e4eSSatish Balay     Output Parameters:
458e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
459e5c89e4eSSatish Balay 
460e5c89e4eSSatish Balay     Level: developer
461e5c89e4eSSatish Balay 
462140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
463e5c89e4eSSatish Balay 
464e5c89e4eSSatish Balay @*/
465140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
466e5c89e4eSSatish Balay {
467e5c89e4eSSatish Balay   PetscErrorCode ierr;
468e5c89e4eSSatish Balay 
469e5c89e4eSSatish Balay   PetscFunctionBegin;
470e5c89e4eSSatish Balay   while (fl) {
471*d4349b43SBarry Smith     ierr = PetscFunctionListAdd(nl,fl->name,fl->rname,fl->routine);CHKERRQ(ierr);
472e5c89e4eSSatish Balay     fl   = fl->next;
473e5c89e4eSSatish Balay   }
474e5c89e4eSSatish Balay   PetscFunctionReturn(0);
475e5c89e4eSSatish Balay }
476e5c89e4eSSatish Balay 
477