<s> import os <EOL> from mpi import MPI , collective <EOL> from mpi4py import MPI as _MPI <EOL> import subprocess <EOL> import sys <EOL> import ctypes <EOL> from hashlib import md5 <EOL> from configuration import configuration <EOL> from logger import progress , INFO <EOL> from exceptions import CompilationError <EOL> def _check_hashes ( x , y , datatype ) : <EOL> """<STR_LIT>""" <EOL> if x == y : <EOL> return x <EOL> return False <EOL> _check_op = _MPI . Op . Create ( _check_hashes , commute = True ) <EOL> class Compiler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cc , ld = None , cppargs = [ ] , ldargs = [ ] , <EOL> cpp = False ) : <EOL> ccenv = '<STR_LIT>' if cpp else '<STR_LIT>' <EOL> self . _cc = os . environ . get ( ccenv , cc ) <EOL> self . _ld = os . environ . get ( '<STR_LIT>' , ld ) <EOL> self . _cppargs = cppargs <EOL> self . _ldargs = ldargs <EOL> @ collective <EOL> def get_so ( self , src , extension ) : <EOL> """<STR_LIT>""" <EOL> hsh = md5 ( src ) <EOL> hsh . update ( self . _cc ) <EOL> if self . _ld : <EOL> hsh . update ( self . _ld ) <EOL> hsh . update ( "<STR_LIT>" . join ( self . _cppargs ) ) <EOL> hsh . update ( "<STR_LIT>" . join ( self . _ldargs ) ) <EOL> basename = hsh . hexdigest ( ) <EOL> cachedir = configuration [ '<STR_LIT>' ] <EOL> pid = os . getpid ( ) <EOL> cname = os . path . join ( cachedir , "<STR_LIT>" % ( basename , pid , extension ) ) <EOL> oname = os . path . join ( cachedir , "<STR_LIT>" % ( basename , pid ) ) <EOL> soname = os . path . join ( cachedir , "<STR_LIT>" % basename ) <EOL> tmpname = os . path . join ( cachedir , "<STR_LIT>" % ( basename , pid ) ) <EOL> if configuration [ '<STR_LIT>' ] or configuration [ '<STR_LIT>' ] : <EOL> matching = MPI . comm . allreduce ( basename , op = _check_op ) <EOL> if matching != basename : <EOL> output = os . path . join ( cachedir , "<STR_LIT>" ) <EOL> srcfile = os . path . join ( output , "<STR_LIT>" % MPI . comm . rank ) <EOL> if MPI . comm . rank == <NUM_LIT:0> : <EOL> if not os . path . exists ( output ) : <EOL> os . makedirs ( output ) <EOL> MPI . comm . barrier ( ) <EOL> with open ( srcfile , "<STR_LIT:w>" ) as f : <EOL> f . write ( src ) <EOL> MPI . comm . barrier ( ) <EOL> raise CompilationError ( "<STR_LIT>" % output ) <EOL> try : <EOL> return ctypes . CDLL ( soname ) <EOL> except OSError : <EOL> if MPI . comm . rank == <NUM_LIT:0> : <EOL> if not os . path . exists ( cachedir ) : <EOL> os . makedirs ( cachedir ) <EOL> logfile = os . path . join ( cachedir , "<STR_LIT>" % ( basename , pid ) ) <EOL> errfile = os . path . join ( cachedir , "<STR_LIT>" % ( basename , pid ) ) <EOL> with progress ( INFO , '<STR_LIT>' ) : <EOL> with file ( cname , "<STR_LIT:w>" ) as f : <EOL> f . write ( src ) <EOL> if self . _ld is None : <EOL> cc = [ self . _cc ] + self . _cppargs + [ '<STR_LIT>' , tmpname , cname ] + self . _ldargs <EOL> with file ( logfile , "<STR_LIT:w>" ) as log : <EOL> with file ( errfile , "<STR_LIT:w>" ) as err : <EOL> log . write ( "<STR_LIT>" ) <EOL> log . write ( "<STR_LIT:U+0020>" . join ( cc ) ) <EOL> log . write ( "<STR_LIT>" ) <EOL> try : <EOL> if configuration [ '<STR_LIT>' ] : <EOL> cc += [ "<STR_LIT>" , errfile , "<STR_LIT:>>" , logfile ] <EOL> cmd = "<STR_LIT:U+0020>" . join ( cc ) <EOL> status = os . system ( cmd ) <EOL> if status != <NUM_LIT:0> : <EOL> raise subprocess . CalledProcessError ( status , cmd ) <EOL> else : <EOL> subprocess . check_call ( cc , stderr = err , <EOL> stdout = log ) <EOL> except subprocess . CalledProcessError as e : <EOL> raise CompilationError ( <EOL> """<STR_LIT>""" % ( e . cmd , e . returncode , logfile , errfile ) ) <EOL> else : <EOL> cc = [ self . _cc ] + self . _cppargs + [ '<STR_LIT:-c>' , '<STR_LIT>' , oname , cname ] <EOL> ld = self . _ld . split ( ) + [ '<STR_LIT>' , tmpname , oname ] + self . _ldargs <EOL> with file ( logfile , "<STR_LIT:w>" ) as log : <EOL> with file ( errfile , "<STR_LIT:w>" ) as err : <EOL> log . write ( "<STR_LIT>" ) <EOL> log . write ( "<STR_LIT:U+0020>" . join ( cc ) ) <EOL> log . write ( "<STR_LIT>" ) <EOL> log . write ( "<STR_LIT>" ) <EOL> log . write ( "<STR_LIT:U+0020>" . join ( ld ) ) <EOL> log . write ( "<STR_LIT>" ) <EOL> try : <EOL> if configuration [ '<STR_LIT>' ] : <EOL> cc += [ "<STR_LIT>" , errfile , "<STR_LIT:>>" , logfile ] <EOL> ld += [ "<STR_LIT>" , errfile , "<STR_LIT:>>" , logfile ] <EOL> cccmd = "<STR_LIT:U+0020>" . join ( cc ) <EOL> ldcmd = "<STR_LIT:U+0020>" . join ( ld ) <EOL> status = os . system ( cccmd ) <EOL> if status != <NUM_LIT:0> : <EOL> raise subprocess . CalledProcessError ( status , cccmd ) <EOL> status = os . system ( ldcmd ) <EOL> if status != <NUM_LIT:0> : <EOL> raise subprocess . CalledProcessError ( status , ldcmd ) <EOL> else : <EOL> subprocess . check_call ( cc , stderr = err , <EOL> stdout = log ) <EOL> subprocess . check_call ( ld , stderr = err , <EOL> stdout = log ) <EOL> except subprocess . CalledProcessError as e : <EOL> raise CompilationError ( <EOL> """<STR_LIT>""" % ( e . cmd , e . returncode , logfile , errfile ) ) <EOL> os . rename ( tmpname , soname ) <EOL> MPI . comm . barrier ( ) <EOL> return ctypes . CDLL ( soname ) <EOL> class MacCompiler ( Compiler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cppargs = [ ] , ldargs = [ ] , cpp = False ) : <EOL> opt_flags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if configuration [ '<STR_LIT>' ] : <EOL> opt_flags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cc = "<STR_LIT>" <EOL> stdargs = [ "<STR_LIT>" ] <EOL> if cpp : <EOL> cc = "<STR_LIT>" <EOL> stdargs = [ ] <EOL> cppargs = stdargs + [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] + opt_flags + cppargs <EOL> ldargs = [ '<STR_LIT>' ] + ldargs <EOL> super ( MacCompiler , self ) . __init__ ( cc , <EOL> cppargs = cppargs , <EOL> ldargs = ldargs , <EOL> cpp = cpp ) <EOL> class LinuxCompiler ( Compiler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cppargs = [ ] , ldargs = [ ] , cpp = False ) : <EOL> opt_flags = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if configuration [ '<STR_LIT>' ] : <EOL> opt_flags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cc = "<STR_LIT>" <EOL> stdargs = [ "<STR_LIT>" ] <EOL> if cpp : <EOL> cc = "<STR_LIT>" <EOL> stdargs = [ ] <EOL> cppargs = stdargs + [ '<STR_LIT>' , '<STR_LIT>' ] + opt_flags + cppargs <EOL> ldargs = [ '<STR_LIT>' ] + ldargs <EOL> super ( LinuxCompiler , self ) . __init__ ( cc , cppargs = cppargs , ldargs = ldargs , <EOL> cpp = cpp ) <EOL> class LinuxIntelCompiler ( Compiler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cppargs = [ ] , ldargs = [ ] , cpp = False ) : <EOL> opt_flags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if configuration [ '<STR_LIT>' ] : <EOL> opt_flags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> cc = "<STR_LIT>" <EOL> stdargs = [ "<STR_LIT>" ] <EOL> if cpp : <EOL> cc = "<STR_LIT>" <EOL> stdargs = [ ] <EOL> cppargs = stdargs + [ '<STR_LIT>' , '<STR_LIT>' ] + opt_flags + cppargs <EOL> ldargs = [ '<STR_LIT>' ] + ldargs <EOL> super ( LinuxIntelCompiler , self ) . __init__ ( cc , cppargs = cppargs , ldargs = ldargs , <EOL> cpp = cpp ) <EOL> @ collective <EOL> def load ( src , extension , fn_name , cppargs = [ ] , ldargs = [ ] , argtypes = None , restype = None , compiler = None ) : <EOL> """<STR_LIT>""" <EOL> platform = sys . platform <EOL> cpp = extension == "<STR_LIT>" <EOL> if platform . find ( '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> if compiler == '<STR_LIT>' : <EOL> compiler = LinuxIntelCompiler ( cppargs , ldargs , cpp = cpp ) <EOL> else : <EOL> compiler = LinuxCompiler ( cppargs , ldargs , cpp = cpp ) <EOL> elif platform . find ( '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> compiler = MacCompiler ( cppargs , ldargs , cpp = cpp ) <EOL> else : <EOL> raise CompilationError ( "<STR_LIT>" % <EOL> platform ) <EOL> dll = compiler . get_so ( src , extension ) <EOL> fn = getattr ( dll , fn_name ) <EOL> fn . argtypes = argtypes <EOL> fn . restype = restype <EOL> return fn <EOL> def clear_cache ( prompt = False ) : <EOL> """<STR_LIT>""" <EOL> cachedir = configuration [ '<STR_LIT>' ] <EOL> if not os . path . exists ( cachedir ) : <EOL> return <EOL> files = [ os . path . join ( cachedir , f ) for f in os . listdir ( cachedir ) <EOL> if os . path . isfile ( os . path . join ( cachedir , f ) ) ] <EOL> nfiles = len ( files ) <EOL> if nfiles == <NUM_LIT:0> : <EOL> print "<STR_LIT>" <EOL> return <EOL> remove = True <EOL> if prompt : <EOL> user = raw_input ( "<STR_LIT>" % ( nfiles , cachedir ) ) <EOL> while user . lower ( ) not in [ '<STR_LIT>' , '<STR_LIT:y>' , '<STR_LIT:n>' ] : <EOL> print "<STR_LIT>" <EOL> user = raw_input ( "<STR_LIT>" % ( nfiles , cachedir ) ) <EOL> if user . lower ( ) == '<STR_LIT:n>' : <EOL> remove = False <EOL> if remove : <EOL> print "<STR_LIT>" % ( nfiles , cachedir ) <EOL> [ os . remove ( f ) for f in files ] <EOL> else : <EOL> print "<STR_LIT>" </s>
