Salome HOME
Adapt multithread test to work on arbitrary nb of CPU cores - another attempt
[plugins/blsurfplugin.git] / tests / multithread.py
1 # -*- coding: utf-8 -*-
2
3 import os
4 import sys
5 import salome
6 import time
7 import multiprocessing
8
9 salome.salome_init()
10 theStudy = salome.myStudy
11
12 cpu_count = multiprocessing.cpu_count()
13 divider = min(4, cpu_count)/2.
14
15 print "Running test on workstation with %d available cores" % cpu_count
16
17 ###
18 ### GEOM component
19 ###
20
21 import GEOM
22 from salome.geom import geomBuilder
23 import math
24 import SALOMEDS
25
26
27 geompy = geomBuilder.New(theStudy)
28
29 O = geompy.MakeVertex(0, 0, 0)
30 OX = geompy.MakeVectorDXDYDZ(1, 0, 0)
31 OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
32 OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
33 flight_solid_brep_1 = geompy.ImportBREP(os.path.join(os.getenv("DATA_DIR"),"Shapes/Brep/flight_solid.brep" ))
34 geompy.addToStudy( O, 'O' )
35 geompy.addToStudy( OX, 'OX' )
36 geompy.addToStudy( OY, 'OY' )
37 geompy.addToStudy( OZ, 'OZ' )
38 geompy.addToStudy( flight_solid_brep_1, 'flight_solid.brep_1' )
39
40 ###
41 ### SMESH component
42 ###
43
44 import  SMESH, SALOMEDS
45 from salome.smesh import smeshBuilder
46
47 smesh = smeshBuilder.New(theStudy)
48
49 Mesh_1 = smesh.Mesh(flight_solid_brep_1)
50 MG_CADSurf = Mesh_1.Triangle(algo=smeshBuilder.MG_CADSurf)
51 MG_CADSurf_Parameters_1 = MG_CADSurf.Parameters()
52 MG_CADSurf_Parameters_1.SetPhySize( 1 )
53 MG_CADSurf_Parameters_1.SetMaxSize( 1 )
54 MG_CADSurf_Parameters_1.SetGradation( 1.05 )
55 MG_CADSurf_Parameters_1.SetAngleMesh( 1 )
56 MG_CADSurf_Parameters_1.SetChordalError( 2.40018 )
57 # 4 procs are used by default
58 # => No need to set an option
59
60 time0 = time.time()
61 isDone = Mesh_1.Compute()
62 time1 = time.time()
63
64 time_multithread = time1-time0
65
66 print "Time in multi thread (%d cores): %.3s"%(cpu_count, time_multithread)
67
68 Mesh_2 = smesh.Mesh(flight_solid_brep_1)
69 MG_CADSurf = Mesh_2.Triangle(algo=smeshBuilder.MG_CADSurf)
70 MG_CADSurf_Parameters_2 = MG_CADSurf.Parameters()
71 MG_CADSurf_Parameters_2.SetPhySize( 1 )
72 MG_CADSurf_Parameters_2.SetMaxSize( 1 )
73 MG_CADSurf_Parameters_2.SetGradation( 1.05 )
74 MG_CADSurf_Parameters_2.SetAngleMesh( 1 )
75 MG_CADSurf_Parameters_2.SetChordalError( 2.40018 )
76 # Use only one thread
77 MG_CADSurf_Parameters_2.SetMaxNumberOfThreads( 1 )
78
79 time2 = time.time()
80 isDone = Mesh_2.Compute()
81 time3 = time.time()
82
83 time_singlethread = time3-time2
84 print "Time in single thread (1 proc): %.3s"%(time_singlethread)
85
86 if cpu_count == 1:
87     print "Warning: cannot validate test - only 1 cpu core is available"
88 else:
89     assert time_multithread < time_singlethread/divider
90
91 if salome.sg.hasDesktop():
92   salome.sg.updateObjBrowser(True)