def __init__(self, toolchain=None, cc='gcc', cflags='-std=c99 -O3 -fPIC'.split(), ldflags='-shared'.split(), libraries=[], include_dirs=[], library_dirs=[], defines=[], source_suffix='c'): # try to get a default toolchain # or subclass supplied version if available self.toolchain = guess_toolchain() if toolchain is None else toolchain self.source_suffix = source_suffix if toolchain is None: # copy in all differing values diff = {'cc': cc, 'cflags': cflags, 'ldflags': ldflags, 'libraries': libraries, 'include_dirs': include_dirs, 'library_dirs': library_dirs, 'defines': defines} # filter empty and those equal to toolchain defaults diff = dict((k, v) for k, v in six.iteritems(diff) if v and not hasattr(self.toolchain, k) or getattr(self.toolchain, k) != v) self.toolchain = self.toolchain.copy(**diff) self.tempdir = tempfile.mkdtemp(prefix="tmp_loopy") self.source_suffix = source_suffix
def test(): toolchain = guess_toolchain() module_code = """ extern "C" { int const greet() { return 1; } } """ # compile to object file _, _, obj_path, _ = compile_from_string(toolchain, "module", module_code, object=True) # and then to shared lib with open(obj_path, "rb") as file: obj = file.read() _, _, ext_file, _ = compile_from_string(toolchain, "module", obj, source_name=["module.o"], object=False, source_is_binary=True) # test module dll = CDLL(ext_file) _fn = dll.greet _fn.restype = int assert _fn() == 1
def test(): toolchain = guess_toolchain() module_code = """ extern "C" { int const greet() { return 1; } } """ # compile to object file _, _, obj_path, _ = compile_from_string(toolchain, 'module', module_code, object=True) # and then to shared lib with open(obj_path, 'rb') as file: obj = file.read() _, _, ext_file, _ = compile_from_string( toolchain, 'module', obj, source_name=['module.o'], object=False, source_is_binary=True) # test module dll = CDLL(ext_file) _fn = getattr(dll, 'greet') _fn.restype = int assert _fn() == 1
def __init__(self, toolchain=None, cc="gcc", cflags="-std=c99 -O3 -fPIC".split(), ldflags="-shared".split(), libraries=[], include_dirs=[], library_dirs=[], defines=[], source_suffix="c"): # try to get a default toolchain # or subclass supplied version if available self.toolchain = toolchain if toolchain is None: try: self.toolchain = guess_toolchain() except (ToolchainGuessError, ExecError): # missing compiler python was built with (likely, Conda) # use a default GCCToolchain logger = logging.getLogger(__name__) logger.warn("Default toolchain guessed from python config " "not found, replacing with default GCCToolchain.") # this is ugly, but I'm not sure there's a clean way to copy the # default args self.toolchain = GCCToolchain( cc="gcc", cflags="-std=c99 -O3 -fPIC".split(), ldflags="-shared".split(), libraries=[], library_dirs=[], defines=[], undefines=[], source_suffix="c", so_ext=".so", o_ext=".o", include_dirs=[]) if toolchain is None: # copy in all differing values diff = { "cc": cc, "cflags": cflags, "ldflags": ldflags, "libraries": libraries, "include_dirs": include_dirs, "library_dirs": library_dirs, "defines": defines } # filter empty and those equal to toolchain defaults diff = { k: v for k, v in diff.items() if v and (not hasattr(self.toolchain, k) or getattr(self.toolchain, k) != v) } self.toolchain = self.toolchain.copy(**diff) self.tempdir = tempfile.mkdtemp(prefix="tmp_loopy") self.source_suffix = source_suffix
def __init__(self, toolchain=None, cc='gcc', cflags='-std=c99 -O3 -fPIC'.split(), ldflags='-shared'.split(), libraries=[], include_dirs=[], library_dirs=[], defines=[], source_suffix='c'): # try to get a default toolchain # or subclass supplied version if available self.toolchain = toolchain if toolchain is None: try: self.toolchain = guess_toolchain() except (ToolchainGuessError, ExecError): # missing compiler python was built with (likely, Conda) # use a default GCCToolchain logger = logging.getLogger(__name__) logger.warn('Default toolchain guessed from python config ' 'not found, replacing with default GCCToolchain.') # this is ugly, but I'm not sure there's a clean way to copy the # default args self.toolchain = GCCToolchain( cc='gcc', cflags='-std=c99 -O3 -fPIC'.split(), ldflags='-shared'.split(), libraries=[], library_dirs=[], defines=[], undefines=[], source_suffix='c', so_ext='.so', o_ext='.o', include_dirs=[]) if toolchain is None: # copy in all differing values diff = { 'cc': cc, 'cflags': cflags, 'ldflags': ldflags, 'libraries': libraries, 'include_dirs': include_dirs, 'library_dirs': library_dirs, 'defines': defines } # filter empty and those equal to toolchain defaults diff = dict((k, v) for k, v in six.iteritems(diff) if v and (not hasattr(self.toolchain, k) or getattr(self.toolchain, k) != v)) self.toolchain = self.toolchain.copy(**diff) self.tempdir = tempfile.mkdtemp(prefix="tmp_loopy") self.source_suffix = source_suffix
def get_elwise_module_binary(arguments, operation, name="kernel", toolchain=None): if toolchain is None: from codepy.toolchain import guess_toolchain toolchain = guess_toolchain() toolchain = toolchain.copy() from codepy.libraries import add_pyublas toolchain = toolchain.copy() add_pyublas(toolchain) return get_elwise_module_descriptor(arguments, operation, name) \ .compile(toolchain)
def make_greet_mod(greeting): from cgen import FunctionBody, FunctionDeclaration, Block, \ Const, Pointer, Value, Statement from codepy.bpl import BoostPythonModule mod = BoostPythonModule() mod.add_function( FunctionBody( FunctionDeclaration(Const(Pointer(Value("char", "greet"))), []), Block([Statement('return "%s"' % greeting)]))) from codepy.toolchain import guess_toolchain return mod.compile(guess_toolchain(), wait_on_error=True)
def make_greet_mod(greeting): from codepy.cgen import FunctionBody, FunctionDeclaration, Block, \ Const, Pointer, Value, Statement from codepy.bpl import BoostPythonModule mod = BoostPythonModule() mod.add_function( FunctionBody( FunctionDeclaration(Const(Pointer(Value("char", "greet"))), []), Block([Statement('return "%s"' % greeting)]) )) from codepy.toolchain import guess_toolchain return mod.compile(guess_toolchain(), wait_on_error=True)
def __init__(self, toolchain=None, cc='gcc', cflags='-std=c99 -O3 -fPIC'.split(), ldflags='-shared'.split(), libraries=[], include_dirs=[], library_dirs=[], defines=[], source_suffix='c'): # try to get a default toolchain # or subclass supplied version if available self.toolchain = toolchain if toolchain is None: try: self.toolchain = guess_toolchain() except (ToolchainGuessError, ExecError): # missing compiler python was built with (likely, Conda) # use a default GCCToolchain logger = logging.getLogger(__name__) logger.warn('Default toolchain guessed from python config ' 'not found, replacing with default GCCToolchain.') # this is ugly, but I'm not sure there's a clean way to copy the # default args self.toolchain = GCCToolchain( cc='gcc', cflags='-std=c99 -O3 -fPIC'.split(), ldflags='-shared'.split(), libraries=[], library_dirs=[], defines=[], undefines=[], source_suffix='c', so_ext='.so', o_ext='.o', include_dirs=[]) if toolchain is None: # copy in all differing values diff = {'cc': cc, 'cflags': cflags, 'ldflags': ldflags, 'libraries': libraries, 'include_dirs': include_dirs, 'library_dirs': library_dirs, 'defines': defines} # filter empty and those equal to toolchain defaults diff = dict((k, v) for k, v in six.iteritems(diff) if v and (not hasattr(self.toolchain, k) or getattr(self.toolchain, k) != v)) self.toolchain = self.toolchain.copy(**diff) self.tempdir = tempfile.mkdtemp(prefix="tmp_loopy") self.source_suffix = source_suffix
def __init__(self, *args, **kwargs): toolchain = kwargs.pop("toolchain", None) # tolerate (and ignore) the CUDA backend's tune_for argument _ = kwargs.pop("tune_for", None) hedge.discretization.Discretization.__init__(self, *args, **kwargs) if toolchain is None: from codepy.toolchain import guess_toolchain toolchain = guess_toolchain() toolchain = toolchain.with_optimization_level(3) from codepy.libraries import add_hedge add_hedge(toolchain) self.toolchain = toolchain
def __init__(self, *args, **kwargs): logger.info("init jit discretization: start") toolchain = kwargs.pop("toolchain", None) # tolerate (and ignore) the CUDA backend's tune_for argument kwargs.pop("tune_for", None) hedge.discretization.Discretization.__init__(self, *args, **kwargs) if toolchain is None: from codepy.toolchain import guess_toolchain toolchain = guess_toolchain() toolchain = toolchain.with_optimization_level(3) from codepy.libraries import add_hedge add_hedge(toolchain) self.toolchain = toolchain logger.info("init jit discretization: done")
import cgen as c from codepy.bpl import BoostPythonModule mod = BoostPythonModule() mod.add_function( c.FunctionBody( c.FunctionDeclaration(c.Const(c.Pointer(c.Value("char", "greet"))), []), c.Block([c.Statement('return "hello world"')]))) from codepy.toolchain import guess_toolchain cmod = mod.compile(guess_toolchain()) print(cmod.greet())
MODULE_CODE = """ #include <boost/python.hpp> namespace { char const *greet() { return "hello world"; } } BOOST_PYTHON_MODULE(module) { boost::python::def("greet", &greet); } """ from codepy.toolchain import guess_toolchain toolchain = guess_toolchain() from codepy.libraries import add_boost_python add_boost_python(toolchain) from codepy.jit import extension_from_string cmod = extension_from_string(toolchain, "module", MODULE_CODE) print cmod.greet()
import cgen as c from codepy.bpl import BoostPythonModule mod = BoostPythonModule() mod.add_function( c.FunctionBody( c.FunctionDeclaration(c.Const(c.Pointer(c.Value("char", "greet"))), []), c.Block([c.Statement('return "hello world"')]) )) from codepy.toolchain import guess_toolchain cmod = mod.compile(guess_toolchain()) print(cmod.greet())
from codepy.cgen import * from codepy.bpl import BoostPythonModule mod = BoostPythonModule() mod.add_function( FunctionBody( FunctionDeclaration(Const(Pointer(Value("char", "greet"))), []), Block([Statement('return "hello world"')]) )) from codepy.toolchain import guess_toolchain cmod = mod.compile(guess_toolchain(), wait_on_error=True, debug=True) print cmod.greet()
MODULE_CODE = """ #include <boost/python.hpp> namespace { char const *greet() { return "hello world"; } } BOOST_PYTHON_MODULE(module) { boost::python::def("greet", &greet); } """ from codepy.toolchain import guess_toolchain toolchain = guess_toolchain() from codepy.libraries import add_boost_python add_boost_python(toolchain) from codepy.jit import extension_from_string cmod = extension_from_string(toolchain, "module", MODULE_CODE) print(cmod.greet())
from codepy.cgen import * from codepy.bpl import BoostPythonModule mod = BoostPythonModule() mod.add_function( FunctionBody( FunctionDeclaration(Const(Pointer(Value("char", "greet"))), []), Block([Statement('return "hello world"')]))) from codepy.toolchain import guess_toolchain cmod = mod.compile(guess_toolchain(), wait_on_error=True, debug=True) print cmod.greet()