def test_product(dtype, nelem): if np.dtype(dtype).kind == 'i': data = np.ones(nelem, dtype=dtype) # Set at most 30 items to [0..2) to keep the value within 2^32 for _ in range(30): data[random.randrange(nelem)] = random.random() * 2 else: data = gen_rand(dtype, nelem) print('max', data.max(), 'min', data.min()) d_data = cuda.to_device(data) d_result = cuda.device_array(libgdf.gdf_reduce_optimal_output_size(), dtype=d_data.dtype) col_data = new_column() gdf_dtype = get_dtype(dtype) libgdf.gdf_column_view(col_data, unwrap_devary(d_data), ffi.NULL, nelem, gdf_dtype) libgdf.gdf_product_generic(col_data, unwrap_devary(d_result), d_result.size) got = d_result.copy_to_host()[0] expect = np.product(data) print('expect:', expect) print('got:', got) np.testing.assert_array_almost_equal(expect, got)
def test_sum_of_squares(dtype, nelem): data = gen_rand(dtype, nelem) d_data = rmm.to_device(data) d_result = rmm.device_array(libgdf.gdf_reduce_optimal_output_size(), dtype=d_data.dtype) col_data = new_column() gdf_dtype = get_dtype(dtype) libgdf.gdf_column_view(col_data, unwrap_devary(d_data), ffi.NULL, nelem, gdf_dtype) libgdf.gdf_sum_of_squares(col_data, unwrap_devary(d_result), d_result.size) got = d_result.copy_to_host()[0] expect = (data ** 2).sum() print('expect:', expect) print('got:', got) if np.dtype(dtype).kind == 'i': if 0 <= expect <= np.iinfo(dtype).max: np.testing.assert_array_almost_equal(expect, got) else: print('overflow, passing') else: np.testing.assert_array_almost_equal(expect, got, decimal=accuracy_for_dtype[dtype])
def apply_reduce(fn, inp): # allocate output+temp array outsz = libgdf.gdf_reduce_optimal_output_size() out = rmm.device_array(outsz, dtype=inp.dtype) # call reduction fn(inp.cffi_view, unwrap_devary(out), outsz) # return 1st element return out[0]
def apply_reduce(fn, inp): # allocate output+temp array outsz = libgdf.gdf_reduce_optimal_output_size() out = cuda.device_array(outsz, dtype=inp.dtype) # call reduction fn(inp.cffi_view, unwrap_devary(out), outsz) # return 1st element return out[0]
def test_sum(dtype, nelem): data = gen_rand(dtype, nelem) d_data = cuda.to_device(data) d_result = cuda.device_array(libgdf.gdf_reduce_optimal_output_size(), dtype=d_data.dtype) col_data = new_column() gdf_dtype = get_dtype(dtype) libgdf.gdf_column_view(col_data, unwrap_devary(d_data), ffi.NULL, nelem, gdf_dtype) libgdf.gdf_sum_generic(col_data, unwrap_devary(d_result), d_result.size) got = d_result.copy_to_host()[0] expect = data.sum() print('expect:', expect) print('got:', got) np.testing.assert_array_almost_equal(expect, got)
def test_max(dtype, nelem): data = gen_rand(dtype, nelem) d_data = rmm.to_device(data) d_result = rmm.device_array(libgdf.gdf_reduce_optimal_output_size(), dtype=d_data.dtype) col_data = new_column() gdf_dtype = get_dtype(dtype) libgdf.gdf_column_view(col_data, unwrap_devary(d_data), ffi.NULL, nelem, gdf_dtype) libgdf.gdf_max(col_data, unwrap_devary(d_result), d_result.size) got = d_result.copy_to_host()[0] expect = data.max() print('expect:', expect) print('got:', got) assert expect == got
def test_sum_masked(nelem): dtype = np.float64 data = gen_rand(dtype, nelem) mask = gen_rand(np.int8, (nelem + 8 - 1) // 8) d_data = cuda.to_device(data) d_mask = cuda.to_device(mask) d_result = cuda.device_array(libgdf.gdf_reduce_optimal_output_size(), dtype=d_data.dtype) col_data = new_column() gdf_dtype = get_dtype(dtype) libgdf.gdf_column_view(col_data, unwrap_devary(d_data), unwrap_devary(d_mask), nelem, gdf_dtype) libgdf.gdf_sum_generic(col_data, unwrap_devary(d_result), d_result.size) got = d_result.copy_to_host()[0] boolmask = buffer_as_bits(mask)[:nelem] expect = data[boolmask].sum() np.testing.assert_almost_equal(expect, got)