def alloc_remote_npy(self, typecode, comps, width, height = 1, globl = False): """Allocate a NumPy ndarray backed by remote (main) memory.""" if not HAS_NUMPY: raise ImportError("NumPy array support requires NumPy installation") fmt = self._get_fmt(typecode, comps) if typecode == 'f': dtype = numpy.float32 elif typecode == 'i': dtype = numpy.int32 elif typecode == 'I': dtype = numpy.uint32 else: raise Exception("Unsupported data type: " + str(typecode)) if globl: globl = cal_exec.GLOBAL_BUFFER buf = cal_exec.calmembuffer(self.device, fmt, width, height, globl) arr = numpy.frombuffer(buf, dtype=dtype) if height == 1: arr.shape = (width, comps) else: arr.shape = (buf.pitch, height, comps) return arr
def alloc_remote_npy(self, typecode, comps, width, height=1, globl=False): try: import numpy except ImportError: raise ImportError("alloc_remote_npy() requires NumPy") if typecode == 'f': if comps == 1: fmt = cal_exec.FMT_FLOAT32_1 elif comps == 2: fmt = cal_exec.FMT_FLOAT32_2 elif comps == 4: fmt = cal_exec.FMT_FLOAT32_4 else: raise Exception("Number of components must be 1, 2, or 4") dtype = numpy.float32 elif typecode == 'i': if comps == 1: fmt = cal_exec.FMT_SIGNED_INT32_1 elif comps == 2: fmt = cal_exec.FMT_SIGNED_INT32_2 elif comps == 4: fmt = cal_exec.FMT_SIGNED_INT32_4 else: raise Exception("Number of components must be 1, 2, or 4") dtype = numpy.int32 elif typecode == 'I': if comps == 1: fmt = cal_exec.FMT_UNSIGNED_INT32_1 elif comps == 2: fmt = cal_exec.FMT_UNSIGNED_INT32_2 elif comps == 4: fmt = cal_exec.FMT_UNSIGNED_INT32_4 else: raise Exception("Number of components must be 1, 2, or 4") dtype = numpy.uint32 else: raise Exception("Unsupported data type: " + str(typecode)) if globl: globl = cal_exec.GLOBAL_BUFFER buf = cal_exec.calmembuffer(self.device, fmt, width, height, globl) arr = numpy.frombuffer(buf, dtype=dtype) if height == 1: arr.shape = (width, comps) else: arr.shape = (buf.pitch, height, comps) return arr
def alloc_remote_npy(self, typecode, comps, width, height = 1, globl = False): try: import numpy except ImportError: raise ImportError("alloc_remote_npy() requires NumPy") if typecode == 'f': if comps == 1: fmt = cal_exec.FMT_FLOAT32_1 elif comps == 2: fmt = cal_exec.FMT_FLOAT32_2 elif comps == 4: fmt = cal_exec.FMT_FLOAT32_4 else: raise Exception("Number of components must be 1, 2, or 4") dtype = numpy.float32 elif typecode == 'i': if comps == 1: fmt = cal_exec.FMT_SIGNED_INT32_1 elif comps == 2: fmt = cal_exec.FMT_SIGNED_INT32_2 elif comps == 4: fmt = cal_exec.FMT_SIGNED_INT32_4 else: raise Exception("Number of components must be 1, 2, or 4") dtype = numpy.int32 elif typecode == 'I': if comps == 1: fmt = cal_exec.FMT_UNSIGNED_INT32_1 elif comps == 2: fmt = cal_exec.FMT_UNSIGNED_INT32_2 elif comps == 4: fmt = cal_exec.FMT_UNSIGNED_INT32_4 else: raise Exception("Number of components must be 1, 2, or 4") dtype = numpy.uint32 else: raise Exception("Unsupported data type: " + str(typecode)) if globl: globl = cal_exec.GLOBAL_BUFFER buf = cal_exec.calmembuffer(self.device, fmt, width, height, globl) arr = numpy.frombuffer(buf, dtype=dtype) if height == 1: arr.shape = (width, comps) else: arr.shape = (buf.pitch, height, comps) return arr