def set_bhc_data_from_ary(self, ary): """ Assigns the data using memmove """ dtype = dtype_name(self) assert dtype == dtype_name(ary) ptr = get_data_pointer(self, allocate=True, nullify=False) ctypes.memmove(ptr, ary.ctypes.data, ary.dtype.itemsize * ary.size)
def set_data_pointer(ary, mem_ptr_as_int, host_ptr=True): """ Set the data pointer `mem_ptr_as_int` in the Bohrium Runtime. """ if ary.size == 0 or ary.base.size == 0: return 0 dtype = dtype_name(ary) ary = ary.bhc_obj bhc.call_single_dtype("sync", dtype, ary) runtime_flush() bhc.call_single_dtype("data_set", dtype, ary, host_ptr, mem_ptr_as_int)
def __init__(self, size, dtype, bhc_obj=None): # Total number of elements self.size = size # Data type self.dtype = dtype # Data type name self.dtype_name = dtype_name(dtype) if size == 0: return if bhc_obj is None: bhc_obj = bhc.call_single_dtype("new", self.dtype_name, size) self.bhc_obj = bhc_obj
def extmethod(name, out, in1, in2): """ Apply the extended method 'name' :name str: Name of the extension method. :out ?: :in1 ?: :in2 ?: :rtype: None """ if out.size == 0 or out.base.size == 0: return func = getattr( bhc, "extmethod_A%s_A%s_A%s" % (dtype_name(out), dtype_name(in1), dtype_name(in2))) ret = _bhc_exec(func, name, out, in1, in2) if ret != 0: raise NotImplementedError( "The current runtime system does not support " "the extension method '%s'" % name)
def get_data_pointer(ary, copy2host=True, allocate=False, nullify=False): """ Retrieves the data pointer from Bohrium Runtime. """ if ary.size == 0 or ary.base.size == 0: return 0 dtype = dtype_name(ary) ary = ary.bhc_obj if copy2host: bhc.call_single_dtype("sync", dtype, ary) runtime_flush() data = bhc.call_single_dtype("data_get", dtype, ary, copy2host, allocate, nullify) if data is None: if not allocate: return 0 else: raise MemoryError() return int(data)
def ufunc(op, *args, **kwd): """ Apply the 'op' on args, which is the output followed by one or two inputs Use the 'dtypes' option in 'kwd' to force the data types (None is default) :op npbackend.ufunc.Ufunc: Instance of a Ufunc. :args *?: Probably any one of ndarray, Base, Scalar, View, and npscalar. :rtype: None """ dtypes = kwd.get("dtypes", [None] * len(args)) # Make sure that 'op' is the operation name if hasattr(op, "info"): op = op.info['name'] # The dtype of the scalar argument (if any) is the same as the array input scalar_type = None for arg in args[1:]: if not numpy.isscalar(arg): scalar_type = dtype_name(arg) break # All inputs are scalars if scalar_type is None: if len(args) == 1: scalar_type = dtype_name(args[0]) else: scalar_type = dtype_name(args[1]) fname = "%s" % op for arg, dtype in zip(args, dtypes): if numpy.isscalar(arg): if dtype is None: fname += "_K%s" % scalar_type else: fname += "_K%s" % dtype_name(dtype) else: if dtype is None: fname += "_A%s" % dtype_name(arg) else: fname += "_A%s" % dtype_name(dtype) _bhc_exec(getattr(bhc, fname), *args)
def runtime_sync(ary): """Sync `ary` to host memory""" dtype = dtype_name(ary) ary = ary.bhc_obj bhc.call_single_dtype("sync", dtype, ary)