xref: /petsc/src/sys/dll/reg.c (revision a8a6328f83d9a300c338659cfcc920a28066f738)
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 
14aa2d57e9SJed Brown #if defined(PETSC_HAVE_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;
53aa2d57e9SJed Brown #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
54aa2d57e9SJed Brown   PetscBool      preload;
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 
65aa2d57e9SJed Brown #if !defined(PETSC_HAVE_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
74aa2d57e9SJed Brown   preload = PETSC_FALSE;
75aa2d57e9SJed Brown   ierr = PetscOptionsGetBool(NULL,"-dynamic_library_preload",&preload,NULL);CHKERRQ(ierr);
76aa2d57e9SJed Brown   if (preload) {
77aa2d57e9SJed Brown     PetscBool found;
7860154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY)
79487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
80e32f2f54SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
8160154eb2SBarry Smith #else
8260154eb2SBarry Smith     ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
8360154eb2SBarry Smith     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
84487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
85e32f2f54SBarry 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!");
86487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
87e32f2f54SBarry 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!");
88487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
89e32f2f54SBarry 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!");
90487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
91e32f2f54SBarry 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!");
92487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
93e32f2f54SBarry 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!");
94487e5849SBarry Smith     ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
95e32f2f54SBarry 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!");
96bb84e0fdSBarry Smith #endif
97aa2d57e9SJed Brown   }
983fa76a5bSLisandro Dalcin #endif
99e5c89e4eSSatish Balay 
100e5c89e4eSSatish Balay   nmax = 32;
1010298fd71SBarry Smith   ierr = PetscOptionsGetStringArray(NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr);
102e5c89e4eSSatish Balay   for (i=0; i<nmax; i++) {
103d44a1e48SBarry Smith     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
104e5c89e4eSSatish Balay     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
105e5c89e4eSSatish Balay   }
1063020f62cSBarry Smith 
1073020f62cSBarry Smith   ierr = PetscThreadCommInitializePackage();CHKERRQ(ierr);
1083020f62cSBarry Smith   ierr = PetscThreadCommWorldInitialize();CHKERRQ(ierr);
1093020f62cSBarry Smith   ierr = AOInitializePackage();CHKERRQ(ierr);
1103020f62cSBarry Smith   ierr = PetscSFInitializePackage();CHKERRQ(ierr);
1113020f62cSBarry Smith   ierr = CharacteristicInitializePackage();CHKERRQ(ierr);
1123020f62cSBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
1133020f62cSBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
1143020f62cSBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1153020f62cSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
1163020f62cSBarry Smith   ierr = PCInitializePackage();CHKERRQ(ierr);
1173020f62cSBarry Smith   ierr = KSPInitializePackage();CHKERRQ(ierr);
1183020f62cSBarry Smith   ierr = SNESInitializePackage();CHKERRQ(ierr);
1193020f62cSBarry Smith   ierr = TSInitializePackage();CHKERRQ(ierr);
1203020f62cSBarry Smith   {
1213020f62cSBarry Smith     MPI_Comm comm;
1223020f62cSBarry Smith     ierr = PetscCommDuplicate(PETSC_COMM_SELF,&comm,NULL);CHKERRQ(ierr);
1233020f62cSBarry Smith     ierr = PetscCommDuplicate(PETSC_COMM_WORLD,&comm,NULL);CHKERRQ(ierr);
1243020f62cSBarry Smith   }
125e5c89e4eSSatish Balay   PetscFunctionReturn(0);
126e5c89e4eSSatish Balay }
127e5c89e4eSSatish Balay 
128e5c89e4eSSatish Balay #undef __FUNCT__
129e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize_DynamicLibraries"
130ebd79076SLisandro Dalcin /*
131ebd79076SLisandro Dalcin      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
132ebd79076SLisandro Dalcin */
133e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void)
134e5c89e4eSSatish Balay {
135ebd79076SLisandro Dalcin   PetscErrorCode ierr;
136ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
137e5c89e4eSSatish Balay 
138ebd79076SLisandro Dalcin   PetscFunctionBegin;
1390298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
140d44a1e48SBarry Smith   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
141d44a1e48SBarry Smith   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
142a297a907SKarl Rupp 
143d44a1e48SBarry Smith   PetscDLLibrariesLoaded = 0;
144e5c89e4eSSatish Balay   PetscFunctionReturn(0);
145e5c89e4eSSatish Balay }
146e5c89e4eSSatish Balay 
147e4d1774bSDmitry Karpeev 
148e4d1774bSDmitry Karpeev 
149e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
150140e18c1SBarry Smith struct _n_PetscFunctionList {
151e5c89e4eSSatish Balay   void              (*routine)(void);    /* the routine */
152e5c89e4eSSatish Balay   char              *name;               /* string to identify routine */
153140e18c1SBarry Smith   PetscFunctionList next;                /* next pointer */
154140e18c1SBarry Smith   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
155e5c89e4eSSatish Balay };
156e5c89e4eSSatish Balay 
157e5c89e4eSSatish Balay /*
158140e18c1SBarry Smith      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
159e5c89e4eSSatish Balay */
160140e18c1SBarry Smith static PetscFunctionList dlallhead = 0;
161e5c89e4eSSatish Balay 
162a240a19fSJed Brown /*MC
163140e18c1SBarry Smith    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
164e5c89e4eSSatish Balay    specified registry.
165e5c89e4eSSatish Balay 
166a240a19fSJed Brown    Synopsis:
167aaa7dc30SBarry Smith    #include <petscsys.h>
168a240a19fSJed Brown    PetscErrorCode PetscFunctionListAdd(PetscFunctionList flist,const char name[],void (*fptr)(void))
169a240a19fSJed Brown 
170d4349b43SBarry Smith    Not Collective
171e5c89e4eSSatish Balay 
172e5c89e4eSSatish Balay    Input Parameters:
173a240a19fSJed Brown +  flist - pointer registry
174e5c89e4eSSatish Balay .  name - string to identify routine
175a240a19fSJed Brown -  fptr - function pointer
176e5c89e4eSSatish Balay 
177e5c89e4eSSatish Balay    Notes:
178a240a19fSJed Brown    To remove a registered routine, pass in a NULL fptr.
179e5c89e4eSSatish Balay 
180e5c89e4eSSatish Balay    Users who wish to register new classes for use by a particular PETSc
181e5c89e4eSSatish Balay    component (e.g., SNES) should generally call the registration routine
1821c84c290SBarry Smith    for that particular component (e.g., SNESRegister()) instead of
183140e18c1SBarry Smith    calling PetscFunctionListAdd() directly.
184e5c89e4eSSatish Balay 
185e5c89e4eSSatish Balay     Level: developer
186e5c89e4eSSatish Balay 
1871c84c290SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(),
188a240a19fSJed Brown           PCRegister(), TSRegister(), PetscFunctionList, PetscObjectComposeFunction()
189a240a19fSJed Brown M*/
190a240a19fSJed Brown #undef __FUNCT__
191a240a19fSJed Brown #define __FUNCT__ "PetscFunctionListAdd_Private"
192a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl,const char name[],void (*fnc)(void))
193e5c89e4eSSatish Balay {
194140e18c1SBarry Smith   PetscFunctionList entry,ne;
195e5c89e4eSSatish Balay   PetscErrorCode    ierr;
196e5c89e4eSSatish Balay 
197e5c89e4eSSatish Balay   PetscFunctionBegin;
198e5c89e4eSSatish Balay   if (!*fl) {
199b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
200e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
201e5c89e4eSSatish Balay     entry->routine = fnc;
202e5c89e4eSSatish Balay     entry->next    = 0;
203e5c89e4eSSatish Balay     *fl            = entry;
204e5c89e4eSSatish Balay 
205*a8a6328fSBarry Smith #if defined(PETSC_USE_LOG)
206e5c89e4eSSatish Balay     /* add this new list to list of all lists */
207e5c89e4eSSatish Balay     if (!dlallhead) {
208e5c89e4eSSatish Balay       dlallhead        = *fl;
209e5c89e4eSSatish Balay       (*fl)->next_list = 0;
210e5c89e4eSSatish Balay     } else {
211e5c89e4eSSatish Balay       ne               = dlallhead;
212e5c89e4eSSatish Balay       dlallhead        = *fl;
213e5c89e4eSSatish Balay       (*fl)->next_list = ne;
214e5c89e4eSSatish Balay     }
215*a8a6328fSBarry Smith #endif
216*a8a6328fSBarry Smith 
217e5c89e4eSSatish Balay   } else {
218e5c89e4eSSatish Balay     /* search list to see if it is already there */
219e5c89e4eSSatish Balay     ne = *fl;
220e5c89e4eSSatish Balay     while (ne) {
221ace3abfcSBarry Smith       PetscBool founddup;
222e5c89e4eSSatish Balay 
223e5c89e4eSSatish Balay       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
224e5c89e4eSSatish Balay       if (founddup) { /* found duplicate */
225e5c89e4eSSatish Balay         ne->routine = fnc;
226e5c89e4eSSatish Balay         PetscFunctionReturn(0);
227e5c89e4eSSatish Balay       }
228a297a907SKarl Rupp       if (ne->next) ne = ne->next;
229a297a907SKarl Rupp       else break;
230e5c89e4eSSatish Balay     }
231e5c89e4eSSatish Balay     /* create new entry and add to end of list */
232b00a9115SJed Brown     ierr           = PetscNew(&entry);CHKERRQ(ierr);
233e5c89e4eSSatish Balay     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
234e5c89e4eSSatish Balay     entry->routine = fnc;
235e5c89e4eSSatish Balay     entry->next    = 0;
236e5c89e4eSSatish Balay     ne->next       = entry;
237e5c89e4eSSatish Balay   }
238e5c89e4eSSatish Balay   PetscFunctionReturn(0);
239e5c89e4eSSatish Balay }
240e5c89e4eSSatish Balay 
241e5c89e4eSSatish Balay #undef __FUNCT__
242140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroy"
243e5c89e4eSSatish Balay /*@
244140e18c1SBarry Smith     PetscFunctionListDestroy - Destroys a list of registered routines.
245e5c89e4eSSatish Balay 
246e5c89e4eSSatish Balay     Input Parameter:
247e5c89e4eSSatish Balay .   fl  - pointer to list
248e5c89e4eSSatish Balay 
249e5c89e4eSSatish Balay     Level: developer
250e5c89e4eSSatish Balay 
251a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
252e5c89e4eSSatish Balay @*/
253140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
254e5c89e4eSSatish Balay {
255140e18c1SBarry Smith   PetscFunctionList next,entry,tmp = dlallhead;
256e5c89e4eSSatish Balay   PetscErrorCode    ierr;
257e5c89e4eSSatish Balay 
258e5c89e4eSSatish Balay   PetscFunctionBegin;
2591441b1d3SBarry Smith   if (!*fl) PetscFunctionReturn(0);
260e5c89e4eSSatish Balay 
261e5c89e4eSSatish Balay   /*
262e5c89e4eSSatish Balay        Remove this entry from the master DL list (if it is in it)
263e5c89e4eSSatish Balay   */
2641441b1d3SBarry Smith   if (dlallhead == *fl) {
265a297a907SKarl Rupp     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
26637e93019SBarry Smith     else dlallhead = NULL;
26737e93019SBarry Smith   } else if (tmp) {
2681441b1d3SBarry Smith     while (tmp->next_list != *fl) {
269e5c89e4eSSatish Balay       tmp = tmp->next_list;
270e5c89e4eSSatish Balay       if (!tmp->next_list) break;
271e5c89e4eSSatish Balay     }
272e5c89e4eSSatish Balay     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
273e5c89e4eSSatish Balay   }
274e5c89e4eSSatish Balay 
275e5c89e4eSSatish Balay   /* free this list */
2761441b1d3SBarry Smith   entry = *fl;
277e5c89e4eSSatish Balay   while (entry) {
278e5c89e4eSSatish Balay     next  = entry->next;
279e5c89e4eSSatish Balay     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
280e5c89e4eSSatish Balay     ierr  = PetscFree(entry);CHKERRQ(ierr);
281e5c89e4eSSatish Balay     entry = next;
282e5c89e4eSSatish Balay   }
2831441b1d3SBarry Smith   *fl = 0;
284e5c89e4eSSatish Balay   PetscFunctionReturn(0);
285e5c89e4eSSatish Balay }
286e5c89e4eSSatish Balay 
287e5c89e4eSSatish Balay /*
28837e93019SBarry Smith    Print any PetscFunctionLists that have not be destroyed
289e5c89e4eSSatish Balay */
290e5c89e4eSSatish Balay #undef __FUNCT__
29137e93019SBarry Smith #define __FUNCT__ "PetscFunctionListPrintAll"
29237e93019SBarry Smith PetscErrorCode  PetscFunctionListPrintAll(void)
293e5c89e4eSSatish Balay {
29437e93019SBarry Smith   PetscFunctionList tmp = dlallhead;
295e5c89e4eSSatish Balay   PetscErrorCode    ierr;
296e5c89e4eSSatish Balay 
297e5c89e4eSSatish Balay   PetscFunctionBegin;
29837e93019SBarry Smith   if (tmp) {
29937e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"The following PetscFunctionLists were not destroyed\n");CHKERRQ(ierr);
300e5c89e4eSSatish Balay   }
30137e93019SBarry Smith   while (tmp) {
30237e93019SBarry Smith     ierr = PetscPrintf(PETSC_COMM_WORLD,"%s \n",tmp->name);CHKERRQ(ierr);
30337e93019SBarry Smith     tmp = tmp->next_list;
30437e93019SBarry Smith   }
305e5c89e4eSSatish Balay   PetscFunctionReturn(0);
306e5c89e4eSSatish Balay }
307e5c89e4eSSatish Balay 
3081c9cd337SJed Brown /*MC
3091c9cd337SJed Brown     PetscFunctionListFind - Find function registered under given name
3101c9cd337SJed Brown 
3111c9cd337SJed Brown     Synopsis:
312aaa7dc30SBarry Smith     #include <petscsys.h>
3131c9cd337SJed Brown     PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void))
314e5c89e4eSSatish Balay 
315e5c89e4eSSatish Balay     Input Parameters:
3161c9cd337SJed Brown +   flist   - pointer to list
3171c9cd337SJed Brown -   name - name registered for the function
318e5c89e4eSSatish Balay 
319e5c89e4eSSatish Balay     Output Parameters:
3201c9cd337SJed Brown .   fptr - the function pointer if name was found, else NULL
321e5c89e4eSSatish Balay 
322e5c89e4eSSatish Balay     Level: developer
323e5c89e4eSSatish Balay 
3241c9cd337SJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList, PetscObjectQueryFunction()
3251c9cd337SJed Brown M*/
3261c9cd337SJed Brown #undef __FUNCT__
3271c9cd337SJed Brown #define __FUNCT__ "PetscFunctionListFind_Private"
3281c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void))
329e5c89e4eSSatish Balay {
330140e18c1SBarry Smith   PetscFunctionList entry = fl;
331e5c89e4eSSatish Balay   PetscErrorCode    ierr;
332bdf89e91SBarry Smith   PetscBool         flg;
333e5c89e4eSSatish Balay 
334e5c89e4eSSatish Balay   PetscFunctionBegin;
335e32f2f54SBarry Smith   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
336e5c89e4eSSatish Balay 
337e5c89e4eSSatish Balay   *r = 0;
338e5c89e4eSSatish Balay   while (entry) {
339bdf89e91SBarry Smith     ierr = PetscStrcmp(name,entry->name,&flg);CHKERRQ(ierr);
340bdf89e91SBarry Smith     if (flg) {
341e5c89e4eSSatish Balay       *r   = entry->routine;
342e5c89e4eSSatish Balay       PetscFunctionReturn(0);
343e5c89e4eSSatish Balay     }
344e5c89e4eSSatish Balay     entry = entry->next;
345e5c89e4eSSatish Balay   }
346e5c89e4eSSatish Balay   PetscFunctionReturn(0);
347e5c89e4eSSatish Balay }
348e5c89e4eSSatish Balay 
349e5c89e4eSSatish Balay #undef __FUNCT__
350140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListView"
351e5c89e4eSSatish Balay /*@
352140e18c1SBarry Smith    PetscFunctionListView - prints out contents of an PetscFunctionList
353e5c89e4eSSatish Balay 
354e5c89e4eSSatish Balay    Collective over MPI_Comm
355e5c89e4eSSatish Balay 
356e5c89e4eSSatish Balay    Input Parameters:
357e5c89e4eSSatish Balay +  list - the list of functions
358e5c89e4eSSatish Balay -  viewer - currently ignored
359e5c89e4eSSatish Balay 
360e5c89e4eSSatish Balay    Level: developer
361e5c89e4eSSatish Balay 
362a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionListPrintTypes(), PetscFunctionList
363e5c89e4eSSatish Balay @*/
364140e18c1SBarry Smith PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
365e5c89e4eSSatish Balay {
366e5c89e4eSSatish Balay   PetscErrorCode ierr;
367ace3abfcSBarry Smith   PetscBool      iascii;
368e5c89e4eSSatish Balay 
369e5c89e4eSSatish Balay   PetscFunctionBegin;
370e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
371e5c89e4eSSatish Balay   PetscValidPointer(list,1);
3720700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
373e5c89e4eSSatish Balay 
374251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
375e32f2f54SBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
376e5c89e4eSSatish Balay 
377e5c89e4eSSatish Balay   while (list) {
378bdf89e91SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %s\n",list->name);CHKERRQ(ierr);
379e5c89e4eSSatish Balay     list = list->next;
380e5c89e4eSSatish Balay   }
381e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
382e5c89e4eSSatish Balay   PetscFunctionReturn(0);
383e5c89e4eSSatish Balay }
384e5c89e4eSSatish Balay 
385e5c89e4eSSatish Balay #undef __FUNCT__
386140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGet"
387065533a5SJed Brown /*@C
388140e18c1SBarry Smith    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
389e5c89e4eSSatish Balay          by help etc.
390e5c89e4eSSatish Balay 
391d4349b43SBarry Smith    Not Collective
392e5c89e4eSSatish Balay 
393e5c89e4eSSatish Balay    Input Parameter:
394e5c89e4eSSatish Balay .  list   - list of types
395e5c89e4eSSatish Balay 
396e5c89e4eSSatish Balay    Output Parameter:
397e5c89e4eSSatish Balay +  array - array of names
398e5c89e4eSSatish Balay -  n - length of array
399e5c89e4eSSatish Balay 
400e5c89e4eSSatish Balay    Notes:
401e5c89e4eSSatish Balay        This allocates the array so that must be freed. BUT the individual entries are
402e5c89e4eSSatish Balay     not copied so should not be freed.
403e5c89e4eSSatish Balay 
404e5c89e4eSSatish Balay    Level: developer
405e5c89e4eSSatish Balay 
406a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
407e5c89e4eSSatish Balay @*/
408140e18c1SBarry Smith PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
409e5c89e4eSSatish Balay {
410e5c89e4eSSatish Balay   PetscErrorCode    ierr;
411e5c89e4eSSatish Balay   PetscInt          count = 0;
412140e18c1SBarry Smith   PetscFunctionList klist = list;
413e5c89e4eSSatish Balay 
414e5c89e4eSSatish Balay   PetscFunctionBegin;
415e5c89e4eSSatish Balay   while (list) {
416e5c89e4eSSatish Balay     list = list->next;
417e5c89e4eSSatish Balay     count++;
418e5c89e4eSSatish Balay   }
419854ce69bSBarry Smith   ierr  = PetscMalloc1(count+1,array);CHKERRQ(ierr);
420e5c89e4eSSatish Balay   count = 0;
421e5c89e4eSSatish Balay   while (klist) {
422e5c89e4eSSatish Balay     (*array)[count] = klist->name;
423e5c89e4eSSatish Balay     klist           = klist->next;
424e5c89e4eSSatish Balay     count++;
425e5c89e4eSSatish Balay   }
426e5c89e4eSSatish Balay   (*array)[count] = 0;
427e5c89e4eSSatish Balay   *n              = count+1;
428e5c89e4eSSatish Balay   PetscFunctionReturn(0);
429e5c89e4eSSatish Balay }
430e5c89e4eSSatish Balay 
431e5c89e4eSSatish Balay 
432e5c89e4eSSatish Balay #undef __FUNCT__
433140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListPrintTypes"
434e5c89e4eSSatish Balay /*@C
435140e18c1SBarry Smith    PetscFunctionListPrintTypes - Prints the methods available.
436e5c89e4eSSatish Balay 
437e5c89e4eSSatish Balay    Collective over MPI_Comm
438e5c89e4eSSatish Balay 
439e5c89e4eSSatish Balay    Input Parameters:
440e5c89e4eSSatish Balay +  comm   - the communicator (usually MPI_COMM_WORLD)
441e5c89e4eSSatish Balay .  fd     - file to print to, usually stdout
442e5c89e4eSSatish Balay .  prefix - prefix to prepend to name (optional)
443e5c89e4eSSatish Balay .  name   - option string (for example, "-ksp_type")
444e5c89e4eSSatish Balay .  text - short description of the object (for example, "Krylov solvers")
445e5c89e4eSSatish Balay .  man - name of manual page that discusses the object (for example, "KSPCreate")
4463cc1e11dSBarry Smith .  list   - list of types
4473cc1e11dSBarry Smith -  def - default (current) value
448e5c89e4eSSatish Balay 
449e5c89e4eSSatish Balay    Level: developer
450e5c89e4eSSatish Balay 
451a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList
452e5c89e4eSSatish Balay @*/
453140e18c1SBarry 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[])
454e5c89e4eSSatish Balay {
455e5c89e4eSSatish Balay   PetscErrorCode ierr;
456e5c89e4eSSatish Balay   PetscInt       count = 0;
457e5c89e4eSSatish Balay   char           p[64];
458e5c89e4eSSatish Balay 
459e5c89e4eSSatish Balay   PetscFunctionBegin;
460da9f1d6bSBarry Smith   if (!fd) fd = PETSC_STDOUT;
461e5c89e4eSSatish Balay 
462e5c89e4eSSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
463e5c89e4eSSatish Balay   if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);}
4643cc1e11dSBarry Smith   ierr = PetscFPrintf(comm,fd,"  %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr);
465e5c89e4eSSatish Balay 
466e5c89e4eSSatish Balay   while (list) {
467e5c89e4eSSatish Balay     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
468e5c89e4eSSatish Balay     list = list->next;
469e5c89e4eSSatish Balay     count++;
470e5c89e4eSSatish Balay     if (count == 8) {ierr = PetscFPrintf(comm,fd,"\n     ");CHKERRQ(ierr);}
471e5c89e4eSSatish Balay   }
472e5c89e4eSSatish Balay   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
473e5c89e4eSSatish Balay   PetscFunctionReturn(0);
474e5c89e4eSSatish Balay }
475e5c89e4eSSatish Balay 
476e5c89e4eSSatish Balay #undef __FUNCT__
477140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDuplicate"
478e5c89e4eSSatish Balay /*@
479140e18c1SBarry Smith     PetscFunctionListDuplicate - Creates a new list from a given object list.
480e5c89e4eSSatish Balay 
481e5c89e4eSSatish Balay     Input Parameters:
482e5c89e4eSSatish Balay .   fl   - pointer to list
483e5c89e4eSSatish Balay 
484e5c89e4eSSatish Balay     Output Parameters:
485e5c89e4eSSatish Balay .   nl - the new list (should point to 0 to start, otherwise appends)
486e5c89e4eSSatish Balay 
487e5c89e4eSSatish Balay     Level: developer
488e5c89e4eSSatish Balay 
489140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
490e5c89e4eSSatish Balay 
491e5c89e4eSSatish Balay @*/
492140e18c1SBarry Smith PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
493e5c89e4eSSatish Balay {
494e5c89e4eSSatish Balay   PetscErrorCode ierr;
495e5c89e4eSSatish Balay 
496e5c89e4eSSatish Balay   PetscFunctionBegin;
497e5c89e4eSSatish Balay   while (fl) {
498bdf89e91SBarry Smith     ierr = PetscFunctionListAdd(nl,fl->name,fl->routine);CHKERRQ(ierr);
499e5c89e4eSSatish Balay     fl   = fl->next;
500e5c89e4eSSatish Balay   }
501e5c89e4eSSatish Balay   PetscFunctionReturn(0);
502e5c89e4eSSatish Balay }
503e5c89e4eSSatish Balay 
504