Actual source code: reg.c

  1: /*
  2:     Provides a general mechanism to allow one to register new routines in
  3:     dynamic libraries for many of the PETSc objects (including, e.g., KSP and PC).
  4: */
  5: #include <petsc/private/petscimpl.h>
  6: #include <petscviewer.h>

  8: #include <petsc/private/hashmap.h>
  9: /*
 10:     This is the default list used by PETSc with the PetscDLLibrary register routines
 11: */
 12: PetscDLLibrary PetscDLLibrariesLoaded = NULL;

 14: #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)

 16: static PetscErrorCode PetscLoadDynamicLibrary(const char *name, PetscBool *found)
 17: {
 18:   char libs[PETSC_MAX_PATH_LEN], dlib[PETSC_MAX_PATH_LEN];

 20:   PetscFunctionBegin;
 21:   PetscCall(PetscStrncpy(libs, "${PETSC_LIB_DIR}/libpetsc", sizeof(libs)));
 22:   PetscCall(PetscStrlcat(libs, name, sizeof(libs)));
 23:   #if defined(PETSC_LIB_NAME_SUFFIX)
 24:   PetscCall(PetscStrlcat(libs, PETSC_LIB_NAME_SUFFIX, sizeof(libs)));
 25:   #endif
 26:   PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_WORLD, libs, dlib, 1024, found));
 27:   if (*found) {
 28:     PetscCall(PetscDLLibraryAppend(PETSC_COMM_WORLD, &PetscDLLibrariesLoaded, dlib));
 29:   } else {
 30:     PetscCall(PetscStrncpy(libs, "${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc", sizeof(libs)));
 31:     PetscCall(PetscStrlcat(libs, name, sizeof(libs)));
 32:   #if defined(PETSC_LIB_NAME_SUFFIX)
 33:     PetscCall(PetscStrlcat(libs, PETSC_LIB_NAME_SUFFIX, sizeof(libs)));
 34:   #endif
 35:     PetscCall(PetscDLLibraryRetrieve(PETSC_COMM_WORLD, libs, dlib, 1024, found));
 36:     if (*found) PetscCall(PetscDLLibraryAppend(PETSC_COMM_WORLD, &PetscDLLibrariesLoaded, dlib));
 37:   }
 38:   PetscFunctionReturn(PETSC_SUCCESS);
 39: }
 40: #endif

 42: #if defined(PETSC_USE_SINGLE_LIBRARY) && !(defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES))
 43: PETSC_EXTERN PetscErrorCode AOInitializePackage(void);
 44: PETSC_EXTERN PetscErrorCode PetscSFInitializePackage(void);
 45:   #if !defined(PETSC_USE_COMPLEX)
 46: PETSC_EXTERN PetscErrorCode CharacteristicInitializePackage(void);
 47:   #endif
 48: PETSC_EXTERN PetscErrorCode ISInitializePackage(void);
 49: PETSC_EXTERN PetscErrorCode VecInitializePackage(void);
 50: PETSC_EXTERN PetscErrorCode MatInitializePackage(void);
 51: PETSC_EXTERN PetscErrorCode DMInitializePackage(void);
 52: PETSC_EXTERN PetscErrorCode PCInitializePackage(void);
 53: PETSC_EXTERN PetscErrorCode KSPInitializePackage(void);
 54: PETSC_EXTERN PetscErrorCode SNESInitializePackage(void);
 55: PETSC_EXTERN PetscErrorCode TSInitializePackage(void);
 56: PETSC_EXTERN PetscErrorCode TaoInitializePackage(void);
 57: #endif

 59: /*
 60:     PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the
 61:     search path.
 62: */
 63: PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void)
 64: {
 65:   char     *libname[32];
 66:   PetscInt  nmax, i;
 67:   PetscBool preload = PETSC_FALSE;
 68: #if defined(PETSC_HAVE_ELEMENTAL)
 69:   PetscBool PetscInitialized = PetscInitializeCalled;
 70: #endif

 72:   PetscFunctionBegin;
 73: #if defined(PETSC_HAVE_THREADSAFETY)
 74:   /* These must be all initialized here because it is not safe for individual threads to call these initialize routines */
 75:   preload = PETSC_TRUE;
 76: #endif

 78:   nmax = 32;
 79:   PetscCall(PetscOptionsGetStringArray(NULL, NULL, "-dll_prepend", libname, &nmax, NULL));
 80:   for (i = 0; i < nmax; i++) {
 81:     PetscCall(PetscDLLibraryPrepend(PETSC_COMM_WORLD, &PetscDLLibrariesLoaded, libname[i]));
 82:     PetscCall(PetscFree(libname[i]));
 83:   }

 85:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-library_preload", &preload, NULL));
 86:   if (!preload) {
 87:     PetscCall(PetscSysInitializePackage());
 88:   } else {
 89: #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES)
 90:     PetscBool found;
 91:   #if defined(PETSC_USE_SINGLE_LIBRARY)
 92:     PetscCall(PetscLoadDynamicLibrary("", &found));
 93:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc dynamic library. You cannot move the dynamic libraries!");
 94:   #else
 95:     PetscCall(PetscLoadDynamicLibrary("sys", &found));
 96:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc Sys dynamic library. You cannot move the dynamic libraries!");
 97:     PetscCall(PetscLoadDynamicLibrary("vec", &found));
 98:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc Vec dynamic library. You cannot move the dynamic libraries!");
 99:     PetscCall(PetscLoadDynamicLibrary("mat", &found));
100:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc Mat dynamic library. You cannot move the dynamic libraries!");
101:     PetscCall(PetscLoadDynamicLibrary("dm", &found));
102:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc DM dynamic library. You cannot move the dynamic libraries!");
103:     PetscCall(PetscLoadDynamicLibrary("ksp", &found));
104:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc KSP dynamic library. You cannot move the dynamic libraries!");
105:     PetscCall(PetscLoadDynamicLibrary("snes", &found));
106:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc SNES dynamic library. You cannot move the dynamic libraries!");
107:     PetscCall(PetscLoadDynamicLibrary("ts", &found));
108:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc TS dynamic library. You cannot move the dynamic libraries!");
109:     PetscCall(PetscLoadDynamicLibrary("tao", &found));
110:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate Tao dynamic library. You cannot move the dynamic libraries!");
111:   #endif
112: #else /* defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) */
113:   #if defined(PETSC_USE_SINGLE_LIBRARY)
114:     PetscCall(AOInitializePackage());
115:     PetscCall(PetscSFInitializePackage());
116:     #if !defined(PETSC_USE_COMPLEX)
117:     PetscCall(CharacteristicInitializePackage());
118:     #endif
119:     PetscCall(ISInitializePackage());
120:     PetscCall(VecInitializePackage());
121:     PetscCall(MatInitializePackage());
122:     PetscCall(DMInitializePackage());
123:     PetscCall(PCInitializePackage());
124:     PetscCall(KSPInitializePackage());
125:     PetscCall(SNESInitializePackage());
126:     PetscCall(TSInitializePackage());
127:     PetscCall(TaoInitializePackage());
128:   #else
129:     SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Cannot use -library_preload with multiple static PETSc libraries");
130:   #endif
131: #endif /* defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) */
132:   }

134: #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) && defined(PETSC_HAVE_BAMG)
135:   {
136:     PetscBool found;
137:     PetscCall(PetscLoadDynamicLibrary("bamg", &found));
138:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PETSc BAMG dynamic library. You cannot move the dynamic libraries!");
139:   }
140: #endif
141: #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) && defined(PETSC_USE_SHARED_LIBRARIES) && defined(PETSC_HAVE_PFLARE)
142:   {
143:     PetscBool found;
144:     PetscCall(PetscLoadDynamicLibrary("pflare", &found));
145:     PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to locate PFLARE dynamic library. You cannot move the dynamic libraries!");
146:   }
147: #endif

149:   nmax = 32;
150:   PetscCall(PetscOptionsGetStringArray(NULL, NULL, "-dll_append", libname, &nmax, NULL));
151:   for (i = 0; i < nmax; i++) {
152:     PetscCall(PetscDLLibraryAppend(PETSC_COMM_WORLD, &PetscDLLibrariesLoaded, libname[i]));
153:     PetscCall(PetscFree(libname[i]));
154:   }

156: #if defined(PETSC_HAVE_ELEMENTAL)
157:   /* in Fortran, PetscInitializeCalled is set to PETSC_TRUE before PetscInitialize_DynamicLibraries() */
158:   /* in C, it is not the case, but the value is forced to PETSC_TRUE so that PetscRegisterFinalize() is called */
159:   PetscInitializeCalled = PETSC_TRUE;
160:   PetscCall(PetscElementalInitializePackage());
161:   PetscInitializeCalled = PetscInitialized;
162: #endif
163:   PetscFunctionReturn(PETSC_SUCCESS);
164: }

