Salome HOME
Copyright update 2020
[plugins/blsurfplugin.git] / tests / multithread.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2017-2020  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 os
22 import sys
23 import salome
24 import time
25 import multiprocessing
26
27 salome.salome_init()
28
29 cpu_count = multiprocessing.cpu_count()
30 divider = 1 + (min(cpu_count, 4)-1)*0.4
31
32 if cpu_count > 2:
33     print ("Running test on workstation with %d available cores" % cpu_count)
34
35     ###
36     ### GEOM component
37     ###
38
39     import GEOM
40     from salome.geom import geomBuilder
41     import math
42     import SALOMEDS
43
44     geompy = geomBuilder.New()
45
46     O = geompy.MakeVertex(0, 0, 0)
47     OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
48     OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
49     OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
50     flight_solid_brep_1 = geompy.ImportBREP(os.path.join(os.getenv("BLSURFPLUGIN_ROOT_DIR"),"share/salome/resources/blsurfplugin/flight_solid.brep"))
51     geompy.addToStudy( O, 'O' )
52     geompy.addToStudy( OX, 'OX' )
53     geompy.addToStudy( OY, 'OY' )
54     geompy.addToStudy( OZ, 'OZ' )
55     geompy.addToStudy( flight_solid_brep_1, 'flight_solid.brep_1' )
56
57     ###
58     ### SMESH component
59     ###
60
61     import  SMESH, SALOMEDS
62     from salome.smesh import smeshBuilder
63
64     smesh = smeshBuilder.New()
65
66     Mesh_1 = smesh.Mesh(flight_solid_brep_1)
67     MG_CADSurf = Mesh_1.Triangle(algo=smeshBuilder.MG_CADSurf)
68     MG_CADSurf_Parameters_1 = MG_CADSurf.Parameters()
69     MG_CADSurf_Parameters_1.SetPhySize( 1 )
70     MG_CADSurf_Parameters_1.SetMaxSize( 1 )
71     MG_CADSurf_Parameters_1.SetGradation( 1.05 )
72     MG_CADSurf_Parameters_1.SetAngleMesh( 1 )
73     MG_CADSurf_Parameters_1.SetChordalError( 2.40018 )
74     # 4 procs are used by default
75     # => No need to set an option
76
77     time0 = time.time()
78     isDone = Mesh_1.Compute()
79     time1 = time.time()
80
81     time_multithread = time1-time0
82
83     print ("Time in multi thread (%d procs): %.3f s"%(cpu_count, time_multithread))
84
85     Mesh_2 = smesh.Mesh(flight_solid_brep_1)
86     MG_CADSurf = Mesh_2.Triangle(algo=smeshBuilder.MG_CADSurf)
87     MG_CADSurf_Parameters_2 = MG_CADSurf.Parameters()
88     MG_CADSurf_Parameters_2.SetPhySize( 1 )
89     MG_CADSurf_Parameters_2.SetMaxSize( 1 )
90     MG_CADSurf_Parameters_2.SetGradation( 1.05 )
91     MG_CADSurf_Parameters_2.SetAngleMesh( 1 )
92     MG_CADSurf_Parameters_2.SetChordalError( 2.40018 )
93     # Use only one thread
94     MG_CADSurf_Parameters_2.SetMaxNumberOfThreads( 1 )
95
96     time2 = time.time()
97     isDone = Mesh_2.Compute()
98     time3 = time.time()
99
100     time_singlethread = time3-time2
101     print ("Time in single thread (1 proc): %.3f s"%(time_singlethread))
102
103     assert time_multithread < time_singlethread/divider
104
105     if salome.sg.hasDesktop():
106       salome.sg.updateObjBrowser()
107 else:
108     print ("Warning: multithread test is disabled for %d cpu core(s)" % cpu_count)