1 #define PETSCVEC_DLL 2 3 #include "src/vec/pf/pfimpl.h" /*I "petscpf.h" I*/ 4 5 /* 6 Ths PF generates a function on the fly and loads it into the running 7 program. 8 */ 9 10 #undef __FUNCT__ 11 #define __FUNCT__ "PFView_String" 12 PetscErrorCode PFView_String(void *value,PetscViewer viewer) 13 { 14 PetscErrorCode ierr; 15 PetscTruth iascii; 16 17 PetscFunctionBegin; 18 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 19 if (iascii) { 20 ierr = PetscViewerASCIIPrintf(viewer,"String = %s\n",(char*)value);CHKERRQ(ierr); 21 } 22 PetscFunctionReturn(0); 23 } 24 25 #undef __FUNCT__ 26 #define __FUNCT__ "PFDestroy_String" 27 PetscErrorCode PFDestroy_String(void *value) 28 { 29 PetscErrorCode ierr; 30 31 PetscFunctionBegin; 32 ierr = PetscStrfree(value);CHKERRQ(ierr); 33 PetscFunctionReturn(0); 34 } 35 36 #undef __FUNCT__ 37 #define __FUNCT__ "PFStringCreateFunction" 38 /* 39 PFStringCreateFunction - Creates a function from a string 40 41 Collective over PF 42 43 Input Parameters: 44 + pf - the function object 45 - string - the string that defines the function 46 47 Output Parameter: 48 . f - the function pointer. 49 50 .seealso: PFSetFromOptions() 51 52 */ 53 PetscErrorCode PETSCVEC_DLLEXPORT PFStringCreateFunction(PF pf,char *string,void **f) 54 { 55 #if defined(PETSC_USE_DYNAMIC_LIBRARIES) 56 PetscErrorCode ierr; 57 char task[1024],tmp[256],lib[PETSC_MAX_PATH_LEN],username[64]; 58 FILE *fd; 59 PetscTruth tmpshared,wdshared,keeptmpfiles = PETSC_FALSE; 60 MPI_Comm comm; 61 #endif 62 63 PetscFunctionBegin; 64 #if defined(PETSC_USE_DYNAMIC_LIBRARIES) 65 ierr = PetscStrfree(pf->data);CHKERRQ(ierr); 66 ierr = PetscStrallocpy(string,(char**)&pf->data);CHKERRQ(ierr); 67 68 /* create the new C function and compile it */ 69 ierr = PetscSharedTmp(((PetscObject)pf)->comm,&tmpshared);CHKERRQ(ierr); 70 ierr = PetscSharedWorkingDirectory(((PetscObject)pf)->comm,&wdshared);CHKERRQ(ierr); 71 if (tmpshared) { /* do it in /tmp since everyone has one */ 72 ierr = PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 73 comm = ((PetscObject)pf)->comm; 74 } else if (!wdshared) { /* each one does in private /tmp */ 75 ierr = PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 76 comm = PETSC_COMM_SELF; 77 } else { /* do it in current directory */ 78 ierr = PetscStrcpy(tmp,".");CHKERRQ(ierr); 79 comm = ((PetscObject)pf)->comm; 80 } 81 ierr = PetscOptionsHasName(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles);CHKERRQ(ierr); 82 if (keeptmpfiles) { 83 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); 84 } else { 85 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); 86 } 87 #if defined(PETSC_HAVE_POPEN) 88 ierr = PetscPOpen(comm,PETSC_NULL,task,"r",&fd);CHKERRQ(ierr); 89 ierr = PetscPClose(comm,fd);CHKERRQ(ierr); 90 #else 91 SETERRQ(PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 92 #endif 93 94 ierr = MPI_Barrier(comm);CHKERRQ(ierr); 95 96 /* load the apply function from the dynamic library */ 97 ierr = PetscGetUserName(username,64);CHKERRQ(ierr); 98 sprintf(lib,"%s/%s/libpetscdlib",tmp,username); 99 ierr = PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);CHKERRQ(ierr); 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 PetscTruth flag; 110 char value[PETSC_MAX_PATH_LEN]; 111 PetscErrorCode (*f)(void*,PetscInt,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,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 PETSCVEC_DLLEXPORT 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