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 42*acff04ddSBarry Smith #define PETSC_HAVE_THREADSAFETY 1 43*acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY) 44*acff04ddSBarry Smith static MPI_Comm PETSC_COMM_WORLD_INNER,PETSC_COMM_SELF_INNER; 45*acff04ddSBarry Smith #endif 46*acff04ddSBarry Smith 47487e5849SBarry Smith #undef __FUNCT__ 48e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize_DynamicLibraries" 49e5c89e4eSSatish Balay /* 50e5c89e4eSSatish Balay PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the 51e5c89e4eSSatish Balay search path. 52e5c89e4eSSatish Balay */ 537087cfbeSBarry Smith PetscErrorCode PetscInitialize_DynamicLibraries(void) 54e5c89e4eSSatish Balay { 55487e5849SBarry Smith char *libname[32]; 56e5c89e4eSSatish Balay PetscErrorCode ierr; 57e5c89e4eSSatish Balay PetscInt nmax,i; 58aa2d57e9SJed Brown #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 59aa2d57e9SJed Brown PetscBool preload; 603fa76a5bSLisandro Dalcin #endif 61e5c89e4eSSatish Balay 6260154eb2SBarry Smith PetscFunctionBegin; 63e5c89e4eSSatish Balay nmax = 32; 640298fd71SBarry Smith ierr = PetscOptionsGetStringArray(NULL,"-dll_prepend",libname,&nmax,NULL);CHKERRQ(ierr); 65e5c89e4eSSatish Balay for (i=0; i<nmax; i++) { 66d44a1e48SBarry Smith ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr); 67e5c89e4eSSatish Balay ierr = PetscFree(libname[i]);CHKERRQ(ierr); 68e5c89e4eSSatish Balay } 69e5c89e4eSSatish Balay 70aa2d57e9SJed Brown #if !defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 713fa76a5bSLisandro Dalcin /* 723fa76a5bSLisandro Dalcin This just initializes the most basic PETSc stuff. 733fa76a5bSLisandro Dalcin 743fa76a5bSLisandro Dalcin The classes, from PetscDraw to PetscTS, are initialized the first 753fa76a5bSLisandro Dalcin time an XXCreate() is called. 763fa76a5bSLisandro Dalcin */ 77607a6623SBarry Smith ierr = PetscSysInitializePackage();CHKERRQ(ierr); 783fa76a5bSLisandro Dalcin #else 79aa2d57e9SJed Brown preload = PETSC_FALSE; 80aa2d57e9SJed Brown ierr = PetscOptionsGetBool(NULL,"-dynamic_library_preload",&preload,NULL);CHKERRQ(ierr); 81aa2d57e9SJed Brown if (preload) { 82aa2d57e9SJed Brown PetscBool found; 8360154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY) 84487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr); 85e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!"); 8660154eb2SBarry Smith #else 8760154eb2SBarry Smith ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr); 8860154eb2SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!"); 89487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr); 90e32f2f54SBarry 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!"); 91487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr); 92e32f2f54SBarry 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!"); 93487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr); 94e32f2f54SBarry 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!"); 95487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr); 96e32f2f54SBarry 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!"); 97487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr); 98e32f2f54SBarry 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!"); 99487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr); 100e32f2f54SBarry 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!"); 101bb84e0fdSBarry Smith #endif 102aa2d57e9SJed Brown } 1033fa76a5bSLisandro Dalcin #endif 104e5c89e4eSSatish Balay 105e5c89e4eSSatish Balay nmax = 32; 1060298fd71SBarry Smith ierr = PetscOptionsGetStringArray(NULL,"-dll_append",libname,&nmax,NULL);CHKERRQ(ierr); 107e5c89e4eSSatish Balay for (i=0; i<nmax; i++) { 108d44a1e48SBarry Smith ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr); 109e5c89e4eSSatish Balay ierr = PetscFree(libname[i]);CHKERRQ(ierr); 110e5c89e4eSSatish Balay } 1113020f62cSBarry Smith 112*acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY) 1133020f62cSBarry Smith ierr = PetscThreadCommInitializePackage();CHKERRQ(ierr); 1143020f62cSBarry Smith ierr = PetscThreadCommWorldInitialize();CHKERRQ(ierr); 1153020f62cSBarry Smith ierr = AOInitializePackage();CHKERRQ(ierr); 1163020f62cSBarry Smith ierr = PetscSFInitializePackage();CHKERRQ(ierr); 1173020f62cSBarry Smith ierr = CharacteristicInitializePackage();CHKERRQ(ierr); 1183020f62cSBarry Smith ierr = ISInitializePackage();CHKERRQ(ierr); 1193020f62cSBarry Smith ierr = VecInitializePackage();CHKERRQ(ierr); 1203020f62cSBarry Smith ierr = MatInitializePackage();CHKERRQ(ierr); 1213020f62cSBarry Smith ierr = DMInitializePackage();CHKERRQ(ierr); 1223020f62cSBarry Smith ierr = PCInitializePackage();CHKERRQ(ierr); 1233020f62cSBarry Smith ierr = KSPInitializePackage();CHKERRQ(ierr); 1243020f62cSBarry Smith ierr = SNESInitializePackage();CHKERRQ(ierr); 1253020f62cSBarry Smith ierr = TSInitializePackage();CHKERRQ(ierr); 126*acff04ddSBarry Smith ierr = PetscCommDuplicate(PETSC_COMM_SELF,&PETSC_COMM_SELF_INNER,NULL);CHKERRQ(ierr); 127*acff04ddSBarry Smith ierr = PetscCommDuplicate(PETSC_COMM_WORLD,&PETSC_COMM_WORLD_INNER,NULL);CHKERRQ(ierr); 128*acff04ddSBarry Smith #endif 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; 1430298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-dll_view",&flg,NULL);CHKERRQ(ierr); 144d44a1e48SBarry Smith if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); } 145d44a1e48SBarry Smith ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr); 146a297a907SKarl Rupp 147*acff04ddSBarry Smith #if defined(PETSC_HAVE_THREADSAFETY) 148*acff04ddSBarry Smith ierr = PetscCommDestroy(&PETSC_COMM_SELF_INNER);CHKERRQ(ierr); 149*acff04ddSBarry Smith ierr = PetscCommDestroy(&PETSC_COMM_WORLD_INNER);CHKERRQ(ierr); 150*acff04ddSBarry Smith #endif 151*acff04ddSBarry Smith 152d44a1e48SBarry Smith PetscDLLibrariesLoaded = 0; 153e5c89e4eSSatish Balay PetscFunctionReturn(0); 154e5c89e4eSSatish Balay } 155e5c89e4eSSatish Balay 156e4d1774bSDmitry Karpeev 157e4d1774bSDmitry Karpeev 158e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/ 159140e18c1SBarry Smith struct _n_PetscFunctionList { 160e5c89e4eSSatish Balay void (*routine)(void); /* the routine */ 161e5c89e4eSSatish Balay char *name; /* string to identify routine */ 162140e18c1SBarry Smith PetscFunctionList next; /* next pointer */ 163140e18c1SBarry Smith PetscFunctionList next_list; /* used to maintain list of all lists for freeing */ 164e5c89e4eSSatish Balay }; 165e5c89e4eSSatish Balay 166e5c89e4eSSatish Balay /* 167140e18c1SBarry Smith Keep a linked list of PetscFunctionLists so that we can destroy all the left-over ones. 168e5c89e4eSSatish Balay */ 169140e18c1SBarry Smith static PetscFunctionList dlallhead = 0; 170e5c89e4eSSatish Balay 171a240a19fSJed Brown /*MC 172140e18c1SBarry Smith PetscFunctionListAdd - Given a routine and a string id, saves that routine in the 173e5c89e4eSSatish Balay specified registry. 174e5c89e4eSSatish Balay 175a240a19fSJed Brown Synopsis: 176aaa7dc30SBarry Smith #include <petscsys.h> 177a240a19fSJed Brown PetscErrorCode PetscFunctionListAdd(PetscFunctionList flist,const char name[],void (*fptr)(void)) 178a240a19fSJed Brown 179d4349b43SBarry Smith Not Collective 180e5c89e4eSSatish Balay 181e5c89e4eSSatish Balay Input Parameters: 182a240a19fSJed Brown + flist - pointer registry 183e5c89e4eSSatish Balay . name - string to identify routine 184a240a19fSJed Brown - fptr - function pointer 185e5c89e4eSSatish Balay 186e5c89e4eSSatish Balay Notes: 187a240a19fSJed Brown To remove a registered routine, pass in a NULL fptr. 188e5c89e4eSSatish Balay 189e5c89e4eSSatish Balay Users who wish to register new classes for use by a particular PETSc 190e5c89e4eSSatish Balay component (e.g., SNES) should generally call the registration routine 1911c84c290SBarry Smith for that particular component (e.g., SNESRegister()) instead of 192140e18c1SBarry Smith calling PetscFunctionListAdd() directly. 193e5c89e4eSSatish Balay 194e5c89e4eSSatish Balay Level: developer 195e5c89e4eSSatish Balay 1961c84c290SBarry Smith .seealso: PetscFunctionListDestroy(), SNESRegister(), KSPRegister(), 197a240a19fSJed Brown PCRegister(), TSRegister(), PetscFunctionList, PetscObjectComposeFunction() 198a240a19fSJed Brown M*/ 199a240a19fSJed Brown #undef __FUNCT__ 200a240a19fSJed Brown #define __FUNCT__ "PetscFunctionListAdd_Private" 201a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *fl,const char name[],void (*fnc)(void)) 202e5c89e4eSSatish Balay { 203140e18c1SBarry Smith PetscFunctionList entry,ne; 204e5c89e4eSSatish Balay PetscErrorCode ierr; 205e5c89e4eSSatish Balay 206e5c89e4eSSatish Balay PetscFunctionBegin; 207e5c89e4eSSatish Balay if (!*fl) { 208b00a9115SJed Brown ierr = PetscNew(&entry);CHKERRQ(ierr); 209e5c89e4eSSatish Balay ierr = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr); 210e5c89e4eSSatish Balay entry->routine = fnc; 211e5c89e4eSSatish Balay entry->next = 0; 212e5c89e4eSSatish Balay *fl = entry; 213e5c89e4eSSatish Balay 214a8a6328fSBarry Smith #if defined(PETSC_USE_LOG) 215e5c89e4eSSatish Balay /* add this new list to list of all lists */ 216e5c89e4eSSatish Balay if (!dlallhead) { 217e5c89e4eSSatish Balay dlallhead = *fl; 218e5c89e4eSSatish Balay (*fl)->next_list = 0; 219e5c89e4eSSatish Balay } else { 220e5c89e4eSSatish Balay ne = dlallhead; 221e5c89e4eSSatish Balay dlallhead = *fl; 222e5c89e4eSSatish Balay (*fl)->next_list = ne; 223e5c89e4eSSatish Balay } 224a8a6328fSBarry Smith #endif 225a8a6328fSBarry Smith 226e5c89e4eSSatish Balay } else { 227e5c89e4eSSatish Balay /* search list to see if it is already there */ 228e5c89e4eSSatish Balay ne = *fl; 229e5c89e4eSSatish Balay while (ne) { 230ace3abfcSBarry Smith PetscBool founddup; 231e5c89e4eSSatish Balay 232e5c89e4eSSatish Balay ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr); 233e5c89e4eSSatish Balay if (founddup) { /* found duplicate */ 234e5c89e4eSSatish Balay ne->routine = fnc; 235e5c89e4eSSatish Balay PetscFunctionReturn(0); 236e5c89e4eSSatish Balay } 237a297a907SKarl Rupp if (ne->next) ne = ne->next; 238a297a907SKarl Rupp else break; 239e5c89e4eSSatish Balay } 240e5c89e4eSSatish Balay /* create new entry and add to end of list */ 241b00a9115SJed Brown ierr = PetscNew(&entry);CHKERRQ(ierr); 242e5c89e4eSSatish Balay ierr = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr); 243e5c89e4eSSatish Balay entry->routine = fnc; 244e5c89e4eSSatish Balay entry->next = 0; 245e5c89e4eSSatish Balay ne->next = entry; 246e5c89e4eSSatish Balay } 247e5c89e4eSSatish Balay PetscFunctionReturn(0); 248e5c89e4eSSatish Balay } 249e5c89e4eSSatish Balay 250e5c89e4eSSatish Balay #undef __FUNCT__ 251140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDestroy" 252e5c89e4eSSatish Balay /*@ 253140e18c1SBarry Smith PetscFunctionListDestroy - Destroys a list of registered routines. 254e5c89e4eSSatish Balay 255e5c89e4eSSatish Balay Input Parameter: 256e5c89e4eSSatish Balay . fl - pointer to list 257e5c89e4eSSatish Balay 258e5c89e4eSSatish Balay Level: developer 259e5c89e4eSSatish Balay 260a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList 261e5c89e4eSSatish Balay @*/ 262140e18c1SBarry Smith PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *fl) 263e5c89e4eSSatish Balay { 264140e18c1SBarry Smith PetscFunctionList next,entry,tmp = dlallhead; 265e5c89e4eSSatish Balay PetscErrorCode ierr; 266e5c89e4eSSatish Balay 267e5c89e4eSSatish Balay PetscFunctionBegin; 2681441b1d3SBarry Smith if (!*fl) PetscFunctionReturn(0); 269e5c89e4eSSatish Balay 270e5c89e4eSSatish Balay /* 271e5c89e4eSSatish Balay Remove this entry from the master DL list (if it is in it) 272e5c89e4eSSatish Balay */ 2731441b1d3SBarry Smith if (dlallhead == *fl) { 274a297a907SKarl Rupp if (dlallhead->next_list) dlallhead = dlallhead->next_list; 27537e93019SBarry Smith else dlallhead = NULL; 27637e93019SBarry Smith } else if (tmp) { 2771441b1d3SBarry Smith while (tmp->next_list != *fl) { 278e5c89e4eSSatish Balay tmp = tmp->next_list; 279e5c89e4eSSatish Balay if (!tmp->next_list) break; 280e5c89e4eSSatish Balay } 281e5c89e4eSSatish Balay if (tmp->next_list) tmp->next_list = tmp->next_list->next_list; 282e5c89e4eSSatish Balay } 283e5c89e4eSSatish Balay 284e5c89e4eSSatish Balay /* free this list */ 2851441b1d3SBarry Smith entry = *fl; 286e5c89e4eSSatish Balay while (entry) { 287e5c89e4eSSatish Balay next = entry->next; 288e5c89e4eSSatish Balay ierr = PetscFree(entry->name);CHKERRQ(ierr); 289e5c89e4eSSatish Balay ierr = PetscFree(entry);CHKERRQ(ierr); 290e5c89e4eSSatish Balay entry = next; 291e5c89e4eSSatish Balay } 2921441b1d3SBarry Smith *fl = 0; 293e5c89e4eSSatish Balay PetscFunctionReturn(0); 294e5c89e4eSSatish Balay } 295e5c89e4eSSatish Balay 296e5c89e4eSSatish Balay /* 29737e93019SBarry Smith Print any PetscFunctionLists that have not be destroyed 298e5c89e4eSSatish Balay */ 299e5c89e4eSSatish Balay #undef __FUNCT__ 30037e93019SBarry Smith #define __FUNCT__ "PetscFunctionListPrintAll" 30137e93019SBarry Smith PetscErrorCode PetscFunctionListPrintAll(void) 302e5c89e4eSSatish Balay { 30337e93019SBarry Smith PetscFunctionList tmp = dlallhead; 304e5c89e4eSSatish Balay PetscErrorCode ierr; 305e5c89e4eSSatish Balay 306e5c89e4eSSatish Balay PetscFunctionBegin; 30737e93019SBarry Smith if (tmp) { 30837e93019SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"The following PetscFunctionLists were not destroyed\n");CHKERRQ(ierr); 309e5c89e4eSSatish Balay } 31037e93019SBarry Smith while (tmp) { 31137e93019SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"%s \n",tmp->name);CHKERRQ(ierr); 31237e93019SBarry Smith tmp = tmp->next_list; 31337e93019SBarry Smith } 314e5c89e4eSSatish Balay PetscFunctionReturn(0); 315e5c89e4eSSatish Balay } 316e5c89e4eSSatish Balay 3171c9cd337SJed Brown /*MC 3181c9cd337SJed Brown PetscFunctionListFind - Find function registered under given name 3191c9cd337SJed Brown 3201c9cd337SJed Brown Synopsis: 321aaa7dc30SBarry Smith #include <petscsys.h> 3221c9cd337SJed Brown PetscErrorCode PetscFunctionListFind(PetscFunctionList flist,const char name[],void (**fptr)(void)) 323e5c89e4eSSatish Balay 324e5c89e4eSSatish Balay Input Parameters: 3251c9cd337SJed Brown + flist - pointer to list 3261c9cd337SJed Brown - name - name registered for the function 327e5c89e4eSSatish Balay 328e5c89e4eSSatish Balay Output Parameters: 3291c9cd337SJed Brown . fptr - the function pointer if name was found, else NULL 330e5c89e4eSSatish Balay 331e5c89e4eSSatish Balay Level: developer 332e5c89e4eSSatish Balay 3331c9cd337SJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList, PetscObjectQueryFunction() 3341c9cd337SJed Brown M*/ 3351c9cd337SJed Brown #undef __FUNCT__ 3361c9cd337SJed Brown #define __FUNCT__ "PetscFunctionListFind_Private" 3371c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList fl,const char name[],void (**r)(void)) 338e5c89e4eSSatish Balay { 339140e18c1SBarry Smith PetscFunctionList entry = fl; 340e5c89e4eSSatish Balay PetscErrorCode ierr; 341bdf89e91SBarry Smith PetscBool flg; 342e5c89e4eSSatish Balay 343e5c89e4eSSatish Balay PetscFunctionBegin; 344e32f2f54SBarry Smith if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name"); 345e5c89e4eSSatish Balay 346e5c89e4eSSatish Balay *r = 0; 347e5c89e4eSSatish Balay while (entry) { 348bdf89e91SBarry Smith ierr = PetscStrcmp(name,entry->name,&flg);CHKERRQ(ierr); 349bdf89e91SBarry Smith if (flg) { 350e5c89e4eSSatish Balay *r = entry->routine; 351e5c89e4eSSatish Balay PetscFunctionReturn(0); 352e5c89e4eSSatish Balay } 353e5c89e4eSSatish Balay entry = entry->next; 354e5c89e4eSSatish Balay } 355e5c89e4eSSatish Balay PetscFunctionReturn(0); 356e5c89e4eSSatish Balay } 357e5c89e4eSSatish Balay 358e5c89e4eSSatish Balay #undef __FUNCT__ 359140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListView" 360e5c89e4eSSatish Balay /*@ 361140e18c1SBarry Smith PetscFunctionListView - prints out contents of an PetscFunctionList 362e5c89e4eSSatish Balay 363e5c89e4eSSatish Balay Collective over MPI_Comm 364e5c89e4eSSatish Balay 365e5c89e4eSSatish Balay Input Parameters: 366e5c89e4eSSatish Balay + list - the list of functions 367e5c89e4eSSatish Balay - viewer - currently ignored 368e5c89e4eSSatish Balay 369e5c89e4eSSatish Balay Level: developer 370e5c89e4eSSatish Balay 371a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionListPrintTypes(), PetscFunctionList 372e5c89e4eSSatish Balay @*/ 373140e18c1SBarry Smith PetscErrorCode PetscFunctionListView(PetscFunctionList list,PetscViewer viewer) 374e5c89e4eSSatish Balay { 375e5c89e4eSSatish Balay PetscErrorCode ierr; 376ace3abfcSBarry Smith PetscBool iascii; 377e5c89e4eSSatish Balay 378e5c89e4eSSatish Balay PetscFunctionBegin; 379e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 380e5c89e4eSSatish Balay PetscValidPointer(list,1); 3810700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 382e5c89e4eSSatish Balay 383251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 384e32f2f54SBarry Smith if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported"); 385e5c89e4eSSatish Balay 386e5c89e4eSSatish Balay while (list) { 387bdf89e91SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %s\n",list->name);CHKERRQ(ierr); 388e5c89e4eSSatish Balay list = list->next; 389e5c89e4eSSatish Balay } 390e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 391e5c89e4eSSatish Balay PetscFunctionReturn(0); 392e5c89e4eSSatish Balay } 393e5c89e4eSSatish Balay 394e5c89e4eSSatish Balay #undef __FUNCT__ 395140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListGet" 396065533a5SJed Brown /*@C 397140e18c1SBarry Smith PetscFunctionListGet - Gets an array the contains the entries in PetscFunctionList, this is used 398e5c89e4eSSatish Balay by help etc. 399e5c89e4eSSatish Balay 400d4349b43SBarry Smith Not Collective 401e5c89e4eSSatish Balay 402e5c89e4eSSatish Balay Input Parameter: 403e5c89e4eSSatish Balay . list - list of types 404e5c89e4eSSatish Balay 405e5c89e4eSSatish Balay Output Parameter: 406e5c89e4eSSatish Balay + array - array of names 407e5c89e4eSSatish Balay - n - length of array 408e5c89e4eSSatish Balay 409e5c89e4eSSatish Balay Notes: 410e5c89e4eSSatish Balay This allocates the array so that must be freed. BUT the individual entries are 411e5c89e4eSSatish Balay not copied so should not be freed. 412e5c89e4eSSatish Balay 413e5c89e4eSSatish Balay Level: developer 414e5c89e4eSSatish Balay 415a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList 416e5c89e4eSSatish Balay @*/ 417140e18c1SBarry Smith PetscErrorCode PetscFunctionListGet(PetscFunctionList list,const char ***array,int *n) 418e5c89e4eSSatish Balay { 419e5c89e4eSSatish Balay PetscErrorCode ierr; 420e5c89e4eSSatish Balay PetscInt count = 0; 421140e18c1SBarry Smith PetscFunctionList klist = list; 422e5c89e4eSSatish Balay 423e5c89e4eSSatish Balay PetscFunctionBegin; 424e5c89e4eSSatish Balay while (list) { 425e5c89e4eSSatish Balay list = list->next; 426e5c89e4eSSatish Balay count++; 427e5c89e4eSSatish Balay } 428854ce69bSBarry Smith ierr = PetscMalloc1(count+1,array);CHKERRQ(ierr); 429e5c89e4eSSatish Balay count = 0; 430e5c89e4eSSatish Balay while (klist) { 431e5c89e4eSSatish Balay (*array)[count] = klist->name; 432e5c89e4eSSatish Balay klist = klist->next; 433e5c89e4eSSatish Balay count++; 434e5c89e4eSSatish Balay } 435e5c89e4eSSatish Balay (*array)[count] = 0; 436e5c89e4eSSatish Balay *n = count+1; 437e5c89e4eSSatish Balay PetscFunctionReturn(0); 438e5c89e4eSSatish Balay } 439e5c89e4eSSatish Balay 440e5c89e4eSSatish Balay 441e5c89e4eSSatish Balay #undef __FUNCT__ 442140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListPrintTypes" 443e5c89e4eSSatish Balay /*@C 444140e18c1SBarry Smith PetscFunctionListPrintTypes - Prints the methods available. 445e5c89e4eSSatish Balay 446e5c89e4eSSatish Balay Collective over MPI_Comm 447e5c89e4eSSatish Balay 448e5c89e4eSSatish Balay Input Parameters: 449e5c89e4eSSatish Balay + comm - the communicator (usually MPI_COMM_WORLD) 450e5c89e4eSSatish Balay . fd - file to print to, usually stdout 451e5c89e4eSSatish Balay . prefix - prefix to prepend to name (optional) 452e5c89e4eSSatish Balay . name - option string (for example, "-ksp_type") 453e5c89e4eSSatish Balay . text - short description of the object (for example, "Krylov solvers") 454e5c89e4eSSatish Balay . man - name of manual page that discusses the object (for example, "KSPCreate") 4553cc1e11dSBarry Smith . list - list of types 4563cc1e11dSBarry Smith - def - default (current) value 457e5c89e4eSSatish Balay 458e5c89e4eSSatish Balay Level: developer 459e5c89e4eSSatish Balay 460a240a19fSJed Brown .seealso: PetscFunctionListAdd(), PetscFunctionList 461e5c89e4eSSatish Balay @*/ 462140e18c1SBarry 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[]) 463e5c89e4eSSatish Balay { 464e5c89e4eSSatish Balay PetscErrorCode ierr; 465e5c89e4eSSatish Balay PetscInt count = 0; 466e5c89e4eSSatish Balay char p[64]; 467e5c89e4eSSatish Balay 468e5c89e4eSSatish Balay PetscFunctionBegin; 469da9f1d6bSBarry Smith if (!fd) fd = PETSC_STDOUT; 470e5c89e4eSSatish Balay 471e5c89e4eSSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 472e5c89e4eSSatish Balay if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);} 4733cc1e11dSBarry Smith ierr = PetscFPrintf(comm,fd," %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr); 474e5c89e4eSSatish Balay 475e5c89e4eSSatish Balay while (list) { 476e5c89e4eSSatish Balay ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr); 477e5c89e4eSSatish Balay list = list->next; 478e5c89e4eSSatish Balay count++; 479e5c89e4eSSatish Balay if (count == 8) {ierr = PetscFPrintf(comm,fd,"\n ");CHKERRQ(ierr);} 480e5c89e4eSSatish Balay } 481e5c89e4eSSatish Balay ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr); 482e5c89e4eSSatish Balay PetscFunctionReturn(0); 483e5c89e4eSSatish Balay } 484e5c89e4eSSatish Balay 485e5c89e4eSSatish Balay #undef __FUNCT__ 486140e18c1SBarry Smith #define __FUNCT__ "PetscFunctionListDuplicate" 487e5c89e4eSSatish Balay /*@ 488140e18c1SBarry Smith PetscFunctionListDuplicate - Creates a new list from a given object list. 489e5c89e4eSSatish Balay 490e5c89e4eSSatish Balay Input Parameters: 491e5c89e4eSSatish Balay . fl - pointer to list 492e5c89e4eSSatish Balay 493e5c89e4eSSatish Balay Output Parameters: 494e5c89e4eSSatish Balay . nl - the new list (should point to 0 to start, otherwise appends) 495e5c89e4eSSatish Balay 496e5c89e4eSSatish Balay Level: developer 497e5c89e4eSSatish Balay 498140e18c1SBarry Smith .seealso: PetscFunctionList, PetscFunctionListAdd(), PetscFlistDestroy() 499e5c89e4eSSatish Balay 500e5c89e4eSSatish Balay @*/ 501140e18c1SBarry Smith PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList fl,PetscFunctionList *nl) 502e5c89e4eSSatish Balay { 503e5c89e4eSSatish Balay PetscErrorCode ierr; 504e5c89e4eSSatish Balay 505e5c89e4eSSatish Balay PetscFunctionBegin; 506e5c89e4eSSatish Balay while (fl) { 507bdf89e91SBarry Smith ierr = PetscFunctionListAdd(nl,fl->name,fl->routine);CHKERRQ(ierr); 508e5c89e4eSSatish Balay fl = fl->next; 509e5c89e4eSSatish Balay } 510e5c89e4eSSatish Balay PetscFunctionReturn(0); 511e5c89e4eSSatish Balay } 512e5c89e4eSSatish Balay 513