]> SALOME platform Git repositories - tools/ydefx.git/commitdiff
Salome HOME
Local study with examples.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 28 Oct 2020 10:42:11 +0000 (11:42 +0100)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Wed, 28 Oct 2020 10:42:11 +0000 (11:42 +0100)
src/pydefx/CMakeLists.txt
src/pydefx/configuration.py
src/pydefx/execconfiguration.py [deleted file]
src/pydefx/localstudy.py
src/pyexample/multijob/jobstudy.py [new file with mode: 0644]
src/pyexample/multijob/launch_local_basic.py
src/pyexample/multijob/launch_local_jobs.py [new file with mode: 0755]
src/pyexample/multijob/simple_command.sh [new file with mode: 0755]

index abb06320d6d769772ed1e5e257bce7d07f400ffc..e5c500ed09aa6c2f2d8f6b66465686927abcf25d 100644 (file)
@@ -36,11 +36,8 @@ SET(SCRIPTS
   slurmstudy.py
   localbuilder.py
   localstudy.py
-  execconfiguration.py
   )
 
 INSTALL(FILES ${SCRIPTS} DESTINATION ${SALOME_INSTALL_PYTHON}/pydefx)
 ADD_SUBDIRECTORY(schemas)
 ADD_SUBDIRECTORY(plugins)
-#ADD_SUBDIRECTORY(multijob)
-#ADD_SUBDIRECTORY(slurm)
index 27d5ff002067a8273b0aff702b9b6a3a9ba1b4cc..4e07e822463c504365cc468fa3519764347337e1 100644 (file)
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
 from . import salome_proxy
+from . import parameters
 import tempfile
 import pathlib
+import os
+import json
 
 def defaultWorkingDir(resource):
   resManager = salome_proxy.getResourcesManager()
@@ -57,3 +60,40 @@ def availableResources():
   # GetFittingResources returns a tuple if in no salome session mode.
   # Force to list for uniformity between the two modes.
   return list(resManager.GetFittingResources(params.resource_required))
+
+def exportConfig(dicconfig, directory = None):
+  """ Save the configuration to a directory.
+      dicconfig is a dictionary which contains the parameters to be saved.
+      If directory is None, the configuration is saved to the current directory.
+  """
+  if directory is None:
+    directory = os.getcwd()
+  configpath = os.path.join(directory, "idefixconfig.json")
+  with open(configpath, "w") as f:
+    json.dump(dicconfig, f, indent=2)
+
+def loadConfig(directory = None):
+  """ Return the configuration dictionary from a directory.
+      If the directory is None, use the current directory.
+  """
+  if directory is None:
+    directory = os.getcwd()
+  configpath = os.path.join(directory, "idefixconfig.json")
+  with open("idefixconfig.json", "r") as f:
+    config = json.load(f)
+  return config
+
+def loadJobConfig(directory = None):
+  """ Return the salome job parameters loaded from a directory which contains
+      a idefixconfig.json file.
+      If the directory is None, use the current directory.
+  """
+  result = None
+  try:
+    config = loadConfig(directory)
+    params = parameters.Parameters()
+    params.loadDict(config["params"])
+    result = params.salome_parameters
+  except:
+    result = None
+  return result
diff --git a/src/pydefx/execconfiguration.py b/src/pydefx/execconfiguration.py
deleted file mode 100644 (file)
index a6cc5bc..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-# -*- coding: utf-8 -*-
-# Copyright (C) 2019  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 json
-from . import parameters
-
-def SalomeParameters():
-  """
-  This function can be called during the evaluation of a point in order to get
-  the parameters of the job.
-  """
-  result = None
-  try:
-    with open("idefixconfig.json", "r") as f:
-      config = json.load(f)
-    params = parameters.Parameters()
-    params.loadDict(config["params"])
-    result = params.salome_parameters
-  except:
-    result = None
-  return result
-
-def GetConfig():
-  with open("idefixconfig.json", "r") as f:
-    config = json.load(f)
-  return config
index 0d046eef6206a4e07c21f03ca9e02cd380e3f8a1..e733cb8742e823211888e0e68179442d1aa4fb5b 100644 (file)
@@ -27,7 +27,7 @@ from . import salome_proxy
 
 class LocalStudy(pystudy.PyStudy):
   """
-  This study uses one different job for each evaluation.
+  This study is always localy evaluated.
   """
   def __init__(self, sampleManager=None, schemaBuilder=None):
     if schemaBuilder is None:
