Skip to content

Commit

Permalink
Compile and stash results locally, if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
bryancatanzaro committed Apr 25, 2012
1 parent 90e1af3 commit 41b48c7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -13,3 +13,4 @@ setuptools*egg-info
setuptools.pth
siteconf.py
doc/html
*__pycache__
2 changes: 1 addition & 1 deletion backend
Submodule backend updated from fe0cfc to 0c4f8f
9 changes: 7 additions & 2 deletions copperhead/compiler/binarygenerator.py
Expand Up @@ -78,6 +78,9 @@ def prepare_cuda_compilation(M):
M.toolchains = (host_toolchain, nvcc_toolchain)
M.codepy_module = device_module
M.code = (str(host_module.generate()), str(device_module.generate()))
M.kwargs = dict(host_kwargs=dict(cache_dir=M.code_dir),
nvcc_kwargs=dict(cache_dir=M.code_dir),
debug=M.verbose)
return []

def prepare_host_compilation(M):
Expand Down Expand Up @@ -105,7 +108,8 @@ def prepare_host_compilation(M):
from ..runtime import host_toolchain
M.toolchains = (host_toolchain,)
M.code = (str(host_module.generate()),)

M.kwargs = dict(cache_dir=M.code_dir,
debug=M.verbose)
return []

def make_binary(M):
Expand All @@ -115,8 +119,9 @@ def make_binary(M):
code = M.code
codepy_module = M.codepy_module
toolchains = M.toolchains
kwargs = M.kwargs
try:
module = codepy_module.compile(*toolchains, debug=M.verbose)
module = codepy_module.compile(*toolchains, **kwargs)
except Exception as e:
for m in code:
print m
Expand Down
1 change: 1 addition & 0 deletions copperhead/compiler/passes.py
Expand Up @@ -247,6 +247,7 @@ def compile(source,
M.time = opts.pop('time', False)
M.tag = tag
M.verbose = opts.pop('verbose', False)
M.code_dir = opts['code_dir']
return run_compilation(target, source, M)


22 changes: 22 additions & 0 deletions copperhead/runtime/cufunction.py
Expand Up @@ -20,6 +20,8 @@
from __future__ import absolute_import

import inspect
import tempfile
import os.path
from ..compiler import pyast, typeinference
from . import places

Expand All @@ -44,6 +46,8 @@ def __init__(self, fn):
# Parse and cache the Copperhead AST for this function
stmts = pyast.statement_from_text(self.get_source())
self.syntax_tree = stmts
# Establish code directory
self.code_dir = self.get_code_dir()
self.cache = {}
self.code = {}

Expand Down Expand Up @@ -105,3 +109,21 @@ def infer_type(self):
def get_code(self):
return self.code

def get_code_dir(self):
source_dir, source_file = os.path.split(inspect.getsourcefile(self.fn))
candidate = os.path.join(source_dir, '__pycache__', source_file, self.__name__)
if os.path.exists(candidate):
return candidate
try:
os.makedirs(candidate)
return candidate
except OSError:
#Can't create a directory where the source file lives
#Let's put it in tempdir
candidate = os.path.join(tempfile.gettempdir(),
'copperhead-cache-uid%s' % os.getuid(),
source_file, self.__name__)
if os.path.exists(candidate):
return candidate
os.makedirs(candidate)
return candidate
1 change: 1 addition & 0 deletions copperhead/runtime/driver.py
Expand Up @@ -124,6 +124,7 @@ def execute(tag, cufn, *v, **k):
globals=cufn.get_globals(),
input_types={name : cu_types},
tag=tag,
code_dir=cufn.code_dir,
**k)
#Store the binary and the compilation result
cufn.cache[signature] = compiled_fn
Expand Down

0 comments on commit 41b48c7

Please sign in to comment.