1 2 #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/ 3 4 /* 5 Ths PF generates a function on the fly and loads it into the running 6 program. 7 */ 8 9 #undef __FUNCT__ 10 #define __FUNCT__ "PFView_String" 11 PetscErrorCode PFView_String(void *value,PetscViewer viewer) 12 { 13 PetscErrorCode ierr; 14 PetscBool iascii; 15 16 PetscFunctionBegin; 17 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 18 if (iascii) { 19 ierr = PetscViewerASCIIPrintf(viewer,"String = %s\n",(char*)value);CHKERRQ(ierr); 20 } 21 PetscFunctionReturn(0); 22 } 23 24 #undef __FUNCT__ 25 #define __FUNCT__ "PFDestroy_String" 26 PetscErrorCode PFDestroy_String(void *value) 27 { 28 PetscErrorCode ierr; 29 30 PetscFunctionBegin; 31 ierr = PetscFree(value);CHKERRQ(ierr); 32 PetscFunctionReturn(0); 33 } 34 35 #undef __FUNCT__ 36 #define __FUNCT__ "PFStringCreateFunction" 37 /* 38 PFStringCreateFunction - Creates a function from a string 39 40 Collective over PF 41 42 Input Parameters: 43 + pf - the function object 44 - string - the string that defines the function 45 46 Output Parameter: 47 . f - the function pointer. 48 49 .seealso: PFSetFromOptions() 50 51 */ 52 PetscErrorCode PFStringCreateFunction(PF pf,char *string,void **f) 53 { 54 #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 55 PetscErrorCode ierr; 56 char task[1024],tmp[256],lib[PETSC_MAX_PATH_LEN],username[64]; 57 FILE *fd; 58 PetscBool tmpshared,wdshared,keeptmpfiles = PETSC_FALSE; 59 MPI_Comm comm; 60 #endif 61 62 PetscFunctionBegin; 63 #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES) 64 ierr = PetscFree(pf->data);CHKERRQ(ierr); 65 ierr = PetscStrallocpy(string,(char**)&pf->data);CHKERRQ(ierr); 66 67 /* create the new C function and compile it */ 68 ierr = PetscSharedTmp(PetscObjectComm((PetscObject)pf),&tmpshared);CHKERRQ(ierr); 69 ierr = PetscSharedWorkingDirectory(PetscObjectComm((PetscObject)pf),&wdshared);CHKERRQ(ierr); 70 if (tmpshared) { /* do it in /tmp since everyone has one */ 71 ierr = PetscGetTmp(PetscObjectComm((PetscObject)pf),tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 72 ierr = PetscObjectGetComm((PetscObject)pf,&comm);CHKERRQ(ierr); 73 } else if (!wdshared) { /* each one does in private /tmp */ 74 ierr = PetscGetTmp(PetscObjectComm((PetscObject)pf),tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 75 comm = PETSC_COMM_SELF; 76 } else { /* do it in current directory */ 77 ierr = PetscStrcpy(tmp,".");CHKERRQ(ierr); 78 ierr = PetscObjectGetComm((PetscObject)pf,&comm);CHKERRQ(ierr); 79 } 80 ierr = PetscOptionsGetBool(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles,NULL);CHKERRQ(ierr); 81 if (keeptmpfiles) sprintf(task,"cd %s ; mkdir ${USERNAME} ; cd ${USERNAME} ; \\cp -f ${PETSC_DIR}/src/pf/impls/string/makefile ./makefile ; ke MIN=%d NOUT=%d petscdlib STRINGFUNCTION=\"%s\" ; sync\n",tmp,(int)pf->dimin,(int)pf->dimout,string); 82 else sprintf(task,"cd %s ; mkdir ${USERNAME} ; cd ${USERNAME} ; \\cp -f ${PETSC_DIR}/src/pf/impls/string/makefile ./makefile ; make MIN=%d NOUT=%d -f makefile petscdlib STRINGFUNCTION=\"%s\" ; \\rm -f makefile petscdlib.c libpetscdlib.a ; sync\n",tmp,(int)pf->dimin,(int)pf->dimout,string); 83 84 #if defined(PETSC_HAVE_POPEN) 85 ierr = PetscPOpen(comm,NULL,task,"r",&fd);CHKERRQ(ierr); 86 ierr = PetscPClose(comm,fd,NULL);CHKERRQ(ierr); 87 #else 88 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 89 #endif 90 91 ierr = MPI_Barrier(comm);CHKERRQ(ierr); 92 93 /* load the apply function from the dynamic library */ 94 ierr = PetscGetUserName(username,64);CHKERRQ(ierr); 95 sprintf(lib,"%s/%s/libpetscdlib",tmp,username); 96 ierr = PetscDLLibrarySym(comm,NULL,lib,"PFApply_String",f);CHKERRQ(ierr); 97 if (!f) SETERRQ1(PetscObjectComm((PetscObject)pf),PETSC_ERR_ARG_WRONGSTATE,"Cannot find function %s",lib); 98 #endif 99 PetscFunctionReturn(0); 100 } 101 102 #undef __FUNCT__ 103 #define __FUNCT__ "PFSetFromOptions_String" 104 PetscErrorCode PFSetFromOptions_String(PetscOptions *PetscOptionsObject,PF pf) 105 { 106 PetscErrorCode ierr; 107 PetscBool flag; 108 char value[PETSC_MAX_PATH_LEN]; 109 PetscErrorCode (*f)(void*,PetscInt,const PetscScalar*,PetscScalar*) = 0; 110 111 PetscFunctionBegin; 112 ierr = PetscOptionsHead(PetscOptionsObject,"String function options");CHKERRQ(ierr); 113 ierr = PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);CHKERRQ(ierr); 114 if (flag) { 115 ierr = PFStringCreateFunction(pf,value,(void**)&f);CHKERRQ(ierr); 116 pf->ops->apply = f; 117 } 118 ierr = PetscOptionsTail();CHKERRQ(ierr); 119 PetscFunctionReturn(0); 120 } 121 122 typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/ 123 124 #undef __FUNCT__ 125 #define __FUNCT__ "PFCreate_String" 126 PETSC_EXTERN PetscErrorCode PFCreate_String(PF pf,void *value) 127 { 128 PetscErrorCode ierr; 129 FCN f = 0; 130 131 PetscFunctionBegin; 132 if (value) { 133 ierr = PFStringCreateFunction(pf,(char*)value,(void**)&f);CHKERRQ(ierr); 134 } 135 ierr = PFSet(pf,f,NULL,PFView_String,PFDestroy_String,NULL);CHKERRQ(ierr); 136 pf->ops->setfromoptions = PFSetFromOptions_String; 137 PetscFunctionReturn(0); 138 } 139 140 141 142 143 144