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(((PetscObject)pf)->comm,&tmpshared);CHKERRQ(ierr); 69 ierr = PetscSharedWorkingDirectory(((PetscObject)pf)->comm,&wdshared);CHKERRQ(ierr); 70 if (tmpshared) { /* do it in /tmp since everyone has one */ 71 ierr = PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 72 comm = ((PetscObject)pf)->comm; 73 } else if (!wdshared) { /* each one does in private /tmp */ 74 ierr = PetscGetTmp(((PetscObject)pf)->comm,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 comm = ((PetscObject)pf)->comm; 79 } 80 ierr = PetscOptionsGetBool(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles,PETSC_NULL);CHKERRQ(ierr); 81 if (keeptmpfiles) { 82 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); 83 } else { 84 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); 85 } 86 #if defined(PETSC_HAVE_POPEN) 87 ierr = PetscPOpen(comm,PETSC_NULL,task,"r",&fd);CHKERRQ(ierr); 88 ierr = PetscPClose(comm,fd);CHKERRQ(ierr); 89 #else 90 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 91 #endif 92 93 ierr = MPI_Barrier(comm);CHKERRQ(ierr); 94 95 /* load the apply function from the dynamic library */ 96 ierr = PetscGetUserName(username,64);CHKERRQ(ierr); 97 sprintf(lib,"%s/%s/libpetscdlib",tmp,username); 98 ierr = PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);CHKERRQ(ierr); 99 if (!f) SETERRQ1(((PetscObject)pf)->comm,PETSC_ERR_ARG_WRONGSTATE,"Cannot find function %s",lib); 100 #endif 101 PetscFunctionReturn(0); 102 } 103 104 #undef __FUNCT__ 105 #define __FUNCT__ "PFSetFromOptions_String" 106 PetscErrorCode PFSetFromOptions_String(PF pf) 107 { 108 PetscErrorCode ierr; 109 PetscBool flag; 110 char value[PETSC_MAX_PATH_LEN]; 111 PetscErrorCode (*f)(void*,PetscInt,const PetscScalar*,PetscScalar*) = 0; 112 113 PetscFunctionBegin; 114 ierr = PetscOptionsHead("String function options");CHKERRQ(ierr); 115 ierr = PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);CHKERRQ(ierr); 116 if (flag) { 117 ierr = PFStringCreateFunction(pf,value,(void**)&f);CHKERRQ(ierr); 118 pf->ops->apply = f; 119 } 120 ierr = PetscOptionsTail();CHKERRQ(ierr); 121 PetscFunctionReturn(0); 122 } 123 124 typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/ 125 EXTERN_C_BEGIN 126 #undef __FUNCT__ 127 #define __FUNCT__ "PFCreate_String" 128 PetscErrorCode PFCreate_String(PF pf,void *value) 129 { 130 PetscErrorCode ierr; 131 FCN f = 0; 132 133 PetscFunctionBegin; 134 if (value) { 135 ierr = PFStringCreateFunction(pf,(char*)value,(void**)&f);CHKERRQ(ierr); 136 } 137 ierr = PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);CHKERRQ(ierr); 138 pf->ops->setfromoptions = PFSetFromOptions_String; 139 PetscFunctionReturn(0); 140 } 141 EXTERN_C_END 142 143 144 145 146 147