Actual source code: cstring.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: #include <../src/vec/pf/pfimpl.h>            /*I "petscpf.h" I*/

  4: /*
  5:         Ths PF generates a function on the fly and loads it into the running
  6:    program.
  7: */

 11: PetscErrorCode PFView_String(void *value,PetscViewer viewer)
 12: {
 14:   PetscBool      iascii;

 17:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
 18:   if (iascii) {
 19:     PetscViewerASCIIPrintf(viewer,"String = %s\n",(char*)value);
 20:   }
 21:   return(0);
 22: }

 26: PetscErrorCode PFDestroy_String(void *value)
 27: {

 31:   PetscFree(value);
 32:   return(0);
 33: }

 37: /*
 38:     PFStringCreateFunction - Creates a function from a string

 40:    Collective over PF

 42:   Input Parameters:
 43: +    pf - the function object
 44: -    string - the string that defines the function

 46:   Output Parameter:
 47: .    f - the function pointer.

 49: .seealso: PFSetFromOptions()

 51: */
 52: PetscErrorCode  PFStringCreateFunction(PF pf,char *string,void **f)
 53: {
 54: #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
 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

 63: #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
 64:   PetscFree(pf->data);
 65:   PetscStrallocpy(string,(char**)&pf->data);

 67:   /* create the new C function and compile it */
 68:   PetscSharedTmp(PetscObjectComm((PetscObject)pf),&tmpshared);
 69:   PetscSharedWorkingDirectory(PetscObjectComm((PetscObject)pf),&wdshared);
 70:   if (tmpshared) {  /* do it in /tmp since everyone has one */
 71:     PetscGetTmp(PetscObjectComm((PetscObject)pf),tmp,PETSC_MAX_PATH_LEN);
 72:     PetscObjectGetComm((PetscObject)pf,&comm);
 73:   } else if (!wdshared) {  /* each one does in private /tmp */
 74:     PetscGetTmp(PetscObjectComm((PetscObject)pf),tmp,PETSC_MAX_PATH_LEN);
 75:     comm = PETSC_COMM_SELF;
 76:   } else { /* do it in current directory */
 77:     PetscStrcpy(tmp,".");
 78:     PetscObjectGetComm((PetscObject)pf,&comm);
 79:   }
 80:   PetscOptionsGetBool(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles,NULL);
 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);

 84: #if defined(PETSC_HAVE_POPEN)
 85:   PetscPOpen(comm,NULL,task,"r",&fd);
 86:   PetscPClose(comm,fd,NULL);
 87: #else
 88:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
 89: #endif

 91:   MPI_Barrier(comm);

 93:   /* load the apply function from the dynamic library */
 94:   PetscGetUserName(username,64);
 95:   sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
 96:   PetscDLLibrarySym(comm,NULL,lib,"PFApply_String",f);
 97:   if (!f) SETERRQ1(PetscObjectComm((PetscObject)pf),PETSC_ERR_ARG_WRONGSTATE,"Cannot find function %s",lib);
 98: #endif
 99:   return(0);
100: }

104: PetscErrorCode PFSetFromOptions_String(PetscOptions *PetscOptionsObject,PF pf)
105: {
107:   PetscBool      flag;
108:   char           value[PETSC_MAX_PATH_LEN];
109:   PetscErrorCode (*f)(void*,PetscInt,const PetscScalar*,PetscScalar*) = 0;

112:   PetscOptionsHead(PetscOptionsObject,"String function options");
113:   PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);
114:   if (flag) {
115:     PFStringCreateFunction(pf,value,(void**)&f);
116:     pf->ops->apply = f;
117:   }
118:   PetscOptionsTail();
119:   return(0);
120: }

122: typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/

126: PETSC_EXTERN PetscErrorCode PFCreate_String(PF pf,void *value)
127: {
129:   FCN            f = 0;

132:   if (value) {
133:     PFStringCreateFunction(pf,(char*)value,(void**)&f);
134:   }
135:   PFSet(pf,f,NULL,PFView_String,PFDestroy_String,NULL);
136:   pf->ops->setfromoptions = PFSetFromOptions_String;
137:   return(0);
138: }