Actual source code: cstring.c

  1: /*$Id: cstring.c,v 1.17 2001/08/06 21:19:09 bsmith Exp $*/
 2:  #include src/pf/pfimpl.h

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

 11: int PFView_String(void *value,PetscViewer viewer)
 12: {
 13:   int        ierr;
 14:   PetscTruth isascii;

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

 26: int PFDestroy_String(void *value)
 27: {
 28:   int       ierr;

 31:   PetscStrfree((char*)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: int PFStringCreateFunction(PF pf,char *string,void **f)
 53: {
 54: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
 55:   int        ierr;
 56:   char       task[1024],tmp[256],lib[256],username[64];
 57:   FILE       *fd;
 58:   PetscTruth tmpshared,wdshared,keeptmpfiles = PETSC_FALSE;
 59:   MPI_Comm   comm;
 60: #endif

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

 67:   /* create the new C function and compile it */
 68:   PetscSharedTmp(pf->comm,&tmpshared);
 69:   PetscSharedWorkingDirectory(pf->comm,&wdshared);
 70:   if (tmpshared) {  /* do it in /tmp since everyone has one */
 71:     PetscGetTmp(pf->comm,tmp,256);
 72:     comm = pf->comm;
 73:   } else if (!wdshared) {  /* each one does in private /tmp */
 74:     PetscGetTmp(pf->comm,tmp,256);
 75:     comm = PETSC_COMM_SELF;
 76:   } else { /* do it in current directory */
 77:     PetscStrcpy(tmp,".");
 78:     comm = pf->comm;
 79:   }
 80:   PetscOptionsHasName(pf->prefix,"-pf_string_keep_files",&keeptmpfiles);
 81:   if (keeptmpfiles) {
 82:     sprintf(task,"cd %s ; mkdir ${USERNAME} ; cd ${USERNAME} ; \\cp -f ${PETSC_DIR}/src/pf/impls/string/makefile ./makefile ; make BOPT=${BOPT} MIN=%d NOUT=%d petscdlib STRINGFUNCTION=\"%s\" ; sync\n",tmp,pf->dimin,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 BOPT=${BOPT} MIN=%d NOUT=%d -f makefile petscdlib STRINGFUNCTION=\"%s\" ; \\rm -f makefile petscdlib.c libpetscdlib.a ;  sync\n",tmp,pf->dimin,pf->dimout,string);
 85:   }
 86:   PetscPOpen(comm,PETSC_NULL,task,"r",&fd);
 87:   PetscPClose(comm,fd);

 89:   MPI_Barrier(comm);

 91:   /* load the apply function from the dynamic library */
 92:   PetscGetUserName(username,64);
 93:   sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
 94:   PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);
 95: #endif
 96:   return(0);
 97: }

101: int PFSetFromOptions_String(PF pf)
102: {
103:   int        ierr;
104:   PetscTruth flag;
105:   char       value[256];
106:   int        (*f)(void *,int,PetscScalar*,PetscScalar*) = 0;

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

119: typedef int (*FCN)(void*,int,PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/
120: EXTERN_C_BEGIN
123: int PFCreate_String(PF pf,void *value)
124: {
125:   int        ierr;
126:   FCN        f = 0;

129: 
130:   if (value) {
131:     PFStringCreateFunction(pf,(char*)value,(void**)&f);
132:   }
133:   PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);

135:   pf->ops->setfromoptions = PFSetFromOptions_String;
136:   return(0);
137: }
138: EXTERN_C_END