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 */ 6c6db04a5SJed Brown #include <petscsys.h> /*I "petscsys.h" I*/ 7e5c89e4eSSatish Balay 8e5c89e4eSSatish Balay #undef __FUNCT__ 9e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListGetPathAndFunction" 107087cfbeSBarry Smith PetscErrorCode PetscFListGetPathAndFunction(const char name[],char *path[],char *function[]) 11e5c89e4eSSatish Balay { 12e5c89e4eSSatish Balay PetscErrorCode ierr; 13e5c89e4eSSatish Balay char work[PETSC_MAX_PATH_LEN],*lfunction; 14e5c89e4eSSatish Balay 15e5c89e4eSSatish Balay PetscFunctionBegin; 168caf3d72SBarry Smith ierr = PetscStrncpy(work,name,sizeof(work));CHKERRQ(ierr); 178caf3d72SBarry Smith work[sizeof(work) - 1] = 0; 18e5c89e4eSSatish Balay ierr = PetscStrchr(work,':',&lfunction);CHKERRQ(ierr); 19e5c89e4eSSatish Balay if (lfunction != work && lfunction && lfunction[1] != ':') { 20e5c89e4eSSatish Balay lfunction[0] = 0; 21e5c89e4eSSatish Balay ierr = PetscStrallocpy(work,path);CHKERRQ(ierr); 22e5c89e4eSSatish Balay ierr = PetscStrallocpy(lfunction+1,function);CHKERRQ(ierr); 23e5c89e4eSSatish Balay } else { 24e5c89e4eSSatish Balay *path = 0; 25e5c89e4eSSatish Balay ierr = PetscStrallocpy(name,function);CHKERRQ(ierr); 26e5c89e4eSSatish Balay } 27e5c89e4eSSatish Balay PetscFunctionReturn(0); 28e5c89e4eSSatish Balay } 29e5c89e4eSSatish Balay 303fa76a5bSLisandro Dalcin /* 313fa76a5bSLisandro Dalcin This is the default list used by PETSc with the PetscDLLibrary register routines 323fa76a5bSLisandro Dalcin */ 33d44a1e48SBarry Smith PetscDLLibrary PetscDLLibrariesLoaded = 0; 343fa76a5bSLisandro Dalcin 35ebd79076SLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES) 36ebd79076SLisandro Dalcin 37e5c89e4eSSatish Balay #undef __FUNCT__ 38487e5849SBarry Smith #define __FUNCT__ "PetscLoadDynamicLibrary" 397087cfbeSBarry Smith static PetscErrorCode PetscLoadDynamicLibrary(const char *name,PetscBool *found) 40487e5849SBarry Smith { 41487e5849SBarry Smith char libs[PETSC_MAX_PATH_LEN],dlib[PETSC_MAX_PATH_LEN]; 42487e5849SBarry Smith PetscErrorCode ierr; 43487e5849SBarry Smith 44487e5849SBarry Smith PetscFunctionBegin; 45487e5849SBarry Smith ierr = PetscStrcpy(libs,"${PETSC_LIB_DIR}/libpetsc");CHKERRQ(ierr); 46487e5849SBarry Smith ierr = PetscStrcat(libs,name);CHKERRQ(ierr); 47487e5849SBarry Smith ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr); 48487e5849SBarry Smith if (*found) { 49d44a1e48SBarry Smith ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr); 50487e5849SBarry Smith } else { 51487e5849SBarry Smith ierr = PetscStrcpy(libs,"${PETSC_DIR}/${PETSC_ARCH}/lib/libpetsc");CHKERRQ(ierr); 52487e5849SBarry Smith ierr = PetscStrcat(libs,name);CHKERRQ(ierr); 53487e5849SBarry Smith ierr = PetscDLLibraryRetrieve(PETSC_COMM_WORLD,libs,dlib,1024,found);CHKERRQ(ierr); 54487e5849SBarry Smith if (*found) { 55d44a1e48SBarry Smith ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,dlib);CHKERRQ(ierr); 56487e5849SBarry Smith } 57487e5849SBarry Smith } 58487e5849SBarry Smith PetscFunctionReturn(0); 59487e5849SBarry Smith } 60487e5849SBarry Smith 613fa76a5bSLisandro Dalcin #endif 623fa76a5bSLisandro Dalcin 63487e5849SBarry Smith #undef __FUNCT__ 64e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize_DynamicLibraries" 65e5c89e4eSSatish Balay /* 66e5c89e4eSSatish Balay PetscInitialize_DynamicLibraries - Adds the default dynamic link libraries to the 67e5c89e4eSSatish Balay search path. 68e5c89e4eSSatish Balay */ 697087cfbeSBarry Smith PetscErrorCode PetscInitialize_DynamicLibraries(void) 70e5c89e4eSSatish Balay { 71487e5849SBarry Smith char *libname[32]; 72e5c89e4eSSatish Balay PetscErrorCode ierr; 73e5c89e4eSSatish Balay PetscInt nmax,i; 743fa76a5bSLisandro Dalcin #if defined(PETSC_USE_DYNAMIC_LIBRARIES) 75ace3abfcSBarry Smith PetscBool found; 763fa76a5bSLisandro Dalcin #endif 77e5c89e4eSSatish Balay 7860154eb2SBarry Smith PetscFunctionBegin; 79e5c89e4eSSatish Balay nmax = 32; 80e5c89e4eSSatish Balay ierr = PetscOptionsGetStringArray(PETSC_NULL,"-dll_prepend",libname,&nmax,PETSC_NULL);CHKERRQ(ierr); 81e5c89e4eSSatish Balay for (i=0; i<nmax; i++) { 82d44a1e48SBarry Smith ierr = PetscDLLibraryPrepend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr); 83e5c89e4eSSatish Balay ierr = PetscFree(libname[i]);CHKERRQ(ierr); 84e5c89e4eSSatish Balay } 85e5c89e4eSSatish Balay 863fa76a5bSLisandro Dalcin #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 873fa76a5bSLisandro Dalcin /* 883fa76a5bSLisandro Dalcin This just initializes the most basic PETSc stuff. 893fa76a5bSLisandro Dalcin 903fa76a5bSLisandro Dalcin The classes, from PetscDraw to PetscTS, are initialized the first 913fa76a5bSLisandro Dalcin time an XXCreate() is called. 923fa76a5bSLisandro Dalcin */ 9360154eb2SBarry Smith ierr = PetscSysInitializePackage(PETSC_NULL);CHKERRQ(ierr); 943fa76a5bSLisandro Dalcin #else 9560154eb2SBarry Smith #if defined(PETSC_USE_SINGLE_LIBRARY) 96487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("",&found);CHKERRQ(ierr); 97e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!"); 9860154eb2SBarry Smith #else 9960154eb2SBarry Smith ierr = PetscLoadDynamicLibrary("sys",&found);CHKERRQ(ierr); 10060154eb2SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc dynamic library \n You cannot move the dynamic libraries!"); 101487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("vec",&found);CHKERRQ(ierr); 102e32f2f54SBarry 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!"); 103487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("mat",&found);CHKERRQ(ierr); 104e32f2f54SBarry 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!"); 105487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("dm",&found);CHKERRQ(ierr); 106e32f2f54SBarry 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!"); 10760154eb2SBarry Smith ierr = PetscLoadDynamicLibrary("characteristic",&found);CHKERRQ(ierr); 10860154eb2SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate PETSc Characteristic dynamic library \n You cannot move the dynamic libraries!"); 109487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("ksp",&found);CHKERRQ(ierr); 110e32f2f54SBarry 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!"); 111487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("snes",&found);CHKERRQ(ierr); 112e32f2f54SBarry 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!"); 113487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("ts",&found);CHKERRQ(ierr); 114e32f2f54SBarry 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!"); 115bb84e0fdSBarry Smith #endif 116e5c89e4eSSatish Balay 117487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("mesh",&found);CHKERRQ(ierr); 118487e5849SBarry Smith ierr = PetscLoadDynamicLibrary("contrib",&found);CHKERRQ(ierr); 1193fa76a5bSLisandro Dalcin #endif 120e5c89e4eSSatish Balay 121e5c89e4eSSatish Balay nmax = 32; 122e5c89e4eSSatish Balay ierr = PetscOptionsGetStringArray(PETSC_NULL,"-dll_append",libname,&nmax,PETSC_NULL);CHKERRQ(ierr); 123e5c89e4eSSatish Balay for (i=0; i<nmax; i++) { 124d44a1e48SBarry Smith ierr = PetscDLLibraryAppend(PETSC_COMM_WORLD,&PetscDLLibrariesLoaded,libname[i]);CHKERRQ(ierr); 125e5c89e4eSSatish Balay ierr = PetscFree(libname[i]);CHKERRQ(ierr); 126e5c89e4eSSatish Balay } 1273fa76a5bSLisandro Dalcin 128e5c89e4eSSatish Balay PetscFunctionReturn(0); 129e5c89e4eSSatish Balay } 130e5c89e4eSSatish Balay 131e5c89e4eSSatish Balay #undef __FUNCT__ 132e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize_DynamicLibraries" 133ebd79076SLisandro Dalcin /* 134ebd79076SLisandro Dalcin PetscFinalize_DynamicLibraries - Closes the opened dynamic libraries. 135ebd79076SLisandro Dalcin */ 136e5c89e4eSSatish Balay PetscErrorCode PetscFinalize_DynamicLibraries(void) 137e5c89e4eSSatish Balay { 138ebd79076SLisandro Dalcin PetscErrorCode ierr; 139ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 140e5c89e4eSSatish Balay 141ebd79076SLisandro Dalcin PetscFunctionBegin; 142acfcf0e5SJed Brown ierr = PetscOptionsGetBool(PETSC_NULL,"-dll_view",&flg,PETSC_NULL);CHKERRQ(ierr); 143d44a1e48SBarry Smith if (flg) { ierr = PetscDLLibraryPrintPath(PetscDLLibrariesLoaded);CHKERRQ(ierr); } 144d44a1e48SBarry Smith ierr = PetscDLLibraryClose(PetscDLLibrariesLoaded);CHKERRQ(ierr); 145d44a1e48SBarry Smith PetscDLLibrariesLoaded = 0; 146e5c89e4eSSatish Balay PetscFunctionReturn(0); 147e5c89e4eSSatish Balay } 148e5c89e4eSSatish Balay 149e4d1774bSDmitry Karpeev 150e4d1774bSDmitry Karpeev 151e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/ 1522bb46157SSatish Balay struct _n_PetscFList { 153e5c89e4eSSatish Balay void (*routine)(void); /* the routine */ 154e5c89e4eSSatish Balay char *path; /* path of link library containing routine */ 155e5c89e4eSSatish Balay char *name; /* string to identify routine */ 156e5c89e4eSSatish Balay char *rname; /* routine name in dynamic library */ 157e5c89e4eSSatish Balay PetscFList next; /* next pointer */ 158e5c89e4eSSatish Balay PetscFList next_list; /* used to maintain list of all lists for freeing */ 159e5c89e4eSSatish Balay }; 160e5c89e4eSSatish Balay 161e5c89e4eSSatish Balay /* 162e5c89e4eSSatish Balay Keep a linked list of PetscFLists so that we can destroy all the left-over ones. 163e5c89e4eSSatish Balay */ 164e5c89e4eSSatish Balay static PetscFList dlallhead = 0; 165e5c89e4eSSatish Balay 166e5c89e4eSSatish Balay #undef __FUNCT__ 167e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListAdd" 168e5c89e4eSSatish Balay /*@C 169e4d1774bSDmitry Karpeev PetscFListAdd - Given a routine and a string id, saves that routine in the 170e5c89e4eSSatish Balay specified registry. 171e5c89e4eSSatish Balay 172*6d75e210SBarry Smith Formally Collective on MPI_Comm 173e5c89e4eSSatish Balay 174e5c89e4eSSatish Balay Input Parameters: 175*6d75e210SBarry Smith + comm - the comm where this exists (currently not used) 176*6d75e210SBarry Smith . fl - pointer registry 177e5c89e4eSSatish Balay . name - string to identify routine 178e5c89e4eSSatish Balay . rname - routine name in dynamic library 179e5c89e4eSSatish Balay - fnc - function pointer (optional if using dynamic libraries) 180e5c89e4eSSatish Balay 181e5c89e4eSSatish Balay Notes: 182e5c89e4eSSatish Balay To remove a registered routine, pass in a PETSC_NULL rname and fnc(). 183e5c89e4eSSatish Balay 184e5c89e4eSSatish Balay Users who wish to register new classes for use by a particular PETSc 185e5c89e4eSSatish Balay component (e.g., SNES) should generally call the registration routine 186e5c89e4eSSatish Balay for that particular component (e.g., SNESRegisterDynamic()) instead of 187e4d1774bSDmitry Karpeev calling PetscFListAdd() directly. 188e5c89e4eSSatish Balay 189e5c89e4eSSatish Balay ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environmental variable} 190e5c89e4eSSatish Balay occuring in pathname will be replaced with appropriate values. 191e5c89e4eSSatish Balay 192e5c89e4eSSatish Balay Level: developer 193e5c89e4eSSatish Balay 194e5c89e4eSSatish Balay .seealso: PetscFListDestroy(), SNESRegisterDynamic(), KSPRegisterDynamic(), 195e5c89e4eSSatish Balay PCRegisterDynamic(), TSRegisterDynamic(), PetscFList 196e5c89e4eSSatish Balay @*/ 197*6d75e210SBarry Smith PetscErrorCode PetscFListAdd(MPI_Comm comm,PetscFList *fl,const char name[],const char rname[],void (*fnc)(void)) 198e5c89e4eSSatish Balay { 199e5c89e4eSSatish Balay PetscFList entry,ne; 200e5c89e4eSSatish Balay PetscErrorCode ierr; 201e5c89e4eSSatish Balay char *fpath,*fname; 202e5c89e4eSSatish Balay 203e5c89e4eSSatish Balay PetscFunctionBegin; 204e5c89e4eSSatish Balay if (!*fl) { 2052bb46157SSatish Balay ierr = PetscNew(struct _n_PetscFList,&entry);CHKERRQ(ierr); 206e5c89e4eSSatish Balay ierr = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr); 207e5c89e4eSSatish Balay ierr = PetscFListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr); 208e5c89e4eSSatish Balay entry->path = fpath; 209e5c89e4eSSatish Balay entry->rname = fname; 210e5c89e4eSSatish Balay entry->routine = fnc; 211e5c89e4eSSatish Balay entry->next = 0; 212e5c89e4eSSatish Balay *fl = entry; 213e5c89e4eSSatish Balay 214e5c89e4eSSatish Balay /* add this new list to list of all lists */ 215e5c89e4eSSatish Balay if (!dlallhead) { 216e5c89e4eSSatish Balay dlallhead = *fl; 217e5c89e4eSSatish Balay (*fl)->next_list = 0; 218e5c89e4eSSatish Balay } else { 219e5c89e4eSSatish Balay ne = dlallhead; 220e5c89e4eSSatish Balay dlallhead = *fl; 221e5c89e4eSSatish Balay (*fl)->next_list = ne; 222e5c89e4eSSatish Balay } 223e5c89e4eSSatish Balay } else { 224e5c89e4eSSatish Balay /* search list to see if it is already there */ 225e5c89e4eSSatish Balay ne = *fl; 226e5c89e4eSSatish Balay while (ne) { 227ace3abfcSBarry Smith PetscBool founddup; 228e5c89e4eSSatish Balay 229e5c89e4eSSatish Balay ierr = PetscStrcmp(ne->name,name,&founddup);CHKERRQ(ierr); 230e5c89e4eSSatish Balay if (founddup) { /* found duplicate */ 231e5c89e4eSSatish Balay ierr = PetscFListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr); 232503cfb0cSBarry Smith ierr = PetscFree(ne->path);CHKERRQ(ierr); 233503cfb0cSBarry Smith ierr = PetscFree(ne->rname);CHKERRQ(ierr); 234e5c89e4eSSatish Balay ne->path = fpath; 235e5c89e4eSSatish Balay ne->rname = fname; 236e5c89e4eSSatish Balay ne->routine = fnc; 237e5c89e4eSSatish Balay PetscFunctionReturn(0); 238e5c89e4eSSatish Balay } 239e5c89e4eSSatish Balay if (ne->next) ne = ne->next; else break; 240e5c89e4eSSatish Balay } 241e5c89e4eSSatish Balay /* create new entry and add to end of list */ 2422bb46157SSatish Balay ierr = PetscNew(struct _n_PetscFList,&entry);CHKERRQ(ierr); 243e5c89e4eSSatish Balay ierr = PetscStrallocpy(name,&entry->name);CHKERRQ(ierr); 244e5c89e4eSSatish Balay ierr = PetscFListGetPathAndFunction(rname,&fpath,&fname);CHKERRQ(ierr); 245e5c89e4eSSatish Balay entry->path = fpath; 246e5c89e4eSSatish Balay entry->rname = fname; 247e5c89e4eSSatish Balay entry->routine = fnc; 248e5c89e4eSSatish Balay entry->next = 0; 249e5c89e4eSSatish Balay ne->next = entry; 250e5c89e4eSSatish Balay } 251e5c89e4eSSatish Balay PetscFunctionReturn(0); 252e5c89e4eSSatish Balay } 253e5c89e4eSSatish Balay 254e5c89e4eSSatish Balay #undef __FUNCT__ 255e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListDestroy" 256e5c89e4eSSatish Balay /*@ 257e5c89e4eSSatish Balay PetscFListDestroy - Destroys a list of registered routines. 258e5c89e4eSSatish Balay 259e5c89e4eSSatish Balay Input Parameter: 260e5c89e4eSSatish Balay . fl - pointer to list 261e5c89e4eSSatish Balay 262e5c89e4eSSatish Balay Level: developer 263e5c89e4eSSatish Balay 264e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList 265e5c89e4eSSatish Balay @*/ 2667087cfbeSBarry Smith PetscErrorCode PetscFListDestroy(PetscFList *fl) 267e5c89e4eSSatish Balay { 268e5c89e4eSSatish Balay PetscFList next,entry,tmp = dlallhead; 269e5c89e4eSSatish Balay PetscErrorCode ierr; 270e5c89e4eSSatish Balay 271e5c89e4eSSatish Balay PetscFunctionBegin; 2721441b1d3SBarry Smith if (!*fl) PetscFunctionReturn(0); 27360154eb2SBarry Smith if (!dlallhead) PetscFunctionReturn(0); 274e5c89e4eSSatish Balay 275e5c89e4eSSatish Balay /* 276e5c89e4eSSatish Balay Remove this entry from the master DL list (if it is in it) 277e5c89e4eSSatish Balay */ 2781441b1d3SBarry Smith if (dlallhead == *fl) { 279e5c89e4eSSatish Balay if (dlallhead->next_list) { 280e5c89e4eSSatish Balay dlallhead = dlallhead->next_list; 281e5c89e4eSSatish Balay } else { 282e5c89e4eSSatish Balay dlallhead = 0; 283e5c89e4eSSatish Balay } 284e5c89e4eSSatish Balay } else { 2851441b1d3SBarry Smith while (tmp->next_list != *fl) { 286e5c89e4eSSatish Balay tmp = tmp->next_list; 287e5c89e4eSSatish Balay if (!tmp->next_list) break; 288e5c89e4eSSatish Balay } 289e5c89e4eSSatish Balay if (tmp->next_list) tmp->next_list = tmp->next_list->next_list; 290e5c89e4eSSatish Balay } 291e5c89e4eSSatish Balay 292e5c89e4eSSatish Balay /* free this list */ 2931441b1d3SBarry Smith entry = *fl; 294e5c89e4eSSatish Balay while (entry) { 295e5c89e4eSSatish Balay next = entry->next; 296503cfb0cSBarry Smith ierr = PetscFree(entry->path);CHKERRQ(ierr); 297e5c89e4eSSatish Balay ierr = PetscFree(entry->name);CHKERRQ(ierr); 298e5c89e4eSSatish Balay ierr = PetscFree(entry->rname);CHKERRQ(ierr); 299e5c89e4eSSatish Balay ierr = PetscFree(entry);CHKERRQ(ierr); 300e5c89e4eSSatish Balay entry = next; 301e5c89e4eSSatish Balay } 3021441b1d3SBarry Smith *fl = 0; 303e5c89e4eSSatish Balay PetscFunctionReturn(0); 304e5c89e4eSSatish Balay } 305e5c89e4eSSatish Balay 306e5c89e4eSSatish Balay /* 307e5c89e4eSSatish Balay Destroys all the function lists that anyone has every registered, such as KSPList, VecList, etc. 308e5c89e4eSSatish Balay */ 309e5c89e4eSSatish Balay #undef __FUNCT__ 310e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListDestroyAll" 3117087cfbeSBarry Smith PetscErrorCode PetscFListDestroyAll(void) 312e5c89e4eSSatish Balay { 313e5c89e4eSSatish Balay PetscFList tmp2,tmp1 = dlallhead; 314e5c89e4eSSatish Balay PetscErrorCode ierr; 315e5c89e4eSSatish Balay 316e5c89e4eSSatish Balay PetscFunctionBegin; 317e5c89e4eSSatish Balay while (tmp1) { 318e5c89e4eSSatish Balay tmp2 = tmp1->next_list; 3191441b1d3SBarry Smith ierr = PetscFListDestroy(&tmp1);CHKERRQ(ierr); 320e5c89e4eSSatish Balay tmp1 = tmp2; 321e5c89e4eSSatish Balay } 322e5c89e4eSSatish Balay dlallhead = 0; 323e5c89e4eSSatish Balay PetscFunctionReturn(0); 324e5c89e4eSSatish Balay } 325e5c89e4eSSatish Balay 326e5c89e4eSSatish Balay #undef __FUNCT__ 327e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListFind" 328e5c89e4eSSatish Balay /*@C 329e5c89e4eSSatish Balay PetscFListFind - Given a name, finds the matching routine. 330e5c89e4eSSatish Balay 331e5c89e4eSSatish Balay Input Parameters: 3321d280d73SBarry Smith + fl - pointer to list 3331d280d73SBarry Smith . comm - processors looking for routine 3344b91b6eaSBarry Smith . name - name string 3354b91b6eaSBarry Smith - searchlibraries - if not found in the list then search the dynamic libraries and executable for the symbol 336e5c89e4eSSatish Balay 337e5c89e4eSSatish Balay Output Parameters: 338e5c89e4eSSatish Balay . r - the routine 339e5c89e4eSSatish Balay 340e5c89e4eSSatish Balay Level: developer 341e5c89e4eSSatish Balay 342e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList 343e5c89e4eSSatish Balay @*/ 344*6d75e210SBarry Smith PetscErrorCode PetscFListFind(MPI_Comm comm,PetscFList fl,const char name[],PetscBool searchlibraries,void (**r)(void)) 345e5c89e4eSSatish Balay { 346e5c89e4eSSatish Balay PetscFList entry = fl; 347e5c89e4eSSatish Balay PetscErrorCode ierr; 348e5c89e4eSSatish Balay char *function,*path; 349ace3abfcSBarry Smith PetscBool flg,f1,f2,f3; 3500598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 351e5c89e4eSSatish Balay char *newpath; 352e5c89e4eSSatish Balay #endif 353e5c89e4eSSatish Balay 354e5c89e4eSSatish Balay PetscFunctionBegin; 355e32f2f54SBarry Smith if (!name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to find routine with null name"); 356e5c89e4eSSatish Balay 357e5c89e4eSSatish Balay *r = 0; 358e5c89e4eSSatish Balay ierr = PetscFListGetPathAndFunction(name,&path,&function);CHKERRQ(ierr); 359e5c89e4eSSatish Balay 360e5c89e4eSSatish Balay /* 361e5c89e4eSSatish Balay If path then append it to search libraries 362e5c89e4eSSatish Balay */ 3630598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 364e5c89e4eSSatish Balay if (path) { 365d44a1e48SBarry Smith ierr = PetscDLLibraryAppend(comm,&PetscDLLibrariesLoaded,path);CHKERRQ(ierr); 366e5c89e4eSSatish Balay } 367e5c89e4eSSatish Balay #endif 368e5c89e4eSSatish Balay 369e5c89e4eSSatish Balay while (entry) { 370e5c89e4eSSatish Balay flg = PETSC_FALSE; 371e5c89e4eSSatish Balay if (path && entry->path) { 372e5c89e4eSSatish Balay ierr = PetscStrcmp(path,entry->path,&f1);CHKERRQ(ierr); 373e5c89e4eSSatish Balay ierr = PetscStrcmp(function,entry->rname,&f2);CHKERRQ(ierr); 374e5c89e4eSSatish Balay ierr = PetscStrcmp(function,entry->name,&f3);CHKERRQ(ierr); 375ace3abfcSBarry Smith flg = (PetscBool) ((f1 && f2) || (f1 && f3)); 376e5c89e4eSSatish Balay } else if (!path) { 377e5c89e4eSSatish Balay ierr = PetscStrcmp(function,entry->name,&f1);CHKERRQ(ierr); 378e5c89e4eSSatish Balay ierr = PetscStrcmp(function,entry->rname,&f2);CHKERRQ(ierr); 379ace3abfcSBarry Smith flg = (PetscBool) (f1 || f2); 380e5c89e4eSSatish Balay } else { 381e5c89e4eSSatish Balay ierr = PetscStrcmp(function,entry->name,&flg);CHKERRQ(ierr); 382e5c89e4eSSatish Balay if (flg) { 383e5c89e4eSSatish Balay ierr = PetscFree(function);CHKERRQ(ierr); 384e5c89e4eSSatish Balay ierr = PetscStrallocpy(entry->rname,&function);CHKERRQ(ierr); 385e5c89e4eSSatish Balay } else { 386e5c89e4eSSatish Balay ierr = PetscStrcmp(function,entry->rname,&flg);CHKERRQ(ierr); 387e5c89e4eSSatish Balay } 388e5c89e4eSSatish Balay } 389e5c89e4eSSatish Balay 390e5c89e4eSSatish Balay if (flg) { 391e5c89e4eSSatish Balay if (entry->routine) { 392e5c89e4eSSatish Balay *r = entry->routine; 393503cfb0cSBarry Smith ierr = PetscFree(path);CHKERRQ(ierr); 394e5c89e4eSSatish Balay ierr = PetscFree(function);CHKERRQ(ierr); 395e5c89e4eSSatish Balay PetscFunctionReturn(0); 396e5c89e4eSSatish Balay } 3975629bde7SJed Brown if (!(entry->rname && entry->rname[0])) { /* The entry has been cleared */ 3985629bde7SJed Brown ierr = PetscFree(function);CHKERRQ(ierr); 3995629bde7SJed Brown PetscFunctionReturn(0); 4005629bde7SJed Brown } 401e5c89e4eSSatish Balay if ((path && entry->path && f3) || (!path && f1)) { /* convert name of function (alias) to actual function name */ 402e5c89e4eSSatish Balay ierr = PetscFree(function);CHKERRQ(ierr); 403e5c89e4eSSatish Balay ierr = PetscStrallocpy(entry->rname,&function);CHKERRQ(ierr); 404e5c89e4eSSatish Balay } 405e5c89e4eSSatish Balay 406e5c89e4eSSatish Balay /* it is not yet in memory so load from dynamic library */ 4070598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 408e5c89e4eSSatish Balay newpath = path; 409e5c89e4eSSatish Balay if (!path) newpath = entry->path; 410d44a1e48SBarry Smith ierr = PetscDLLibrarySym(comm,&PetscDLLibrariesLoaded,newpath,entry->rname,(void **)r);CHKERRQ(ierr); 411e5c89e4eSSatish Balay if (*r) { 412e5c89e4eSSatish Balay entry->routine = *r; 413503cfb0cSBarry Smith ierr = PetscFree(path);CHKERRQ(ierr); 414e5c89e4eSSatish Balay ierr = PetscFree(function);CHKERRQ(ierr); 415e5c89e4eSSatish Balay PetscFunctionReturn(0); 416e5c89e4eSSatish Balay } 417e5c89e4eSSatish Balay #endif 418e5c89e4eSSatish Balay } 419e5c89e4eSSatish Balay entry = entry->next; 420e5c89e4eSSatish Balay } 421e5c89e4eSSatish Balay 4220598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 4234b91b6eaSBarry Smith if (searchlibraries) { 424e5c89e4eSSatish Balay /* Function never registered; try for it anyway */ 425d44a1e48SBarry Smith ierr = PetscDLLibrarySym(comm,&PetscDLLibrariesLoaded,path,function,(void **)r);CHKERRQ(ierr); 426503cfb0cSBarry Smith ierr = PetscFree(path);CHKERRQ(ierr); 427e5c89e4eSSatish Balay if (*r) { 428*6d75e210SBarry Smith ierr = PetscFListAdd(comm,&fl,name,name,*r);CHKERRQ(ierr); 429e5c89e4eSSatish Balay } 4304b91b6eaSBarry Smith } 431e5c89e4eSSatish Balay #endif 432e5c89e4eSSatish Balay ierr = PetscFree(function);CHKERRQ(ierr); 433e5c89e4eSSatish Balay PetscFunctionReturn(0); 434e5c89e4eSSatish Balay } 435e5c89e4eSSatish Balay 436e5c89e4eSSatish Balay #undef __FUNCT__ 437e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListView" 438e5c89e4eSSatish Balay /*@ 439e5c89e4eSSatish Balay PetscFListView - prints out contents of an PetscFList 440e5c89e4eSSatish Balay 441e5c89e4eSSatish Balay Collective over MPI_Comm 442e5c89e4eSSatish Balay 443e5c89e4eSSatish Balay Input Parameters: 444e5c89e4eSSatish Balay + list - the list of functions 445e5c89e4eSSatish Balay - viewer - currently ignored 446e5c89e4eSSatish Balay 447e5c89e4eSSatish Balay Level: developer 448e5c89e4eSSatish Balay 449e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFListPrintTypes(), PetscFList 450e5c89e4eSSatish Balay @*/ 4517087cfbeSBarry Smith PetscErrorCode PetscFListView(PetscFList list,PetscViewer viewer) 452e5c89e4eSSatish Balay { 453e5c89e4eSSatish Balay PetscErrorCode ierr; 454ace3abfcSBarry Smith PetscBool iascii; 455e5c89e4eSSatish Balay 456e5c89e4eSSatish Balay PetscFunctionBegin; 457e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 458e5c89e4eSSatish Balay PetscValidPointer(list,1); 4590700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 460e5c89e4eSSatish Balay 461251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 462e32f2f54SBarry Smith if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported"); 463e5c89e4eSSatish Balay 464e5c89e4eSSatish Balay while (list) { 465e5c89e4eSSatish Balay if (list->path) { 466e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer," %s %s %s\n",list->path,list->name,list->rname);CHKERRQ(ierr); 467e5c89e4eSSatish Balay } else { 468e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer," %s %s\n",list->name,list->rname);CHKERRQ(ierr); 469e5c89e4eSSatish Balay } 470e5c89e4eSSatish Balay list = list->next; 471e5c89e4eSSatish Balay } 472e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 473e5c89e4eSSatish Balay PetscFunctionReturn(0); 474e5c89e4eSSatish Balay } 475e5c89e4eSSatish Balay 476e5c89e4eSSatish Balay #undef __FUNCT__ 477e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListGet" 478065533a5SJed Brown /*@C 479e5c89e4eSSatish Balay PetscFListGet - Gets an array the contains the entries in PetscFList, this is used 480e5c89e4eSSatish Balay by help etc. 481e5c89e4eSSatish Balay 482e5c89e4eSSatish Balay Collective over MPI_Comm 483e5c89e4eSSatish Balay 484e5c89e4eSSatish Balay Input Parameter: 485e5c89e4eSSatish Balay . list - list of types 486e5c89e4eSSatish Balay 487e5c89e4eSSatish Balay Output Parameter: 488e5c89e4eSSatish Balay + array - array of names 489e5c89e4eSSatish Balay - n - length of array 490e5c89e4eSSatish Balay 491e5c89e4eSSatish Balay Notes: 492e5c89e4eSSatish Balay This allocates the array so that must be freed. BUT the individual entries are 493e5c89e4eSSatish Balay not copied so should not be freed. 494e5c89e4eSSatish Balay 495e5c89e4eSSatish Balay Level: developer 496e5c89e4eSSatish Balay 497e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList 498e5c89e4eSSatish Balay @*/ 499065533a5SJed Brown PetscErrorCode PetscFListGet(PetscFList list,const char ***array,int *n) 500e5c89e4eSSatish Balay { 501e5c89e4eSSatish Balay PetscErrorCode ierr; 502e5c89e4eSSatish Balay PetscInt count = 0; 503e5c89e4eSSatish Balay PetscFList klist = list; 504e5c89e4eSSatish Balay 505e5c89e4eSSatish Balay PetscFunctionBegin; 506e5c89e4eSSatish Balay while (list) { 507e5c89e4eSSatish Balay list = list->next; 508e5c89e4eSSatish Balay count++; 509e5c89e4eSSatish Balay } 510e5c89e4eSSatish Balay ierr = PetscMalloc((count+1)*sizeof(char *),array);CHKERRQ(ierr); 511e5c89e4eSSatish Balay count = 0; 512e5c89e4eSSatish Balay while (klist) { 513e5c89e4eSSatish Balay (*array)[count] = klist->name; 514e5c89e4eSSatish Balay klist = klist->next; 515e5c89e4eSSatish Balay count++; 516e5c89e4eSSatish Balay } 517e5c89e4eSSatish Balay (*array)[count] = 0; 518e5c89e4eSSatish Balay *n = count+1; 519e5c89e4eSSatish Balay PetscFunctionReturn(0); 520e5c89e4eSSatish Balay } 521e5c89e4eSSatish Balay 522e5c89e4eSSatish Balay 523e5c89e4eSSatish Balay #undef __FUNCT__ 524e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListPrintTypes" 525e5c89e4eSSatish Balay /*@C 526e5c89e4eSSatish Balay PetscFListPrintTypes - Prints the methods available. 527e5c89e4eSSatish Balay 528e5c89e4eSSatish Balay Collective over MPI_Comm 529e5c89e4eSSatish Balay 530e5c89e4eSSatish Balay Input Parameters: 531e5c89e4eSSatish Balay + comm - the communicator (usually MPI_COMM_WORLD) 532e5c89e4eSSatish Balay . fd - file to print to, usually stdout 533e5c89e4eSSatish Balay . prefix - prefix to prepend to name (optional) 534e5c89e4eSSatish Balay . name - option string (for example, "-ksp_type") 535e5c89e4eSSatish Balay . text - short description of the object (for example, "Krylov solvers") 536e5c89e4eSSatish Balay . man - name of manual page that discusses the object (for example, "KSPCreate") 5373cc1e11dSBarry Smith . list - list of types 5383cc1e11dSBarry Smith - def - default (current) value 539e5c89e4eSSatish Balay 540e5c89e4eSSatish Balay Level: developer 541e5c89e4eSSatish Balay 542e5c89e4eSSatish Balay .seealso: PetscFListAddDynamic(), PetscFList 543e5c89e4eSSatish Balay @*/ 5447087cfbeSBarry Smith PetscErrorCode PetscFListPrintTypes(MPI_Comm comm,FILE *fd,const char prefix[],const char name[],const char text[],const char man[],PetscFList list,const char def[]) 545e5c89e4eSSatish Balay { 546e5c89e4eSSatish Balay PetscErrorCode ierr; 547e5c89e4eSSatish Balay PetscInt count = 0; 548e5c89e4eSSatish Balay char p[64]; 549e5c89e4eSSatish Balay 550e5c89e4eSSatish Balay PetscFunctionBegin; 551da9f1d6bSBarry Smith if (!fd) fd = PETSC_STDOUT; 552e5c89e4eSSatish Balay 553e5c89e4eSSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 554e5c89e4eSSatish Balay if (prefix) {ierr = PetscStrcat(p,prefix);CHKERRQ(ierr);} 5553cc1e11dSBarry Smith ierr = PetscFPrintf(comm,fd," %s%s <%s>: %s (one of)",p,name+1,def,text);CHKERRQ(ierr); 556e5c89e4eSSatish Balay 557e5c89e4eSSatish Balay while (list) { 558e5c89e4eSSatish Balay ierr = PetscFPrintf(comm,fd," %s",list->name);CHKERRQ(ierr); 559e5c89e4eSSatish Balay list = list->next; 560e5c89e4eSSatish Balay count++; 561e5c89e4eSSatish Balay if (count == 8) {ierr = PetscFPrintf(comm,fd,"\n ");CHKERRQ(ierr);} 562e5c89e4eSSatish Balay } 563e5c89e4eSSatish Balay ierr = PetscFPrintf(comm,fd," (%s)\n",man);CHKERRQ(ierr); 564e5c89e4eSSatish Balay PetscFunctionReturn(0); 565e5c89e4eSSatish Balay } 566e5c89e4eSSatish Balay 567e5c89e4eSSatish Balay #undef __FUNCT__ 568e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListDuplicate" 569e5c89e4eSSatish Balay /*@ 570e5c89e4eSSatish Balay PetscFListDuplicate - Creates a new list from a given object list. 571e5c89e4eSSatish Balay 572e5c89e4eSSatish Balay Input Parameters: 573e5c89e4eSSatish Balay . fl - pointer to list 574e5c89e4eSSatish Balay 575e5c89e4eSSatish Balay Output Parameters: 576e5c89e4eSSatish Balay . nl - the new list (should point to 0 to start, otherwise appends) 577e5c89e4eSSatish Balay 578e5c89e4eSSatish Balay Level: developer 579e5c89e4eSSatish Balay 580e5c89e4eSSatish Balay .seealso: PetscFList, PetscFListAdd(), PetscFlistDestroy() 581e5c89e4eSSatish Balay 582e5c89e4eSSatish Balay @*/ 5837087cfbeSBarry Smith PetscErrorCode PetscFListDuplicate(PetscFList fl,PetscFList *nl) 584e5c89e4eSSatish Balay { 585e5c89e4eSSatish Balay PetscErrorCode ierr; 586e5c89e4eSSatish Balay char path[PETSC_MAX_PATH_LEN]; 587e5c89e4eSSatish Balay 588e5c89e4eSSatish Balay PetscFunctionBegin; 589e5c89e4eSSatish Balay while (fl) { 590e5c89e4eSSatish Balay /* this is silly, rebuild the complete pathname */ 591e5c89e4eSSatish Balay if (fl->path) { 592e5c89e4eSSatish Balay ierr = PetscStrcpy(path,fl->path);CHKERRQ(ierr); 593e5c89e4eSSatish Balay ierr = PetscStrcat(path,":");CHKERRQ(ierr); 594e5c89e4eSSatish Balay ierr = PetscStrcat(path,fl->name);CHKERRQ(ierr); 595e5c89e4eSSatish Balay } else { 596e5c89e4eSSatish Balay ierr = PetscStrcpy(path,fl->name);CHKERRQ(ierr); 597e5c89e4eSSatish Balay } 598*6d75e210SBarry Smith ierr = PetscFListAdd(PETSC_COMM_WORLD,nl,path,fl->rname,fl->routine);CHKERRQ(ierr); 599e5c89e4eSSatish Balay fl = fl->next; 600e5c89e4eSSatish Balay } 601e5c89e4eSSatish Balay PetscFunctionReturn(0); 602e5c89e4eSSatish Balay } 603e5c89e4eSSatish Balay 604e5c89e4eSSatish Balay 605e5c89e4eSSatish Balay #undef __FUNCT__ 606e5c89e4eSSatish Balay #define __FUNCT__ "PetscFListConcat" 607e5c89e4eSSatish Balay /* 608e5c89e4eSSatish Balay PetscFListConcat - joins name of a libary, and the path where it is located 609e5c89e4eSSatish Balay into a single string. 610e5c89e4eSSatish Balay 611e5c89e4eSSatish Balay Input Parameters: 612e5c89e4eSSatish Balay . path - path to the library name. 613e5c89e4eSSatish Balay . name - name of the library 614e5c89e4eSSatish Balay 615e5c89e4eSSatish Balay Output Parameters: 616e5c89e4eSSatish Balay . fullname - the name that is the union of the path and the library name, 617e5c89e4eSSatish Balay delimited by a semicolon, i.e., path:name 618e5c89e4eSSatish Balay 619e5c89e4eSSatish Balay Notes: 620e5c89e4eSSatish Balay If the path is NULL, assumes that the name, specified also includes 621e5c89e4eSSatish Balay the path as path:name 622e5c89e4eSSatish Balay 623e5c89e4eSSatish Balay */ 6247087cfbeSBarry Smith PetscErrorCode PetscFListConcat(const char path[],const char name[],char fullname[]) 625e5c89e4eSSatish Balay { 626e5c89e4eSSatish Balay PetscErrorCode ierr; 627e5c89e4eSSatish Balay PetscFunctionBegin; 628e5c89e4eSSatish Balay if (path) { 629e5c89e4eSSatish Balay ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr); 630e5c89e4eSSatish Balay ierr = PetscStrcat(fullname,":");CHKERRQ(ierr); 631e5c89e4eSSatish Balay ierr = PetscStrcat(fullname,name);CHKERRQ(ierr); 632e5c89e4eSSatish Balay } else { 633e5c89e4eSSatish Balay ierr = PetscStrcpy(fullname,name);CHKERRQ(ierr); 634e5c89e4eSSatish Balay } 635e5c89e4eSSatish Balay PetscFunctionReturn(0); 636e5c89e4eSSatish Balay } 637e4d1774bSDmitry Karpeev 638e4d1774bSDmitry Karpeev 639e4d1774bSDmitry Karpeev 640e4d1774bSDmitry Karpeev /* ------------------------------------------------------------------------------*/ 641e4d1774bSDmitry Karpeev struct _n_PetscOpFList { 642e4d1774bSDmitry Karpeev char *op; /* op name */ 643e4d1774bSDmitry Karpeev PetscInt numArgs; /* number of arguments to the operation */ 644e4d1774bSDmitry Karpeev char **argTypes; /* list of argument types */ 6452356f84bSDmitry Karpeev PetscVoidFunction routine; /* the routine */ 646e4d1774bSDmitry Karpeev char *url; /* url naming the link library and the routine */ 647e4d1774bSDmitry Karpeev char *path; /* path of link library containing routine */ 648e4d1774bSDmitry Karpeev char *name; /* routine name in dynamic library */ 649e4d1774bSDmitry Karpeev PetscOpFList next; /* next pointer */ 650e4d1774bSDmitry Karpeev PetscOpFList next_list; /* used to maintain list of all lists for freeing */ 651e4d1774bSDmitry Karpeev }; 652e4d1774bSDmitry Karpeev 653e4d1774bSDmitry Karpeev /* 654e4d1774bSDmitry Karpeev Keep a linked list of PetscOfFLists so that we can destroy all the left-over ones. 655e4d1774bSDmitry Karpeev */ 656e4d1774bSDmitry Karpeev static PetscOpFList opallhead = 0; 657e4d1774bSDmitry Karpeev 658e4d1774bSDmitry Karpeev #undef __FUNCT__ 659f64744f7SDmitry Karpeev #define __FUNCT__ "PetscOpFListAdd" 660e4d1774bSDmitry Karpeev /*@C 661fc153e51SBarry Smith PetscOpFListAdd - Given a routine, a string id, and the type names of arguments saves that routine in the specified registry. 662e4d1774bSDmitry Karpeev 66348703020SDmitry Karpeev Formally collective on comm. 664e4d1774bSDmitry Karpeev 665e4d1774bSDmitry Karpeev Input Parameters: 666e4d1774bSDmitry Karpeev + comm - processors adding the op 667e4d1774bSDmitry Karpeev . fl - list of known ops 668e4d1774bSDmitry Karpeev . url - routine locator (optional, if not using dynamic libraries and a nonempty fnc) 669e4d1774bSDmitry Karpeev . fnc - function pointer (optional, if using dynamic libraries and a nonempty url) 670e4d1774bSDmitry Karpeev . op - operation name 671e4d1774bSDmitry Karpeev . numArgs - number of op arguments 67248703020SDmitry Karpeev - argTypes - list of argument type names (const char*) 673e4d1774bSDmitry Karpeev 674e4d1774bSDmitry Karpeev Notes: 675e4d1774bSDmitry Karpeev To remove a registered routine, pass in a PETSC_NULL url and fnc(). 676e4d1774bSDmitry Karpeev 677e4d1774bSDmitry Karpeev url can be of the form [/path/libname[.so.1.0]:]functionname[()] where items in [] denote optional 678e4d1774bSDmitry Karpeev 679e4d1774bSDmitry Karpeev ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, or ${any environment variable} 680e4d1774bSDmitry Karpeev occuring in url will be replaced with appropriate values. 681e4d1774bSDmitry Karpeev 682e4d1774bSDmitry Karpeev Level: developer 683e4d1774bSDmitry Karpeev 684e4d1774bSDmitry Karpeev .seealso: PetscOpFListDestroy(), PetscOpFList, PetscFListAdd(), PetscFList 685e4d1774bSDmitry Karpeev @*/ 6862356f84bSDmitry Karpeev PetscErrorCode PetscOpFListAdd(MPI_Comm comm, PetscOpFList *fl,const char url[],PetscVoidFunction fnc,const char op[], PetscInt numArgs, char* argTypes[]) 687e4d1774bSDmitry Karpeev { 688e4d1774bSDmitry Karpeev PetscOpFList entry,e,ne; 689e4d1774bSDmitry Karpeev PetscErrorCode ierr; 690e4d1774bSDmitry Karpeev char *fpath,*fname; 691e4d1774bSDmitry Karpeev PetscInt i; 692e4d1774bSDmitry Karpeev 693e4d1774bSDmitry Karpeev PetscFunctionBegin; 694e4d1774bSDmitry Karpeev if (!*fl) { 695e4d1774bSDmitry Karpeev ierr = PetscNew(struct _n_PetscOpFList,&entry);CHKERRQ(ierr); 696e4d1774bSDmitry Karpeev ierr = PetscStrallocpy(op,&entry->op);CHKERRQ(ierr); 697e4d1774bSDmitry Karpeev ierr = PetscStrallocpy(url,&(entry->url));CHKERRQ(ierr); 698e4d1774bSDmitry Karpeev ierr = PetscFListGetPathAndFunction(url,&fpath,&fname);CHKERRQ(ierr); 699e4d1774bSDmitry Karpeev entry->path = fpath; 700e4d1774bSDmitry Karpeev entry->name = fname; 701e4d1774bSDmitry Karpeev entry->routine = fnc; 70248703020SDmitry Karpeev entry->numArgs = numArgs; 70348703020SDmitry Karpeev if (numArgs) { 70448703020SDmitry Karpeev ierr = PetscMalloc(sizeof(char*)*numArgs, &(entry->argTypes));CHKERRQ(ierr); 70548703020SDmitry Karpeev for (i = 0; i < numArgs; ++i) { 70648703020SDmitry Karpeev ierr = PetscStrallocpy(argTypes[i], &(entry->argTypes[i]));CHKERRQ(ierr); 70748703020SDmitry Karpeev } 70848703020SDmitry Karpeev } 709e4d1774bSDmitry Karpeev entry->next = 0; 710e4d1774bSDmitry Karpeev *fl = entry; 711e4d1774bSDmitry Karpeev 712e4d1774bSDmitry Karpeev /* add this new list to list of all lists */ 713e4d1774bSDmitry Karpeev if (!opallhead) { 714e4d1774bSDmitry Karpeev opallhead = *fl; 715e4d1774bSDmitry Karpeev (*fl)->next_list = 0; 716e4d1774bSDmitry Karpeev } else { 717e4d1774bSDmitry Karpeev ne = opallhead; 718e4d1774bSDmitry Karpeev opallhead = *fl; 719e4d1774bSDmitry Karpeev (*fl)->next_list = ne; 720e4d1774bSDmitry Karpeev } 721e4d1774bSDmitry Karpeev } else { 722e4d1774bSDmitry Karpeev /* search list to see if it is already there */ 723e4d1774bSDmitry Karpeev e = PETSC_NULL; 724e4d1774bSDmitry Karpeev ne = *fl; 725e4d1774bSDmitry Karpeev while (ne) { 726e4d1774bSDmitry Karpeev PetscBool match; 727e4d1774bSDmitry Karpeev ierr = PetscStrcmp(ne->op,op,&match);CHKERRQ(ierr); 728e4d1774bSDmitry Karpeev if (!match) goto next; 729*6d75e210SBarry Smith if (numArgs == ne->numArgs) match = PETSC_TRUE; 730*6d75e210SBarry Smith else match = PETSC_FALSE; 731e4d1774bSDmitry Karpeev if (!match) goto next; 732e4d1774bSDmitry Karpeev if (numArgs) { 733e4d1774bSDmitry Karpeev for (i = 0; i < numArgs; ++i) { 734f3b60a6eSDmitry Karpeev ierr = PetscStrcmp(argTypes[i], ne->argTypes[i], &match);CHKERRQ(ierr); 73548703020SDmitry Karpeev if (!match) goto next; 736e4d1774bSDmitry Karpeev } 737e4d1774bSDmitry Karpeev } 738e4d1774bSDmitry Karpeev if (!url && !fnc) { 739e4d1774bSDmitry Karpeev /* remove this record */ 740e4d1774bSDmitry Karpeev if (e) e->next = ne->next; 741e4d1774bSDmitry Karpeev ierr = PetscFree(ne->op);CHKERRQ(ierr); 742e4d1774bSDmitry Karpeev ierr = PetscFree(ne->url);CHKERRQ(ierr); 743e4d1774bSDmitry Karpeev ierr = PetscFree(ne->path);CHKERRQ(ierr); 744e4d1774bSDmitry Karpeev ierr = PetscFree(ne->name);CHKERRQ(ierr); 745e4d1774bSDmitry Karpeev if (numArgs) { 746e4d1774bSDmitry Karpeev for (i = 0; i < numArgs; ++i) { 747e4d1774bSDmitry Karpeev ierr = PetscFree(ne->argTypes[i]);CHKERRQ(ierr); 748e4d1774bSDmitry Karpeev } 749e4d1774bSDmitry Karpeev ierr = PetscFree(ne->argTypes);CHKERRQ(ierr); 750e4d1774bSDmitry Karpeev } 751e4d1774bSDmitry Karpeev ierr = PetscFree(ne);CHKERRQ(ierr); 752*6d75e210SBarry Smith } else { 753e4d1774bSDmitry Karpeev /* Replace url, fpath, fname and fnc. */ 754e4d1774bSDmitry Karpeev ierr = PetscStrallocpy(url, &(ne->url));CHKERRQ(ierr); 755e4d1774bSDmitry Karpeev ierr = PetscFListGetPathAndFunction(url,&fpath,&fname);CHKERRQ(ierr); 756e4d1774bSDmitry Karpeev ierr = PetscFree(ne->path);CHKERRQ(ierr); 757e4d1774bSDmitry Karpeev ierr = PetscFree(ne->name);CHKERRQ(ierr); 758e4d1774bSDmitry Karpeev ne->path = fpath; 759e4d1774bSDmitry Karpeev ne->name = fname; 760e4d1774bSDmitry Karpeev ne->routine = fnc; 761e4d1774bSDmitry Karpeev } 762e4d1774bSDmitry Karpeev PetscFunctionReturn(0); 763e4d1774bSDmitry Karpeev next: {e = ne; ne = ne->next;} 764e4d1774bSDmitry Karpeev } 765e4d1774bSDmitry Karpeev /* create new entry and add to end of list */ 766e4d1774bSDmitry Karpeev ierr = PetscNew(struct _n_PetscOpFList,&entry);CHKERRQ(ierr); 767e4d1774bSDmitry Karpeev ierr = PetscStrallocpy(op,&entry->op);CHKERRQ(ierr); 768e4d1774bSDmitry Karpeev entry->numArgs = numArgs; 769e4d1774bSDmitry Karpeev if (numArgs) { 770e4d1774bSDmitry Karpeev ierr = PetscMalloc(sizeof(char*)*numArgs, &(entry->argTypes));CHKERRQ(ierr); 771e4d1774bSDmitry Karpeev for (i = 0; i < numArgs; ++i) { 77248703020SDmitry Karpeev ierr = PetscStrallocpy(argTypes[i], &(entry->argTypes[i]));CHKERRQ(ierr); 773e4d1774bSDmitry Karpeev } 774e4d1774bSDmitry Karpeev } 775e4d1774bSDmitry Karpeev ierr = PetscStrallocpy(url, &(entry->url));CHKERRQ(ierr); 776e4d1774bSDmitry Karpeev ierr = PetscFListGetPathAndFunction(url,&fpath,&fname);CHKERRQ(ierr); 777e4d1774bSDmitry Karpeev entry->path = fpath; 778e4d1774bSDmitry Karpeev entry->name = fname; 779e4d1774bSDmitry Karpeev entry->routine = fnc; 780e4d1774bSDmitry Karpeev entry->next = 0; 781e4d1774bSDmitry Karpeev ne->next = entry; 782e4d1774bSDmitry Karpeev } 783e4d1774bSDmitry Karpeev PetscFunctionReturn(0); 784e4d1774bSDmitry Karpeev } 785e4d1774bSDmitry Karpeev 786e4d1774bSDmitry Karpeev #undef __FUNCT__ 787e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListDestroy" 788e4d1774bSDmitry Karpeev /*@C 789e4d1774bSDmitry Karpeev PetscOpFListDestroy - Destroys a list of registered op routines. 790e4d1774bSDmitry Karpeev 791e4d1774bSDmitry Karpeev Input Parameter: 792e4d1774bSDmitry Karpeev . fl - pointer to list 793e4d1774bSDmitry Karpeev 794e4d1774bSDmitry Karpeev Level: developer 795e4d1774bSDmitry Karpeev 796e4d1774bSDmitry Karpeev .seealso: PetscOpFListAdd(), PetscOpFList 797e4d1774bSDmitry Karpeev @*/ 798e4d1774bSDmitry Karpeev PetscErrorCode PetscOpFListDestroy(PetscOpFList *fl) 799e4d1774bSDmitry Karpeev { 800e4d1774bSDmitry Karpeev PetscOpFList next,entry,tmp; 801e4d1774bSDmitry Karpeev PetscErrorCode ierr; 802e4d1774bSDmitry Karpeev PetscInt i; 803e4d1774bSDmitry Karpeev 804e4d1774bSDmitry Karpeev PetscFunctionBegin; 805e4d1774bSDmitry Karpeev if (!*fl) PetscFunctionReturn(0); 806e4d1774bSDmitry Karpeev if (!opallhead) PetscFunctionReturn(0); 807e4d1774bSDmitry Karpeev 808e4d1774bSDmitry Karpeev /* 809e4d1774bSDmitry Karpeev Remove this entry from the master Op list (if it is in it) 810e4d1774bSDmitry Karpeev */ 811e4d1774bSDmitry Karpeev if (opallhead == *fl) { 812e4d1774bSDmitry Karpeev if (opallhead->next_list) { 813e4d1774bSDmitry Karpeev opallhead = opallhead->next_list; 814e4d1774bSDmitry Karpeev } else { 815e4d1774bSDmitry Karpeev opallhead = 0; 816e4d1774bSDmitry Karpeev } 817e4d1774bSDmitry Karpeev } else { 818e4d1774bSDmitry Karpeev tmp = opallhead; 819e4d1774bSDmitry Karpeev while (tmp->next_list != *fl) { 820e4d1774bSDmitry Karpeev tmp = tmp->next_list; 821e4d1774bSDmitry Karpeev if (!tmp->next_list) break; 822e4d1774bSDmitry Karpeev } 823e4d1774bSDmitry Karpeev if (tmp->next_list) tmp->next_list = tmp->next_list->next_list; 824e4d1774bSDmitry Karpeev } 825e4d1774bSDmitry Karpeev 826e4d1774bSDmitry Karpeev /* free this list */ 827e4d1774bSDmitry Karpeev entry = *fl; 828e4d1774bSDmitry Karpeev while (entry) { 829e4d1774bSDmitry Karpeev next = entry->next; 830e4d1774bSDmitry Karpeev ierr = PetscFree(entry->op);CHKERRQ(ierr); 831e4d1774bSDmitry Karpeev for (i = 0; i < entry->numArgs; ++i) { 832e4d1774bSDmitry Karpeev ierr = PetscFree(entry->argTypes[i]);CHKERRQ(ierr); 833e4d1774bSDmitry Karpeev } 834e4d1774bSDmitry Karpeev ierr = PetscFree(entry->argTypes);CHKERRQ(ierr); 835e4d1774bSDmitry Karpeev ierr = PetscFree(entry->url);CHKERRQ(ierr); 836e4d1774bSDmitry Karpeev ierr = PetscFree(entry->path);CHKERRQ(ierr); 837e4d1774bSDmitry Karpeev ierr = PetscFree(entry->name);CHKERRQ(ierr); 838e4d1774bSDmitry Karpeev ierr = PetscFree(entry);CHKERRQ(ierr); 839e4d1774bSDmitry Karpeev entry = next; 840e4d1774bSDmitry Karpeev } 841e4d1774bSDmitry Karpeev *fl = 0; 842e4d1774bSDmitry Karpeev PetscFunctionReturn(0); 843e4d1774bSDmitry Karpeev } 844e4d1774bSDmitry Karpeev 845e4d1774bSDmitry Karpeev /* 846e4d1774bSDmitry Karpeev Destroys all the function lists that anyone has every registered, such as MatOpList, etc. 847e4d1774bSDmitry Karpeev */ 848e4d1774bSDmitry Karpeev #undef __FUNCT__ 849e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListDestroyAll" 850e4d1774bSDmitry Karpeev PetscErrorCode PetscOpFListDestroyAll(void) 851e4d1774bSDmitry Karpeev { 852e4d1774bSDmitry Karpeev PetscOpFList tmp2,tmp1 = opallhead; 853e4d1774bSDmitry Karpeev PetscErrorCode ierr; 854e4d1774bSDmitry Karpeev 855e4d1774bSDmitry Karpeev PetscFunctionBegin; 856e4d1774bSDmitry Karpeev while (tmp1) { 857e4d1774bSDmitry Karpeev tmp2 = tmp1->next_list; 858e4d1774bSDmitry Karpeev ierr = PetscOpFListDestroy(&tmp1);CHKERRQ(ierr); 859e4d1774bSDmitry Karpeev tmp1 = tmp2; 860e4d1774bSDmitry Karpeev } 861e4d1774bSDmitry Karpeev opallhead = 0; 862e4d1774bSDmitry Karpeev PetscFunctionReturn(0); 863e4d1774bSDmitry Karpeev } 864e4d1774bSDmitry Karpeev 865e4d1774bSDmitry Karpeev #undef __FUNCT__ 866e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListFind" 867e4d1774bSDmitry Karpeev /*@C 868fc153e51SBarry Smith PetscOpFListFind - Given a name, finds the matching op routine based on the declared arguments' type names. 869fc153e51SBarry Smith 870fc153e51SBarry Smith Formally collective on MPI_Comm 871e4d1774bSDmitry Karpeev 872e4d1774bSDmitry Karpeev Input Parameters: 873e4d1774bSDmitry Karpeev + comm - processes looking for the op 874e4d1774bSDmitry Karpeev . fl - pointer to list of known ops 875e4d1774bSDmitry Karpeev . op - operation name 876e4d1774bSDmitry Karpeev . numArgs - number of op arguments 87748703020SDmitry Karpeev - argTypes - list of argument type names 878e4d1774bSDmitry Karpeev 879e4d1774bSDmitry Karpeev Output Parameters: 880e4d1774bSDmitry Karpeev . r - routine implementing op with the given arg types 881e4d1774bSDmitry Karpeev 882e4d1774bSDmitry Karpeev Level: developer 883e4d1774bSDmitry Karpeev 884fc153e51SBarry Smith Notes: This is used to implement double dispatch and multiple dispatch based on the type names of the function arguments 885fc153e51SBarry Smith 886e4d1774bSDmitry Karpeev .seealso: PetscOpFListAdd(), PetscOpFList 887e4d1774bSDmitry Karpeev @*/ 8882356f84bSDmitry Karpeev PetscErrorCode PetscOpFListFind(MPI_Comm comm, PetscOpFList fl,PetscVoidFunction *r, const char* op, PetscInt numArgs, char* argTypes[]) 889e4d1774bSDmitry Karpeev { 890e4d1774bSDmitry Karpeev PetscOpFList entry; 891e4d1774bSDmitry Karpeev PetscErrorCode ierr; 892e4d1774bSDmitry Karpeev PetscBool match; 893e4d1774bSDmitry Karpeev PetscInt i; 894e4d1774bSDmitry Karpeev 895e4d1774bSDmitry Karpeev PetscFunctionBegin; 896e4d1774bSDmitry Karpeev PetscValidPointer(r,3); 897e4d1774bSDmitry Karpeev if (!op) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Attempting to find operation with null name"); 898e4d1774bSDmitry Karpeev *r = PETSC_NULL; 899495ad40fSDmitry Karpeev match = PETSC_FALSE; 900e4d1774bSDmitry Karpeev entry = fl; 901e4d1774bSDmitry Karpeev while (entry) { 902e4d1774bSDmitry Karpeev ierr = PetscStrcmp(entry->op,op,&match);CHKERRQ(ierr); 903e4d1774bSDmitry Karpeev if (!match) goto next; 904e4d1774bSDmitry Karpeev if (numArgs == entry->numArgs) 905e4d1774bSDmitry Karpeev match = PETSC_TRUE; 906e4d1774bSDmitry Karpeev else 907e4d1774bSDmitry Karpeev match = PETSC_FALSE; 908e4d1774bSDmitry Karpeev if (!match) goto next; 909e4d1774bSDmitry Karpeev if (numArgs) { 910e4d1774bSDmitry Karpeev for (i = 0; i < numArgs; ++i) { 91148703020SDmitry Karpeev ierr = PetscStrcmp(argTypes[i], entry->argTypes[i], &match);CHKERRQ(ierr); 91248703020SDmitry Karpeev if (!match) goto next; 913e4d1774bSDmitry Karpeev } 914e4d1774bSDmitry Karpeev } 915f3b60a6eSDmitry Karpeev break; 916e4d1774bSDmitry Karpeev next: entry = entry->next; 917e4d1774bSDmitry Karpeev } 918e4d1774bSDmitry Karpeev if (match) { 919e4d1774bSDmitry Karpeev if (entry->routine) { 920e4d1774bSDmitry Karpeev *r = entry->routine; 921e4d1774bSDmitry Karpeev } 922e4d1774bSDmitry Karpeev #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 923e4d1774bSDmitry Karpeev else { 924e4d1774bSDmitry Karpeev /* it is not yet in memory so load from dynamic library */ 925d44a1e48SBarry Smith ierr = PetscDLLibrarySym(comm,&PetscDLLibrariesLoaded,entry->path,entry->name,(void **)r);CHKERRQ(ierr); 926e4d1774bSDmitry Karpeev if (*r) { 927e4d1774bSDmitry Karpeev entry->routine = *r; 928e4d1774bSDmitry Karpeev } 929e4d1774bSDmitry Karpeev } 930e4d1774bSDmitry Karpeev #endif 931e4d1774bSDmitry Karpeev } 932e4d1774bSDmitry Karpeev 933e4d1774bSDmitry Karpeev PetscFunctionReturn(0); 934e4d1774bSDmitry Karpeev } 935e4d1774bSDmitry Karpeev 936e4d1774bSDmitry Karpeev #undef __FUNCT__ 937e4d1774bSDmitry Karpeev #define __FUNCT__ "PetscOpFListView" 938e4d1774bSDmitry Karpeev /*@C 939e4d1774bSDmitry Karpeev PetscOpFListView - prints out contents of a PetscOpFList 940e4d1774bSDmitry Karpeev 941e4d1774bSDmitry Karpeev Collective on viewer 942e4d1774bSDmitry Karpeev 943e4d1774bSDmitry Karpeev Input Parameters: 944e4d1774bSDmitry Karpeev + list - the list of functions 94548703020SDmitry Karpeev - viewer - ASCII viewer Level: developer 946e4d1774bSDmitry Karpeev 947e4d1774bSDmitry Karpeev .seealso: PetscOpFListAdd(), PetscOpFList 948e4d1774bSDmitry Karpeev @*/ 949e4d1774bSDmitry Karpeev PetscErrorCode PetscOpFListView(PetscOpFList list,PetscViewer viewer) 950e4d1774bSDmitry Karpeev { 951e4d1774bSDmitry Karpeev PetscErrorCode ierr; 952e4d1774bSDmitry Karpeev PetscBool iascii; 953e4d1774bSDmitry Karpeev PetscInt i; 954e4d1774bSDmitry Karpeev 955e4d1774bSDmitry Karpeev PetscFunctionBegin; 956e4d1774bSDmitry Karpeev if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF; 957e4d1774bSDmitry Karpeev PetscValidPointer(list,1); 958e4d1774bSDmitry Karpeev PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 959e4d1774bSDmitry Karpeev 960251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 961e4d1774bSDmitry Karpeev if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only ASCII viewer supported"); 962e4d1774bSDmitry Karpeev 963e4d1774bSDmitry Karpeev while (list) { 964e4d1774bSDmitry Karpeev if (list->url) { 965e4d1774bSDmitry Karpeev ierr = PetscViewerASCIIPrintf(viewer," %s: ",list->url);CHKERRQ(ierr); 966e4d1774bSDmitry Karpeev } 967e4d1774bSDmitry Karpeev ierr = PetscViewerASCIIPrintf(viewer, "%s(", list->op);CHKERRQ(ierr); 968e4d1774bSDmitry Karpeev for (i = 0; i < list->numArgs;++i) { 969e4d1774bSDmitry Karpeev if (i > 0) { 970e4d1774bSDmitry Karpeev ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr); 971e4d1774bSDmitry Karpeev } 972e4d1774bSDmitry Karpeev ierr = PetscViewerASCIIPrintf(viewer, "%s", list->argTypes[i]);CHKERRQ(ierr); 973e4d1774bSDmitry Karpeev } 974e4d1774bSDmitry Karpeev ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr); 975e4d1774bSDmitry Karpeev list = list->next; 976e4d1774bSDmitry Karpeev } 977e4d1774bSDmitry Karpeev PetscFunctionReturn(0); 978e4d1774bSDmitry Karpeev } 979