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: */
9: int PFView_String(void *value,PetscViewer viewer)
10: {
11: int ierr;
12: PetscTruth isascii;
15: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
16: if (isascii) {
17: PetscViewerASCIIPrintf(viewer,"String = %sn",(char*)value);
18: }
19: return(0);
20: }
22: int PFDestroy_String(void *value)
23: {
24: int ierr;
27: PetscStrfree((char*)value);
28: return(0);
29: }
31: /*
32: PFStringCreateFunction - Creates a function from a string
34: Collective over PF
36: Input Parameters:
37: + pf - the function object
38: - string - the string that defines the function
40: Output Parameter:
41: . f - the function pointer.
43: .seealso: PFSetFromOptions()
45: */
46: int PFStringCreateFunction(PF pf,char *string,void **f)
47: {
48: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
49: int ierr;
50: char task[1024],tmp[256],lib[256],username[64];
51: FILE *fd;
52: PetscTruth tmpshared,wdshared,keeptmpfiles = PETSC_FALSE;
53: MPI_Comm comm;
54: #endif
57: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
58: PetscStrfree((char*)pf->data);
59: PetscStrallocpy(string,(char**)&pf->data);
61: /* create the new C function and compile it */
62: PetscSharedTmp(pf->comm,&tmpshared);
63: PetscSharedWorkingDirectory(pf->comm,&wdshared);
64: if (tmpshared) { /* do it in /tmp since everyone has one */
65: PetscGetTmp(pf->comm,tmp,256);
66: comm = pf->comm;
67: } else if (!wdshared) { /* each one does in private /tmp */
68: PetscGetTmp(pf->comm,tmp,256);
69: comm = PETSC_COMM_SELF;
70: } else { /* do it in current directory */
71: PetscStrcpy(tmp,".");
72: comm = pf->comm;
73: }
74: PetscOptionsHasName(pf->prefix,"-pf_string_keep_files",&keeptmpfiles);
75: if (keeptmpfiles) {
76: 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" ; syncn",tmp,pf->dimin,pf->dimout,string);
77: } else {
78: 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 ; syncn",tmp,pf->dimin,pf->dimout,string);
79: }
80: PetscPOpen(comm,PETSC_NULL,task,"r",&fd);
81: PetscPClose(comm,fd);
83: MPI_Barrier(comm);
85: /* load the apply function from the dynamic library */
86: PetscGetUserName(username,64);
87: sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
88: PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);
89: #endif
90: return(0);
91: }
93: int PFSetFromOptions_String(PF pf)
94: {
95: int ierr;
96: PetscTruth flag;
97: char value[256];
98: int (*f)(void *,int,PetscScalar*,PetscScalar*) = 0;
101: PetscOptionsHead("String function options");
102: PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,256,&flag);
103: if (flag) {
104: PFStringCreateFunction(pf,value,(void**)&f);
105: pf->ops->apply = f;
106: }
107: PetscOptionsTail();
108: return(0);
109: }
112: EXTERN_C_BEGIN
113: int PFCreate_String(PF pf,void *value)
114: {
115: int ierr;
116: int (*f)(void *,int,PetscScalar*,PetscScalar*) = 0;
119:
120: if (value) {
121: PFStringCreateFunction(pf,(char*)value,(void**)&f);
122: }
123: ierr = PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);
125: pf->ops->setfromoptions = PFSetFromOptions_String;
126: return(0);
127: }
128: EXTERN_C_END