xref: /petsc/src/vec/pf/impls/string/cstring.c (revision 8cc058d9cd56c1ccb3be12a47760ddfc446aaffc)
1292f8084SBarry Smith 
2c6db04a5SJed Brown #include <../src/vec/pf/pfimpl.h>            /*I "petscpf.h" I*/
3292f8084SBarry Smith 
4292f8084SBarry Smith /*
5292f8084SBarry Smith         Ths PF generates a function on the fly and loads it into the running
6292f8084SBarry Smith    program.
7292f8084SBarry Smith */
8292f8084SBarry Smith 
9292f8084SBarry Smith #undef __FUNCT__
10292f8084SBarry Smith #define __FUNCT__ "PFView_String"
11292f8084SBarry Smith PetscErrorCode PFView_String(void *value,PetscViewer viewer)
12292f8084SBarry Smith {
13292f8084SBarry Smith   PetscErrorCode ierr;
14ace3abfcSBarry Smith   PetscBool      iascii;
15292f8084SBarry Smith 
16292f8084SBarry Smith   PetscFunctionBegin;
17251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
18292f8084SBarry Smith   if (iascii) {
19292f8084SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"String = %s\n",(char*)value);CHKERRQ(ierr);
20292f8084SBarry Smith   }
21292f8084SBarry Smith   PetscFunctionReturn(0);
22292f8084SBarry Smith }
23292f8084SBarry Smith 
24292f8084SBarry Smith #undef __FUNCT__
25292f8084SBarry Smith #define __FUNCT__ "PFDestroy_String"
26292f8084SBarry Smith PetscErrorCode PFDestroy_String(void *value)
27292f8084SBarry Smith {
28292f8084SBarry Smith   PetscErrorCode ierr;
29292f8084SBarry Smith 
30292f8084SBarry Smith   PetscFunctionBegin;
31503cfb0cSBarry Smith   ierr = PetscFree(value);CHKERRQ(ierr);
32292f8084SBarry Smith   PetscFunctionReturn(0);
33292f8084SBarry Smith }
34292f8084SBarry Smith 
35292f8084SBarry Smith #undef __FUNCT__
36292f8084SBarry Smith #define __FUNCT__ "PFStringCreateFunction"
37292f8084SBarry Smith /*
38292f8084SBarry Smith     PFStringCreateFunction - Creates a function from a string
39292f8084SBarry Smith 
40292f8084SBarry Smith    Collective over PF
41292f8084SBarry Smith 
42292f8084SBarry Smith   Input Parameters:
43292f8084SBarry Smith +    pf - the function object
44292f8084SBarry Smith -    string - the string that defines the function
45292f8084SBarry Smith 
46292f8084SBarry Smith   Output Parameter:
47292f8084SBarry Smith .    f - the function pointer.
48292f8084SBarry Smith 
49292f8084SBarry Smith .seealso: PFSetFromOptions()
50292f8084SBarry Smith 
51292f8084SBarry Smith */
527087cfbeSBarry Smith PetscErrorCode  PFStringCreateFunction(PF pf,char *string,void **f)
53292f8084SBarry Smith {
540598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
55292f8084SBarry Smith   PetscErrorCode ierr;
56292f8084SBarry Smith   char           task[1024],tmp[256],lib[PETSC_MAX_PATH_LEN],username[64];
57292f8084SBarry Smith   FILE           *fd;
58ace3abfcSBarry Smith   PetscBool      tmpshared,wdshared,keeptmpfiles = PETSC_FALSE;
59292f8084SBarry Smith   MPI_Comm       comm;
60292f8084SBarry Smith #endif
61292f8084SBarry Smith 
62292f8084SBarry Smith   PetscFunctionBegin;
630598bfebSBarry Smith #if defined(PETSC_HAVE_DYNAMIC_LIBRARIES)
64503cfb0cSBarry Smith   ierr = PetscFree(pf->data);CHKERRQ(ierr);
65292f8084SBarry Smith   ierr = PetscStrallocpy(string,(char**)&pf->data);CHKERRQ(ierr);
66292f8084SBarry Smith 
67292f8084SBarry Smith   /* create the new C function and compile it */
68ce94432eSBarry Smith   ierr = PetscSharedTmp(PetscObjectComm((PetscObject)pf),&tmpshared);CHKERRQ(ierr);
69ce94432eSBarry Smith   ierr = PetscSharedWorkingDirectory(PetscObjectComm((PetscObject)pf),&wdshared);CHKERRQ(ierr);
70292f8084SBarry Smith   if (tmpshared) {  /* do it in /tmp since everyone has one */
71ce94432eSBarry Smith     ierr = PetscGetTmp(PetscObjectComm((PetscObject)pf),tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
72ce94432eSBarry Smith     ierr = PetscObjectGetComm((PetscObject)pf,&comm);CHKERRQ(ierr);
73292f8084SBarry Smith   } else if (!wdshared) {  /* each one does in private /tmp */
74ce94432eSBarry Smith     ierr = PetscGetTmp(PetscObjectComm((PetscObject)pf),tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
75292f8084SBarry Smith     comm = PETSC_COMM_SELF;
76292f8084SBarry Smith   } else { /* do it in current directory */
77292f8084SBarry Smith     ierr = PetscStrcpy(tmp,".");CHKERRQ(ierr);
78ce94432eSBarry Smith     ierr = PetscObjectGetComm((PetscObject)pf,&comm);CHKERRQ(ierr);
79292f8084SBarry Smith   }
800298fd71SBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles,NULL);CHKERRQ(ierr);
81f6e5521dSKarl Rupp   if (keeptmpfiles) 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);
82f6e5521dSKarl Rupp   else              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);
83f6e5521dSKarl Rupp 
847cfd0b05SBarry Smith #if defined(PETSC_HAVE_POPEN)
850298fd71SBarry Smith   ierr = PetscPOpen(comm,NULL,task,"r",&fd);CHKERRQ(ierr);
860298fd71SBarry Smith   ierr = PetscPClose(comm,fd,NULL);CHKERRQ(ierr);
877cfd0b05SBarry Smith #else
88e32f2f54SBarry Smith   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
897cfd0b05SBarry Smith #endif
90292f8084SBarry Smith 
91292f8084SBarry Smith   ierr = MPI_Barrier(comm);CHKERRQ(ierr);
92292f8084SBarry Smith 
93292f8084SBarry Smith   /* load the apply function from the dynamic library */
94292f8084SBarry Smith   ierr = PetscGetUserName(username,64);CHKERRQ(ierr);
95292f8084SBarry Smith   sprintf(lib,"%s/%s/libpetscdlib",tmp,username);
960298fd71SBarry Smith   ierr = PetscDLLibrarySym(comm,NULL,lib,"PFApply_String",f);CHKERRQ(ierr);
97ce94432eSBarry Smith   if (!f) SETERRQ1(PetscObjectComm((PetscObject)pf),PETSC_ERR_ARG_WRONGSTATE,"Cannot find function %s",lib);
98292f8084SBarry Smith #endif
99292f8084SBarry Smith   PetscFunctionReturn(0);
100292f8084SBarry Smith }
101292f8084SBarry Smith 
102292f8084SBarry Smith #undef __FUNCT__
103292f8084SBarry Smith #define __FUNCT__ "PFSetFromOptions_String"
104292f8084SBarry Smith PetscErrorCode PFSetFromOptions_String(PF pf)
105292f8084SBarry Smith {
106292f8084SBarry Smith   PetscErrorCode ierr;
107ace3abfcSBarry Smith   PetscBool      flag;
108292f8084SBarry Smith   char           value[PETSC_MAX_PATH_LEN];
10926e7fa26SJed Brown   PetscErrorCode (*f)(void*,PetscInt,const PetscScalar*,PetscScalar*) = 0;
110292f8084SBarry Smith 
111292f8084SBarry Smith   PetscFunctionBegin;
112292f8084SBarry Smith   ierr = PetscOptionsHead("String function options");CHKERRQ(ierr);
113292f8084SBarry Smith   ierr = PetscOptionsString("-pf_string","Enter the function","PFStringCreateFunction","",value,PETSC_MAX_PATH_LEN,&flag);CHKERRQ(ierr);
114292f8084SBarry Smith   if (flag) {
115292f8084SBarry Smith     ierr           = PFStringCreateFunction(pf,value,(void**)&f);CHKERRQ(ierr);
116292f8084SBarry Smith     pf->ops->apply = f;
117292f8084SBarry Smith   }
118292f8084SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
119292f8084SBarry Smith   PetscFunctionReturn(0);
120292f8084SBarry Smith }
121292f8084SBarry Smith 
12226e7fa26SJed Brown typedef PetscErrorCode (*FCN)(void*,PetscInt,const PetscScalar*,PetscScalar*); /* force argument to next function to not be extern C*/
1236ac5842eSBarry Smith 
124292f8084SBarry Smith #undef __FUNCT__
125292f8084SBarry Smith #define __FUNCT__ "PFCreate_String"
126*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PFCreate_String(PF pf,void *value)
127292f8084SBarry Smith {
128292f8084SBarry Smith   PetscErrorCode ierr;
129292f8084SBarry Smith   FCN            f = 0;
130292f8084SBarry Smith 
131292f8084SBarry Smith   PetscFunctionBegin;
132292f8084SBarry Smith   if (value) {
133292f8084SBarry Smith     ierr = PFStringCreateFunction(pf,(char*)value,(void**)&f);CHKERRQ(ierr);
134292f8084SBarry Smith   }
1350298fd71SBarry Smith   ierr                    = PFSet(pf,f,NULL,PFView_String,PFDestroy_String,NULL);CHKERRQ(ierr);
136292f8084SBarry Smith   pf->ops->setfromoptions = PFSetFromOptions_String;
137292f8084SBarry Smith   PetscFunctionReturn(0);
138292f8084SBarry Smith }
139292f8084SBarry Smith 
140292f8084SBarry Smith 
141292f8084SBarry Smith 
142292f8084SBarry Smith 
143292f8084SBarry Smith 
144