def build(self, src): # Compute a digest of the current processor, compiler, and source ckey = digest(self.proc, self.version, self.cmd, src) # Attempt to load the library from the cache mod = self._cache_loadlib(ckey) # Otherwise, we need to compile the kernel if not mod: # Create a scratch directory tmpidx = next(self._dir_seq) tmpdir = tempfile.mkdtemp(prefix=f'pyfr-{tmpidx}-') try: # Compile and link the source into a shared library cname, lname = 'tmp.c', platform_libname('tmp') # Write the source code out with open(os.path.join(tmpdir, cname), 'w') as f: f.write(src) # Invoke the compiler call_capture_output(self.cc_cmd(cname, lname), cwd=tmpdir) # Determine the fully qualified library name lpath = os.path.join(tmpdir, lname) # Add it to the cache and load mod = self._cache_set_and_loadlib(ckey, lpath) finally: # Unless we're debugging delete the scratch directory if 'PYFR_DEBUG_OMP_KEEP_LIBS' not in os.environ: rm(tmpdir) return OpenMPCompilerModule(mod)
def process_unpack(args): # Load the file inf = np.load(args.inf) # Determine the dir and prefix of the input file dirname, basename = os.path.split(args.inf.name) # Get the output pyfrs directory name outdir = args.outdir or args.inf.name # Create a temporary directory tmpdir = mkdtemp(prefix=basename, dir=dirname) # Write out the files to this temporary directory try: for n, d in inf.iteritems(): np.save(os.path.join(tmpdir, n), d) # Remove the output path if it should exist if os.path.exists(outdir): os.remove(outdir) # Rename the temporary directory into place os.rename(tmpdir, outdir) except: # Clean up the temporary directory if os.path.exists(tmpdir): rm(tmpdir) # Re-raise raise
def __init__(self, src, cfg): # Find GCC (or a compatible alternative) self.cc = cfg.getpath('backend-openmp', 'cc', 'cc') # User specified compiler flags self.cflags = shlex.split(cfg.get('backend-openmp', 'cflags', '')) # Get the processor string proc = platform.processor() # Get the compiler version string version = call_capture_output([self.cc, '-v']) # Get the base compiler command strig cmd = self.cc_cmd(None, None) # Compute a digest of the current processor, compiler, and source self.digest = digest(proc, version, cmd, src) # Attempt to load the library from the cache self.mod = self._cache_loadlib() # Otherwise, we need to compile the kernel if not self.mod: # Create a scratch directory tmpidx = next(self._dir_seq) tmpdir = tempfile.mkdtemp(prefix='pyfr-{0}-'.format(tmpidx)) try: # Compile and link the source into a shared library cname, lname = 'tmp.c', platform_libname('tmp') # Write the source code out with open(os.path.join(tmpdir, cname), 'w') as f: f.write(src) # Invoke the compiler call_capture_output(self.cc_cmd(cname, lname), cwd=tmpdir) # Determine the fully qualified library name lpath = os.path.join(tmpdir, lname) # Add it to the cache and load self.mod = self._cache_set_and_loadlib(lpath) finally: # Unless we're debugging delete the scratch directory if 'PYFR_DEBUG_OMP_KEEP_LIBS' not in os.environ: rm(tmpdir)
def __init__(self, src, cfg): self._src = src self._cfg = cfg # Create a scratch directory tmpdir = tempfile.mkdtemp(prefix='pyfr-%d-' % next(self._dir_seq)) try: # Compile and link the source lname = self._build(tmpdir) # Load self._mod = CDLL(os.path.join(tmpdir, lname)) finally: # Unless we're debugging delete the scratch directory if 'PYFR_DEBUG_OMP_KEEP_LIBS' not in os.environ: rm(tmpdir)
def _write(self, path, solnmap, metadata): comm, rank, root = get_comm_rank_root() # Create the output directory and save the config/status files if rank == root: if os.path.exists(path): rm(path) os.mkdir(path) # Write out our metadata for name, data in metadata.items(): np.save(os.path.join(path, name), data) # Wait for this to complete comm.barrier() # Save the solutions for etype, buf in solnmap.items(): solnpath = os.path.join(path, self._get_name_for_soln(etype)) np.save(solnpath, buf)
def process_pack(args): # List the contents of the directory relnames = os.listdir(args.indir) # Get the absolute file names and extension-less file names absnames = [os.path.join(args.indir, f) for f in relnames] repnames = [f[:-4] for f in relnames] # Open/load the files files = [np.load(f, mmap_mode='r') for f in absnames] # Get the output pyfrs file name outname = args.outf or args.indir.rstrip('/') # Determine the dir and prefix of the temp file dirname, basename = os.path.split(outname) # Create a named temp file tempf = NamedTemporaryFile(prefix=basename, dir=dirname, delete=False) try: # Write the contents of the directory out as an npz (pyfrs) file np.savez(tempf, **dict(zip(repnames, files))) tempf.close() # Remove the output path if it should exist if os.path.exists(outname): rm(outname) # Rename the temp file into place os.rename(tempf.name, outname) except: # Clean up the temporary file if os.path.exists(tempf.name): os.remove(tempf.name) # Re-raise raise
def __init__(self, src, dev, cfg): # Create a scratch directory tmpidx = next(self._dir_seq) tmpdir = tempfile.mkdtemp(prefix='pyfr-{0}-'.format(tmpidx)) # Find MKL mklroot = cfg.get('backend-mic', 'mkl-root', '$MKLROOT') try: # File names cn, ln = 'tmp.c', 'libtmp.so' # Write the source code out with open(os.path.join(tmpdir, cn), 'w') as f: f.write(src) # Compile and link cmd = [ 'icc', '-shared', # Create a shared library '-std=c99', # Enable C99 support '-Ofast', # Optimise '-mmic', # Compile for the MIC '-fopenmp', # Enable OpenMP support '-L{0}/lib/mic'.format(mklroot), # MKL stuff '-lmkl_intel_lp64', # ... '-lmkl_core', # ... '-lmkl_intel_thread', # ... '-fPIC', # Position-independent code '-o', ln, cn ] call_capture_output(cmd, cwd=tmpdir) # Load self._mod = dev.load_library(os.path.join(tmpdir, ln)) finally: rm(tmpdir)