xref: /petsc/src/vec/pf/impls/string/cstring.c (revision 292f8084fb157dadf9a2ae26c5bd14368ed7ffcb)
1*292f8084SBarry Smith 
2*292f8084SBarry Smith #include "src/pf/pfimpl.h"            /*I "petscpf.h" I*/
3*292f8084SBarry Smith 
4*292f8084SBarry Smith /*
5*292f8084SBarry Smith         Ths PF generates a function on the fly and loads it into the running
6*292f8084SBarry Smith    program.
7*292f8084SBarry Smith */
8*292f8084SBarry Smith 
9*292f8084SBarry Smith #undef __FUNCT__
10*292f8084SBarry Smith #define __FUNCT__ "PFView_String"
11*292f8084SBarry Smith PetscErrorCode PFView_String(void *value,PetscViewer viewer)
12*292f8084SBarry Smith {
13*292f8084SBarry Smith   PetscErrorCode ierr;
14*292f8084SBarry Smith   PetscTruth iascii;
15*292f8084SBarry Smith 
16*292f8084SBarry Smith   PetscFunctionBegin;
17*292f8084SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
18*292f8084SBarry Smith   if (iascii) {
19*292f8084SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"String = %s\n",(char*)value);CHKERRQ(ierr);
20*292f8084SBarry Smith   }
21*292f8084SBarry Smith   PetscFunctionReturn(0);
22*292f8084SBarry Smith }
23*292f8084SBarry Smith 
24*292f8084SBarry Smith #undef __FUNCT__
25*292f8084SBarry Smith #define __FUNCT__ "PFDestroy_String"
26*292f8084SBarry Smith PetscErrorCode PFDestroy_String(void *value)
27*292f8084SBarry Smith {
28*292f8084SBarry Smith   PetscErrorCode ierr;
29*292f8084SBarry Smith 
30*292f8084SBarry Smith   PetscFunctionBegin;
31*292f8084SBarry Smith   ierr = PetscStrfree((char*)value);CHKERRQ(ierr);
32*292f8084SBarry Smith   PetscFunctionReturn(0);
33*292f8084SBarry Smith }
34*292f8084SBarry Smith 
35*292f8084SBarry Smith #undef __FUNCT__
36*292f8084SBarry Smith #define __FUNCT__ "PFStringCreateFunction"
37*292f8084SBarry Smith /*
38*292f8084SBarry Smith     PFStringCreateFunction - Creates a function from a string
39*292f8084SBarry Smith 
40*292f8084SBarry Smith    Collective over PF
41*292f8084SBarry Smith 
42*292f8084SBarry Smith   Input Parameters:
43*292f8084SBarry Smith +    pf - the function object
44*292f8084SBarry Smith -    string - the string that defines the function
45*292f8084SBarry Smith 
46*292f8084SBarry Smith   Output Parameter:
47*292f8084SBarry Smith .    f - the function pointer.
48*292f8084SBarry Smith 
49*292f8084SBarry Smith .seealso: PFSetFromOptions()
50*292f8084SBarry Smith 
51*292f8084SBarry Smith */
52*292f8084SBarry Smith PetscErrorCode PFStringCreateFunction(PF pf,char *string,void **f)
53*292f8084SBarry Smith {
54*292f8084SBarry Smith #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
55*292f8084SBarry Smith   PetscErrorCode ierr;
56*292f8084SBarry Smith   char       task[1024],tmp[256],lib[PETSC_MAX_PATH_LEN],username[64];
57*292f8084SBarry Smith   FILE       *fd;
58*292f8084SBarry Smith   PetscTruth tmpshared,wdshared,keeptmpfiles = PETSC_FALSE;
59*292f8084SBarry Smith   MPI_Comm   comm;
60*292f8084SBarry Smith #endif
61*292f8084SBarry Smith 
62*292f8084SBarry Smith   PetscFunctionBegin;
63*292f8084SBarry Smith #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
64*292f8084SBarry Smith   ierr = PetscStrfree((char*)pf->data);CHKERRQ(ierr);
65*292f8084SBarry Smith   ierr = PetscStrallocpy(string,(char**)&pf->data);CHKERRQ(ierr);
66*292f8084SBarry Smith 
67*292f8084SBarry Smith   /* create the new C function and compile it */
68*292f8084SBarry Smith   ierr = PetscSharedTmp(pf->comm,&tmpshared);CHKERRQ(ierr);
69*292f8084SBarry Smith   ierr = PetscSharedWorkingDirectory(pf->comm,&wdshared);CHKERRQ(ierr);
70*292f8084SBarry Smith   if (tmpshared) {  /* do it in /tmp since everyone has one */
71*292f8084SBarry Smith     ierr = PetscGetTmp(pf->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
72*292f8084SBarry Smith     comm = pf->comm;
73*292f8084SBarry Smith   } else if (!wdshared) {  /* each one does in private /tmp */
74*292f8084SBarry Smith     ierr = PetscGetTmp(pf->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
75*292f8084SBarry Smith     comm = PETSC_COMM_SELF;
76*292f8084SBarry Smith   } else { /* do it in current directory */
77*292f8084SBarry Smith     ierr = PetscStrcpy(tmp,".");CHKERRQ(ierr);
78*292f8084SBarry Smith     comm = pf->comm;
79*292f8084SBarry Smith   }
80*292f8084SBarry Smith   ierr = PetscOptionsHasName(pf->prefix,"-pf_string_keep_files",&keeptmpfiles);CHKERRQ(ierr);
81*292f8084SBarry Smith   if (keeptmpfiles) {
82*292f8084SBarry Smith     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*292f8084SBarry Smith   } else {
84*292f8084SBarry Smith     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*292f8084SBarry Smith   }
86*292f8084SBarry Smith   ierr = PetscPOpen(comm,PETSC_NULL,task,"r",&fd);CHKERRQ(ierr);
87*292f8084SBarry Smith   ierr = PetscPClose(comm,fd);CHKERRQ(ierr);
88*292f8084SBarry Smith 
89*292f8084SBarry Smith   ierr = MPI_Barrier(comm);CHKERRQ(ierr);
90*292f8084SBarry Smith 
91*292f8084SBarry Smith   /* load the apply function from the dynamic library */
92*292f8084SBarry Smith   ierr = PetscGetUserName(username,64);CHKERRQ(ierr);
93*292f8084SBarry Smith   sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
94*292f8084SBarry Smith   ierr = PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);CHKERRQ(ierr);
95*292f8084SBarry Smith #endif
96*292f8084SBarry Smith   PetscFunctionReturn(0);
97*292f8084SBarry Smith }
98*292f8084SBarry Smith 
99*292f8084SBarry Smith #undef __FUNCT__
100*292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_String"
101*292f8084SBarry Smith PetscErrorCode PFSetFromOptions_String(PF pf)
102*292f8084SBarry Smith {
103*292f8084SBarry Smith   PetscErrorCode ierr;
104*292f8084SBarry Smith   PetscTruth flag;
105*292f8084SBarry Smith   char       value[PETSC_MAX_PATH_LEN];
106*292f8084SBarry Smith   PetscErrorCode (*f)(void*,PetscInt,PetscScalar*,PetscScalar*) = 0;
107*292f8084SBarry Smith 
108*292f8084SBarry Smith   PetscFunctionBegin;
109*292f8084SBarry Smith   ierr = PetscOptionsHead("String function options");CHKERRQ(ierr);
110*292f8084SBarry Smith     ierr = PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);CHKERRQ(ierr);
111*292f8084SBarry Smith     if (flag) {
112*292f8084SBarry Smith       ierr = PFStringCreateFunction(pf,value,(void**)&f);CHKERRQ(ierr);
113*292f8084SBarry Smith       pf->ops->apply = f;
114*292f8084SBarry Smith     }
115*292f8084SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
116*292f8084SBarry Smith   PetscFunctionReturn(0);
117*292f8084SBarry Smith }
118*292f8084SBarry Smith 
119*292f8084SBarry Smith typedef PetscErrorCode (*FCN)(void*,PetscInt,PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/
120*292f8084SBarry Smith EXTERN_C_BEGIN
121*292f8084SBarry Smith #undef __FUNCT__
122*292f8084SBarry Smith #define __FUNCT__ "PFCreate_String"
123*292f8084SBarry Smith PetscErrorCode PFCreate_String(PF pf,void *value)
124*292f8084SBarry Smith {
125*292f8084SBarry Smith   PetscErrorCode ierr;
126*292f8084SBarry Smith   FCN        f = 0;
127*292f8084SBarry Smith 
128*292f8084SBarry Smith   PetscFunctionBegin;
129*292f8084SBarry Smith   if (value) {
130*292f8084SBarry Smith     ierr = PFStringCreateFunction(pf,(char*)value,(void**)&f);CHKERRQ(ierr);
131*292f8084SBarry Smith   }
132*292f8084SBarry Smith   ierr   = PFSet(pf,f,PETSC_NULL,PFView_String,PFDestroy_String,PETSC_NULL);CHKERRQ(ierr);
133*292f8084SBarry Smith   pf->ops->setfromoptions = PFSetFromOptions_String;
134*292f8084SBarry Smith   PetscFunctionReturn(0);
135*292f8084SBarry Smith }
136*292f8084SBarry Smith EXTERN_C_END
137*292f8084SBarry Smith 
138*292f8084SBarry Smith 
139*292f8084SBarry Smith 
140*292f8084SBarry Smith 
141*292f8084SBarry Smith 
142