166: /*
167:      PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries.
168: */
169: PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void)
170: {
171:   PetscBool flg = PETSC_FALSE;

173:   PetscFunctionBegin;
174:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-dll_view", &flg, NULL));
175:   if (flg) PetscCall(PetscDLLibraryPrintPath(PetscDLLibrariesLoaded));
176:   PetscCall(PetscDLLibraryClose(PetscDLLibrariesLoaded));
177:   PetscDLLibrariesLoaded = NULL;
178:   PetscFunctionReturn(PETSC_SUCCESS);
179: }

181: PETSC_HASH_MAP(HMapFunc, const char *, PetscErrorCodeFn *, kh_str_hash_func, kh_str_hash_equal, NULL)

183: struct _n_PetscFunctionList {
184:   PetscHMapFunc map;
185: };

187: /* Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones. */
188: typedef struct n_PetscFunctionListDLAll *PetscFunctionListDLAll;
189: struct n_PetscFunctionListDLAll {
190:   PetscFunctionList      data;
191:   PetscFunctionListDLAll next;
192: };

194: static PetscFunctionListDLAll dlallhead = NULL;

196: static PetscErrorCode PetscFunctionListDLAllPush_Private(PetscFunctionList fl)
197: {
198:   PetscFunctionBegin;
199:   if (PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)) {
200:     PetscFunctionListDLAll head;

202:     PetscCall(PetscNew(&head));
203:     head->data = fl;
204:     head->next = dlallhead;
205:     dlallhead  = head;
206:   }
207:   PetscFunctionReturn(PETSC_SUCCESS);
208: }

