]> SALOME platform Git repositories - tools/ydefx.git/commitdiff
Salome HOME
WIP
authorAnthony Geay <anthony.geay@edf.fr>
Fri, 30 Dec 2022 16:00:02 +0000 (17:00 +0100)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 30 Dec 2022 16:00:02 +0000 (17:00 +0100)
src/pydefx/CMakeLists.txt
src/pydefx/mpmcn.py [new file with mode: 0644]

index f5a2ca3dd437c76f492c30c62230b9c05ddd1f15..36174574dd803f0a75af232f436c23f5ff697e1e 100644 (file)
@@ -39,5 +39,6 @@ SET(SCRIPTS
   )
 
 INSTALL(FILES ${SCRIPTS} DESTINATION ${SALOME_INSTALL_PYTHON}/pydefx)
+install(FILES mpmcn.py DESTINATION ${SALOME_INSTALL_PYTHON})
 ADD_SUBDIRECTORY(schemas)
 ADD_SUBDIRECTORY(plugins)
diff --git a/src/pydefx/mpmcn.py b/src/pydefx/mpmcn.py
new file mode 100644 (file)
index 0000000..1232c49
--- /dev/null
@@ -0,0 +1,101 @@
+#!/usr/bin/env python3
+#  -*- coding: utf-8 -*-
+# Copyright (C) 2022 EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+
+import pydefx
+
+myParams = None
+
+class Pool:
+    def __init__(self):
+        self.myStudy = None
+        self.myScript = pydefx.PyScript()
+        self.mySample = None
+    
+    def map(self, func, iterable):
+        global myParams
+        if len(iterable) == 0:
+            return []
+        #
+        import inspect
+        if not inspect.isfunction(func):
+            raise RuntimeError("Input is expected to be a function")
+        import importlib
+        fModule = importlib.import_module(func.__module__)
+        if fModule == "__main__":
+            raise RuntimeError("Input function is expected to be part of a module")
+        st = None
+        with open(fModule.__file__,"r") as ff:
+            st = ff.read()
+        fStr = func.__code__.co_name
+        """fStr = None
+        for elt in dir(fModule):
+            if eval(elt) == f:
+                fStr = elt
+                print(fStr)
+                break"""
+        if fStr is None:
+            raise RuntimeError("Impossible to locate function in the module containing it !")
+        fArgs = inspect.getfullargspec(func).args
+        pyScript = """{}
+def _exec({}):
+    yxyx = {}({})
+    return yxyx
+""".format(st,", ".join(fArgs),fStr,", ".join(fArgs))
+        #
+        self.myScript.loadString(pyScript)
+        self.mySample = self.myScript.CreateEmptySample()
+        #
+        #print(fArgs)
+        #print(iterable)
+        #
+        if not hasattr(iterable[0],"__iter__"):
+            iterable = [[elt] for elt in iterable]
+        #
+        #print(pyScript)
+        #print({k:v for k,*v in zip(fArgs,*iterable)})
+        #
+        self.mySample.setInputValues( {k:v for k,*v in zip(fArgs,*iterable)} )
+        #
+        self.myStudy.createNewJob(self.myScript, self.mySample, myParams)
+        #
+        self.myStudy.launch()
+        self.myStudy.wait()
+        #
+        return self.myStudy.getResult()
+
+    def __enter__(self):
+        self.myStudy = pydefx.PyStudy()
+        return self
+        
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        pass
+    pass
+
+def init(resultDirectory = "/tmp"):
+    """
+    :param resourceName: Name of the resource matching one of the ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
+    :param resultDirectory: Directory used to transfer results
+    """
+    global myParams
+    myParams = pydefx.Parameters()
+    myParams.configureResource("localhost")
+    myParams.createResultDirectory(resultDirectory)
+    pass
+