Actual source code: cstring.c
petsc-3.3-p7 2013-05-11
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(((PetscObject)pf)->comm,&tmpshared);
69: PetscSharedWorkingDirectory(((PetscObject)pf)->comm,&wdshared);
70: if (tmpshared) { /* do it in /tmp since everyone has one */
71: PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);
72: comm = ((PetscObject)pf)->comm;
73: } else if (!wdshared) { /* each one does in private /tmp */
74: PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);
75: comm = PETSC_COMM_SELF;
76: } else { /* do it in current directory */
77: PetscStrcpy(tmp,".");
78: comm = ((PetscObject)pf)->comm;
79: }
80: PetscOptionsGetBool(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles,PETSC_NULL);
81: if (keeptmpfiles) {
82: 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);
83: } else {
84: 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);
85: }
86: #if defined(PETSC_HAVE_POPEN)
87: PetscPOpen(comm,PETSC_NULL,task,"r",&fd);
88: PetscPClose(comm,fd);
89: #else
90: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
91: #endif
93: MPI_Barrier(comm);
95: /* load the apply function from the dynamic library */
96: PetscGetUserName(username,64);
97: sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
98: PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);
99: if (!f) SETERRQ1(((PetscObject)pf)->comm,PETSC_ERR_ARG_WRONGSTATE,"Cannot find function %s",lib);
100: #endif
101: return(0);
102: }
106: PetscErrorCode PFSetFromOptions_String(PF pf)
107: {
109: PetscBool flag;
110: char value[PETSC_MAX_PATH_LEN];
111: PetscErrorCode (*f)(void*,PetscInt,const 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: }
124: typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/
125: EXTERN_C_BEGIN
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: }
141: EXTERN_C_END