210: static PetscErrorCode PetscFunctionListDLAllPop_Private(PetscFunctionList fl)
211: {
212:   PetscFunctionBegin;
213:   if (PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)) {
214:     PetscFunctionListDLAll current = dlallhead, prev = NULL;

216:     /* Remove this entry from the main DL list (if it is in it) */
217:     while (current) {
218:       const PetscFunctionListDLAll next = current->next;

220:       if (current->data == fl) {
221:         if (prev) {
222:           // somewhere in the middle (or end) of the list
223:           prev->next = next;
224:         } else {
225:           // prev = NULL implies current = dlallhead, so front of list
226:           dlallhead = next;
227:         }
228:         PetscCall(PetscFree(current));
229:         break;
230:       }
231:       prev    = current;
232:       current = next;
233:     }
234:   }
235:   PetscFunctionReturn(PETSC_SUCCESS);
236: }

238: static PetscErrorCode PetscHMapFuncInsert_Private(PetscHMapFunc map, const char name[], PetscErrorCodeFn *fnc)
239: {
240:   PetscHashIter it;
241:   PetscBool     found;

243:   PetscFunctionBegin;
244:   PetscAssertPointer(name, 2);
246:   PetscCall(PetscHMapFuncFind(map, name, &it, &found));
247:   if (fnc) {
248:     if (found) {
249:       PetscCall(PetscHMapFuncIterSet(map, it, fnc));
250:     } else {
251:       char *tmp_name;

253:       PetscCall(PetscStrallocpy(name, &tmp_name));
254:       PetscCall(PetscHMapFuncSet(map, tmp_name, fnc));
255:     }
256:   } else if (found) {
257:     const char *tmp_name;

259:     PetscHashIterGetKey(map, it, tmp_name);
260:     PetscCall(PetscFree(tmp_name));
261:     PetscCall(PetscHMapFuncIterDel(map, it));
262:   }
263:   PetscFunctionReturn(PETSC_SUCCESS);
264: }

