1*0c735eedSKris Buschelman #define PETSCVEC_DLL 2292f8084SBarry Smith 358d819adSvictorle #include "src/vec/pf/pfimpl.h" /*I "petscpf.h" I*/ 4292f8084SBarry Smith 5292f8084SBarry Smith /* 6292f8084SBarry Smith Ths PF generates a function on the fly and loads it into the running 7292f8084SBarry Smith program. 8292f8084SBarry Smith */ 9292f8084SBarry Smith 10292f8084SBarry Smith #undef __FUNCT__ 11292f8084SBarry Smith #define __FUNCT__ "PFView_String" 12292f8084SBarry Smith PetscErrorCode PFView_String(void *value,PetscViewer viewer) 13292f8084SBarry Smith { 14292f8084SBarry Smith PetscErrorCode ierr; 15292f8084SBarry Smith PetscTruth iascii; 16292f8084SBarry Smith 17292f8084SBarry Smith PetscFunctionBegin; 18292f8084SBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 19292f8084SBarry Smith if (iascii) { 20292f8084SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"String = %s\n",(char*)value);CHKERRQ(ierr); 21292f8084SBarry Smith } 22292f8084SBarry Smith PetscFunctionReturn(0); 23292f8084SBarry Smith } 24292f8084SBarry Smith 25292f8084SBarry Smith #undef __FUNCT__ 26292f8084SBarry Smith #define __FUNCT__ "PFDestroy_String" 27292f8084SBarry Smith PetscErrorCode PFDestroy_String(void *value) 28292f8084SBarry Smith { 29292f8084SBarry Smith PetscErrorCode ierr; 30292f8084SBarry Smith 31292f8084SBarry Smith PetscFunctionBegin; 324bf112e7SSatish Balay ierr = PetscStrfree(value);CHKERRQ(ierr); 33292f8084SBarry Smith PetscFunctionReturn(0); 34292f8084SBarry Smith } 35292f8084SBarry Smith 36292f8084SBarry Smith #undef __FUNCT__ 37292f8084SBarry Smith #define __FUNCT__ "PFStringCreateFunction" 38292f8084SBarry Smith /* 39292f8084SBarry Smith PFStringCreateFunction - Creates a function from a string 40292f8084SBarry Smith 41292f8084SBarry Smith Collective over PF 42292f8084SBarry Smith 43292f8084SBarry Smith Input Parameters: 44292f8084SBarry Smith + pf - the function object 45292f8084SBarry Smith - string - the string that defines the function 46292f8084SBarry Smith 47292f8084SBarry Smith Output Parameter: 48292f8084SBarry Smith . f - the function pointer. 49292f8084SBarry Smith 50292f8084SBarry Smith .seealso: PFSetFromOptions() 51292f8084SBarry Smith 52292f8084SBarry Smith */ 53*0c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFStringCreateFunction(PF pf,char *string,void **f) 54292f8084SBarry Smith { 55292f8084SBarry Smith #if defined(PETSC_USE_DYNAMIC_LIBRARIES) 56292f8084SBarry Smith PetscErrorCode ierr; 57292f8084SBarry Smith char task[1024],tmp[256],lib[PETSC_MAX_PATH_LEN],username[64]; 58292f8084SBarry Smith FILE *fd; 59292f8084SBarry Smith PetscTruth tmpshared,wdshared,keeptmpfiles = PETSC_FALSE; 60292f8084SBarry Smith MPI_Comm comm; 61292f8084SBarry Smith #endif 62292f8084SBarry Smith 63292f8084SBarry Smith PetscFunctionBegin; 64292f8084SBarry Smith #if defined(PETSC_USE_DYNAMIC_LIBRARIES) 654bf112e7SSatish Balay ierr = PetscStrfree(pf->data);CHKERRQ(ierr); 66292f8084SBarry Smith ierr = PetscStrallocpy(string,(char**)&pf->data);CHKERRQ(ierr); 67292f8084SBarry Smith 68292f8084SBarry Smith /* create the new C function and compile it */ 69292f8084SBarry Smith ierr = PetscSharedTmp(pf->comm,&tmpshared);CHKERRQ(ierr); 70292f8084SBarry Smith ierr = PetscSharedWorkingDirectory(pf->comm,&wdshared);CHKERRQ(ierr); 71292f8084SBarry Smith if (tmpshared) { /* do it in /tmp since everyone has one */ 72292f8084SBarry Smith ierr = PetscGetTmp(pf->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 73292f8084SBarry Smith comm = pf->comm; 74292f8084SBarry Smith } else if (!wdshared) { /* each one does in private /tmp */ 75292f8084SBarry Smith ierr = PetscGetTmp(pf->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 76292f8084SBarry Smith comm = PETSC_COMM_SELF; 77292f8084SBarry Smith } else { /* do it in current directory */ 78292f8084SBarry Smith ierr = PetscStrcpy(tmp,".");CHKERRQ(ierr); 79292f8084SBarry Smith comm = pf->comm; 80292f8084SBarry Smith } 81292f8084SBarry Smith ierr = PetscOptionsHasName(pf->prefix,"-pf_string_keep_files",&keeptmpfiles);CHKERRQ(ierr); 82292f8084SBarry Smith if (keeptmpfiles) { 83ab901514SBarry Smith 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); 84292f8084SBarry Smith } else { 85ab901514SBarry Smith 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); 86292f8084SBarry Smith } 87292f8084SBarry Smith ierr = PetscPOpen(comm,PETSC_NULL,task,"r",&fd);CHKERRQ(ierr); 88292f8084SBarry Smith ierr = PetscPClose(comm,fd);CHKERRQ(ierr); 89292f8084SBarry Smith 90292f8084SBarry Smith ierr = MPI_Barrier(comm);CHKERRQ(ierr); 91292f8084SBarry Smith 92292f8084SBarry Smith /* load the apply function from the dynamic library */ 93292f8084SBarry Smith ierr = PetscGetUserName(username,64);CHKERRQ(ierr); 94292f8084SBarry Smith sprintf(lib,"%s/%s/libpetscdlib",tmp,username); 95292f8084SBarry Smith ierr = PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);CHKERRQ(ierr); 96292f8084SBarry Smith #endif 97292f8084SBarry Smith PetscFunctionReturn(0); 98292f8084SBarry Smith } 99292f8084SBarry Smith 100292f8084SBarry Smith #undef __FUNCT__ 101292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_String" 102292f8084SBarry Smith PetscErrorCode PFSetFromOptions_String(PF pf) 103292f8084SBarry Smith { 104292f8084SBarry Smith PetscErrorCode ierr; 105292f8084SBarry Smith PetscTruth flag; 106292f8084SBarry Smith char value[PETSC_MAX_PATH_LEN]; 107292f8084SBarry Smith PetscErrorCode (*f)(void*,PetscInt,PetscScalar*,PetscScalar*) = 0; 108292f8084SBarry Smith 109292f8084SBarry Smith PetscFunctionBegin; 110292f8084SBarry Smith ierr = PetscOptionsHead("String function options");CHKERRQ(ierr); 111292f8084SBarry Smith ierr = PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);CHKERRQ(ierr); 112292f8084SBarry Smith if (flag) { 113292f8084SBarry Smith ierr = PFStringCreateFunction(pf,value,(void**)&f);CHKERRQ(ierr); 114292f8084SBarry Smith pf->ops->apply = f; 115292f8084SBarry Smith } 116292f8084SBarry Smith ierr = PetscOptionsTail();CHKERRQ(ierr); 117292f8084SBarry Smith PetscFunctionReturn(0); 118292f8084SBarry Smith } 119292f8084SBarry Smith 120292f8084SBarry Smith typedef PetscErrorCode (*FCN)(void*,PetscInt,PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/ 121292f8084SBarry Smith EXTERN_C_BEGIN 122292f8084SBarry Smith #undef __FUNCT__ 123292f8084SBarry Smith #define __FUNCT__ "PFCreate_String" 124*0c735eedSKris Buschelman PetscErrorCode PETSCVEC_DLLEXPORT PFCreate_String(PF pf,void *value) 125292f8084SBarry Smith { 126292f8084SBarry Smith PetscErrorCode ierr; 127292f8084SBarry Smith FCN f = 0; 128292f8084SBarry Smith 129292f8084SBarry Smith PetscFunctionBegin; 130292f8084SBarry Smith if (value) { 131292f8084SBarry Smith ierr = PFStringCreateFunction(pf,(char*)value,(void**)&f);CHKERRQ(ierr); 132292f8084SBarry Smith } 133292f8084SBarry Smith ierr = PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);CHKERRQ(ierr); 134292f8084SBarry Smith pf->ops->setfromoptions = PFSetFromOptions_String; 135292f8084SBarry Smith PetscFunctionReturn(0); 136292f8084SBarry Smith } 137292f8084SBarry Smith EXTERN_C_END 138292f8084SBarry Smith 139292f8084SBarry Smith 140292f8084SBarry Smith 141292f8084SBarry Smith 142292f8084SBarry Smith 143