Actual source code: cstring.c
2: #include src/vec/pf/pfimpl.h
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: PetscTruth iascii;
17: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&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: PetscStrfree(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_USE_DYNAMIC_LIBRARIES)
56: char task[1024],tmp[256],lib[PETSC_MAX_PATH_LEN],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(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,PETSC_MAX_PATH_LEN);
72: comm = pf->comm;
73: } else if (!wdshared) { /* each one does in private /tmp */
74: PetscGetTmp(pf->comm,tmp,PETSC_MAX_PATH_LEN);
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,(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 BOPT=${BOPT} 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: 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: PetscErrorCode PFSetFromOptions_String(PF pf)
102: {
104: PetscTruth flag;
105: char value[PETSC_MAX_PATH_LEN];
106: PetscErrorCode (*f)(void*,PetscInt,PetscScalar*,PetscScalar*) = 0;
109: PetscOptionsHead("String function options");
110: PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);
111: if (flag) {
112: PFStringCreateFunction(pf,value,(void**)&f);
113: pf->ops->apply = f;
114: }
115: PetscOptionsTail();
116: return(0);
117: }
123: PetscErrorCode PFCreate_String(PF pf,void *value)
124: {
126: FCN f = 0;
129: if (value) {
130: PFStringCreateFunction(pf,(char*)value,(void**)&f);
131: }
132: PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);
133: pf->ops->setfromoptions = PFSetFromOptions_String;
134: return(0);
135: }