xref: /petsc/src/sys/dll/reg.c (revision e4d1774bba497c88855ca48ceec8de09b31e2d79)
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*/
7*e4d1774bSDmitry Karpeev #include <stdarg.h>             /* Variadic functions. */
8e5c89e4eSSatish Balay 
9e5c89e4eSSatish Balay #undef __FUNCT__
10e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListGetPathAndFunction"
117087cfbeSBarry Smith PetscErrorCode  PetscFListGetPathAndFunction(const char name[],char *path[],char *function[])
12e5c89e4eSSatish Balay {
13e5c89e4eSSatish Balay   PetscErrorCode ierr;
14e5c89e4eSSatish Balay   char           work[PETSC_MAX_PATH_LEN],*lfunction;
15e5c89e4eSSatish Balay 
16e5c89e4eSSatish Balay   PetscFunctionBegin;
17e5c89e4eSSatish Balay   ierr = PetscStrncpy(work,name,256);CHKERRQ(ierr);
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 */
333fa76a5bSLisandro Dalcin PetscDLLibrary DLLibrariesLoaded = 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) {
49487e5849SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&DLLibrariesLoaded,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) {
55487e5849SBarry Smith       ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&DLLibrariesLoaded,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++) {
82e5c89e4eSSatish Balay     ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&DLLibrariesLoaded,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++) {
124e5c89e4eSSatish Balay     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&DLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
125e5c89e4eSSatish Balay     ierr = PetscDLLibraryCCAAppend(PETSC_COMM_WORLD,&DLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
126e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
127e5c89e4eSSatish Balay   }
1283fa76a5bSLisandro Dalcin 
129e5c89e4eSSatish Balay   PetscFunctionReturn(0);
130e5c89e4eSSatish Balay }
131e5c89e4eSSatish Balay 
132e5c89e4eSSatish Balay #undef __FUNCT__
133e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize_DynamicLibraries"
134ebd79076SLisandro Dalcin /*
135ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
136ebd79076SLisandro Dalcin */
137e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void)
138e5c89e4eSSatish Balay {
139ebd79076SLisandro Dalcin   PetscErrorCode ierr;
140ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
141e5c89e4eSSatish Balay 
142ebd79076SLisandro Dalcin   PetscFunctionBegin;
143acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-dll_view",&flg,PETSC_NULL);CHKERRQ(ierr);
1445673baf8SLisandro Dalcin   if (flg) { ierr = PetscDLLibraryPrintPath(DLLibrariesLoaded);CHKERRQ(ierr); }
1450f31fb7fSLisandro Dalcin   ierr = PetscDLLibraryClose(DLLibrariesLoaded);CHKERRQ(ierr);
1460f31fb7fSLisandro Dalcin   DLLibrariesLoaded = 0;
147e5c89e4eSSatish Balay   PetscFunctionReturn(0);
148e5c89e4eSSatish Balay }
149e5c89e4eSSatish Balay 
150*e4d1774bSDmitry Karpeev 
151*e4d1774bSDmitry Karpeev 
152e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
1532bb46157SSatish Balay struct _n_PetscFList {
154e5c89e4eSSatish Balay   void        (*routine)(void);   /* the routine */
155e5c89e4eSSatish Balay   char        *path;              /* path of link library containing routine */
156e5c89e4eSSatish Balay   char        *name;              /* string to identify routine */
157e5c89e4eSSatish Balay   char        *rname;             /* routine name in dynamic library */
158e5c89e4eSSatish Balay   PetscFList  next;               /* next pointer */
159e5c89e4eSSatish Balay   PetscFList  next_list;          /* used to maintain list of all lists for freeing */
160e5c89e4eSSatish Balay };
161e5c89e4eSSatish Balay 
162e5c89e4eSSatish Balay /*
163e5c89e4eSSatish Balay      Keep a linked list of PetscFLists so that we can destroy all the left-over ones.
164e5c89e4eSSatish Balay */
165e5c89e4eSSatish Balay static PetscFList   dlallhead = 0;
166e5c89e4eSSatish Balay 
167e5c89e4eSSatish Balay #undef __FUNCT__
168e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListAdd"
169e5c89e4eSSatish Balay /*@C
170*e4d1774bSDmitry Karpeev    PetscFListAdd - Given a routine and a string id, saves that routine in the
171e5c89e4eSSatish Balay    specified registry.
172e5c89e4eSSatish Balay 
173e5c89e4eSSatish Balay      Not Collective
174e5c89e4eSSatish Balay 
175e5c89e4eSSatish Balay    Input Parameters:
176e5c89e4eSSatish Balay +  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*e4d1774bSDmitry Karpeev    calling PetscFListAdd() 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 
194e5c89e4eSSatish Balay .seealso: PetscFListDestroy(), SNESRegisterDynamic(), KSPRegisterDynamic(),
195e5c89e4eSSatish Balay           PCRegisterDynamic(), TSRegisterDynamic(), PetscFList
196e5c89e4eSSatish Balay @*/
1977087cfbeSBarry Smith PetscErrorCode  PetscFListAdd(PetscFList *fl,const char name[],const char rname[],void (*fnc)(void))
198e5c89e4eSSatish Balay {
199e5c89e4eSSatish Balay   PetscFList     entry,ne;
200e5c89e4eSSatish Balay   PetscErrorCode ierr;
201e5c89e4eSSatish Balay   char           *fpath,*fname;
202e5c89e4eSSatish Balay 
203e5c89e4eSSatish Balay   PetscFunctionBegin;
204e5c89e4eSSatish Balay   if (!*fl) {
2052bb46157SSatish Balay     ierr           = PetscNew(struct _n_PetscFList,&entry);CHKERRQ(ierr);
206e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
207e5c89e4eSSatish Balay     ierr           = PetscFListGetPathAndFunction(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 */
231e5c89e4eSSatish Balay         ierr = PetscFListGetPathAndFunction(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 */
2422bb46157SSatish Balay     ierr           = PetscNew(struct _n_PetscFList,&entry);CHKERRQ(ierr);
243e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
244e5c89e4eSSatish Balay     ierr           = PetscFListGetPathAndFunction(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__
255e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListDestroy"
256e5c89e4eSSatish Balay /*@
257e5c89e4eSSatish Balay     PetscFListDestroy - 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 
264e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList
265e5c89e4eSSatish Balay @*/
2667087cfbeSBarry Smith PetscErrorCode  PetscFListDestroy(PetscFList *fl)
267e5c89e4eSSatish Balay {
268e5c89e4eSSatish Balay   PetscFList     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__
310e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListDestroyAll"
3117087cfbeSBarry Smith PetscErrorCode  PetscFListDestroyAll(void)
312e5c89e4eSSatish Balay {
313e5c89e4eSSatish Balay   PetscFList     tmp2,tmp1 = dlallhead;
314e5c89e4eSSatish Balay   PetscErrorCode ierr;
315e5c89e4eSSatish Balay 
316e5c89e4eSSatish Balay   PetscFunctionBegin;
317e5c89e4eSSatish Balay   while (tmp1) {
318e5c89e4eSSatish Balay     tmp2 = tmp1->next_list;
3191441b1d3SBarry Smith     ierr = PetscFListDestroy(&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__
327e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListFind"
328e5c89e4eSSatish Balay /*@C
329e5c89e4eSSatish Balay     PetscFListFind - 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 
342e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList
343e5c89e4eSSatish Balay @*/
3444b91b6eaSBarry Smith PetscErrorCode  PetscFListFind(PetscFList fl,MPI_Comm comm,const char name[],PetscBool searchlibraries,void (**r)(void))
345e5c89e4eSSatish Balay {
346e5c89e4eSSatish Balay   PetscFList     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;
358e5c89e4eSSatish Balay   ierr = PetscFListGetPathAndFunction(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) {
365e5c89e4eSSatish Balay     ierr = PetscDLLibraryAppend(comm,&DLLibrariesLoaded,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;
410e5c89e4eSSatish Balay       ierr = PetscDLLibrarySym(comm,&DLLibrariesLoaded,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 */
425e5c89e4eSSatish Balay     ierr = PetscDLLibrarySym(comm,&DLLibrariesLoaded,path,function,(void **)r);CHKERRQ(ierr);
426503cfb0cSBarry Smith     ierr = PetscFree(path);CHKERRQ(ierr);
427e5c89e4eSSatish Balay     if (*r) {
428e5c89e4eSSatish Balay       ierr = PetscFListAdd(&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__
437e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListView"
438e5c89e4eSSatish Balay /*@
439e5c89e4eSSatish Balay    PetscFListView - prints out contents of an PetscFList
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 
449e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFListPrintTypes(), PetscFList
450e5c89e4eSSatish Balay @*/
4517087cfbeSBarry Smith PetscErrorCode  PetscFListView(PetscFList 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 
4612692d6eeSBarry Smith   ierr = PetscTypeCompare((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__
477e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListGet"
478e5c89e4eSSatish Balay /*@
479e5c89e4eSSatish Balay    PetscFListGet - Gets an array the contains the entries in PetscFList, 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 
497e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList
498e5c89e4eSSatish Balay @*/
4997087cfbeSBarry Smith PetscErrorCode  PetscFListGet(PetscFList list,char ***array,int *n)
500e5c89e4eSSatish Balay {
501e5c89e4eSSatish Balay   PetscErrorCode ierr;
502e5c89e4eSSatish Balay   PetscInt       count = 0;
503e5c89e4eSSatish Balay   PetscFList     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__
524e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListPrintTypes"
525e5c89e4eSSatish Balay /*@C
526e5c89e4eSSatish Balay    PetscFListPrintTypes - 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 
542e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList
543e5c89e4eSSatish Balay @*/
5447087cfbeSBarry Smith PetscErrorCode  PetscFListPrintTypes(MPI_Comm comm,FILE *fd,const char prefix[],const char name[],const char text[],const char man[],PetscFList 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__
568e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListDuplicate"
569e5c89e4eSSatish Balay /*@
570e5c89e4eSSatish Balay     PetscFListDuplicate - 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 
580e5c89e4eSSatish Balay .seealso: PetscFList, PetscFListAdd(), PetscFlistDestroy()
581e5c89e4eSSatish Balay 
582e5c89e4eSSatish Balay @*/
5837087cfbeSBarry Smith PetscErrorCode  PetscFListDuplicate(PetscFList fl,PetscFList *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     }
598e5c89e4eSSatish Balay     ierr = PetscFListAdd(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__
606e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListConcat"
607e5c89e4eSSatish Balay /*
608e5c89e4eSSatish Balay     PetscFListConcat - 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 */
6247087cfbeSBarry Smith PetscErrorCode  PetscFListConcat(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*e4d1774bSDmitry Karpeev 
638*e4d1774bSDmitry Karpeev 
639*e4d1774bSDmitry Karpeev 
640*e4d1774bSDmitry Karpeev /* ------------------------------------------------------------------------------*/
641*e4d1774bSDmitry Karpeev struct _n_PetscOpFList {
642*e4d1774bSDmitry Karpeev   char        *op;                /* op name */
643*e4d1774bSDmitry Karpeev   PetscInt    numArgs;            /* number of arguments to the operation */
644*e4d1774bSDmitry Karpeev   char        **argTypes;         /* list of argument types */
645*e4d1774bSDmitry Karpeev   void        (*routine)(void);   /* the routine */
646*e4d1774bSDmitry Karpeev   char        *url;               /* url naming the link library and the routine */
647*e4d1774bSDmitry Karpeev   char        *path;              /* path of link library containing routine */
648*e4d1774bSDmitry Karpeev   char        *name;              /* routine name in dynamic library */
649*e4d1774bSDmitry Karpeev   PetscOpFList next;              /* next pointer */
650*e4d1774bSDmitry Karpeev   PetscOpFList next_list;         /* used to maintain list of all lists for freeing */
651*e4d1774bSDmitry Karpeev };
652*e4d1774bSDmitry Karpeev 
653*e4d1774bSDmitry Karpeev /*
654*e4d1774bSDmitry Karpeev      Keep a linked list of PetscOfFLists so that we can destroy all the left-over ones.
655*e4d1774bSDmitry Karpeev */
656*e4d1774bSDmitry Karpeev static PetscOpFList   opallhead = 0;
657*e4d1774bSDmitry Karpeev 
658*e4d1774bSDmitry Karpeev #undef __FUNCT__
659*e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOfFListAdd"
660*e4d1774bSDmitry Karpeev /*@C
661*e4d1774bSDmitry Karpeev    PetscOfFListAdd - Given a routine and a string id, saves that routine in the
662*e4d1774bSDmitry Karpeev    specified registry.
663*e4d1774bSDmitry Karpeev 
664*e4d1774bSDmitry Karpeev      Not Collective
665*e4d1774bSDmitry Karpeev 
666*e4d1774bSDmitry Karpeev    Input Parameters:
667*e4d1774bSDmitry Karpeev +  comm    - processors adding the op
668*e4d1774bSDmitry Karpeev .  fl      - list of known ops
669*e4d1774bSDmitry Karpeev .  url     - routine locator  (optional, if not using dynamic libraries and a nonempty fnc)
670*e4d1774bSDmitry Karpeev .  fnc     - function pointer (optional, if using dynamic libraries and a nonempty url)
671*e4d1774bSDmitry Karpeev .  op      - operation name
672*e4d1774bSDmitry Karpeev .  numArgs - number of op arguments
673*e4d1774bSDmitry Karpeev -  ...     - list of argument type names (const char*)
674*e4d1774bSDmitry Karpeev 
675*e4d1774bSDmitry Karpeev    Notes:
676*e4d1774bSDmitry Karpeev    To remove a registered routine, pass in a PETSC_NULL url and fnc().
677*e4d1774bSDmitry Karpeev 
678*e4d1774bSDmitry Karpeev    url can be of the form  [/path/libname[.so.1.0]:]functionname[()]  where items in [] denote optional
679*e4d1774bSDmitry Karpeev 
680*e4d1774bSDmitry Karpeev    ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environment variable}
681*e4d1774bSDmitry Karpeev    occuring in url will be replaced with appropriate values.
682*e4d1774bSDmitry Karpeev 
683*e4d1774bSDmitry Karpeev    Level: developer
684*e4d1774bSDmitry Karpeev 
685*e4d1774bSDmitry Karpeev .seealso: PetscOpFListDestroy(),PetscOpFList,  PetscFListAdd(), PetscFList
686*e4d1774bSDmitry Karpeev @*/
687*e4d1774bSDmitry Karpeev PetscErrorCode  PetscOfFListAdd(MPI_Comm comm, PetscOpFList *fl,const char url[],void (*fnc)(void),const char op[], PetscInt numArgs, ...)
688*e4d1774bSDmitry Karpeev {
689*e4d1774bSDmitry Karpeev   PetscOpFList   entry,e,ne;
690*e4d1774bSDmitry Karpeev   PetscErrorCode ierr;
691*e4d1774bSDmitry Karpeev   char           *fpath,*fname;
692*e4d1774bSDmitry Karpeev   PetscInt       i;
693*e4d1774bSDmitry Karpeev   const char     *argType;
694*e4d1774bSDmitry Karpeev   va_list        ap;
695*e4d1774bSDmitry Karpeev 
696*e4d1774bSDmitry Karpeev   PetscFunctionBegin;
697*e4d1774bSDmitry Karpeev   if (!*fl) {
698*e4d1774bSDmitry Karpeev     ierr           = PetscNew(struct _n_PetscOpFList,&entry); CHKERRQ(ierr);
699*e4d1774bSDmitry Karpeev     ierr           = PetscStrallocpy(op,&entry->op);          CHKERRQ(ierr);
700*e4d1774bSDmitry Karpeev     ierr           = PetscStrallocpy(url,&(entry->url));      CHKERRQ(ierr);
701*e4d1774bSDmitry Karpeev     ierr           = PetscFListGetPathAndFunction(url,&fpath,&fname);CHKERRQ(ierr);
702*e4d1774bSDmitry Karpeev     entry->path    = fpath;
703*e4d1774bSDmitry Karpeev     entry->name    = fname;
704*e4d1774bSDmitry Karpeev     entry->routine = fnc;
705*e4d1774bSDmitry Karpeev     entry->next    = 0;
706*e4d1774bSDmitry Karpeev     *fl = entry;
707*e4d1774bSDmitry Karpeev 
708*e4d1774bSDmitry Karpeev     /* add this new list to list of all lists */
709*e4d1774bSDmitry Karpeev     if (!opallhead) {
710*e4d1774bSDmitry Karpeev       opallhead       = *fl;
711*e4d1774bSDmitry Karpeev       (*fl)->next_list = 0;
712*e4d1774bSDmitry Karpeev     } else {
713*e4d1774bSDmitry Karpeev       ne               = opallhead;
714*e4d1774bSDmitry Karpeev       opallhead        = *fl;
715*e4d1774bSDmitry Karpeev       (*fl)->next_list = ne;
716*e4d1774bSDmitry Karpeev     }
717*e4d1774bSDmitry Karpeev   } else {
718*e4d1774bSDmitry Karpeev     /* search list to see if it is already there */
719*e4d1774bSDmitry Karpeev     e  = PETSC_NULL;
720*e4d1774bSDmitry Karpeev     ne = *fl;
721*e4d1774bSDmitry Karpeev     while (ne) {
722*e4d1774bSDmitry Karpeev       PetscBool  match;
723*e4d1774bSDmitry Karpeev       ierr = PetscStrcmp(ne->op,op,&match);CHKERRQ(ierr);
724*e4d1774bSDmitry Karpeev       if(!match) goto next;
725*e4d1774bSDmitry Karpeev       if(numArgs == ne->numArgs)
726*e4d1774bSDmitry Karpeev         match = PETSC_TRUE;
727*e4d1774bSDmitry Karpeev       else
728*e4d1774bSDmitry Karpeev         match = PETSC_FALSE;
729*e4d1774bSDmitry Karpeev       if(!match) goto next;
730*e4d1774bSDmitry Karpeev       if(numArgs) {
731*e4d1774bSDmitry Karpeev         va_start(ap,numArgs);
732*e4d1774bSDmitry Karpeev         for(i = 0; i < numArgs; ++i) {
733*e4d1774bSDmitry Karpeev           argType = va_arg(ap,const char*);
734*e4d1774bSDmitry Karpeev           ierr = PetscStrcmp(argType, entry->argTypes[i], &match);  CHKERRQ(ierr);
735*e4d1774bSDmitry Karpeev           if(!match) {
736*e4d1774bSDmitry Karpeev             va_end(ap);
737*e4d1774bSDmitry Karpeev             goto next;
738*e4d1774bSDmitry Karpeev           }
739*e4d1774bSDmitry Karpeev         }
740*e4d1774bSDmitry Karpeev         va_end(ap);
741*e4d1774bSDmitry Karpeev       }
742*e4d1774bSDmitry Karpeev       if(!url && !fnc) {
743*e4d1774bSDmitry Karpeev         /* remove this record */
744*e4d1774bSDmitry Karpeev         if(e) e->next = ne->next;
745*e4d1774bSDmitry Karpeev         ierr = PetscFree(ne->op);    CHKERRQ(ierr);
746*e4d1774bSDmitry Karpeev         ierr = PetscFree(ne->url);   CHKERRQ(ierr);
747*e4d1774bSDmitry Karpeev         ierr = PetscFree(ne->path);  CHKERRQ(ierr);
748*e4d1774bSDmitry Karpeev         ierr = PetscFree(ne->name);  CHKERRQ(ierr);
749*e4d1774bSDmitry Karpeev         if(numArgs) {
750*e4d1774bSDmitry Karpeev           for(i = 0; i < numArgs; ++i) {
751*e4d1774bSDmitry Karpeev             ierr = PetscFree(ne->argTypes[i]);  CHKERRQ(ierr);
752*e4d1774bSDmitry Karpeev           }
753*e4d1774bSDmitry Karpeev           ierr = PetscFree(ne->argTypes);       CHKERRQ(ierr);
754*e4d1774bSDmitry Karpeev         }
755*e4d1774bSDmitry Karpeev         ierr = PetscFree(ne);                   CHKERRQ(ierr);
756*e4d1774bSDmitry Karpeev       }
757*e4d1774bSDmitry Karpeev       else {
758*e4d1774bSDmitry Karpeev         /* Replace url, fpath, fname and fnc. */
759*e4d1774bSDmitry Karpeev         ierr = PetscStrallocpy(url, &(ne->url)); CHKERRQ(ierr);
760*e4d1774bSDmitry Karpeev         ierr = PetscFListGetPathAndFunction(url,&fpath,&fname);CHKERRQ(ierr);
761*e4d1774bSDmitry Karpeev         ierr = PetscFree(ne->path);CHKERRQ(ierr);
762*e4d1774bSDmitry Karpeev         ierr = PetscFree(ne->name);CHKERRQ(ierr);
763*e4d1774bSDmitry Karpeev         ne->path    = fpath;
764*e4d1774bSDmitry Karpeev         ne->name    = fname;
765*e4d1774bSDmitry Karpeev         ne->routine = fnc;
766*e4d1774bSDmitry Karpeev       }
767*e4d1774bSDmitry Karpeev       PetscFunctionReturn(0);
768*e4d1774bSDmitry Karpeev       next: {e = ne; ne = ne->next;}
769*e4d1774bSDmitry Karpeev     }
770*e4d1774bSDmitry Karpeev     /* create new entry and add to end of list */
771*e4d1774bSDmitry Karpeev     ierr           = PetscNew(struct _n_PetscOpFList,&entry);           CHKERRQ(ierr);
772*e4d1774bSDmitry Karpeev     ierr           = PetscStrallocpy(op,&entry->op);                    CHKERRQ(ierr);
773*e4d1774bSDmitry Karpeev     entry->numArgs = numArgs;
774*e4d1774bSDmitry Karpeev     if(numArgs) {
775*e4d1774bSDmitry Karpeev       va_start(ap,numArgs);
776*e4d1774bSDmitry Karpeev       ierr = PetscMalloc(sizeof(char*)*numArgs, &(entry->argTypes));    CHKERRQ(ierr);
777*e4d1774bSDmitry Karpeev       for(i = 0; i < numArgs; ++i) {
778*e4d1774bSDmitry Karpeev         argType = va_arg(ap,const char*);
779*e4d1774bSDmitry Karpeev         ierr = PetscStrallocpy(argType, &(entry->argTypes[i]));         CHKERRQ(ierr);
780*e4d1774bSDmitry Karpeev       }
781*e4d1774bSDmitry Karpeev       va_end(ap);
782*e4d1774bSDmitry Karpeev     }
783*e4d1774bSDmitry Karpeev     ierr = PetscStrallocpy(url, &(entry->url));                         CHKERRQ(ierr);
784*e4d1774bSDmitry Karpeev     ierr           = PetscFListGetPathAndFunction(url,&fpath,&fname);   CHKERRQ(ierr);
785*e4d1774bSDmitry Karpeev     entry->path    = fpath;
786*e4d1774bSDmitry Karpeev     entry->name    = fname;
787*e4d1774bSDmitry Karpeev     entry->routine = fnc;
788*e4d1774bSDmitry Karpeev     entry->next    = 0;
789*e4d1774bSDmitry Karpeev     ne->next       = entry;
790*e4d1774bSDmitry Karpeev   }
791*e4d1774bSDmitry Karpeev   PetscFunctionReturn(0);
792*e4d1774bSDmitry Karpeev }
793*e4d1774bSDmitry Karpeev 
794*e4d1774bSDmitry Karpeev #undef __FUNCT__
795*e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListDestroy"
796*e4d1774bSDmitry Karpeev /*@C
797*e4d1774bSDmitry Karpeev     PetscOpFListDestroy - Destroys a list of registered op routines.
798*e4d1774bSDmitry Karpeev 
799*e4d1774bSDmitry Karpeev     Input Parameter:
800*e4d1774bSDmitry Karpeev .   fl  - pointer to list
801*e4d1774bSDmitry Karpeev 
802*e4d1774bSDmitry Karpeev     Level: developer
803*e4d1774bSDmitry Karpeev 
804*e4d1774bSDmitry Karpeev .seealso: PetscOpFListAdd(), PetscOpFList
805*e4d1774bSDmitry Karpeev @*/
806*e4d1774bSDmitry Karpeev PetscErrorCode  PetscOpFListDestroy(PetscOpFList *fl)
807*e4d1774bSDmitry Karpeev {
808*e4d1774bSDmitry Karpeev   PetscOpFList     next,entry,tmp;
809*e4d1774bSDmitry Karpeev   PetscErrorCode   ierr;
810*e4d1774bSDmitry Karpeev   PetscInt         i;
811*e4d1774bSDmitry Karpeev 
812*e4d1774bSDmitry Karpeev   PetscFunctionBegin;
813*e4d1774bSDmitry Karpeev   if (!*fl) PetscFunctionReturn(0);
814*e4d1774bSDmitry Karpeev   if (!opallhead) PetscFunctionReturn(0);
815*e4d1774bSDmitry Karpeev 
816*e4d1774bSDmitry Karpeev   /*
817*e4d1774bSDmitry Karpeev        Remove this entry from the master Op list (if it is in it)
818*e4d1774bSDmitry Karpeev   */
819*e4d1774bSDmitry Karpeev   if (opallhead == *fl) {
820*e4d1774bSDmitry Karpeev     if (opallhead->next_list) {
821*e4d1774bSDmitry Karpeev       opallhead = opallhead->next_list;
822*e4d1774bSDmitry Karpeev     } else {
823*e4d1774bSDmitry Karpeev       opallhead = 0;
824*e4d1774bSDmitry Karpeev     }
825*e4d1774bSDmitry Karpeev   } else {
826*e4d1774bSDmitry Karpeev     tmp = opallhead;
827*e4d1774bSDmitry Karpeev     while (tmp->next_list != *fl) {
828*e4d1774bSDmitry Karpeev       tmp = tmp->next_list;
829*e4d1774bSDmitry Karpeev       if (!tmp->next_list) break;
830*e4d1774bSDmitry Karpeev     }
831*e4d1774bSDmitry Karpeev     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
832*e4d1774bSDmitry Karpeev   }
833*e4d1774bSDmitry Karpeev 
834*e4d1774bSDmitry Karpeev   /* free this list */
835*e4d1774bSDmitry Karpeev   entry = *fl;
836*e4d1774bSDmitry Karpeev   while (entry) {
837*e4d1774bSDmitry Karpeev     next = entry->next;
838*e4d1774bSDmitry Karpeev     ierr = PetscFree(entry->op);  CHKERRQ(ierr);
839*e4d1774bSDmitry Karpeev     for(i = 0; i < entry->numArgs; ++i) {
840*e4d1774bSDmitry Karpeev       ierr = PetscFree(entry->argTypes[i]); CHKERRQ(ierr);
841*e4d1774bSDmitry Karpeev     }
842*e4d1774bSDmitry Karpeev     ierr = PetscFree(entry->argTypes);  CHKERRQ(ierr);
843*e4d1774bSDmitry Karpeev     ierr = PetscFree(entry->url);CHKERRQ(ierr);
844*e4d1774bSDmitry Karpeev     ierr = PetscFree(entry->path);CHKERRQ(ierr);
845*e4d1774bSDmitry Karpeev     ierr = PetscFree(entry->name);CHKERRQ(ierr);
846*e4d1774bSDmitry Karpeev     ierr = PetscFree(entry);CHKERRQ(ierr);
847*e4d1774bSDmitry Karpeev     entry = next;
848*e4d1774bSDmitry Karpeev   }
849*e4d1774bSDmitry Karpeev   *fl = 0;
850*e4d1774bSDmitry Karpeev   PetscFunctionReturn(0);
851*e4d1774bSDmitry Karpeev }
852*e4d1774bSDmitry Karpeev 
853*e4d1774bSDmitry Karpeev /*
854*e4d1774bSDmitry Karpeev    Destroys all the function lists that anyone has every registered, such as MatOpList, etc.
855*e4d1774bSDmitry Karpeev */
856*e4d1774bSDmitry Karpeev #undef __FUNCT__
857*e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListDestroyAll"
858*e4d1774bSDmitry Karpeev PetscErrorCode  PetscOpFListDestroyAll(void)
859*e4d1774bSDmitry Karpeev {
860*e4d1774bSDmitry Karpeev   PetscOpFList     tmp2,tmp1 = opallhead;
861*e4d1774bSDmitry Karpeev   PetscErrorCode ierr;
862*e4d1774bSDmitry Karpeev 
863*e4d1774bSDmitry Karpeev   PetscFunctionBegin;
864*e4d1774bSDmitry Karpeev   while (tmp1) {
865*e4d1774bSDmitry Karpeev     tmp2 = tmp1->next_list;
866*e4d1774bSDmitry Karpeev     ierr = PetscOpFListDestroy(&tmp1);CHKERRQ(ierr);
867*e4d1774bSDmitry Karpeev     tmp1 = tmp2;
868*e4d1774bSDmitry Karpeev   }
869*e4d1774bSDmitry Karpeev   opallhead = 0;
870*e4d1774bSDmitry Karpeev   PetscFunctionReturn(0);
871*e4d1774bSDmitry Karpeev }
872*e4d1774bSDmitry Karpeev 
873*e4d1774bSDmitry Karpeev #undef __FUNCT__
874*e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListFind"
875*e4d1774bSDmitry Karpeev /*@C
876*e4d1774bSDmitry Karpeev     PetscOpFListFind - Given a name, finds the matching op routine.
877*e4d1774bSDmitry Karpeev 
878*e4d1774bSDmitry Karpeev     Input Parameters:
879*e4d1774bSDmitry Karpeev +   comm    - processes looking for the op
880*e4d1774bSDmitry Karpeev .   fl      - pointer to list of known ops
881*e4d1774bSDmitry Karpeev .   op      - operation name
882*e4d1774bSDmitry Karpeev .   numArgs - number of op arguments
883*e4d1774bSDmitry Karpeev -   ...     - list of argument type names (const char*)
884*e4d1774bSDmitry Karpeev 
885*e4d1774bSDmitry Karpeev 
886*e4d1774bSDmitry Karpeev     Output Parameters:
887*e4d1774bSDmitry Karpeev .   r       - routine implementing op with the given arg types
888*e4d1774bSDmitry Karpeev 
889*e4d1774bSDmitry Karpeev     Level: developer
890*e4d1774bSDmitry Karpeev 
891*e4d1774bSDmitry Karpeev .seealso: PetscOpFListAdd(), PetscOpFList
892*e4d1774bSDmitry Karpeev @*/
893*e4d1774bSDmitry Karpeev PetscErrorCode  PetscOpFListFind(MPI_Comm comm, PetscOpFList fl,void (**r)(void), const char* op, PetscInt numArgs, ...)
894*e4d1774bSDmitry Karpeev {
895*e4d1774bSDmitry Karpeev   PetscOpFList   entry;
896*e4d1774bSDmitry Karpeev   PetscErrorCode ierr;
897*e4d1774bSDmitry Karpeev   PetscBool      match;
898*e4d1774bSDmitry Karpeev   const char     *argType;
899*e4d1774bSDmitry Karpeev   PetscInt       i;
900*e4d1774bSDmitry Karpeev   va_list        ap;
901*e4d1774bSDmitry Karpeev 
902*e4d1774bSDmitry Karpeev   PetscFunctionBegin;
903*e4d1774bSDmitry Karpeev   PetscValidPointer(r,3);
904*e4d1774bSDmitry Karpeev   if (!op) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Attempting to find operation with null name");
905*e4d1774bSDmitry Karpeev   *r = PETSC_NULL;
906*e4d1774bSDmitry Karpeev   entry = fl;
907*e4d1774bSDmitry Karpeev   while (entry) {
908*e4d1774bSDmitry Karpeev     ierr = PetscStrcmp(entry->op,op,&match); CHKERRQ(ierr);
909*e4d1774bSDmitry Karpeev     if(!match) goto next;
910*e4d1774bSDmitry Karpeev     if(numArgs == entry->numArgs)
911*e4d1774bSDmitry Karpeev       match = PETSC_TRUE;
912*e4d1774bSDmitry Karpeev     else
913*e4d1774bSDmitry Karpeev       match = PETSC_FALSE;
914*e4d1774bSDmitry Karpeev     if(!match) goto next;
915*e4d1774bSDmitry Karpeev     if(numArgs) {
916*e4d1774bSDmitry Karpeev       va_start(ap,numArgs);
917*e4d1774bSDmitry Karpeev       for(i = 0; i < numArgs; ++i) {
918*e4d1774bSDmitry Karpeev         argType = va_arg(ap,const char*);
919*e4d1774bSDmitry Karpeev         ierr = PetscStrcmp(argType, entry->argTypes[i], &match);  CHKERRQ(ierr);
920*e4d1774bSDmitry Karpeev         if(!match) {
921*e4d1774bSDmitry Karpeev           va_end(ap);
922*e4d1774bSDmitry Karpeev           goto next;
923*e4d1774bSDmitry Karpeev         }
924*e4d1774bSDmitry Karpeev       }
925*e4d1774bSDmitry Karpeev       va_end(ap);
926*e4d1774bSDmitry Karpeev     }
927*e4d1774bSDmitry Karpeev     next: entry = entry->next;
928*e4d1774bSDmitry Karpeev   }
929*e4d1774bSDmitry Karpeev   if (match) {
930*e4d1774bSDmitry Karpeev     if (entry->routine) {
931*e4d1774bSDmitry Karpeev       *r   = entry->routine;
932*e4d1774bSDmitry Karpeev     }
933*e4d1774bSDmitry Karpeev #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
934*e4d1774bSDmitry Karpeev     else {
935*e4d1774bSDmitry Karpeev       /* it is not yet in memory so load from dynamic library */
936*e4d1774bSDmitry Karpeev       ierr = PetscDLLibrarySym(comm,&DLLibrariesLoaded,entry->path,entry->name,(void **)r);CHKERRQ(ierr);
937*e4d1774bSDmitry Karpeev       if (*r) {
938*e4d1774bSDmitry Karpeev         entry->routine = *r;
939*e4d1774bSDmitry Karpeev       }
940*e4d1774bSDmitry Karpeev     }
941*e4d1774bSDmitry Karpeev #endif
942*e4d1774bSDmitry Karpeev   }
943*e4d1774bSDmitry Karpeev 
944*e4d1774bSDmitry Karpeev   PetscFunctionReturn(0);
945*e4d1774bSDmitry Karpeev }
946*e4d1774bSDmitry Karpeev 
947*e4d1774bSDmitry Karpeev #undef __FUNCT__
948*e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListView"
949*e4d1774bSDmitry Karpeev /*@C
950*e4d1774bSDmitry Karpeev    PetscOpFListView - prints out contents of a PetscOpFList
951*e4d1774bSDmitry Karpeev 
952*e4d1774bSDmitry Karpeev    Collective on viewer
953*e4d1774bSDmitry Karpeev 
954*e4d1774bSDmitry Karpeev    Input Parameters:
955*e4d1774bSDmitry Karpeev +  list   - the list of functions
956*e4d1774bSDmitry Karpeev -  viewer - ASCII viewer
957*e4d1774bSDmitry Karpeev 
958*e4d1774bSDmitry Karpeev    Level: developer
959*e4d1774bSDmitry Karpeev 
960*e4d1774bSDmitry Karpeev .seealso: PetscOpFListAdd(), PetscOpFList
961*e4d1774bSDmitry Karpeev @*/
962*e4d1774bSDmitry Karpeev PetscErrorCode  PetscOpFListView(PetscOpFList list,PetscViewer viewer)
963*e4d1774bSDmitry Karpeev {
964*e4d1774bSDmitry Karpeev   PetscErrorCode ierr;
965*e4d1774bSDmitry Karpeev   PetscBool      iascii;
966*e4d1774bSDmitry Karpeev   PetscInt       i;
967*e4d1774bSDmitry Karpeev 
968*e4d1774bSDmitry Karpeev   PetscFunctionBegin;
969*e4d1774bSDmitry Karpeev   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
970*e4d1774bSDmitry Karpeev   PetscValidPointer(list,1);
971*e4d1774bSDmitry Karpeev   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
972*e4d1774bSDmitry Karpeev 
973*e4d1774bSDmitry Karpeev   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
974*e4d1774bSDmitry Karpeev   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
975*e4d1774bSDmitry Karpeev 
976*e4d1774bSDmitry Karpeev   while (list) {
977*e4d1774bSDmitry Karpeev     if (list->url) {
978*e4d1774bSDmitry Karpeev       ierr = PetscViewerASCIIPrintf(viewer," %s: ",list->url); CHKERRQ(ierr);
979*e4d1774bSDmitry Karpeev     }
980*e4d1774bSDmitry Karpeev     ierr = PetscViewerASCIIPrintf(viewer, "%s(", list->op);    CHKERRQ(ierr);
981*e4d1774bSDmitry Karpeev     for(i = 0; i < list->numArgs;++i) {
982*e4d1774bSDmitry Karpeev       if(i > 0) {
983*e4d1774bSDmitry Karpeev         ierr = PetscViewerASCIIPrintf(viewer, ", "); CHKERRQ(ierr);
984*e4d1774bSDmitry Karpeev       }
985*e4d1774bSDmitry Karpeev       ierr = PetscViewerASCIIPrintf(viewer, "%s", list->argTypes[i]);    CHKERRQ(ierr);
986*e4d1774bSDmitry Karpeev     }
987*e4d1774bSDmitry Karpeev     ierr = PetscViewerASCIIPrintf(viewer, ")\n");    CHKERRQ(ierr);
988*e4d1774bSDmitry Karpeev     list = list->next;
989*e4d1774bSDmitry Karpeev   }
990*e4d1774bSDmitry Karpeev   PetscFunctionReturn(0);
991*e4d1774bSDmitry Karpeev }
992