def translate_to_CUDA(Vivaldi_code='', function_name='', function_arguments=''): from vi2cu_translator.main import vi2cu_translator import numpy func_code = find_code(function_name=function_name, code=Vivaldi_code) function_argument_list = get_argument(func_code) dtype_dict = {} i = 0 for dp in function_arguments: data_name = dp.data_name f_data_name = function_argument_list[i] dtype = dp.data_contents_dtype if dp.data_dtype == numpy.ndarray: dtype += '_volume' dtype_dict[f_data_name] = dtype i += 1 # check '[' are exist idx = func_code.find('[') if idx != -1: print "Can not use [ in the Vivaldi, please use xxx_query_function" exit() # translate code, _ = vi2cu_translator(func_code, dtype_dict) return code
def translate_to_CUDA_func_list(func_list, input_merge_function_list): output = {} merge_function_list = [] for func in func_list: func_name = func['func_name'] o_func_name = str(func_name) func_code = func['code'] if 'dtype_dict' in func: dtype_dict = func['dtype_dict'] else: dtype_dict = {'x': 'integer', 'y': 'integer'} # translation from vi2cu_translator.main import vi2cu_translator # argument matching function_call_argument_list = func['args'] function_argument_list = get_argument(func_code) dtype_dict = dtype_matching(function_call_argument_list, function_argument_list, dtype_dict) # translate code, return_dtype = vi2cu_translator(func_code, dtype_dict) # make CUDA function name func_name = make_CUDA_kernel_func_name(func_name, function_argument_list, dtype_dict) output[func_name] = {'code': code, 'return_dtype': return_dtype} # merge function for m_elem in input_merge_function_list: if m_elem['user_function'] == o_func_name: m_elem['args'] = ['front', 'back', 'x', 'y'] # need real function m_elem['dtype_dict'] = {} m_elem['dtype_dict']['front'] = return_dtype + '_volume' m_elem['dtype_dict']['back'] = return_dtype + '_volume' # append with redundant check # id is front, back dtype and function name #append_with_redandant_check(merge_function_list, m_elem, ['func_name', 'args', 'dtype_dict']) append_with_redandant_check(merge_function_list, m_elem, m_elem.keys()) return output, merge_function_list
def translate_to_CUDA_func_list(func_list, input_merge_function_list): output = {} merge_function_list = [] for func in func_list: func_name = func['func_name'] o_func_name = str(func_name) func_code = func['code'] if 'dtype_dict' in func: dtype_dict = func['dtype_dict'] else: dtype_dict = {'x':'integer', 'y':'integer'} # translation from vi2cu_translator.main import vi2cu_translator # argument matching function_call_argument_list = func['args'] function_argument_list = get_argument(func_code) dtype_dict = dtype_matching(function_call_argument_list, function_argument_list, dtype_dict) # translate code, return_dtype = vi2cu_translator(func_code, dtype_dict) # make CUDA function name func_name = make_CUDA_kernel_func_name(func_name, function_argument_list, dtype_dict) output[func_name] = {'code':code,'return_dtype':return_dtype} # merge function for m_elem in input_merge_function_list: if m_elem['user_function'] == o_func_name: m_elem['args'] = ['front','back','x','y'] # need real function m_elem['dtype_dict'] = {} m_elem['dtype_dict']['front'] = return_dtype + '_volume' m_elem['dtype_dict']['back'] = return_dtype + '_volume' # append with redundant check # id is front, back dtype and function name #append_with_redandant_check(merge_function_list, m_elem, ['func_name', 'args', 'dtype_dict']) append_with_redandant_check(merge_function_list, m_elem, m_elem.keys()) return output, merge_function_list
def test(test_data, test_set=True, detail=0): if test_set: vivaldi_code = test_data['test_input'] local_dict = test_data['dtype_dict'] import ast local_dict= ast.literal_eval(local_dict) test_output = test_data['test_output'] test_return_dtype = test_data['return_dtype'] # test result, result_return_dtype = vi2cu_translator(vivaldi_code=vivaldi_code, local_dict=local_dict) flag = True if flag: # check code flag = dif(result.strip(), test_output.strip()) if flag: print "OK" return True else: print "FAILED" print "test_input:", vivaldi_code.strip() print "test_output:", test_output.strip() print "result:", result.strip() return False else: test_input = test_data target = 'CUDA' from preprocessing.main import preprocessing code = preprocessing(test_input) print "CODE" print "==============================================" print code print "FUNCTION_LIST" print "==============================================" from parse_main.main import parse_main mc = find_main(code) mc, func_list, merge_function_list = parse_main(mc) for elem in func_list: print "FUNC", elem print "TRNALSATED" print "==============================================" full_code = str(code) i = 0 n = len(func_list) while i < n: func = func_list[i] func_name = func['func_name'] func['code'] = find_code(func_name, full_code) i += 1 output = {} for func in func_list: func_name = func['func_name'] func_code = func['code'] dtype_dict = func['dtype_dict'] if 'dtype_dict' in func else {} o_func_name = str(func_name) # redundant check arg_list = func['args'] for arg in arg_list: if arg in dtype_dict: dtype = dtype_dict[arg] dtype = dtype.replace('_volume','') func_name += dtype if func_name in output: continue # translation from vi2cu_translator.main import vi2cu_translator # argument matching function_call_argument_list = func['args'] function_argument_list = get_argument(func_code) dtype_dict = dtype_matching(function_call_argument_list, function_argument_list, dtype_dict) code, return_dtype = vi2cu_translator(func_code, dtype_dict) output[func_name] = {'code':code,'return_dtype':return_dtype} #result, result_return_dtype = vi2cu_translator(vivaldi_code=result, local_dict=[]) for elem in output: print "FUNC_NAME", elem print "==============================================" print output[elem]['code'] return True return False
from vi2cu_translator.main import vi2cu_translator code = """ def viv_add(data, x): b = 10 a = list(10) for elem in range(10): #a[elem] = elem b = 10 cnt = x%10 return a[cnt] + data[x] """ _code = """ def mip(volume, x, y): step = 1.0 line_iter = orthogonal_iter(volume, x, y, step) max = 0 for elem in line_iter: val = linear_query_3d(volume, elem) if max < val:max = val return max """ print vi2cu_translator(code)[0]
def test(test_data, test_set=True, detail=0): if test_set: vivaldi_code = test_data['test_input'] local_dict = test_data['dtype_dict'] import ast local_dict = ast.literal_eval(local_dict) test_output = test_data['test_output'] test_return_dtype = test_data['return_dtype'] # test result, result_return_dtype = vi2cu_translator( vivaldi_code=vivaldi_code, local_dict=local_dict) flag = True if flag: # check code flag = dif(result.strip(), test_output.strip()) if flag: print "OK" return True else: print "FAILED" print "test_input:", vivaldi_code.strip() print "test_output:", test_output.strip() print "result:", result.strip() return False else: test_input = test_data target = 'CUDA' from preprocessing.main import preprocessing code = preprocessing(test_input) print "CODE" print "==============================================" print code print "FUNCTION_LIST" print "==============================================" from parse_main.main import parse_main mc = find_main(code) mc, func_list, merge_function_list = parse_main(mc) for elem in func_list: print "FUNC", elem print "TRNALSATED" print "==============================================" full_code = str(code) i = 0 n = len(func_list) while i < n: func = func_list[i] func_name = func['func_name'] func['code'] = find_code(func_name, full_code) i += 1 output = {} for func in func_list: func_name = func['func_name'] func_code = func['code'] dtype_dict = func['dtype_dict'] if 'dtype_dict' in func else {} o_func_name = str(func_name) # redundant check arg_list = func['args'] for arg in arg_list: if arg in dtype_dict: dtype = dtype_dict[arg] dtype = dtype.replace('_volume', '') func_name += dtype if func_name in output: continue # translation from vi2cu_translator.main import vi2cu_translator # argument matching function_call_argument_list = func['args'] function_argument_list = get_argument(func_code) dtype_dict = dtype_matching(function_call_argument_list, function_argument_list, dtype_dict) code, return_dtype = vi2cu_translator(func_code, dtype_dict) output[func_name] = {'code': code, 'return_dtype': return_dtype} #result, result_return_dtype = vi2cu_translator(vivaldi_code=result, local_dict=[]) for elem in output: print "FUNC_NAME", elem print "==============================================" print output[elem]['code'] return True return False