Actual source code: cstring.c
1: #define PETSCVEC_DLL
3: #include ../src/vec/pf/pfimpl.h
5: /*
6: Ths PF generates a function on the fly and loads it into the running
7: program.
8: */
12: PetscErrorCode PFView_String(void *value,PetscViewer viewer)
13: {
15: PetscTruth iascii;
18: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
19: if (iascii) {
20: PetscViewerASCIIPrintf(viewer,"String = %s\n",(char*)value);
21: }
22: return(0);
23: }
27: PetscErrorCode PFDestroy_String(void *value)
28: {
32: PetscStrfree(value);
33: return(0);
34: }
38: /*
39: PFStringCreateFunction - Creates a function from a string
41: Collective over PF
43: Input Parameters:
44: + pf - the function object
45: - string - the string that defines the function
47: Output Parameter:
48: . f - the function pointer.
50: .seealso: PFSetFromOptions()
52: */
53: PetscErrorCode PFStringCreateFunction(PF pf,char *string,void **f)
54: {
55: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
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
64: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
65: PetscStrfree(pf->data);
66: PetscStrallocpy(string,(char**)&pf->data);
68: /* create the new C function and compile it */
69: PetscSharedTmp(((PetscObject)pf)->comm,&tmpshared);
70: PetscSharedWorkingDirectory(((PetscObject)pf)->comm,&wdshared);
71: if (tmpshared) { /* do it in /tmp since everyone has one */
72: PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);
73: comm = ((PetscObject)pf)->comm;
74: } else if (!wdshared) { /* each one does in private /tmp */
75: PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);
76: comm = PETSC_COMM_SELF;
77: } else { /* do it in current directory */
78: PetscStrcpy(tmp,".");
79: comm = ((PetscObject)pf)->comm;
80: }
81: PetscOptionsGetTruth(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles,PETSC_NULL);
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: PetscPOpen(comm,PETSC_NULL,task,"r",&fd);
89: PetscPClose(comm,fd);
90: #else
91: SETERRQ(PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
92: #endif
94: MPI_Barrier(comm);
96: /* load the apply function from the dynamic library */
97: PetscGetUserName(username,64);
98: sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
99: PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);
100: #endif
101: return(0);
102: }
106: PetscErrorCode PFSetFromOptions_String(PF pf)
107: {
109: PetscTruth flag;
110: char value[PETSC_MAX_PATH_LEN];
111: PetscErrorCode (*f)(void*,PetscInt,PetscScalar*,PetscScalar*) = 0;
114: PetscOptionsHead("String function options");
115: PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);
116: if (flag) {
117: PFStringCreateFunction(pf,value,(void**)&f);
118: pf->ops->apply = f;
119: }
120: PetscOptionsTail();
121: return(0);
122: }
128: PetscErrorCode PFCreate_String(PF pf,void *value)
129: {
131: FCN f = 0;
134: if (value) {
135: PFStringCreateFunction(pf,(char*)value,(void**)&f);
136: }
137: PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);
138: pf->ops->setfromoptions = PFSetFromOptions_String;
139: return(0);
140: }