def cuda_backend_render_func(self, param_dict, vals): param_dict['supports_float32_atomic_add'] = GMM.platform_info['cuda']['supports_float32_atomic_add'] cu_kern_tpl = AspTemplate.Template(filename = GMM.template_path + "templates/em_cuda_kernels.mako") cu_kern_rend = cu_kern_tpl.render( param_val_list = vals, **param_dict) GMM.asp_mod.add_to_module([Line(cu_kern_rend)],'cuda') c_decl_tpl = AspTemplate.Template(filename = GMM.template_path + "templates/em_cuda_launch_decl.mako") c_decl_rend = c_decl_tpl.render( param_val_list = vals, **param_dict) GMM.asp_mod.add_to_preamble(c_decl_rend,'c++') #TODO: <4.1 hack
def tbb_backend_render_func(self, param_dict, vals): tbb_kern_tpl = AspTemplate.Template( filename="templates/em_tbb_kernels.mako") tbb_kern_rend = tbb_kern_tpl.render(param_val_list=vals, **param_dict) GMM.asp_mod.add_to_module([Line(tbb_kern_rend)], 'tbb') c_decl_tpl = AspTemplate.Template( filename="templates/em_tbb_kernel_decl.mako") c_decl_rend = c_decl_tpl.render(param_val_list=vals, **param_dict)
def insert_rendered_code_into_cuda_module(self): self.insert_cuda_backend_render_func() cu_base_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/training/svm_train.mako") cu_base_rend = cu_base_tpl.render(num_threads = SVM.num_cuda_threads, sh_mem_size = SVM.cuda_sh_mem_size) SVM.asp_mod.add_to_module([Line(cu_base_rend)],'c++') cu_base_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/classification/svm_classify.mako") cu_base_rend = cu_base_tpl.render(num_threads = SVM.num_cuda_threads, sh_mem_size = SVM.cuda_sh_mem_size) SVM.asp_mod.add_to_module([Line(cu_base_rend)],'c++')
def insert_cuda_backend_render_func(self): cu_kern_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/training/svm_cuda_kernels.mako") cu_kern_rend = cu_kern_tpl.render(num_threads = SVM.num_cuda_threads, sh_mem_size = SVM.cuda_sh_mem_size) SVM.asp_mod.add_to_module([Line(cu_kern_rend)],'cuda') c_decl_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/training/svm_launch_decl.mako") c_decl_rend = c_decl_tpl.render() SVM.asp_mod.add_to_preamble(c_decl_rend,'c++') #TODO: <4.1 hack base_system_header_names = [ 'stdlib.h', 'stdio.h', 'sys/time.h'] for header in base_system_header_names: SVM.asp_mod.add_to_preamble([Include(header, True)], 'cuda')
def insert_non_rendered_code_into_cuda_module(self): #Add C/CUDA source code that is not based on code variant parameters #Add decls to preamble necessary for linking to compiled CUDA sources component_t_decl = """ typedef struct components_struct { float* N; // expected # of pixels in component: [M] float* pi; // probability of component in GMM: [M] float* CP; //cluster probability [M] float* constant; // Normalizing constant [M] float* avgvar; // average variance [M] float* means; // Spectral mean for the component: [M*D] float* R; // Covariance matrix: [M*D*D] float* Rinv; // Inverse of covariance matrix: [M*D*D] } components_t;""" GMM.asp_mod.add_to_preamble(component_t_decl, 'cuda') #TODO: Move this back into insert_base_code_into_listed_modules for cuda 4.1 names_of_helper_funcs = [ "alloc_events_on_CPU", "alloc_components_on_CPU", "alloc_evals_on_CPU", "dealloc_events_on_CPU", "dealloc_components_on_CPU", "dealloc_temp_components_on_CPU", "dealloc_evals_on_CPU", "relink_components_on_CPU", "compute_distance_rissanen", "merge_components", "create_lut_log_table", "compute_KL_distance" ] for fname in names_of_helper_funcs: GMM.asp_mod.add_helper_function(fname, "", 'cuda') #Add bodies of helper functions c_base_tpl = AspTemplate.Template( filename="templates/em_cuda_host_helper_funcs.mako") c_base_rend = c_base_tpl.render() GMM.asp_mod.add_to_module([Line(c_base_rend)], 'c++') cu_base_tpl = AspTemplate.Template( filename="templates/em_cuda_device_helper_funcs.mako") cu_base_rend = cu_base_tpl.render() GMM.asp_mod.add_to_module([Line(cu_base_rend)], 'cuda') #Add Boost interface links for helper functions names_of_cuda_helper_funcs = [ "alloc_events_on_GPU", "alloc_index_list_on_GPU", "alloc_events_from_index_on_GPU", "alloc_components_on_GPU", "alloc_evals_on_GPU", "copy_event_data_CPU_to_GPU", "copy_index_list_data_CPU_to_GPU", "copy_events_from_index_CPU_to_GPU", "copy_component_data_CPU_to_GPU", "copy_component_data_GPU_to_CPU", "copy_evals_CPU_to_GPU", "copy_evals_data_GPU_to_CPU", "dealloc_events_on_GPU", "dealloc_components_on_GPU", "dealloc_evals_on_GPU", "dealloc_index_list_on_GPU" ] for fname in names_of_cuda_helper_funcs: GMM.asp_mod.add_helper_function(fname, "", 'cuda')
def insert_non_rendered_code_into_module(self): #TODO: Move this back into insert_base_code_into_listed_modules for cuda 4.1 names_of_helper_funcs = ["alloc_point_data_on_CPU",\ "alloc_labels_on_CPU",\ "alloc_train_alphas_on_CPU",\ "alloc_classify_alphas_on_CPU",\ "alloc_train_result_on_CPU",\ "alloc_classify_result_on_CPU", "alloc_support_vectors_on_CPU",] for fname in names_of_helper_funcs: SVM.asp_mod.add_helper_function(fname, "", 'cuda') #Add bodies of helper functions c_base_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/training/svm_cuda_host_helpers.mako") c_base_rend = c_base_tpl.render() SVM.asp_mod.add_to_module([Line(c_base_rend)],'c++') cu_base_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/training/svm_cuda_device_helpers.mako") cu_base_rend = cu_base_tpl.render() SVM.asp_mod.add_to_module([Line(cu_base_rend)],'cuda') #Add Boost interface links for helper functions names_of_cuda_helper_funcs = ["alloc_point_data_on_GPU",\ "alloc_point_data_on_GPU_from_ptr",\ "alloc_labels_on_GPU",\ "alloc_train_alphas_on_GPU",\ "alloc_train_result_on_GPU",\ "alloc_classify_result_on_GPU",\ "alloc_classify_alphas_on_GPU",\ "alloc_support_vectors_on_GPU",\ "copy_support_vectors_CPU_to_GPU", "copy_point_data_CPU_to_GPU",\ "copy_labels_CPU_to_GPU",\ "copy_classify_alphas_CPU_to_GPU",\ "dealloc_point_data_on_GPU",\ "dealloc_labels_on_GPU",\ "dealloc_train_result_on_GPU",\ "dealloc_classify_result_on_GPU",\ "dealloc_train_alphas_on_GPU",\ "dealloc_classify_alphas_on_GPU",\ "dealloc_support_vectors_on_GPU",\ "train", "classify"] for fname in names_of_cuda_helper_funcs: SVM.asp_mod.add_helper_function(fname,"",'cuda') c_base_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/training/cache_controller.mako") c_base_rend = c_base_tpl.render() SVM.asp_mod.add_to_module([Line(c_base_rend)],'cuda')
def insert_non_rendered_code_into_cilk_module(self): component_t_decl = """ typedef struct components_struct { float* N; // expected # of pixels in component: [M] float* pi; // probability of component in GMM: [M] float* CP; //cluster probability [M] float* constant; // Normalizing constant [M] float* avgvar; // average variance [M] float* means; // Spectral mean for the component: [M*D] float* R; // Covariance matrix: [M*D*D] float* Rinv; // Inverse of covariance matrix: [M*D*D] } components_t;""" #GMM.asp_mod.add_to_preamble(component_t_decl,'cilk') #TODO: Move this back into insert_base_code_into_listed_modules for cuda 4.1 names_of_helper_funcs = [ "alloc_events_on_CPU", "alloc_components_on_CPU", "alloc_evals_on_CPU", "dealloc_events_on_CPU", "dealloc_components_on_CPU", "dealloc_temp_components_on_CPU", "dealloc_evals_on_CPU", "relink_components_on_CPU", "compute_distance_rissanen", "merge_components", "create_lut_log_table", "compute_KL_distance" ] for fname in names_of_helper_funcs: GMM.asp_mod.add_helper_function(fname, "", 'cilk') cilk_base_tpl = AspTemplate.Template( filename="templates/em_cilk_helper_funcs.mako") cilk_base_rend = cilk_base_tpl.render() #GMM.asp_mod.add_to_module([Line(cilk_base_rend)],'cilk') #Add Cilk source code that is not based on code variant parameters system_header_names = ['cilk/cilk.h', 'cilk/reducer_opadd.h'] for x in system_header_names: GMM.asp_mod.add_to_preamble([Include(x, True)], 'cilk')
def insert_base_code_into_listed_modules(self, names_of_backends): #Add code to all backends that is used by all backends c_base_tpl = AspTemplate.Template(filename=GMM.template_path +\ "templates/em_base_helper_funcs.mako") c_base_rend = c_base_tpl.render() component_t_decl =""" typedef struct components_struct { float* N; // expected # of pixels in component: [M] float* pi; // probability of component in GMM: [M] float* CP; //cluster probability [M] float* constant; // Normalizing constant [M] float* avgvar; // average variance [M] float* means; // Spectral mean for the component: [M*D] float* R; // Covariance matrix: [M*D*D] float* Rinv; // Inverse of covariance matrix: [M*D*D] } components_t;""" base_system_header_names = ['stdlib.h', 'stdio.h', 'string.h',\ 'math.h', 'time.h', 'numpy/arrayobject.h'] for b_name in names_of_backends: for header in base_system_header_names: GMM.asp_mod.add_to_preamble([Include(header, True)], b_name) #Add Boost interface links for components and distance objects GMM.asp_mod.add_to_init("import_array();", b_name) GMM.asp_mod.add_to_init("""boost::python::class_<components_struct>("GMMComponents"); boost::python::scope().attr("components") = boost::python::object(boost::python::ptr(&components));""", b_name) GMM.asp_mod.add_to_init(""" boost::python::class_<return_component_container>("ReturnClusterContainer") .def_readwrite("new_component", &return_component_container::component) .def_readwrite("distance", &return_component_container::distance); boost::python::scope().attr("component_distance") = boost::python::object(boost::python::ptr(&ret));""", b_name) GMM.asp_mod.add_to_module([Line(c_base_rend)],b_name) GMM.asp_mod.add_to_preamble(component_t_decl, b_name)
def double_using_template(self, arr): import asp.codegen.templating.template as template mytemplate = template.Template(filename="templates/double_template.mako", disable_unicode=True) rendered = mytemplate.render(num_items=len(arr)) import asp.jit.asp_module as asp_module mod = asp_module.ASPModule() # remember, must specify function name when using a string mod.add_function("double_in_c", rendered) return mod.double_in_c(arr)
def render_func_variant(self, param_dict, param_val_list, can_be_compiled, backend_name, func_name): #Render a single variant from a template def var_name_generator(base): return '_'.join(['em',backend_name,base]+param_val_list) if can_be_compiled: c_tpl = AspTemplate.Template(filename='_'.join([GMM.template_path +\ "templates/em",backend_name,func_name+".mako"])) func_body = c_tpl.render( param_val_list = param_val_list, **param_dict) else: func_body = "void " + var_name_generator(func_name) + "(int m, int d, int n, PyObject *data){}" return var_name_generator(func_name), func_body
def insert_base_code_into_listed_modules(self, names_of_backends): #Add code to all backends that is used by all backends c_base_tpl = AspTemplate.Template(filename = SVM.template_path +\ "templates/training/svm_base_helpers.mako") c_base_rend = c_base_tpl.render() base_system_header_names = ['stdlib.h', 'stdio.h', 'string.h', 'math.h',\ 'Python.h', 'sys/time.h', 'vector', 'list', 'numpy/arrayobject.h', 'cublas.h'] for b_name in names_of_backends: for header in base_system_header_names: SVM.asp_mod.add_to_preamble([Include(header, True)], b_name) SVM.asp_mod.add_to_preamble([Line(c_base_rend)],b_name) SVM.asp_mod.add_to_init("import_array();", b_name)
def map_using_trees(self, arr): operation_ast = ast_tools.parse_method(self.operation) expr_ast = operation_ast.body[0].body[0].value converter = Converter() expr_cpp = converter.visit(expr_ast) import asp.codegen.templating.template as template mytemplate = template.Template(filename="templates/map_template.mako", disable_unicode=True) rendered = mytemplate.render(num_items=len(arr), expr=expr_cpp) import asp.jit.asp_module as asp_module mod = asp_module.ASPModule() mod.add_function("map_in_c", rendered) return mod.map_in_c(arr)
def map_using_trees(self, arr): import asp.codegen.templating.template as template import inspect import asp.codegen.python_ast as ast import asp.codegen.ast_tools as ast_tools src = inspect.getsource(self.operation) operation_ast = ast.parse(src.lstrip()) return_ast = operation_ast.body[0] expr_ast = return_ast.body[0].value expr_cpp = ast_tools.ConvertAST().visit(expr_ast) mytemplate = template.Template(filename="templates/map_template.mako", disable_unicode=True) rendered = mytemplate.render(num_items=len(arr), expr=expr_cpp) import asp.jit.asp_module as asp_module mod = asp_module.ASPModule() mod.add_function("map_in_c", rendered) return mod.map_in_c(arr)
def build_mod(self, key): template_name = '' if self.with_openMP: template_name = 'blb_omp.mako' elif self.with_cilk: template_name = 'blb_cilk.mako' else: template_name = 'blb_template.mako' import asp.codegen.templating.template as template blb_template = template.Template(filename="templates/%s" % template_name, disable_unicode=True) impl_template = template.Template(filename="templates/blb_impl.mako", disable_unicode=True) #leading dimension of first data object tuple n_vecs = key[0][0][0] vec_n = int(pow(n_vecs, self.subsample_len_exp)) impl_args = {} #impl_args = {'dim': self.dim} #impl_args['n_data'] = key[0] #impl_args['sub_n'] = int( pow( key[0], self.subsample_len_exp ) ) impl_args['vec_n'] = vec_n impl_args['n_vecs'] = n_vecs impl_funcs = [] impl_args['scalar_type'] = 'double' estimate_converter = BLBConverter(self.create_input_model(key, vec_n), input_size=vec_n, weighted=True) estimate_cpp = estimate_converter.render(self.estimate_ast) impl_args['classifier'] = estimate_cpp impl_funcs.extend(estimate_converter.desired_funcs) impl_args['bootstrap_model'] = estimate_converter.get_ret_model() arg_model = [model.clone() for model in estimate_converter.arg_model] for i in range(len(arg_model)): arg_model[i].dimensions = key[i][0] impl_args['arg_model'] = arg_model reduce_input = estimate_converter.get_ret_model().clone() reduce_input.dimensions.insert(0, self.num_bootstraps) reduce_input.set_len(self.num_bootstraps) reduce_converter = BLBConverter([reduce_input], input_size=self.num_bootstraps) reduce_cpp = reduce_converter.render(self.reduce_ast) impl_args['bootstrap_reducer'] = reduce_cpp impl_funcs.extend(reduce_converter.desired_funcs) impl_args['subsample_model'] = reduce_converter.get_ret_model() average_input = reduce_converter.get_ret_model().clone() average_input.dimensions.insert(0, self.num_subsamples) average_input.set_len(self.num_subsamples) average_converter = BLBConverter([average_input], input_size=self.num_subsamples) average_cpp = average_converter.render(self.average_ast) impl_args['subsample_reducer'] = average_cpp impl_funcs.extend(average_converter.desired_funcs) impl_args['average_model'] = average_converter.get_ret_model() impl_args['desired_funcs'] = set(impl_funcs) fwk_args = self.set_framework_args(key, impl_args.copy()) rendered = blb_template.render(**fwk_args) """ === This was for non sejitizing code === if self.compute_estimate in BLB.known_reducers: impl_args['use_classifier'] = self.compute_estimate else: impl_args['classifier'] = self.compute_estimate if self.reduce_bootstraps in BLB.known_reducers: impl_args['use_bootstrap_reducer'] = self.reduce_bootstraps else: impl_args['bootstrap_reducer'] = self.reduce_bootstraps if self.average in BLB.known_reducers: impl_args['use_subsample_reducer'] = self.average else: impl_args['subsample_reducer'] = self.average """ rendered_impl = impl_template.render(**impl_args) import asp.jit.asp_module as asp_module mod = asp_module.ASPModule(specializer='BLB', cache_dir=cache_dir) mod.add_function('compute_estimate', rendered_impl) mod.add_function("compute_blb", rendered) self.set_compiler_flags(mod) self.set_includes(mod) f = open('blbout.cpp', 'w+') f.write(str(mod.backends['c++'].module.generate())) f.close() return mod
def cilk_backend_render_func(self, param_dict, vals): cilk_kern_tpl = AspTemplate.Template(filename=GMM.template_path +"templates/em_cilk_kernels.mako") cilk_kern_rend = cilk_kern_tpl.render( param_val_list = vals, **param_dict) GMM.asp_mod.add_to_module([Line(cilk_kern_rend)],'cilk') c_decl_tpl = AspTemplate.Template(filename=GMM.template_path +"templates/em_cilk_kernel_decl.mako") c_decl_rend = c_decl_tpl.render( param_val_list = vals, **param_dict)