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
40 import multiprocessing as MP # cpu_count
41 import pprint as PP # pretty print
42 import subprocess as SP # Popen
52 NB_PROCS = MP.cpu_count() # current cpu number of proc
53 NAME_OS = platform.system() # 'Linux' or 'Windows'
56 ##########################################################################
58 ##########################################################################
60 def okToSys(aResult, verbose=False):
61 """to get windows or linux result of script"""
63 def extendList(alist):
64 """utility extend list of lists of string results with ok or KO"""
65 # bad: list(itertools.chain.from_list(alist)) iterate on str
67 if type(alist) != list:
74 res.extend(extendList(i))
77 resList = extendList(aResult)
79 if verbose: print("WARNING: result no clear: []")
88 if verbose: print(("INFO: result: %s" % rr))
94 ##########################################################################
95 def getDirAndName(datafile):
96 path, namefile = os.path.split(os.path.realpath(datafile))
97 rootpath = os.getcwd()
98 return (path, rootpath, namefile)
101 ##########################################################################
102 class ArgRange(object):
104 ArgumentParser utility for range float or in in arguments
106 def __init__(self, start, end):
110 def __eq__(self, other):
111 return self.start <= other <= self.end
114 return "[%s,%s]" % (self.start, self.end)
117 ##########################################################################
118 def exec_command(cmd, verbose=False):
119 """Exec ONE command with popen"""
121 time.sleep(3) # wait for (MPI) flush files
122 if verbose: print(("launch process:\n %s" % cmd))
124 pipe = SP.Popen(cmd, shell=True, stdout=SP.PIPE, stderr=SP.PIPE)
125 except Exception as e:
126 result = KO + " ERROR: we have a problem popen on: %s" % PP.pformat(cmd)
129 (out, error) = pipe.communicate()
135 result = OK + " INFO: seems final ok for: %s" % PP.pformat(cmd)
136 time.sleep(3) # wait for (MPI) flush files
141 ##########################################################################
142 def force_DISTENE_LICENSE_FILE():
144 conditionaly overriding/set environ variable DISTENE_LICENSE_FILE,
145 from/if existing FORCE_DISTENE_LICENSE_FILE environ variable
146 (for test new version MeshGems etc...)
150 DISTENE_LICENSE_FILE=Use global envvar: DLIM8VAR
151 DLIM8VAR=dlim8 1:1:29030@132.166.151.49/84c419b8::87af196ab2a936ab31363624539bff8096fbe1f3c83028c8f6b399b0a904ef85
153 FORCE_DISTENE_LICENSE_FILE=/export/home/wambeke/essai_meshgems2.3/dlim8.key
155 force = os.getenv("FORCE_DISTENE_LICENSE_FILE")
157 os.environ["DISTENE_LICENSE_FILE"] = force
158 os.environ["DLIM8VAR"] = "NOTHING"
160 #export PATH=/export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing/bin:$PATH
161 #do not work prefer set before salome launch
162 OPENMPI_INSTALL_DIR = "/export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing/bin"
163 sys.path.insert(0, OPENMPI_INSTALL_DIR)
164 #INSTALL_DIR = /export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing
165 for i in sys.path[0:10]: print "PATH",i
171 ##########################################################################
172 def launchMultithread(args):
173 if verbose: print(("INFO: launchMultithread for %s" % NAME_OS))
175 if NAME_OS == 'Linux':
176 # --out is ONE file: basename_tetra_hpc.mesh
177 outputMulti = os.path.splitext(args.inputFile)[0] + "_tetra_hpc.mesh" # only one file if Multithread
178 outputs = os.path.splitext(args.outputFiles)[0]
179 outputMultiAsMpi = os.path.splitext(args.outputFiles)[0] + ".000001.mesh" # create one output file named as only one from mpi
180 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*; " % \
181 (args.number, args.inputFile, args.gradation, args.max_size, args.min_size, outputMulti, outputMultiAsMpi, outputs)
183 return KO + " ERROR: unknown operating system: %s" % NAME_OS
185 result = exec_command(cmd, verbose=True)
190 ##########################################################################
192 if verbose: print(("INFO: launchMpi for %s" % NAME_OS))
194 if NAME_OS == 'Linux':
197 """ compile libmeshgems_mpi.so: no needs
198 COMPILDIR=os.getenv("MESHGEMSHOME") + "/stubs"
199 TARGETDIR=os.getenv("MESHGEMSHOME") + "/lib/Linux_64"
200 cmd = "which mg-tetra_hpc_mpi.exe; which mpicc; rm /tmp/GHS3DPRL_out*; "
201 cmd += "cd %s; mpicc meshgems_mpi.c -DMESHGEMS_LINUX_BUILD -I../include -shared -fPIC -o %s/libmeshgems_mpi.so; " % (COMPILDIR, TARGETDIR)
204 outputs = os.path.splitext(args.outputFiles)[0]
205 cmd += "mpirun -n %i mg-tetra_hpc_mpi.exe --in %s --out %s --gradation %s --max_size %s --min_size %s; ls -alt %s*; " % \
206 (args.number, args.inputFile, args.outputFiles, args.gradation, args.max_size, args.min_size, outputs)
208 return KO + " ERROR: unknown operating system: %s" % NAME_OS
210 result = exec_command(cmd, verbose=True)
214 ##########################################################################
216 ##########################################################################
218 if __name__ == '__main__':
219 parser = AP.ArgumentParser(description='launch tetra_hpc.exe or tetra_hpc_mpi.exe mesh computation', argument_default=None)
220 # ./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
224 help='set verbose, for debug',
229 help='if multithread: number of threads, else distributed: number of processes MPI',
230 choices=[ArgRange(1, 999999)],
232 metavar='integer >= 0',
236 '-m', '--multithread',
237 help='launch tetra_hpc multithread instead tetra_hpc distributed (MPI)',
238 choices=["no", "yes"],
243 help='size ratio adjacent cell, default 0 is 1.05',
245 choices=[ArgRange(0.0, 3.0)],
246 metavar='float in [0.,3]',
251 help='min size cell, default 0 is no constraint',
253 choices=[ArgRange(0.0, 9e99)],
254 metavar='float >= 0',
259 help='max size cell, default 0 is no constraint',
261 choices=[ArgRange(0.0, 9e99)],
262 metavar='float >= 0',
267 help='input file name',
269 metavar='.../inputFile.mesh'
272 '-o', '--outputFiles',
273 help='output basename file(s) name',
275 metavar='.../outputFile.mesh'
279 '-x', '--xoneargument',
283 help='one argument, for example',
290 args is Namespace, may use it as global to store
291 parameters, data, used arrays and results and other...
293 args = parser.parse_args()
295 verbose = args.verbose
296 if verbose: print(("INFO: args:\n%s" % PP.pformat(args.__dict__)))
298 if len(sys.argv) == 1: # no args as --help
302 if args.inputFile == None:
303 print(("\nERROR: Nothing to do: no input files\n\n%s\n" % PP.pformat(args)))
307 if args.outputFiles == None:
308 tmp, _ = os.path.splitext(args.inputFile)
309 args.outputFiles = tmp + "_out.mesh"
310 print(("\nWARNING: Default ouput files: %s" % args.outputFiles))
312 force_DISTENE_LICENSE_FILE()
314 print(("INFO: mg-tetra_hpc.py assume licence file set:\n DLIM8VAR=%s\n DISTENE_LICENSE_FILE=%s" % \
315 (os.getenv("DLIM8VAR"), os.getenv("DISTENE_LICENSE_FILE"))))
317 if args.multithread == "yes":
318 result = launchMultithread(args)
320 result = launchMpi(args)
321 sys.exit(okToSys(result, verbose=True))