def rand(context, queue, shape, dtype): from pyopencl.array import Array from pyopencl.elementwise import get_elwise_kernel result = Array(context, shape, dtype, queue=queue) if dtype == numpy.float32: func = get_elwise_kernel(context, "float *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0f) dest[i] = a*POW_2_M32; if ((i += gsize) < n) dest[i] = b*POW_2_M32; if ((i += gsize) < n) dest[i] = c*POW_2_M32; if ((i += gsize) < n) dest[i] = d*POW_2_M32; """, "md5_rng_float") elif dtype == numpy.float64: func = get_elwise_kernel(context, "double *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0) #define POW_2_M64 (1/18446744073709551616.) dest[i] = a*POW_2_M32 + b*POW_2_M64; if ((i += gsize) < n) { dest[i] = c*POW_2_M32 + d*POW_2_M64; } """, "md5_rng_float") elif dtype in [numpy.int32, numpy.uint32]: func = get_elwise_kernel(context, "unsigned int *dest, unsigned int seed", md5_code + """ dest[i] = a; if ((i += gsize) < n) dest[i] = b; if ((i += gsize) < n) dest[i] = c; if ((i += gsize) < n) dest[i] = d; """, "md5_rng_int") else: raise NotImplementedError func(queue, result._global_size, result._local_size, result.data, numpy.random.randint(2**31-1), result.size) return result
def rand(context, queue, shape, dtype): from pyopencl.array import Array from pyopencl.elementwise import get_elwise_kernel result = Array(context, shape, dtype, queue=queue) if dtype == numpy.float32: func = get_elwise_kernel( context, "float *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0f) dest[i] = a*POW_2_M32; if ((i += gsize) < n) dest[i] = b*POW_2_M32; if ((i += gsize) < n) dest[i] = c*POW_2_M32; if ((i += gsize) < n) dest[i] = d*POW_2_M32; """, "md5_rng_float") elif dtype == numpy.float64: func = get_elwise_kernel( context, "double *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0) #define POW_2_M64 (1/18446744073709551616.) dest[i] = a*POW_2_M32 + b*POW_2_M64; if ((i += gsize) < n) { dest[i] = c*POW_2_M32 + d*POW_2_M64; } """, "md5_rng_float") elif dtype in [numpy.int32, numpy.uint32]: func = get_elwise_kernel( context, "unsigned int *dest, unsigned int seed", md5_code + """ dest[i] = a; if ((i += gsize) < n) dest[i] = b; if ((i += gsize) < n) dest[i] = c; if ((i += gsize) < n) dest[i] = d; """, "md5_rng_int") else: raise NotImplementedError func(queue, result._global_size, result._local_size, result.data, numpy.random.randint(2**31 - 1), result.size) return result
def _get_wave_kernel(ctx): return cl_elwise.get_elwise_kernel( ctx, [VectorArg(np.float32, 'res', with_offset=True), ConstArg(np.float32, 'in'), ScalarArg(np.float32, 'h'), ScalarArg(np.int32, 'len')], 'res[i] = calc_wave_func(in, h, len, i)', preamble="#include <wave_func.cl>", options=['-I', _path.dirname(__file__)])
def _get_bloch_kernel(ctx): return cl_elwise.get_elwise_kernel( ctx, [VectorArg(np.complex64, 'res', with_offset=True), ConstArg(np.complex64, 'in'), ScalarArg(np.float32, 't'), ScalarArg(np.float32, 'slope'), ScalarArg(np.int32, 'len')], 'calc_bloch_wave(res, in, t, slope, len, i)', preamble="#include <bloch_wave.cl>", options=['-I', _path.dirname(__file__)])
def get_rand_kernel(context, dtype): from pyopencl.elementwise import get_elwise_kernel if dtype == numpy.float32: return get_elwise_kernel(context, "float *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0f) dest[i] = a*POW_2_M32; if ((i += gsize) < n) dest[i] = b*POW_2_M32; if ((i += gsize) < n) dest[i] = c*POW_2_M32; if ((i += gsize) < n) dest[i] = d*POW_2_M32; """, "md5_rng_float") elif dtype == numpy.float64: return get_elwise_kernel(context, "double *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0) #define POW_2_M64 (1/18446744073709551616.) dest[i] = a*POW_2_M32 + b*POW_2_M64; if ((i += gsize) < n) { dest[i] = c*POW_2_M32 + d*POW_2_M64; } """, "md5_rng_float") elif dtype in [numpy.int32, numpy.uint32]: return get_elwise_kernel(context, "unsigned int *dest, unsigned int seed", md5_code + """ dest[i] = a; if ((i += gsize) < n) dest[i] = b; if ((i += gsize) < n) dest[i] = c; if ((i += gsize) < n) dest[i] = d; """, "md5_rng_int") else: raise NotImplementedError
def get_rand_kernel(context, dtype): from pyopencl.elementwise import get_elwise_kernel if dtype == numpy.float32: return get_elwise_kernel( context, "float *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0f) dest[i] = a*POW_2_M32; if ((i += gsize) < n) dest[i] = b*POW_2_M32; if ((i += gsize) < n) dest[i] = c*POW_2_M32; if ((i += gsize) < n) dest[i] = d*POW_2_M32; """, "md5_rng_float") elif dtype == numpy.float64: return get_elwise_kernel( context, "double *dest, unsigned int seed", md5_code + """ #define POW_2_M32 (1/4294967296.0) #define POW_2_M64 (1/18446744073709551616.) dest[i] = a*POW_2_M32 + b*POW_2_M64; if ((i += gsize) < n) { dest[i] = c*POW_2_M32 + d*POW_2_M64; } """, "md5_rng_float") elif dtype in [numpy.int32, numpy.uint32]: return get_elwise_kernel( context, "unsigned int *dest, unsigned int seed", md5_code + """ dest[i] = a; if ((i += gsize) < n) dest[i] = b; if ((i += gsize) < n) dest[i] = c; if ((i += gsize) < n) dest[i] = d; """, "md5_rng_int") else: raise NotImplementedError
def lin_comb_kernel(ctx, res_type, ary_type, weight_type, length, name=None): res_type = np.dtype(res_type) ary_type = np.dtype(ary_type) weight_type = np.dtype(weight_type) res_fmt, mul_fmt, _ = _get_lin_comb_expr_fmts(ary_type, weight_type, res_type) mul_fmt = mul_fmt % ('ary%d[i]', 'weight%d') expr = ' + '.join((mul_fmt % (i, i)) for i in _range(length)) expr = res_fmt % expr name = name or 'lin_comb_kernel' return cl_elwise.get_elwise_kernel( ctx, [VectorArg(res_type, 'res', with_offset=True)] + [ConstArg(ary_type, 'ary%d' % i) for i in _range(length)] + [ScalarArg(weight_type, 'weight%d' % i) for i in _range(length)], 'res[i] = ' + expr, name=name, auto_preamble=True)