<s> from __future__ import division <EOL> import coresyntax as S <EOL> import coretypes as T <EOL> import inspect <EOL> import pltools <EOL> import copy <EOL> import numpy as np <EOL> import parsetypes <EOL> import pdb <EOL> from itertools import ifilter <EOL> import codepy . bpl <EOL> import codepy . cuda <EOL> import codepy . cgen as CG <EOL> import pickle <EOL> import os <EOL> def prepare_compilation ( M ) : <EOL> from . . runtime import cuda_support <EOL> if cuda_support : <EOL> from . . runtime import cuda_tag <EOL> if M . tag == cuda_tag : <EOL> return prepare_cuda_compilation ( M ) <EOL> return prepare_host_compilation ( M ) <EOL> def prepare_cuda_compilation ( M ) : <EOL> assert ( len ( M . entry_points ) == <NUM_LIT:1> ) <EOL> procedure_name = M . entry_points [ <NUM_LIT:0> ] <EOL> hash , ( wrap_type , wrap_name ) , wrap_args = M . wrap_info <EOL> wrap_decl = CG . FunctionDeclaration ( CG . Value ( wrap_type , wrap_name ) , <EOL> [ CG . Value ( x , y ) for x , y in wrap_args ] ) <EOL> host_module = codepy . bpl . BoostPythonModule ( max_arity = max ( <NUM_LIT:10> , M . arity ) , <EOL> use_private_namespace = False ) <EOL> host_module . add_to_preamble ( [ <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Line ( '<STR_LIT>' ) ] ) <EOL> device_module = codepy . cuda . CudaModule ( host_module ) <EOL> hash_namespace_open = CG . Line ( '<STR_LIT>' % hash ) <EOL> hash_namespace_close = CG . Line ( '<STR_LIT:}>' ) <EOL> using_declaration = CG . Line ( '<STR_LIT>' % hash ) <EOL> host_module . add_to_preamble ( [ hash_namespace_open , wrap_decl , <EOL> hash_namespace_close , using_declaration ] ) <EOL> host_module . add_to_init ( [ CG . Statement ( <EOL> "<STR_LIT>" % ( <EOL> procedure_name , wrap_name ) ) ] ) <EOL> device_module . add_to_preamble ( <EOL> [ CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Line ( '<STR_LIT>' ) ] ) <EOL> wrapped_cuda_code = [ CG . Line ( M . compiler_output ) ] <EOL> device_module . add_to_module ( wrapped_cuda_code ) <EOL> M . device_module = device_module <EOL> if M . compile : <EOL> M . current_toolchains = ( M . toolchains . host_toolchain , <EOL> M . toolchains . nvcc_toolchain ) <EOL> else : <EOL> M . current_toolchains = ( M . toolchains . null_host_toolchain , <EOL> M . toolchains . null_nvcc_toolchain ) <EOL> M . codepy_module = device_module <EOL> M . code = ( str ( host_module . generate ( ) ) , str ( device_module . generate ( ) ) ) <EOL> M . kwargs = dict ( host_kwargs = dict ( cache_dir = M . code_dir ) , <EOL> nvcc_kwargs = dict ( cache_dir = M . code_dir ) , <EOL> debug = M . verbose ) <EOL> return [ ] <EOL> def prepare_host_compilation ( M ) : <EOL> assert ( len ( M . entry_points ) == <NUM_LIT:1> ) <EOL> procedure_name = M . entry_points [ <NUM_LIT:0> ] <EOL> hash , ( wrap_type , wrap_name ) , wrap_args = M . wrap_info <EOL> host_module = codepy . bpl . BoostPythonModule ( max_arity = max ( <NUM_LIT:10> , M . arity ) , <EOL> use_private_namespace = False ) <EOL> host_module . add_to_preamble ( [ CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Include ( "<STR_LIT>" ) , <EOL> CG . Line ( '<STR_LIT>' ) ] ) <EOL> host_module . add_to_init ( [ CG . Statement ( <EOL> "<STR_LIT>" % ( <EOL> procedure_name , wrap_name ) ) ] ) <EOL> wrapped_code = [ CG . Line ( M . compiler_output ) , <EOL> CG . Line ( '<STR_LIT>' % hash ) ] <EOL> host_module . add_to_module ( wrapped_code ) <EOL> M . codepy_module = host_module <EOL> if M . compile : <EOL> M . current_toolchains = ( M . toolchains . host_toolchain , ) <EOL> else : <EOL> M . current_toolchains = ( M . toolchains . null_host_toolchain , ) <EOL> M . code = ( str ( host_module . generate ( ) ) , ) <EOL> M . kwargs = dict ( cache_dir = M . code_dir , <EOL> debug = M . verbose ) <EOL> return [ ] <EOL> def make_binary ( M ) : <EOL> assert ( len ( M . entry_points ) == <NUM_LIT:1> ) <EOL> procedure_name = M . entry_points [ <NUM_LIT:0> ] <EOL> code = M . code <EOL> codepy_module = M . codepy_module <EOL> toolchains = M . current_toolchains <EOL> kwargs = M . kwargs <EOL> try : <EOL> module = codepy_module . compile ( * toolchains , ** kwargs ) <EOL> except Exception as e : <EOL> if isinstance ( e , NotImplementedError ) : <EOL> raise e <EOL> for m in code : <EOL> print m <EOL> print e <EOL> raise e <EOL> name = M . input_types . keys ( ) [ <NUM_LIT:0> ] <EOL> input_type = M . input_types [ name ] <EOL> input_name = name [ <NUM_LIT:1> : ] <EOL> copperhead_info = ( input_name , input_type , M . tag ) <EOL> module_dir , module_file = os . path . split ( module . __file__ ) <EOL> info_file = open ( os . path . join ( module_dir , '<STR_LIT>' ) , '<STR_LIT:w>' ) <EOL> pickle . dump ( copperhead_info , info_file ) <EOL> info_file . close ( ) <EOL> return code , getattr ( module , procedure_name ) </s>