diff --git a/src/pyexample/multijob/jobstudy.py b/src/pyexample/multijob/jobstudy.py
new file mode 100644 (file)
index 0000000..6da5d38
--- /dev/null
@@ -0,0 +1,27 @@
+import pydefx.configuration
+import pydefx.salome_proxy
+import os
+import time
+
+def _exec(n):
+  # get the job parameters
+  salome_parameters = pydefx.configuration.loadJobConfig()
+  
+  launcher = pydefx.salome_proxy.getLauncher() # CORBA or not CORBA
+  
+  # have a different working directory for each computation
+  resource = salome_parameters.resource_required.name
+  default_wd = pydefx.configuration.defaultWorkingDir(resource)
+  new_wd = os.path.join(default_wd, "myjob_"+str(n))
+  salome_parameters.work_directory = new_wd
+  
+  # create and launch the job
+  job_id = launcher.createJob(salome_parameters)
+  launcher.launchJob(job_id)
+  
+  # wait for the end of the job
+  jobState = launcher.getJobState(job_id)
+  while jobState != "FINISHED" and jobState != "FAILED" :
+    time.sleep(5)
+    jobState = launcher.getJobState(job_id)
+  return jobState
index 13687dc936dbe62384f951d11bf264a1af7cb75d..282006b89923af35b2c9620299d1f744e96f78f0 100755 (executable)
@@ -3,16 +3,11 @@ import os
 
 myParams = pydefx.Parameters()
 myParams.configureResource("localhost")
-#myParams.createResultDirectory("/tmp")
 myParams.nb_branches = 4
 myParams.salome_parameters.resource_required.nb_proc = 1
-#myParams.salome_parameters.result_directory=os.path.join(os.getcwd(),"runbasic")
-#myParams.salome_parameters.work_directory="/scratch/I35256/workingdir/test_multijob/"
 myParams.salome_parameters.work_directory=os.path.join(os.getcwd(),"runbasic")
 myParams.salome_parameters.local_directory = os.getcwd()
-#myParams.salome_parameters.in_files=["template_jdd.txt", "mysolver.py"]
 
-#pyScript = os.path.join(os.getcwd(), "mystudy.py")
 pyScript = """
 def _exec(a,b):
   d = a / b
@@ -20,11 +15,9 @@ def _exec(a,b):
 """
 
 myScript = pydefx.PyScript()
-#myScript.loadFile(pyScript)
 myScript.loadString(pyScript)
 
 mySample = myScript.CreateEmptySample()
-#mydata = {"x":range(10)}
 mydata = {"a":[x // 10 for x in range(100)],
           "b":[x % 10 for x in range(100)]}
 mySample.setInputValues(mydata)
@@ -37,4 +30,3 @@ myStudy.getJobState()
 myStudy.wait()
 print(myStudy.getResult())
 print(myStudy.sample)
-#print(myStudy.global_result)
diff --git a/src/pyexample/multijob/launch_local_jobs.py b/src/pyexample/multijob/launch_local_jobs.py
new file mode 100755 (executable)
index 0000000..4017892
--- /dev/null
@@ -0,0 +1,31 @@
+import pydefx
+import os
+
+myParams = pydefx.Parameters()
+myParams.nb_branches = 4
+myParams.salome_parameters.work_directory=os.path.join(os.getcwd(),"runbasic")
+myParams.salome_parameters.local_directory = os.getcwd()
+myParams.salome_parameters.resource_required.nb_proc = 1
+myParams.salome_parameters.job_name = "basic_job"
+myParams.salome_parameters.job_type = "command"
+myParams.salome_parameters.job_file = os.path.join(os.getcwd(), "simple_command.sh")
+myParams.salome_parameters.resource_required.name = "eole"
+myParams.salome_parameters.wckey = "P11N0:SALOME"
+
+myScript = pydefx.PyScript()
+
+pyScript = os.path.join(os.getcwd(), "jobstudy.py")
+myScript.loadFile(pyScript)
+
+mySample = myScript.CreateEmptySample()
+mydata = {"n":range(10)}
+mySample.setInputValues(mydata)
+
+myStudy = pydefx.LocalStudy(schemaBuilder=pydefx.LocalBuilder("lightexecutor"))
+myStudy.createNewJob(myScript, mySample, myParams)
+myStudy.launch()
+
+myStudy.getJobState()
+myStudy.wait()
+print(myStudy.getResult())
+print(myStudy.sample)
diff --git a/src/pyexample/multijob/simple_command.sh b/src/pyexample/multijob/simple_command.sh
new file mode 100755 (executable)
index 0000000..9e2740c
--- /dev/null
@@ -0,0 +1 @@
+ls