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:   PetscFunctionListPrintAll - Prints the names of all functions registered in every `PetscFunctionList` known to PETSc

357:   Not Collective

359:   Level: developer

361:   Note:
362:   Intended for debugging; iterates over the global list of registered `PetscFunctionList` objects and prints each
363:   non-empty function name to `PETSC_COMM_SELF` on the calling MPI process.

365: .seealso: `PetscFunctionList`, `PetscFunctionListPrintNonEmpty()`, `PetscFunctionListAdd()`, `PetscFunctionListView()`
366: @*/
367: PetscErrorCode PetscFunctionListPrintAll(void)
368: {
369:   PetscFunctionListDLAll current = dlallhead;

371:   PetscFunctionBegin;
372:   if (current) PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Registered PetscFunctionLists\n", PetscGlobalRank));
373:   while (current) {
374:     PetscCall(PetscFunctionListPrintNonEmpty(current->data));
375:     current = current->next;
376:   }
377:   PetscFunctionReturn(PETSC_SUCCESS);
378: }

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

383:   Logically Collective, No Fortran Support

385:   Input Parameter:
386: . fl - the function list

388:   Level: developer

390: .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`, `PetscObjectQueryFunction()`
391: @*/
392: PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList fl)
393: {
394:   PetscFunctionBegin;
395:   if (fl) {
396:     // clang-format off
397:     PetscHMapFuncForEach(
398:       fl,
399:       name, func,
400:       PetscCall(PetscFPrintf(PETSC_COMM_SELF, PETSC_STDOUT, "[%d] function name: %s\n", PetscGlobalRank, name));
401:     );
402:     // clang-format on
403:   }
404:   PetscFunctionReturn(PETSC_SUCCESS);
405: }

407: PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl, const char name[], PetscErrorCodeFn **fptr)
408: {
409:   PetscFunctionBegin;
410:   PetscAssertPointer(name, 2);
411:   PetscAssertPointer(fptr, 3);
412:   *fptr = NULL;
413:   if (fl) PetscCall(PetscHMapFuncGet(fl->map, name, fptr));
414:   PetscFunctionReturn(PETSC_SUCCESS);
415: }

417: /*@C
418:   PetscFunctionListView - prints out contents of a `PetscFunctionList`

420:   Collective

422:   Input Parameters:
423: + list   - the list of functions
424: - viewer - the `PetscViewer` used to view the `PetscFunctionList`

426:   Level: developer

428: .seealso: `PetscFunctionListAdd()`, `PetscFunctionListPrintTypes()`, `PetscFunctionList`
429: @*/
430: PetscErrorCode PetscFunctionListView(PetscFunctionList list, PetscViewer viewer)
431: {
432:   PetscBool isascii;

434:   PetscFunctionBegin;
435:   PetscAssertPointer(list, 1);
436:   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_SELF, &viewer));

439:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
440:   PetscCheck(isascii, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only ASCII viewer supported");
441:   {
442:     PetscInt size;

444:     PetscCall(PetscHMapFuncGetSize(list->map, &size));
445:     PetscCall(PetscViewerASCIIPrintf(viewer, "PetscFunctionList Object:\n"));
446:     PetscCall(PetscViewerASCIIPushTab(viewer));
447:     PetscCall(PetscViewerASCIIPrintf(viewer, "size: %" PetscInt_FMT "\n", size));
448:     if (size) {
449:       PetscInt count = 0;

451:       PetscCall(PetscViewerASCIIPrintf(viewer, "functions:\n"));
452:       PetscCall(PetscViewerASCIIPushTab(viewer));
453:       PetscHMapFuncForEach(list, name, func, PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT ": %s\n", ++count, name)));
454:       PetscCall(PetscViewerASCIIPopTab(viewer));
455:     }
456:     PetscCall(PetscViewerASCIIPopTab(viewer));
457:   }
458:   PetscFunctionReturn(PETSC_SUCCESS);
459: }

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

465:   Not Collective, No Fortran Support

467:   Input Parameter:
468: . list - list of types

470:   Output Parameters:
471: + array - array of names
472: - n     - length of `array`

474:   Level: developer

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

479: .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`
480: @*/
481: PetscErrorCode PetscFunctionListGet(PetscFunctionList list, const char ***array, int *n)
482: {
483:   PetscInt size = 0;

485:   PetscFunctionBegin;
486:   PetscAssertPointer(array, 2);
487:   *array = NULL;
488:   if (list) {
489:     const PetscHMapFunc map = list->map;
490:     PetscInt            off = 0;

492:     PetscCall(PetscHMapFuncGetSize(map, &size));
493:     PetscCall(PetscMalloc1(size, (char ***)array));
494:     PetscCall(PetscHMapFuncGetKeys(map, &off, *array));
495:   }
496:   PetscCall(PetscCIntCast(size, n));
497:   PetscFunctionReturn(PETSC_SUCCESS);
498: }

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

503:   Collective, No Fortran Support

505:   Input Parameters:
506: + comm   - the communicator (usually `MPI_COMM_WORLD`)
507: . fd     - file to print to, usually `stdout`
508: . prefix - prefix to prepend to name (optional)
509: . name   - option string (for example, `-ksp_type`)
510: . text   - short description of the object (for example, "Krylov solvers")
511: . man    - name of manual page that discusses the object (for example, `KSPCreate`)
512: . list   - list of types
513: . def    - default (current) value
514: - newv   - new value

516:   Level: developer

518: .seealso: `PetscFunctionListAdd()`, `PetscFunctionList`
519: @*/
520: 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[])
521: {
522:   char p[64];

524:   PetscFunctionBegin;
525:   (void)fd;
526:   PetscCall(PetscStrncpy(p, "-", sizeof(p)));
527:   if (prefix) PetscCall(PetscStrlcat(p, prefix, sizeof(p)));
528:   PetscCall((*PetscHelpPrintf)(comm, "  %s%s <now %s : formerly %s>: %s (one of)", p, name + 1, newv, def, text));

530:   if (list) PetscHMapFuncForEach(list, name, func, PetscCall((*PetscHelpPrintf)(comm, " %s", name)));
531:   PetscCall((*PetscHelpPrintf)(comm, " (%s)\n", man));
532:   PetscFunctionReturn(PETSC_SUCCESS);
533: }

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

538:   Input Parameter:
539: . fl - pointer to list

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

544:   Level: developer

546: .seealso: `PetscFunctionList`, `PetscFunctionListAdd()`, `PetscFlistDestroy()`
547: @*/
548: PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList fl, PetscFunctionList *nl)
549: {
550:   PetscFunctionBegin;
551:   if (fl) {
552:     PetscHMapFunc dup_map;

554:     if (!*nl) {
555:       PetscInt n;

557:       PetscCall(PetscHMapFuncGetSize(fl->map, &n));
558:       PetscCall(PetscFunctionListCreate_Private(n, nl));
559:     }
560:     dup_map = (*nl)->map;
561:     PetscHMapFuncForEach(fl, name, func, PetscCall(PetscHMapFuncInsert_Private(dup_map, name, func)));
562:   }
563:   PetscFunctionReturn(PETSC_SUCCESS);
564: }