xref: /petsc/src/vec/pf/impls/string/cstring.c (revision 3405e92ce0a429885711dcb2a39f6cd8565d5879)
1 
2 #include <../src/vec/pf/pfimpl.h> /*I "petscpf.h" I*/
3 
4 /*
5         This PF generates a function on the fly and loads it into the running
6    program.
7 */
8 
9 static PetscErrorCode PFView_String(void *value, PetscViewer viewer)
10 {
11   PetscBool iascii;
12 
13   PetscFunctionBegin;
14   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
15   if (iascii) PetscCall(PetscViewerASCIIPrintf(viewer, "String = %s\n", (char *)value));
16   PetscFunctionReturn(PETSC_SUCCESS);
17 }
18 
19 static PetscErrorCode PFDestroy_String(void *value)
20 {
21   PetscFunctionBegin;
22   PetscCall(PetscFree(value));
23   PetscFunctionReturn(PETSC_SUCCESS);
24 }
25 
26 static PetscErrorCode PFSetFromOptions_String(PF pf, PetscOptionItems *PetscOptionsObject)
27 {
28   PetscBool flag;
29   char      value[PETSC_MAX_PATH_LEN];
30 
31   PetscFunctionBegin;
32   PetscOptionsHeadBegin(PetscOptionsObject, "String function options");
33   PetscCall(PetscOptionsString("-pf_string", "Enter the function", "PFStringCreateFunction", "", value, sizeof(value), &flag));
34   if (flag) PetscCall(PFStringSetFunction(pf, value));
35   PetscOptionsHeadEnd();
36   PetscFunctionReturn(PETSC_SUCCESS);
37 }
38 
39 /*@C
40   PFStringSetFunction - Creates a function from a string
41 
42   Collective
43 
44   Input Parameters:
45 + pf     - the function object
46 - string - the string that defines the function
47 
48   Level: intermediate
49 
50   Developer Notes:
51   Currently this can be used only ONCE in a running code. It needs to be fixed to generate a
52   new library name for each new function added.
53 
54   Requires `PETSC_HAVE_POPEN` `PETSC_USE_SHARED_LIBRARIES` `PETSC_HAVE_DYNAMIC_LIBRARIES` to use
55 
56 .seealso: `PFSetFromOptions()`
57 @*/
58 PetscErrorCode PFStringSetFunction(PF pf, const char *string)
59 {
60   char      task[1024], tmp[PETSC_MAX_PATH_LEN], lib[PETSC_MAX_PATH_LEN];
61   PetscBool tmpshared, wdshared, keeptmpfiles = PETSC_FALSE;
62   MPI_Comm  comm;
63   FILE     *fd;
64   char     *data;
65   PetscErrorCode (*f)(void *, PetscInt, const PetscScalar *, PetscScalar *);
66 
67   PetscFunctionBegin;
68   PetscCall(PetscObjectChangeTypeName((PetscObject)pf, PFSTRING));
69   /* create the new C function and compile it */
70   PetscCall(PetscSharedTmp(PetscObjectComm((PetscObject)pf), &tmpshared));
71   PetscCall(PetscSharedWorkingDirectory(PetscObjectComm((PetscObject)pf), &wdshared));
72   if (tmpshared) { /* do it in /tmp since everyone has one */
73     PetscCall(PetscGetTmp(PetscObjectComm((PetscObject)pf), tmp, PETSC_STATIC_ARRAY_LENGTH(tmp)));
74     PetscCall(PetscObjectGetComm((PetscObject)pf, &comm));
75   } else if (!wdshared) { /* each one does in private /tmp */
76     PetscCall(PetscGetTmp(PetscObjectComm((PetscObject)pf), tmp, PETSC_STATIC_ARRAY_LENGTH(tmp)));
77     comm = PETSC_COMM_SELF;
78   } else { /* do it in current directory */
79     PetscCall(PetscStrncpy(tmp, ".", sizeof(tmp)));
80     PetscCall(PetscObjectGetComm((PetscObject)pf, &comm));
81   }
82   PetscCall(PetscOptionsGetBool(((PetscObject)pf)->options, ((PetscObject)pf)->prefix, "-pf_string_keep_files", &keeptmpfiles, NULL));
83   PetscCall(PetscSNPrintf(task, PETSC_STATIC_ARRAY_LENGTH(task), "cd %s ; if [ ! -d ${USERNAME} ]; then mkdir ${USERNAME}; fi ; cd ${USERNAME} ; rm -f makefile petscdlib.* ; cp -f ${PETSC_DIR}/src/vec/pf/impls/string/makefile ./makefile ; ${PETSC_MAKE} NIN=%" PetscInt_FMT " NOUT=%" PetscInt_FMT " -f makefile libpetscdlib STRINGFUNCTION=\"%s\"  %s ;  sync\n", tmp, pf->dimin, pf->dimout, string, keeptmpfiles ? "; rm -f makefile petscdlib.c" : ""));
84 
85   PetscCall(PetscPOpen(comm, NULL, task, "r", &fd));
86   PetscCall(PetscPClose(comm, fd));
87   PetscCallMPI(MPI_Barrier(comm));
88 
89   /* load the apply function from the dynamic library */
90   PetscCall(PetscSNPrintf(lib, PETSC_STATIC_ARRAY_LENGTH(lib), "%s/${USERNAME}/libpetscdlib", tmp));
91   PetscCall(PetscDLLibrarySym(comm, NULL, lib, "PFApply_String", (void **)&f));
92   PetscCheck(f, PetscObjectComm((PetscObject)pf), PETSC_ERR_ARG_WRONGSTATE, "Cannot find function %s", lib);
93 
94   PetscCall(PetscFree(pf->data));
95   PetscCall(PetscStrallocpy(string, (char **)&data));
96   PetscCall(PFSet(pf, f, NULL, PFView_String, PFDestroy_String, data));
97   pf->ops->setfromoptions = PFSetFromOptions_String;
98   PetscFunctionReturn(PETSC_SUCCESS);
99 }
100 
101 PETSC_EXTERN PetscErrorCode PFCreate_String(PF pf, void *value)
102 {
103   PetscFunctionBegin;
104   PetscCall(PFStringSetFunction(pf, (const char *)value));
105   PetscFunctionReturn(PETSC_SUCCESS);
106 }
107