def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.num_blocks = (size + NUM_THREADS_PER_BLOCK - 1) // NUM_THREADS_PER_BLOCK # Allocate 2 vectors; start = time.time() self.x = polyglot.eval(language="grcuda", string=f"float[{size}]") self.y = polyglot.eval(language="grcuda", string=f"float[{size}]") # Allocate a support vector; self.res = polyglot.eval(language="grcuda", string=f"float[1]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.square_kernel = build_kernel(SQUARE_KERNEL, "square", "pointer, sint32") self.reduce_kernel = build_kernel(REDUCE_KERNEL, "reduce", "pointer, pointer, pointer, sint32") end = time.time() self.benchmark.add_phase({ "name": "allocation", "time_sec": end - start })
def test_internal_languages_dont_eval(): try: polyglot.eval(language="nfi", string="default") except NotImplementedError as e: assert "No language for id nfi found" in str(e) assert polyglot.eval(language="python", string="21 * 2") == 42
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.block_size = block_size["block_size_1d"] # Allocate 4 vectors; self.x = polyglot.eval(language="grcuda", string=f"int[{size}]") self.y = polyglot.eval(language="grcuda", string=f"int[{size}]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.sum_kernel = build_kernel(SUM_KERNEL, "sum", "pointer, sint32")
def get_eval_limited(): global _eval_limited if _eval_limited is None: path = Path(__file__).absolute() eval_file = path.parent / "eval_limited.rb" # Reading the file via polyglot does not work for whatever reason. code = "".join(open(eval_file).readlines()) _eval_limited = polyglot.eval(language="ruby", string=code) return _eval_limited
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.block_size = block_size["block_size_1d"] self.x_tmp = None # self.x_tmp = [0] * self.size # Allocate vectors; for i in range(self.K): self.x[i] = polyglot.eval(language="grcuda", string=f"double[{size}]") self.y[i] = polyglot.eval(language="grcuda", string=f"double[{size}]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.bs_kernel = build_kernel( BS_KERNEL, "bs", "pointer, pointer, sint32, double, double, double, double")
def setup(sre_compiler, error_class, flags_table): global error error = error_class global FLAGS FLAGS = flags_table def configure_fallback_compiler(mode): def fallback_compiler(pattern, flags): sticky = False bit_flags = 0 for flag in flags: # Handle internal stick(y) flag used to signal matching only at the start of input. if flag == "y": sticky = True else: bit_flags = bit_flags | FLAGS[flag] compiled_pattern = sre_compiler(pattern if mode == "str" else _str_to_bytes(pattern), bit_flags) def executable_pattern(regex_object, input, from_index): search_method = compiled_pattern.match if sticky else compiled_pattern.search result = search_method(input, from_index) is_match = result is not None group_count = 1 + compiled_pattern.groups return _RegexResult( input = input, isMatch = is_match, groupCount = group_count if is_match else 0, start = [result.start(i) for i in range(group_count)] if is_match else [], end = [result.end(i) for i in range(group_count)] if is_match else [], regex = regex_object ) return executable_pattern return fallback_compiler engine_builder = _interop.eval(string="", language="regex") global TREGEX_ENGINE_STR global TREGEX_ENGINE_BYTES TREGEX_ENGINE_STR = engine_builder("Flavor=PythonStr", configure_fallback_compiler("str")) TREGEX_ENGINE_BYTES = engine_builder("Flavor=PythonBytes", configure_fallback_compiler("bytes")) def new_compile(p, flags=0): if isinstance(p, (str, bytes)): return _tcompile(p, flags) else: return sre_compiler(p, flags) return new_compile
def test_function(self, language: str, input_code: str, output_code: str) -> bool: from code_importer.utils import eval_limited language = str(language) input_code = str(input_code) output_code = str(output_code) # Sandboxing only works for ruby snippets with ruby code at the moment if language != self.snippet_language: return super().test_function(language, input_code, output_code) # Compile input data for testing input_data = polyglot.eval(language=language, string=str(input_code)) # First filter if the number of arguments don't align if len(input_data) != self.function_argument_count(): return False eval_string = f"{self.code}\n{self.function_return_statement()}.call(*{input_code}) == {output_code}" try: return eval_limited(self.snippet_language, eval_string) except: return False
def gen_matrices_poly(num_rows_R, num_cols_S, tup_ratio, feat_ratio, mode): # Computing matrix dims num_rows_S = num_rows_R * tup_ratio num_cols_R = num_cols_S * feat_ratio num_rows_K = num_rows_S num_cols_K = num_rows_R #num of elements per matrix area_S = num_rows_S * num_cols_S area_R = num_rows_R * num_cols_R area_K = num_rows_K * num_cols_K func = polyglot.eval( language="R", string= "source('benchUtils.r'); function(x,y,z,w){ genMatricesForPy(x,y,z,w); }" ) res = func(num_rows_R, num_cols_S, tup_ratio, feat_ratio) Sarg, Ksarg, Rsarg, matMatrixForeign, target, avatarArg = res nm = morpheus.NormalizedMatrix(S=Sarg, Ks=Ksarg, Rs=Rsarg, foreign_backend=True, avatar=avatarArg) mat = morpheus.NormalizedMatrix(mat=matMatrixForeign, avatar=avatarArg) target = morpheus.NormalizedMatrix(mat=target, avatar=avatarArg) data = nm if mode == "trinity" else mat n_mat, d_mat = (num_rows_S, num_cols_R + num_cols_S) matrices = { "data": data, "matMatrix": mat, "target": target, "nMat": n_mat, "dMat": d_mat, "avatar": avatarArg } return matrices
def get_tasks(params, n_mat, d_mat, T, target, tasks, is_monolang): lmm_num_rows = d_mat lmm_num_cols = 2 rmm_num_rows = 2 rmm_num_cols = n_mat lmm_arg = lmm_num_rows * lmm_num_cols rmm_arg = rmm_num_rows * rmm_num_cols log_reg_max_iter = 20 log_reg_gamma = 0.000001 n_S = n_mat center_num = 10 end = d_mat if is_monolang: raise NotImplementedError else: func = polyglot.eval( language="R", string= "source('benchUtils.r'); function(x,y){ genRanMatrixForPy(x,y); }") log_reg_winit = morpheus.NormalizedMatrix(mat=func(d_mat, 1), avatar=T.avatar) all_tasks = [("linearRegression", lambda x: do_linear_regression( x, log_reg_max_iter, log_reg_winit, log_reg_gamma, target))] chosen_tasks = [] for name, func in all_tasks: if name in tasks: chosen_tasks.append((name, func)) return chosen_tasks
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.block_size = block_size["block_size_1d"] # Allocate 2 vectors; self.x = polyglot.eval(language="grcuda", string=f"float[{size}]") self.y = polyglot.eval(language="grcuda", string=f"float[{size}]") self.x1 = polyglot.eval(language="grcuda", string=f"float[{size}]") self.y1 = polyglot.eval(language="grcuda", string=f"float[{size}]") # Allocate a support vector; self.res = polyglot.eval(language="grcuda", string=f"float[1]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.square_kernel = build_kernel(SQUARE_KERNEL, "square", "pointer, pointer, sint32") self.reduce_kernel = build_kernel( REDUCE_KERNEL, "reduce", "const pointer, const pointer, pointer, sint32")
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.block_size = block_size["block_size_1d"] # Allocate vectors; self.x = polyglot.eval(language="grcuda", string=f"int[{size * self.num_features}]") self.z = polyglot.eval(language="grcuda", string=f"float[{size * self.num_features}]") self.nb_feat_log_prob = polyglot.eval( language="grcuda", string=f"float[{self.num_classes * self.num_features}]") self.nb_class_log_prior = polyglot.eval( language="grcuda", string=f"float[{self.num_classes}]") self.ridge_coeff = polyglot.eval( language="grcuda", string=f"float[{self.num_classes * self.num_features}]") self.ridge_intercept = polyglot.eval( language="grcuda", string=f"float[{self.num_classes}]") self.nb_amax = polyglot.eval(language="grcuda", string=f"float[{self.size}]") self.nb_l = polyglot.eval(language="grcuda", string=f"float[{self.size}]") self.r1 = polyglot.eval( language="grcuda", string=f"float[{self.size * self.num_classes}]") self.r2 = polyglot.eval( language="grcuda", string=f"float[{self.size * self.num_classes}]") self.r = polyglot.eval(language="grcuda", string=f"int[{self.size}]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.nb_1 = build_kernel( NB_KERNEL, "nb_1", "const pointer, pointer, pointer, sint32, sint32, sint32") self.nb_2 = build_kernel(NB_KERNEL, "nb_2", "pointer, pointer, sint32, sint32") self.nb_3 = build_kernel(NB_KERNEL, "nb_3", "pointer, pointer, pointer, sint32, sint32") self.nb_4 = build_kernel(NB_KERNEL, "nb_4", "pointer, pointer, sint32, sint32") self.rr_1 = build_kernel(RR_KERNEL, "rr_1", "const pointer, pointer, sint32, sint32") self.rr_2 = build_kernel( RR_KERNEL, "rr_2", "pointer, pointer, pointer, sint32, sint32, sint32") self.rr_3 = build_kernel(RR_KERNEL, "rr_3", "pointer, pointer, sint32, sint32") self.softmax = build_kernel(ENSEMBLE_KERNEL, "softmax", "pointer, sint32, sint32") self.argmax = build_kernel( ENSEMBLE_KERNEL, "argmax", "pointer, pointer, pointer, sint32, sint32")
# Compute the sum of difference of squares of 2 vectors, using multiple GrCUDA kernels. # Structure of the computation: # A: x^2 ──┐ # ├─> C: z=x-y ──> D: sum(z) # B: x^2 ──┘ if __name__ == "__main__": N = 1000000 NUM_BLOCKS = (N + NUM_THREADS_PER_BLOCK - 1) // NUM_THREADS_PER_BLOCK start_tot = time.time() time_cumulative = 0 # Allocate 2 vectors; start = time.time() x = polyglot.eval(language="grcuda", string=f"float[{N}]") y = polyglot.eval(language="grcuda", string=f"float[{N}]") # Allocate a support vector; z = polyglot.eval(language="grcuda", string=f"float[{N}]") res = polyglot.eval(language="grcuda", string=f"float[1]") end = time.time() time_cumulative += end - start print(f"time to allocate arrays: {end - start:.4f} sec") # Fill the 2 vectors; start = time.time() for i in range(N): x[i] = 1 / (i + 1) y[i] = 2 / (i + 1) res[0] = 0
# SOFTWARE. import polyglot as _interop def default(value, default): return default if not value else value def maxsize(): import sys return sys.maxsize try: TREGEX_ENGINE = _interop.eval(string="", language="regex")() except BaseException: TREGEX_ENGINE = None CODESIZE = 4 MAGIC = 20140917 MAXREPEAT = 4294967295 MAXGROUPS = 2147483647 FLAG_NAMES = [ "re.TEMPLATE", "re.IGNORECASE", "re.LOCALE", "re.MULTILINE", "re.DOTALL", "re.UNICODE", "re.VERBOSE", "re.DEBUG", "re.ASCII" ] _FLAGS_TO_JS = ["", "i", "", "m", "s", "u", "", "", ""]
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.block_size = block_size["block_size_1d"] self.gpu_result = np.zeros(self.size) # Create a random symmetric COO matrix; self.random_seed = randint(0, 10000000) seed(self.random_seed) # Create a random COO symmetric matrix; t = [(0,0,0)] * self.size * self.max_degree * 2 for i in range(self.size): # Create max_degree random edges; edges = sample(range(0, self.size), self.max_degree) for j, e in enumerate(edges): while i == e: e = randint(0, self.size - 1) tmp = random() t[i * self.max_degree + j] = (i, e, tmp) t[i * self.max_degree + j + self.size * self.max_degree] = (e, i, tmp) x, self.idx_cpu, self.val_cpu = zip(*sorted(t, key=lambda l: (l[0], l[1]))) self.num_nnz = len(self.idx_cpu) self.ptr_cpu = [0] * (self.size + 1) for i, x_i in enumerate(x): self.ptr_cpu[x_i + 1] += 1 for i in range(len(self.ptr_cpu) - 1): self.ptr_cpu[i + 1] += self.ptr_cpu[i] self.b_cpu = [0] * self.size # Allocate vectors; self.ptr = polyglot.eval(language="grcuda", string=f"int[{self.size + 1}]") self.idx = polyglot.eval(language="grcuda", string=f"int[{self.num_nnz}]") self.val = polyglot.eval(language="grcuda", string=f"float[{self.num_nnz}]") self.x = polyglot.eval(language="grcuda", string=f"float[{size}]") self.p = polyglot.eval(language="grcuda", string=f"float[{size}]") self.r = polyglot.eval(language="grcuda", string=f"float[{size}]") self.b = polyglot.eval(language="grcuda", string=f"float[{size}]") self.y = polyglot.eval(language="grcuda", string=f"float[{size}]") self.t1 = polyglot.eval(language="grcuda", string=f"float[1]") self.t2 = polyglot.eval(language="grcuda", string=f"float[1]") self.row_cnt_1 = polyglot.eval(language="grcuda", string=f"int[1]") self.row_cnt_2 = polyglot.eval(language="grcuda", string=f"int[1]") self.row_cnt_3 = polyglot.eval(language="grcuda", string=f"int[1]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.spmv_kernel = build_kernel(SPMV_KERNEL, "spmv2", "pointer, pointer, pointer, pointer, pointer, pointer, sint32") self.spmv_full_kernel = build_kernel(SPMV_KERNEL, "spmv_full", "pointer, pointer, pointer, pointer, pointer, pointer, sint32, float, pointer") self.norm_kernel = build_kernel(SUM_KERNEL, "vector_norm", "const pointer, pointer, sint32") self.dp_kernel = build_kernel(SUM_KERNEL, "dot_product", "const pointer, const pointer, pointer, sint32") self.saxpy_kernel = build_kernel(SAXPY_KERNEL, "saxpy", "pointer, const pointer, const pointer, float, sint32") self.cpy_kernel = build_kernel(SAXPY_KERNEL, "cpy", "pointer, const pointer, sint32")
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.block_size_1d = block_size["block_size_1d"] self.block_size_2d = block_size["block_size_2d"] self.gpu_result = 0.0 # Allocate vectors; self.x = polyglot.eval(language="grcuda", string=f"float[{size * size * self.channels}]") self.x1 = polyglot.eval( language="grcuda", string= f"float[{(size // self.stride) * (size // self.stride) * self.kn1}]" ) self.x11 = polyglot.eval( language="grcuda", string= f"float[{(size // self.stride // self.pooling) * (size // self.stride // self.pooling) * self.kn1}]" ) self.x2 = polyglot.eval( language="grcuda", string= f"float[{(size // self.stride // self.pooling // self.stride) * (size // self.stride // self.pooling // self.stride) * self.kn2}]" ) self.x3 = polyglot.eval(language="grcuda", string=f"float[{self.kn2}]") self.y = polyglot.eval(language="grcuda", string=f"float[{size * size * self.channels}]") self.y1 = polyglot.eval( language="grcuda", string= f"float[{(size // self.stride) * (size // self.stride) * self.kn1}]" ) self.y11 = polyglot.eval( language="grcuda", string= f"float[{(size // self.stride // self.pooling) * (size // self.stride // self.pooling) * self.kn1}]" ) self.y2 = polyglot.eval( language="grcuda", string= f"float[{(size // self.stride // self.pooling // self.stride) * (size // self.stride // self.pooling // self.stride) * self.kn2}]" ) self.y3 = polyglot.eval(language="grcuda", string=f"float[{self.kn2}]") self.kernel_1 = polyglot.eval( language="grcuda", string=f"float[{self.kn1 * self.K * self.K * self.channels}]") self.kernel_2 = polyglot.eval( language="grcuda", string=f"float[{self.kn1 * self.K * self.K * self.kn2}]") self.kernel_3 = polyglot.eval( language="grcuda", string=f"float[{self.kn1 * self.K * self.K * self.channels}]") self.kernel_4 = polyglot.eval( language="grcuda", string=f"float[{self.kn1 * self.K * self.K * self.kn2}]") self.z = polyglot.eval(language="grcuda", string=f"float[{len(self.y2) * 2}]") self.dense_weights = polyglot.eval(language="grcuda", string=f"float[{len(self.z)}]") self.res = polyglot.eval(language="grcuda", string=f"float[1]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.conv2d_kernel = build_kernel( CONV2D, "conv2d", "pointer, pointer, const pointer, sint32, sint32, sint32, sint32, sint32, sint32" ) self.pooling_kernel = build_kernel( POOLING, "mean_pooling", "pointer, const pointer, sint32, sint32, sint32, sint32, sint32") self.gap_kernel = build_kernel( GAP, "gap", "pointer, pointer, sint32, sint32, sint32") self.concat_kernel = build_kernel( CONCAT, "concat", "pointer, const pointer, const pointer, sint32") self.dp_kernel = build_kernel( DOT_PRODUCT, "dot_product", "const pointer, const pointer, pointer, sint32")
from argparse import ArgumentParser, Namespace from random import randint from pathlib import Path import os import polyglot project_path = Path(os.path.realpath(__file__)).parent ruby_path = project_path / "codesources" / "stackoverflow" / "stackoverflow_query" StackoverflowQuery = polyglot.eval( language="ruby", string="require '{}'; StackoverflowQuery".format(ruby_path)) def compile_top_function(query): """ Interactive snippet selection for the top function objects :param query: query :return: selected code snippet """ function_input = input("Function input") function_output = input("Function Output") # function_snippets = query.select_functions(1) function_snippets = query.select_method_finder_snippets( function_input, function_output) for snippet in function_snippets: print("Will compile the following snippet:\n{}".format(snippet.code)) while True: user_input = input("Use this snippet (y/n)?").lower()
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.block_size_1d = block_size["block_size_1d"] self.block_size_2d = block_size["block_size_2d"] # Allocate vectors; self.image = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.image2 = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.image3 = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.kernel_small = polyglot.eval(language="grcuda", string=f"float[{self.kernel_small_diameter}][{self.kernel_small_diameter}]") self.kernel_large = polyglot.eval(language="grcuda", string=f"float[{self.kernel_large_diameter}][{self.kernel_large_diameter}]") self.kernel_unsharpen = polyglot.eval(language="grcuda", string=f"float[{self.kernel_unsharpen_diameter}][{self.kernel_unsharpen_diameter}]") self.maximum = polyglot.eval(language="grcuda", string=f"float[1]") self.minimum = polyglot.eval(language="grcuda", string=f"float[1]") self.mask_small = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.mask_large = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.image_unsharpen = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.blurred_small = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.blurred_large = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") self.blurred_unsharpen = polyglot.eval(language="grcuda", string=f"float[{size}][{size}]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.gaussian_blur_kernel = build_kernel(GAUSSIAN_BLUR, "gaussian_blur", "const pointer, pointer, sint32, sint32, const pointer, sint32") self.sobel_kernel = build_kernel(SOBEL, "sobel", "pointer, pointer, sint32, sint32") self.extend_kernel = build_kernel(EXTEND_MASK, "extend", "pointer, const pointer, const pointer, sint32") self.maximum_kernel = build_kernel(EXTEND_MASK, "maximum", "const pointer, pointer, sint32") self.minimum_kernel = build_kernel(EXTEND_MASK, "minimum", "const pointer, pointer, sint32") self.unsharpen_kernel = build_kernel(UNSHARPEN, "unsharpen", "pointer, pointer, pointer, float, sint32") self.combine_mask_kernel = build_kernel(COMBINE, "combine", "const pointer, const pointer, const pointer, pointer, sint32") self.reset_kernel = build_kernel(RESET, "reset", "pointer, sint32")
import polyglot from polyglot_adapters import as_js, as_ruby from collections.abc import Iterable js_func = polyglot.eval(language='js', string='(x) => x.toString()') o = object() assert js_func(as_js(o)) == str(o) ruby_func = polyglot.eval(language='ruby', string='->(*args) { args.sort() }') ruby_result = ruby_func(as_ruby([1, 3]), as_ruby([1, 2]), as_ruby([1, 4])) assert [list(x) for x in ruby_result] == [[1, 2], [1, 3], [1, 4]] def same_content(iterable1, iterable2): if len(iterable1) != len(iterable2): return False for value1, value2 in zip(iterable1, iterable2): if value1 != value2: if isinstance(value1, Iterable) and isinstance(value2, Iterable): return same_content(value1, value2) else: return False return True assert same_content(ruby_result, [[1, 2], [1, 3], [1, 4]]) print('Polyglot adapters examples ran successfully!')
from sys import argv import polyglot polyglot.eval(path="calc-pyext.bc", language="llvm") def getOpId(name): func = polyglot.import_value("@getOp" + name) op = func() return op op_add = getOpId("Add") op_sub = getOpId("Sub") op_mul = getOpId("Mul") op_div = getOpId("Div") op_print = getOpId("Print") c_op = polyglot.import_value("@doOp") c_push = polyglot.import_value("@pushNumber") c_result = polyglot.import_value("@getResult") def stack_entry_printer(num): print('->', str(num)) for operand in argv[1:]: if '+' == operand: c_op(op_add, stack_entry_printer)
def compare_objects(cls, object1, object2) -> bool: func = polyglot.eval(language=cls.snippet_language, string="function(x, y) identical(x, y)") return func(object1, object2)
def compare_objects(cls, object1, object2) -> bool: # Stringify objects so that complex objects like lists can be compared func = polyglot.eval( language=cls.snippet_language, string="(x, y) => JSON.stringify(x) === JSON.stringify(y)") return func(object1, object2)
def compare_objects(cls, object1, object2) -> bool: func = polyglot.eval(language=cls.snippet_language, string="[:x :y | x = y]") return func(object1, object2)
def __getNormalizedTable(self, S, Ks, Rs, Sempty, avatar=None): normalizedTable = polyglot.eval(language="morpheusDSL", string="") if avatar is None: raise NotImplementedError normalizedTable.build(S, Ks, Rs, Sempty, avatar) return normalizedTable
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import polyglot import sys import os # avoid using 'os.path' working_dir_parts = __file__.split(os.sep)[:-1] sys.path.insert(0, os.sep.join(working_dir_parts[:-1])) from image_magix import Image load_jpeg = polyglot.eval(string="""function(file.name) { jimg <- read.csv(gzfile(file.name)) return (jimg) }""", language="R") print("stage 1") working_dir_parts.append("img.csv.gz") raw_data = load_jpeg(os.sep.join(working_dir_parts)) # the dimensions are R attributes; define function to access them getDim = polyglot.eval(string="function(v, pos) dim(v)[[pos]]", language="R") getDataRowMajor = polyglot.eval(string="function(v) as.vector(t(v))", language="R") print("stage 2")
import polyglot array = polyglot.eval(language="js", string="[1,2,42,4]") print(array[2])
def alloc(self, size: int, block_size: dict = None) -> None: self.size = size self.num_nnz = size * self.max_degree self.block_size = block_size["block_size_1d"] self.gpu_result = np.zeros(self.size) # Allocate vectors; self.ptr = polyglot.eval(language="grcuda", string=f"int[{size + 1}]") self.ptr2 = polyglot.eval(language="grcuda", string=f"int[{size + 1}]") self.idx = polyglot.eval(language="grcuda", string=f"int[{self.num_nnz}]") self.idx2 = polyglot.eval(language="grcuda", string=f"int[{self.num_nnz}]") self.val = polyglot.eval(language="grcuda", string=f"int[{self.num_nnz}]") self.val2 = polyglot.eval(language="grcuda", string=f"int[{self.num_nnz}]") self.auth1 = polyglot.eval(language="grcuda", string=f"float[{size}]") self.auth2 = polyglot.eval(language="grcuda", string=f"float[{size}]") self.hub1 = polyglot.eval(language="grcuda", string=f"float[{size}]") self.hub2 = polyglot.eval(language="grcuda", string=f"float[{size}]") self.auth_norm = polyglot.eval(language="grcuda", string=f"float[1]") self.hub_norm = polyglot.eval(language="grcuda", string=f"float[1]") # Build the kernels; build_kernel = polyglot.eval(language="grcuda", string="buildkernel") self.spmv_kernel = build_kernel( SPMV_KERNEL, "spmv", "const pointer, const pointer, const pointer, const pointer, pointer, sint32, sint32" ) self.sum_kernel = build_kernel(SUM_KERNEL, "sum", "const pointer, pointer, sint32") self.divide_kernel = build_kernel( DIVIDE_KERNEL, "divide", "const pointer, pointer, pointer, sint32")
def run(self, num_iter: int, policy: str, size: int, realloc: bool, reinit: bool, time_phases: bool, block_size: dict = None, prevent_reinit=False, number_of_blocks=DEFAULT_NUM_BLOCKS) -> None: # Fix missing block size; if "block_size_1d" not in block_size: block_size["block_size_1d"] = DEFAULT_BLOCK_SIZE_1D if "block_size_2d" not in block_size: block_size["block_size_2d"] = DEFAULT_BLOCK_SIZE_2D if number_of_blocks: self.num_blocks = number_of_blocks self.benchmark.start_new_benchmark(name=self.name, policy=policy, size=size, realloc=realloc, reinit=reinit, block_size=block_size, iteration=num_iter, time_phases=time_phases) self.current_iter = num_iter self.time_phases = time_phases self._block_size = block_size # TODO: set the execution policy; # Start a timer to monitor the total GPU execution time; start = System.nanoTime() # Allocate memory for the benchmark; if (num_iter == 0 or realloc) and not prevent_reinit: self.alloc(size, block_size) # Initialize memory for the benchmark; if (num_iter == 0 or reinit) and not prevent_reinit: self.init() # Reset the result; self.reset_result() # Start nvprof profiling if required; if self.nvprof_profile: polyglot.eval(language="grcuda", string="cudaProfilerStart")() # Execute the benchmark; gpu_result = self.execute() # Stop nvprof profiling if required; if self.nvprof_profile: polyglot.eval(language="grcuda", string="cudaProfilerStop")() # Stop the timer; end = System.nanoTime() self.benchmark.add_total_time((end - start) / 1_000_000_000) # Perform validation on CPU; if self.benchmark.cpu_validation: self.cpu_validation(gpu_result, reinit) # Write to file the current result; self.benchmark.save_to_file() # Book-keeping; self.tot_iter += 1
# import polyglot # kernel source code in CUDA C kernel_source = """__global__ void saxpy(int n, float alpha, float *x, float *y) { int i = blockIdx.x * blockDim.x + threadIdx.x; if (i < n) { y[i] = alpha * x[i] + y[i]; } } """ # build kernel from source and create callable buildkernel = polyglot.eval(language='grcuda', string='buildkernel') kernel = buildkernel(kernel_source, 'saxpy', 'sint32, float, pointer, pointer') # create and initialize two device arrays devicearray = polyglot.eval(language='grcuda', string='DeviceArray') n = 1_000_000 arr_x = devicearray('float', n) arr_y = devicearray('float', n) for i in range(n): arr_x[i] = i arr_y[i] = 1 # launch kernel kernel(80, 128)(n, 2, arr_x, arr_y)
def compare_objects(cls, object1, object2) -> bool: func = polyglot.eval(language=cls.snippet_language, string="lambda {|x, y| x == y}") return func(object1, object2)