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