266: static PetscErrorCode PetscFunctionListCreate_Private(PetscInt size, PetscFunctionList *fl)
267: {
268:   PetscFunctionBegin;
269:   if (*fl) PetscFunctionReturn(PETSC_SUCCESS);
270:   PetscCall(PetscNew(fl));
271:   PetscCall(PetscHMapFuncCreateWithSize(size, &(*fl)->map));
272:   PetscCall(PetscFunctionListDLAllPush_Private(*fl));
273:   PetscFunctionReturn(PETSC_SUCCESS);
274: }

276: PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl, const char name[], PetscErrorCodeFn *fptr)
277: {
278:   PetscFunctionBegin;
279:   PetscAssertPointer(fl, 1);
280:   if (name) PetscAssertPointer(name, 2);
282:   if (!fptr && !*fl) PetscFunctionReturn(PETSC_SUCCESS);
283:   PetscCall(PetscFunctionListCreate_Private(0, fl));
284:   PetscCall(PetscHMapFuncInsert_Private((*fl)->map, name, fptr));
285:   PetscFunctionReturn(PETSC_SUCCESS);
286: }

288: /*@C
289:   PetscFunctionListDestroy - Destroys a list of registered routines.

291:   Input Parameter:
292: . fl - pointer to list

294:   Level: developer

296: .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`, `PetscFunctionListClear()`
297: @*/
298: PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *fl)
299: {
300:   PetscFunctionBegin;
301:   if (!*fl) PetscFunctionReturn(PETSC_SUCCESS);
302:   PetscCall(PetscFunctionListDLAllPop_Private(*fl));
303:   /* free this list */
304:   PetscCall(PetscFunctionListClear(*fl));
305:   PetscCall(PetscHMapFuncDestroy(&(*fl)->map));
306:   PetscCall(PetscFree(*fl));
307:   PetscFunctionReturn(PETSC_SUCCESS);
308: }

310: #define PetscHMapFuncForEach(__func_list__, __key_name__, __val_name__, ...) \
311:   do { \
312:     const PetscHMapFunc phmfi_map_ = (__func_list__)->map; \
313:     PetscHashIter       phmfi_iter_; \
314: \
315:     PetscHashIterBegin(phmfi_map_, phmfi_iter_); \
316:     while (!PetscHashIterAtEnd(phmfi_map_, phmfi_iter_)) { \
317:       const char *PETSC_UNUSED       __key_name__; \
318:       PetscErrorCodeFn *PETSC_UNUSED __val_name__; \
319: \
320:       PetscHashIterGetKey(phmfi_map_, phmfi_iter_, __key_name__); \
321:       PetscHashIterGetVal(phmfi_map_, phmfi_iter_, __val_name__); \
322:       { \
323:         __VA_ARGS__; \
324:       } \
325:       PetscHashIterNext(phmfi_map_, phmfi_iter_); \
326:     } /* end while */ \
327:   } while (0)

