2 # -*- coding: utf-8 -*-
4 # %% LICENSE_SALOME_CEA_BEGIN
5 # Copyright (C) 2008-2016 CEA/DEN
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See http://www.salome-platform.org or email : webmaster.salome@opencascade.com
26 run mg_tetra_hpc.exe or mg_tetra_hpc_mpi.exe
30 ./mg-tetra_hpc.py --help
31 ./mg-tetra_hpc.py -n 3 --in /tmp/GHS3DPRL.mesh --out /tmp/GHS3DPRL_out.mesh --gradation 1.05 --min_size 0.001 --max_size 1.1 --multithread no > /tmp/tetrahpc.log
39 import pprint as PP #pretty print
40 import subprocess as SP #Popen
41 import multiprocessing as MP #cpu_count
50 NB_PROCS = MP.cpu_count() #current cpu number of proc
51 NAME_OS = platform.system() #'Linux' or 'Windows'
54 ##########################################################################
56 ##########################################################################
58 def okToSys(aResult, verbose=False):
59 """to get windows or linux result of script"""
61 def extendList(alist):
62 """utility extend list of lists of string results with ok or KO"""
63 #bad: list(itertools.chain.from_list(alist)) iterate on str
65 if type(alist) != list:
72 res.extend(extendList(i))
75 resList = extendList(aResult)
77 if verbose: print("WARNING: result no clear: []")
86 if verbose: print("INFO: result: %s" % rr)
92 ##########################################################################
93 def getDirAndName(datafile):
94 path, namefile = os.path.split(os.path.realpath(datafile))
95 rootpath = os.getcwd()
96 return (path, rootpath, namefile)
99 ##########################################################################
100 class ArgRange(object):
102 ArgumentParser utility for range float or in in arguments
104 def __init__(self, start, end):
108 def __eq__(self, other):
109 return self.start <= other <= self.end
112 return "[%s,%s]" % (self.start, self.end)
115 ##########################################################################
116 def exec_command(cmd, verbose=False):
117 """Exec ONE command with popen"""
119 time.sleep(3) #wait for (MPI) flush files
120 if verbose: print("launch process:\n %s" % cmd)
122 pipe = SP.Popen(cmd, shell=True, stdout=SP.PIPE, stderr=SP.PIPE )
123 except Exception as e:
124 result = KO + " ERROR: we have a problem popen on: %s" % PP.pformat(cmd)
127 (out, error) = pipe.communicate()
133 result = OK + " INFO: seems final ok for: %s" % PP.pformat(cmd)
134 time.sleep(3) #wait for (MPI) flush files
139 ##########################################################################
140 def force_DISTENE_LICENSE_FILE():
142 conditionaly overriding/set environ variable DISTENE_LICENSE_FILE,
143 from/if existing FORCE_DISTENE_LICENSE_FILE environ variable
144 (for test new version MeshGems etc...)
148 DISTENE_LICENSE_FILE=Use global envvar: DLIM8VAR
149 DLIM8VAR=dlim8 1:1:29030@132.166.151.49/84c419b8::87af196ab2a936ab31363624539bff8096fbe1f3c83028c8f6b399b0a904ef85
151 FORCE_DISTENE_LICENSE_FILE=/export/home/wambeke/essai_meshgems2.3/dlim8.key
153 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
155 os.environ["DISTENE_LICENSE_FILE"] = force
156 os.environ["DLIM8VAR"] = "NOTHING"
158 #export PATH=/export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing/bin:$PATH
159 #do not work prefer set before salome launch
160 OPENMPI_INSTALL_DIR = "/export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing/bin"
161 sys.path.insert(0, OPENMPI_INSTALL_DIR)
162 #INSTALL_DIR = /export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing
163 for i in sys.path[0:10]: print "PATH",i
169 ##########################################################################
170 def launchMultithread(args):
171 if verbose: print("INFO: launchMultithread for %s" % NAME_OS)
173 if NAME_OS == 'Linux':
174 #--out is ONE file: basename_tetra_hpc.mesh
175 outputMulti = os.path.splitext(args.inputFile)[0] + "_tetra_hpc.mesh" #only one file if Multithread
176 outputs = os.path.splitext(args.outputFiles)[0]
177 outputMultiAsMpi = os.path.splitext(args.outputFiles)[0] + ".000001.mesh" #create one output file named as only one from mpi
178 cmd = "mg-tetra_hpc.exe --max_number_of_threads %i --in %s --gradation %s --max_size %s --min_size %s; cp %s %s; ls -alt %s*; " % \
179 (args.number, args.inputFile, args.gradation, args.max_size, args.min_size, outputMulti, outputMultiAsMpi, outputs)
181 return KO + " ERROR: unknown operating system: %s" % NAME_OS
183 result = exec_command(cmd, verbose=True)
188 ##########################################################################
190 if verbose: print("INFO: launchMpi for %s" % NAME_OS)
192 if NAME_OS == 'Linux':
195 """ compile libmeshgems_mpi.so: no needs
196 COMPILDIR=os.getenv("MESHGEMSHOME") + "/stubs"
197 TARGETDIR=os.getenv("MESHGEMSHOME") + "/lib/Linux_64"
198 cmd = "which mg-tetra_hpc_mpi.exe; which mpicc; rm /tmp/GHS3DPRL_out*; "
199 cmd += "cd %s; mpicc meshgems_mpi.c -DMESHGEMS_LINUX_BUILD -I../include -shared -fPIC -o %s/libmeshgems_mpi.so; " % (COMPILDIR, TARGETDIR)
202 outputs = os.path.splitext(args.outputFiles)[0]
203 cmd += "mpirun -n %i mg-tetra_hpc_mpi.exe --in %s --out %s --gradation %s --max_size %s --min_size %s; ls -alt %s*; " % \
204 (args.number, args.inputFile, args.outputFiles, args.gradation, args.max_size, args.min_size, outputs)
206 return KO + " ERROR: unknown operating system: %s" % NAME_OS
208 result = exec_command(cmd, verbose=True)
212 ##########################################################################
214 ##########################################################################
216 if __name__ == '__main__':
217 parser = AP.ArgumentParser(description='launch tetra_hpc.exe or tetra_hpc_mpi.exe mesh computation', argument_default=None)
218 #./mg-tetra_hpc.py -n 3 --in=/tmp/GHS3DPRL.mesh --out=/tmp/GHS3DPRL_out.mesh --gradation=1.05 --min_size=0.001 --max_size=1.1 --multithread no > /tmp/tetrahpc.log
222 help='set verbose, for debug',
227 help='if multithread: number of threads, else distributed: number of processes MPI',
228 choices=[ArgRange(1, 999999)],
230 metavar='integer >= 0',
234 '-m', '--multithread',
235 help='launch tetra_hpc multithread instead tetra_hpc distributed (MPI)',
236 choices=["no", "yes"],
241 help='size ratio adjacent cell, default 0 is 1.05',
243 choices=[ArgRange(0.0, 3.0)],
244 metavar='float in [0.,3]',
249 help='min size cell, default 0 is no constraint',
251 choices=[ArgRange(0.0, 9e99)],
252 metavar='float >= 0',
257 help='max size cell, default 0 is no constraint',
259 choices=[ArgRange(0.0, 9e99)],
260 metavar='float >= 0',
265 help='input file name',
267 metavar='.../inputFile.mesh'
270 '-o', '--outputFiles',
271 help='output basename file(s) name',
273 metavar='.../outputFile.mesh'
277 '-x', '--xoneargument',
281 help='one argument, for example',
288 args is Namespace, may use it as global to store
289 parameters, data, used arrays and results and other...
291 args = parser.parse_args()
293 verbose = args.verbose
294 if verbose: print("INFO: args:\n%s" % PP.pformat(args.__dict__))
296 if len(sys.argv) == 1: #no args as --help
300 if args.inputFile == None:
301 print("\nERROR: Nothing to do: no input files\n\n%s\n" % PP.pformat(args))
305 if args.outputFiles == None:
306 tmp, _ = os.path.splitext(args.inputFile)
307 args.outputFiles = tmp + "_out.mesh"
308 print("\nWARNING: Default ouput files: %s" % args.outputFiles)
310 force_DISTENE_LICENSE_FILE()
312 print("INFO: mg-tetra_hpc.py assume licence file set:\n DLIM8VAR=%s\n DISTENE_LICENSE_FILE=%s" % \
313 (os.getenv("DLIM8VAR"), os.getenv("DISTENE_LICENSE_FILE")))
315 if args.multithread == "yes":
316 result = launchMultithread(args)
318 result = launchMpi(args)
319 sys.exit(okToSys(result, verbose=True))