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