329: /*@C
330:   PetscFunctionListClear - Clear a `PetscFunctionList`

332:   Not Collective

334:   Input Parameter:
335: . fl - The `PetscFunctionList` to clear

337:   Level: developer

339:   Notes:
340:   This clears the contents of `fl` but does not deallocate the entries themselves.

342: .seealso: `PetscFunctionList`, `PetscFunctionListDestroy()`, `PetscFunctionListAdd()`
343: @*/
344: PetscErrorCode PetscFunctionListClear(PetscFunctionList fl)
345: {
346:   PetscFunctionBegin;
347:   if (fl) {
348:     PetscHMapFuncForEach(fl, name, func, PetscCall(PetscFree(name)));
349:     PetscCall(PetscHMapFuncClear(fl->map));
350:   }
351:   PetscFunctionReturn(PETSC_SUCCESS);
352: }

354: /*
355:    Print registered PetscFunctionLists
356: */
357: PetscErrorCode PetscFunctionListPrintAll(void)
358: {
359:   PetscFunctionListDLAll current = dlallhead;

361:   PetscFunctionBegin;
362:   if (current) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Registered PetscFunctionLists\n", PetscGlobalRank));
363:   while (current) {
364:     PetscCall(PetscFunctionListPrintNonEmpty(current->data));
365:     current = current->next;
366:   }
367:   PetscFunctionReturn(PETSC_SUCCESS);
368: }

370: /*@C
371:   PetscFunctionListPrintNonEmpty - Print composed names for non `NULL` function pointers

373:   Logically Collective, No Fortran Support

375:   Input Parameter:
376: . fl - the function list

378:   Level: developer

380: .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`, `PetscObjectQueryFunction()`
381: @*/
382: PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList fl)
383: {
384:   PetscFunctionBegin;
385:   if (fl) {
386:     // clang-format off
387:     PetscHMapFuncForEach(
388:       fl,
389:       name, func,
390:       PetscCall(PetscFPrintf(PETSC_COMM_SELF, PETSC_STDOUT, "[%d] function name: %s\n", PetscGlobalRank, name));
391:     );
392:     // clang-format on
393:   }
394:   PetscFunctionReturn(PETSC_SUCCESS);
395: }

397: PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl, const char name[], PetscErrorCodeFn **fptr)
398: {
399:   PetscFunctionBegin;
400:   PetscAssertPointer(name, 2);
401:   PetscAssertPointer(fptr, 3);
402:   *fptr = NULL;
403:   if (fl) PetscCall(PetscHMapFuncGet(fl->map, name, fptr));
404:   PetscFunctionReturn(PETSC_SUCCESS);
405: }

407: /*@C
408:   PetscFunctionListView - prints out contents of a `PetscFunctionList`

410:   Collective

412:   Input Parameters:
413: + list   - the list of functions
414: - viewer - the `PetscViewer` used to view the `PetscFunctionList`

416:   Level: developer

418: .seealso: `PetscFunctionListAdd()`, `PetscFunctionListPrintTypes()`, `PetscFunctionList`
419: @*/
420: PetscErrorCode PetscFunctionListView(PetscFunctionList list, PetscViewer viewer)
421: {
422:   PetscBool isascii;

424:   PetscFunctionBegin;
425:   PetscAssertPointer(list, 1);
426:   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_SELF, &viewer));

429:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
430:   PetscCheck(isascii, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only ASCII viewer supported");
431:   {
432:     PetscInt size;

434:     PetscCall(PetscHMapFuncGetSize(list->map, &size));
435:     PetscCall(PetscViewerASCIIPrintf(viewer, "PetscFunctionList Object:\n"));
436:     PetscCall(PetscViewerASCIIPushTab(viewer));
437:     PetscCall(PetscViewerASCIIPrintf(viewer, "size: %" PetscInt_FMT "\n", size));
438:     if (size) {
439:       PetscInt count = 0;

441:       PetscCall(PetscViewerASCIIPrintf(viewer, "functions:\n"));
442:       PetscCall(PetscViewerASCIIPushTab(viewer));
443:       PetscHMapFuncForEach(list, name, func, PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ": %s\n", ++count, name)));
444:       PetscCall(PetscViewerASCIIPopTab(viewer));
445:     }
446:     PetscCall(PetscViewerASCIIPopTab(viewer));
447:   }
448:   PetscFunctionReturn(PETSC_SUCCESS);
449: }

