xref: /petsc/src/sys/dll/reg.c (revision 3020f62cd83fd2f9ec68f231e4cbf642612344ef)
1 
2 /*
3     Provides a general mechanism to allow one to register new routines in
4     dynamic libraries for many of the PETSc objects (including, e.g., KSP and PC).
5 */
6 #include <petsc-private/petscimpl.h>           /*I "petscsys.h" I*/
7 #include <petscviewer.h>
8 
9 /*
10     This is the default list used by PETSc with the PetscDLLibrary register routines
11 */
12 PetscDLLibrary PetscDLLibrariesLoaded = 0;
13 
14 #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
15 
16 #undef __FUNCT__
17 #define __FUNCT__ "PetscLoadDynamicLibrary"
18 static PetscErrorCode  PetscLoadDynamicLibrary(const char *name,PetscBool  *found)
19 {
20   char           libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN];
21   PetscErrorCode ierr;
22 
23   PetscFunctionBegin;
24   ierr = PetscStrcpy(libs,"${PETSC_LIB_DIR}/libpetsc");CHKERRQ(ierr);
25   ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
26   ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
27   if (*found) {
28     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
29   } else {
30     ierr = PetscStrcpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc");CHKERRQ(ierr);
31     ierr = PetscStrcat(libs,name);CHKERRQ(ierr);
32     ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr);
33     if (*found) {
34       ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr);
35     }
36   }
37   PetscFunctionReturn(0);
38 }
39 
40 #endif
41 
42 #undef __FUNCT__
43 #define __FUNCT__ "PetscInitialize_DynamicLibraries"
44 /*
45     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
46     search path.
47 */
48 PetscErrorCode  PetscInitialize_DynamicLibraries(void)
49 {
50   char           *libname[32];
51   PetscErrorCode ierr;
52   PetscInt       nmax,i;
53 #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
54   PetscBool      preload;
55 #endif
56 
57   PetscFunctionBegin;
58   nmax = 32;
59   ierr = PetscOptionsGetStringArray(NULL,"-dll_prepend",libname,&nmax,NULL);CHKERRQ(ierr);
60   for (i=0; i<nmax; i++) {
61     ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
62     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
63   }
64 
65 #if !defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
66   /*
67       This just initializes the most basic PETSc stuff.
68 
69     The classes, from PetscDraw to PetscTS, are initialized the first
70     time an XXCreate() is called.
71   */
72   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
73 #else
74   preload = PETSC_FALSE;
75   ierr = PetscOptionsGetBool(NULL,"-dynamic_library_preload",&preload,NULL);CHKERRQ(ierr);
76   if (preload) {
77     PetscBool found;
78 #if defined(PETSC_USE_SINGLE_LIBRARY)
79     ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr);
80     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
81 #else
82     ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr);
83     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!");
84     ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr);
85     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Vec dynamic library \n You cannot move the dynamic libraries!");
86     ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr);
87     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Mat dynamic library \n You cannot move the dynamic libraries!");
88     ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr);
89     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc DM dynamic library \n You cannot move the dynamic libraries!");
90     ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr);
91     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc KSP dynamic library \n You cannot move the dynamic libraries!");
92     ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr);
93     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc SNES dynamic library \n You cannot move the dynamic libraries!");
94     ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr);
95     if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc TS dynamic library \n You cannot move the dynamic libraries!");
96 #endif
97   }
98 #endif
99 
100   nmax = 32;
101   ierr = PetscOptionsGetStringArray(NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr);
102   for (i=0; i<nmax; i++) {
103     ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr);
104     ierr = PetscFree(libname[i]);CHKERRQ(ierr);
105   }
106 
107   ierr = PetscThreadCommInitializePackage();CHKERRQ(ierr);
108   ierr = PetscThreadCommWorldInitialize();CHKERRQ(ierr);
109   ierr = AOInitializePackage();CHKERRQ(ierr);
110   ierr = PetscSFInitializePackage();CHKERRQ(ierr);
111   ierr = CharacteristicInitializePackage();CHKERRQ(ierr);
112   ierr = ISInitializePackage();CHKERRQ(ierr);
113   ierr = VecInitializePackage();CHKERRQ(ierr);
114   ierr = MatInitializePackage();CHKERRQ(ierr);
115   ierr = DMInitializePackage();CHKERRQ(ierr);
116   ierr = PCInitializePackage();CHKERRQ(ierr);
117   ierr = KSPInitializePackage();CHKERRQ(ierr);
118   ierr = SNESInitializePackage();CHKERRQ(ierr);
119   ierr = TSInitializePackage();CHKERRQ(ierr);
120   {
121     MPI_Comm comm;
122     ierr = PetscCommDuplicate(PETSC_COMM_SELF,&comm,NULL);CHKERRQ(ierr);
123     ierr = PetscCommDuplicate(PETSC_COMM_WORLD,&comm,NULL);CHKERRQ(ierr);
124   }
125   PetscFunctionReturn(0);
126 }
127 
128 #undef __FUNCT__
129 #define __FUNCT__ "PetscFinalize_DynamicLibraries"
130 /*
131      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
132 */
133 PetscErrorCode PetscFinalize_DynamicLibraries(void)
134 {
135   PetscErrorCode ierr;
136   PetscBool      flg = PETSC_FALSE;
137 
138   PetscFunctionBegin;
139   ierr = PetscOptionsGetBool(NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr);
140   if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); }
141   ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr);
142 
143   PetscDLLibrariesLoaded = 0;
144   PetscFunctionReturn(0);
145 }
146 
147 
148 
149 /* ------------------------------------------------------------------------------*/
150 struct _n_PetscFunctionList {
151   void              (*routine)(void);    /* the routine */
152   char              *name;               /* string to identify routine */
153   PetscFunctionList next;                /* next pointer */
154   PetscFunctionList next_list;           /* used to maintain list of all lists for freeing */
155 };
156 
157 /*
158      Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones.
159 */
160 static PetscFunctionList dlallhead = 0;
161 
162 /*MC
163    PetscFunctionListAdd - Given a routine and a string id, saves that routine in the
164    specified registry.
165 
166    Synopsis:
167    #include <petscsys.h>
168    PetscErrorCode PetscFunctionListAdd(PetscFunctionList flist,const char name[],void (*fptr)(void))
169 
170    Not Collective
171 
172    Input Parameters:
173 +  flist - pointer registry
174 .  name - string to identify routine
175 -  fptr - function pointer
176 
177    Notes:
178    To remove a registered routine, pass in a NULL fptr.
179 
180    Users who wish to register new classes for use by a particular PETSc
181    component (e.g., SNES) should generally call the registration routine
182    for that particular component (e.g., SNESRegister()) instead of
183    calling PetscFunctionListAdd() directly.
184 
185     Level: developer
186 
187 .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(),
188           PCRegister(), TSRegister(), PetscFunctionList, PetscObjectComposeFunction()
189 M*/
190 #undef __FUNCT__
191 #define __FUNCT__ "PetscFunctionListAdd_Private"
192 PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl,const char name[],void (*fnc)(void))
193 {
194   PetscFunctionList entry,ne;
195   PetscErrorCode    ierr;
196 
197   PetscFunctionBegin;
198   if (!*fl) {
199     ierr           = PetscNew(&entry);CHKERRQ(ierr);
200     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
201     entry->routine = fnc;
202     entry->next    = 0;
203     *fl            = entry;
204 
205     /* add this new list to list of all lists */
206     if (!dlallhead) {
207       dlallhead        = *fl;
208       (*fl)->next_list = 0;
209     } else {
210       ne               = dlallhead;
211       dlallhead        = *fl;
212       (*fl)->next_list = ne;
213     }
214   } else {
215     /* search list to see if it is already there */
216     ne = *fl;
217     while (ne) {
218       PetscBool founddup;
219 
220       ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr);
221       if (founddup) { /* found duplicate */
222         ne->routine = fnc;
223         PetscFunctionReturn(0);
224       }
225       if (ne->next) ne = ne->next;
226       else break;
227     }
228     /* create new entry and add to end of list */
229     ierr           = PetscNew(&entry);CHKERRQ(ierr);
230     ierr           = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr);
231     entry->routine = fnc;
232     entry->next    = 0;
233     ne->next       = entry;
234   }
235   PetscFunctionReturn(0);
236 }
237 
238 #undef __FUNCT__
239 #define __FUNCT__ "PetscFunctionListDestroy"
240 /*@
241     PetscFunctionListDestroy - Destroys a list of registered routines.
242 
243     Input Parameter:
244 .   fl  - pointer to list
245 
246     Level: developer
247 
248 .seealso: PetscFunctionListAdd(), PetscFunctionList
249 @*/
250 PetscErrorCode  PetscFunctionListDestroy(PetscFunctionList *fl)
251 {
252   PetscFunctionList next,entry,tmp = dlallhead;
253   PetscErrorCode    ierr;
254 
255   PetscFunctionBegin;
256   if (!*fl) PetscFunctionReturn(0);
257 
258   /*
259        Remove this entry from the master DL list (if it is in it)
260   */
261   if (dlallhead == *fl) {
262     if (dlallhead->next_list) dlallhead = dlallhead->next_list;
263     else dlallhead = NULL;
264   } else if (tmp) {
265     while (tmp->next_list != *fl) {
266       tmp = tmp->next_list;
267       if (!tmp->next_list) break;
268     }
269     if (tmp->next_list) tmp->next_list = tmp->next_list->next_list;
270   }
271 
272   /* free this list */
273   entry = *fl;
274   while (entry) {
275     next  = entry->next;
276     ierr  = PetscFree(entry->name);CHKERRQ(ierr);
277     ierr  = PetscFree(entry);CHKERRQ(ierr);
278     entry = next;
279   }
280   *fl = 0;
281   PetscFunctionReturn(0);
282 }
283 
284 /*
285    Print any PetscFunctionLists that have not be destroyed
286 */
287 #undef __FUNCT__
288 #define __FUNCT__ "PetscFunctionListPrintAll"
289 PetscErrorCode  PetscFunctionListPrintAll(void)
290 {
291   PetscFunctionList tmp = dlallhead;
292   PetscErrorCode    ierr;
293 
294   PetscFunctionBegin;
295   if (tmp) {
296     ierr = PetscPrintf(PETSC_COMM_WORLD,"The following PetscFunctionLists were not destroyed\n");CHKERRQ(ierr);
297   }
298   while (tmp) {
299     ierr = PetscPrintf(PETSC_COMM_WORLD,"%s \n",tmp->name);CHKERRQ(ierr);
300     tmp = tmp->next_list;
301   }
302   PetscFunctionReturn(0);
303 }
304 
305 /*MC
306     PetscFunctionListFind - Find function registered under given name
307 
308     Synopsis:
309     #include <petscsys.h>
310     PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void))
311 
312     Input Parameters:
313 +   flist   - pointer to list
314 -   name - name registered for the function
315 
316     Output Parameters:
317 .   fptr - the function pointer if name was found, else NULL
318 
319     Level: developer
320 
321 .seealso: PetscFunctionListAdd(), PetscFunctionList, PetscObjectQueryFunction()
322 M*/
323 #undef __FUNCT__
324 #define __FUNCT__ "PetscFunctionListFind_Private"
325 PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void))
326 {
327   PetscFunctionList entry = fl;
328   PetscErrorCode    ierr;
329   PetscBool         flg;
330 
331   PetscFunctionBegin;
332   if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name");
333 
334   *r = 0;
335   while (entry) {
336     ierr = PetscStrcmp(name,entry->name,&flg);CHKERRQ(ierr);
337     if (flg) {
338       *r   = entry->routine;
339       PetscFunctionReturn(0);
340     }
341     entry = entry->next;
342   }
343   PetscFunctionReturn(0);
344 }
345 
346 #undef __FUNCT__
347 #define __FUNCT__ "PetscFunctionListView"
348 /*@
349    PetscFunctionListView - prints out contents of an PetscFunctionList
350 
351    Collective over MPI_Comm
352 
353    Input Parameters:
354 +  list - the list of functions
355 -  viewer - currently ignored
356 
357    Level: developer
358 
359 .seealso: PetscFunctionListAdd(), PetscFunctionListPrintTypes(), PetscFunctionList
360 @*/
361 PetscErrorCode  PetscFunctionListView(PetscFunctionList list,PetscViewer viewer)
362 {
363   PetscErrorCode ierr;
364   PetscBool      iascii;
365 
366   PetscFunctionBegin;
367   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
368   PetscValidPointer(list,1);
369   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
370 
371   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
372   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported");
373 
374   while (list) {
375     ierr = PetscViewerASCIIPrintf(viewer," %s\n",list->name);CHKERRQ(ierr);
376     list = list->next;
377   }
378   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
379   PetscFunctionReturn(0);
380 }
381 
382 #undef __FUNCT__
383 #define __FUNCT__ "PetscFunctionListGet"
384 /*@C
385    PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used
386          by help etc.
387 
388    Not Collective
389 
390    Input Parameter:
391 .  list   - list of types
392 
393    Output Parameter:
394 +  array - array of names
395 -  n - length of array
396 
397    Notes:
398        This allocates the array so that must be freed. BUT the individual entries are
399     not copied so should not be freed.
400 
401    Level: developer
402 
403 .seealso: PetscFunctionListAdd(), PetscFunctionList
404 @*/
405 PetscErrorCode  PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n)
406 {
407   PetscErrorCode    ierr;
408   PetscInt          count = 0;
409   PetscFunctionList klist = list;
410 
411   PetscFunctionBegin;
412   while (list) {
413     list = list->next;
414     count++;
415   }
416   ierr  = PetscMalloc1(count+1,array);CHKERRQ(ierr);
417   count = 0;
418   while (klist) {
419     (*array)[count] = klist->name;
420     klist           = klist->next;
421     count++;
422   }
423   (*array)[count] = 0;
424   *n              = count+1;
425   PetscFunctionReturn(0);
426 }
427 
428 
429 #undef __FUNCT__
430 #define __FUNCT__ "PetscFunctionListPrintTypes"
431 /*@C
432    PetscFunctionListPrintTypes - Prints the methods available.
433 
434    Collective over MPI_Comm
435 
436    Input Parameters:
437 +  comm   - the communicator (usually MPI_COMM_WORLD)
438 .  fd     - file to print to, usually stdout
439 .  prefix - prefix to prepend to name (optional)
440 .  name   - option string (for example, "-ksp_type")
441 .  text - short description of the object (for example, "Krylov solvers")
442 .  man - name of manual page that discusses the object (for example, "KSPCreate")
443 .  list   - list of types
444 -  def - default (current) value
445 
446    Level: developer
447 
448 .seealso: PetscFunctionListAdd(), PetscFunctionList
449 @*/
450 PetscErrorCode  PetscFunctionListPrintTypes(MPI_Comm comm,FILE *fd,const char prefix[],const char name[],const char text[],const char man[],PetscFunctionList list,const char def[])
451 {
452   PetscErrorCode ierr;
453   PetscInt       count = 0;
454   char           p[64];
455 
456   PetscFunctionBegin;
457   if (!fd) fd = PETSC_STDOUT;
458 
459   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
460   if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);}
461   ierr = PetscFPrintf(comm,fd,"  %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr);
462 
463   while (list) {
464     ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr);
465     list = list->next;
466     count++;
467     if (count == 8) {ierr = PetscFPrintf(comm,fd,"\n     ");CHKERRQ(ierr);}
468   }
469   ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr);
470   PetscFunctionReturn(0);
471 }
472 
473 #undef __FUNCT__
474 #define __FUNCT__ "PetscFunctionListDuplicate"
475 /*@
476     PetscFunctionListDuplicate - Creates a new list from a given object list.
477 
478     Input Parameters:
479 .   fl   - pointer to list
480 
481     Output Parameters:
482 .   nl - the new list (should point to 0 to start, otherwise appends)
483 
484     Level: developer
485 
486 .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy()
487 
488 @*/
489 PetscErrorCode  PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl)
490 {
491   PetscErrorCode ierr;
492 
493   PetscFunctionBegin;
494   while (fl) {
495     ierr = PetscFunctionListAdd(nl,fl->name,fl->routine);CHKERRQ(ierr);
496     fl   = fl->next;
497   }
498   PetscFunctionReturn(0);
499 }
500 
501