Salome HOME
Merge branch 'V9_11_BR'
[modules/yacs.git] / src / py2yacs / Test / testDeco.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2023  CEA, EDF
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 tearDown(self):
56         cm = salome.lcc.getContainerManager()
57         cm.ShutdownContainers()
58
59     def test_t1(self):
60       """
61       Schema:
62       jdd -> foreach -> post
63            > f2
64          /
65       f1 -> f3 -> f1
66       """
67       import testforeach
68       from SALOME_PyNode import UnProxyObjectSimple
69       expected_1, expected_2 = testforeach.main()
70       yacs_schema_file = os.path.join(dir_test, "schema_t1.xml")
71       yacs_build_command = "yacsbuild.py"
72       test_script = "testforeach.py"
73       main_function_name = "main"
74       subprocess.run([yacs_build_command,
75                       test_script, main_function_name, yacs_schema_file])
76       l = loader.YACSLoader()
77       ex = pilot.ExecutorSwig()
78       proc = l.load(yacs_schema_file)
79       ex.RunW(proc,0)
80       obtained_1 = UnProxyObjectSimple( proc.getChildByName("post_0").getOutputPort("s").getPyObj() )
81       obtained_2 = UnProxyObjectSimple( proc.getChildByName("f1_1").getOutputPort("r").getPyObj() )
82       self.assertEqual(expected_1, obtained_1)
83       self.assertEqual(expected_2, obtained_2)
84
85     def test_t2(self):
86       """
87       Foreach initialized by value.
88       """
89       import testforeach
90       from SALOME_PyNode import UnProxyObjectSimple
91       expected_1, expected_2 = testforeach.mainblock()
92       yacs_schema_file = os.path.join(dir_test, "schema_t2.xml")
93       yacs_build_command = "yacsbuild.py"
94       test_script = "testforeach.py"
95       main_function_name = "mainblock"
96       subprocess.run([yacs_build_command,
97                       test_script, main_function_name, yacs_schema_file])
98       l = loader.YACSLoader()
99       ex = pilot.ExecutorSwig()
100       proc = l.load(yacs_schema_file)
101       ex.RunW(proc,0)
102       obtained_1 = UnProxyObjectSimple( proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj() )
103       obtained_2 = UnProxyObjectSimple( proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj() )
104       self.assertEqual(expected_1, obtained_1)
105       self.assertEqual(expected_2, obtained_2)
106
107     def test_t3(self):
108       """
109       Foreach on 2 levels.
110       """
111       import testforeach
112       from SALOME_PyNode import UnProxyObjectSimple
113       expected = testforeach.maindoublefr()
114       yacs_schema_file = os.path.join(dir_test, "schema_t3.xml")
115       yacs_build_command = "yacsbuild.py"
116       test_script = "testforeach.py"
117       main_function_name = "maindoublefr"
118       subprocess.run([yacs_build_command,
119                       test_script, main_function_name, yacs_schema_file])
120       l = loader.YACSLoader()
121       ex = pilot.ExecutorSwig()
122       proc = l.load(yacs_schema_file)
123       ex.RunW(proc,0)
124       obtained = UnProxyObjectSimple( proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj() )
125       self.assertEqual(expected, obtained)
126
127     def test_t4(self):
128       """
129       Using specific containers.
130       This test needs at least 4 cores declared in the catalog of resources.
131       """
132       import yacsdecorator
133       cm = yacsdecorator.ContainerManager()
134       cm.addContainer("c1", 1, False)
135       cm.addContainer("c2", 4, True)
136       cm.addContainer(yacsdecorator.ContainerManager.defaultContainerName, 1, False)
137       cont_file = os.path.join(dir_test, "containers_t4.json")
138       cm.saveFile(cont_file)
139       script = """import yacsdecorator
140 @yacsdecorator.leaf("c1")
141 def f_c1(x,y):
142   s = x + y
143   return s
144
145 @yacsdecorator.leaf("c2")
146 def f_c2(x,y):
147   p = x * y
148   return p
149
150 @yacsdecorator.leaf
151 def f_def(x,y):
152   d = x - y
153   return d
154
155 @yacsdecorator.block
156 def main():
157   s1 = f_c1(3,4)
158   p1 = f_c2(5,6)
159   r = f_def(p1, s1)
160 """
161       script_file = os.path.join(dir_test, "script_t4.py")
162       with open(script_file, "w") as f:
163         f.write(script)
164       yacs_build_command = "yacsbuild.py"
165       main_function_name = "main"
166       yacs_schema_file = os.path.join(dir_test, "schema_t4.xml")
167       subprocess.run([yacs_build_command,
168                       script_file, main_function_name, yacs_schema_file,
169                       "-c", cont_file])
170       l = loader.YACSLoader()
171       ex = pilot.ExecutorSwig()
172       proc = l.load(yacs_schema_file)
173       ex.RunW(proc,0)
174       self.assertEqual(proc.getState(),pilot.DONE)
175       c1 = proc.getChildByName("f_c1_0").getContainer()
176       self.assertFalse(c1.isUsingPythonCache())
177       self.assertEqual(c1.getProperty("nb_parallel_procs"), "1")
178       c2 = proc.getChildByName("f_c2_0").getContainer()
179       self.assertTrue(c2.isUsingPythonCache())
180       self.assertEqual(c2.getProperty("nb_parallel_procs"), "4")
181       c3 = proc.getChildByName("f_def_0").getContainer()
182       self.assertFalse(c3.isUsingPythonCache())
183       self.assertEqual(c3.getProperty("nb_parallel_procs"), "1")
184
185 if __name__ == '__main__':
186   file_test = os.path.join(dir_test,"UnitTestsResult")
187   with open(file_test, 'a') as f:
188       f.write("  --- TEST src/py2yacs: testDeco.py\n")
189       suite = unittest.makeSuite(TestDeco)
190       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
191   sys.exit(not result.wasSuccessful())