451: /*@C
452:   PetscFunctionListGet - Gets an array the contains the entries in `PetscFunctionList`, this is used
453:   by help etc.

455:   Not Collective, No Fortran Support

457:   Input Parameter:
458: . list - list of types

460:   Output Parameters:
461: + array - array of names
462: - n     - length of `array`

464:   Level: developer

466:   Note:
467:   This allocates the array so that must be freed with `PetscFree()`. BUT the individual entries should not be freed.

469: .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`
470: @*/
471: PetscErrorCode PetscFunctionListGet(PetscFunctionList list, const char ***array, int *n)
472: {
473:   PetscInt size = 0;

475:   PetscFunctionBegin;
476:   PetscAssertPointer(array, 2);
477:   *array = NULL;
478:   if (list) {
479:     const PetscHMapFunc map = list->map;
480:     PetscInt            off = 0;

482:     PetscCall(PetscHMapFuncGetSize(map, &size));
483:     PetscCall(PetscMalloc1(size, (char ***)array));
484:     PetscCall(PetscHMapFuncGetKeys(map, &off, *array));
485:   }
486:   PetscCall(PetscCIntCast(size, n));
487:   PetscFunctionReturn(PETSC_SUCCESS);
488: }

490: /*@C
491:   PetscFunctionListPrintTypes - Prints the methods available in a list of functions

493:   Collective, No Fortran Support

495:   Input Parameters:
496: + comm   - the communicator (usually `MPI_COMM_WORLD`)
497: . fd     - file to print to, usually `stdout`
498: . prefix - prefix to prepend to name (optional)
499: . name   - option string (for example, `-ksp_type`)
500: . text   - short description of the object (for example, "Krylov solvers")
501: . man    - name of manual page that discusses the object (for example, `KSPCreate`)
502: . list   - list of types
503: . def    - default (current) value
504: - newv   - new value

506:   Level: developer

508: .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`
509: @*/
510: PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm comm, FILE *fd, const char prefix[], const char name[], const char text[], const char man[], PetscFunctionList list, const char def[], const char newv[])
511: {
512:   char p[64];

514:   PetscFunctionBegin;
515:   (void)fd;
516:   PetscCall(PetscStrncpy(p, "-", sizeof(p)));
517:   if (prefix) PetscCall(PetscStrlcat(p, prefix, sizeof(p)));
518:   PetscCall((*PetscHelpPrintf)(comm, "  %s%s <now %s : formerly %s>: %s (one of)", p, name + 1, newv, def, text));

520:   if (list) PetscHMapFuncForEach(list, name, func, PetscCall((*PetscHelpPrintf)(comm, " %s", name)));
521:   PetscCall((*PetscHelpPrintf)(comm, " (%s)\n", man));
522:   PetscFunctionReturn(PETSC_SUCCESS);
523: }

525: /*@C
526:   PetscFunctionListDuplicate - Creates a new list from a given function list `PetscFunctionList`.

528:   Input Parameter:
529: . fl - pointer to list

531:   Output Parameter:
532: . nl - the new list (should point to `NULL` to start, otherwise appends)

534:   Level: developer

536: .seealso: `PetscFunctionList`, `PetscFunctionListAdd()`, `PetscFlistDestroy()`
537: @*/
538: PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList fl, PetscFunctionList *nl)
539: {
540:   PetscFunctionBegin;
541:   if (fl) {
542:     PetscHMapFunc dup_map;

544:     if (!*nl) {
545:       PetscInt n;

547:       PetscCall(PetscHMapFuncGetSize(fl->map, &n));
548:       PetscCall(PetscFunctionListCreate_Private(n, nl));
549:     }
550:     dup_map = (*nl)->map;
551:     PetscHMapFuncForEach(fl, name, func, PetscCall(PetscHMapFuncInsert_Private(dup_map, name, func)));
552:   }
553:   PetscFunctionReturn(PETSC_SUCCESS);
554: }