Salome HOME
Copyright update 2021
[modules/yacs.git] / src / py2yacs / Test / testDeco.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2021  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import unittest
22 import tempfile
23 import os
24 import subprocess
25
26 import SALOMERuntime
27 import loader
28 import pilot
29 import salome
30
31 dir_test = tempfile.mkdtemp(suffix=".yacstest")
32
33 class TestDeco(unittest.TestCase):
34
35     def setUp(self):
36       SALOMERuntime.RuntimeSALOME_setRuntime()
37       # We need a catalog which contains only one resource named "localhost"
38       # with 16 cores. The modifications made here are not saved to the
39       # catalog file.
40       NB_NODE = 16
41       salome.salome_init()
42       resourceManager = salome.lcc.getResourcesManager()
43       resource_definition = resourceManager.GetResourceDefinition("localhost")
44       resource_definition.nb_node = NB_NODE
45       resourceManager.AddResource(resource_definition, False, "")
46       resource_required = salome.ResourceParameters()
47       resource_required.can_run_containers = True
48       res_list = resourceManager.GetFittingResources(resource_required)
49       for r in res_list:
50         if r != "localhost":
51           resourceManager.RemoveResource(r, False, "")
52       resource_definition = resourceManager.GetResourceDefinition("localhost")
53       self.assertEqual(resource_definition.nb_node, NB_NODE)
54
55     def test_t1(self):
56       """
57       Schema:
58       jdd -> foreach -> post
59            > f2
60          /
61       f1 -> f3 -> f1
62       """
63       import testforeach
64       expected_1, expected_2 = testforeach.main()
65       yacs_schema_file = os.path.join(dir_test, "schema_t1.xml")
66       yacs_build_command = "yacsbuild.py"
67       test_script = "testforeach.py"
68       main_function_name = "main"
69       subprocess.run([yacs_build_command,
70                       test_script, main_function_name, yacs_schema_file])
71       l = loader.YACSLoader()
72       ex = pilot.ExecutorSwig()
73       proc = l.load(yacs_schema_file)
74       ex.RunW(proc,0)
75       obtained_1 = proc.getChildByName("post_0").getOutputPort("s").getPyObj()
76       obtained_2 = proc.getChildByName("f1_1").getOutputPort("r").getPyObj()
77       self.assertEqual(expected_1, obtained_1)
78       self.assertEqual(expected_2, obtained_2)
79
80     def test_t2(self):
81       """
82       Foreach initialized by value.
83       """
84       import testforeach
85       expected_1, expected_2 = testforeach.mainbloc()
86       yacs_schema_file = os.path.join(dir_test, "schema_t2.xml")
87       yacs_build_command = "yacsbuild.py"
88       test_script = "testforeach.py"
89       main_function_name = "mainbloc"
90       subprocess.run([yacs_build_command,
91                       test_script, main_function_name, yacs_schema_file])
92       l = loader.YACSLoader()
93       ex = pilot.ExecutorSwig()
94       proc = l.load(yacs_schema_file)
95       ex.RunW(proc,0)
96       obtained_1 = proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj()
97       obtained_2 = proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj()
98       self.assertEqual(expected_1, obtained_1)
99       self.assertEqual(expected_2, obtained_2)
100
101     def test_t3(self):
102       """
103       Foreach on 2 levels.
104       """
105       import testforeach
106       expected = testforeach.maindoublefr()
107       yacs_schema_file = os.path.join(dir_test, "schema_t3.xml")
108       yacs_build_command = "yacsbuild.py"
109       test_script = "testforeach.py"
110       main_function_name = "maindoublefr"
111       subprocess.run([yacs_build_command,
112                       test_script, main_function_name, yacs_schema_file])
113       l = loader.YACSLoader()
114       ex = pilot.ExecutorSwig()
115       proc = l.load(yacs_schema_file)
116       ex.RunW(proc,0)
117       obtained = proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj()
118       self.assertEqual(expected, obtained)
119
120     def test_t4(self):
121       """
122       Using specific containers.
123       This test needs at least 4 cores declared in the catalog of resources.
124       """
125       import yacsdecorator
126       cm = yacsdecorator.ContainerManager()
127       cm.addContainer("c1", 1, False)
128       cm.addContainer("c2", 4, True)
129       cm.addContainer(yacsdecorator.ContainerManager.defaultContainerName, 1, False)
130       cont_file = os.path.join(dir_test, "containers_t4.json")
131       cm.saveFile(cont_file)
132       script = """import yacsdecorator
133 @yacsdecorator.leaf("c1")
134 def f_c1(x,y):
135   s = x + y
136   return s
137
138 @yacsdecorator.leaf("c2")
139 def f_c2(x,y):
140   p = x * y
141   return p
142
143 @yacsdecorator.leaf
144 def f_def(x,y):
145   d = x - y
146   return d
147
148 @yacsdecorator.bloc
149 def main():
150   s1 = f_c1(3,4)
151   p1 = f_c2(5,6)
152   r = f_def(p1, s1)
153 """
154       script_file = os.path.join(dir_test, "script_t4.py")
155       with open(script_file, "w") as f:
156         f.write(script)
157       yacs_build_command = "yacsbuild.py"
158       main_function_name = "main"
159       yacs_schema_file = os.path.join(dir_test, "schema_t4.xml")
160       subprocess.run([yacs_build_command,
161                       script_file, main_function_name, yacs_schema_file,
162                       "-c", cont_file])
163       l = loader.YACSLoader()
164       ex = pilot.ExecutorSwig()
165       proc = l.load(yacs_schema_file)
166       ex.RunW(proc,0)
167       self.assertEqual(proc.getState(),pilot.DONE)
168       c1 = proc.getChildByName("f_c1_0").getContainer()
169       self.assertFalse(c1.isUsingPythonCache())
170       self.assertEqual(c1.getProperty("nb_parallel_procs"), "1")
171       c2 = proc.getChildByName("f_c2_0").getContainer()
172       self.assertTrue(c2.isUsingPythonCache())
173       self.assertEqual(c2.getProperty("nb_parallel_procs"), "4")
174       c3 = proc.getChildByName("f_def_0").getContainer()
175       self.assertFalse(c3.isUsingPythonCache())
176       self.assertEqual(c3.getProperty("nb_parallel_procs"), "1")
177
178 if __name__ == '__main__':
179   file_test = os.path.join(dir_test,"UnitTestsResult")
180   with open(file_test, 'a') as f:
181       f.write("  --- TEST src/py2yacs: testDeco.py\n")
182       suite = unittest.makeSuite(TestDeco)
183       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
184   sys.exit(not result.wasSuccessful())