def test_copy_1D(self): queue = Queue(ctx, ctx.devices[0]) a = np.array([1, 2, 3, 4]) clbuf = DeviceMemoryView.from_host(ctx, a) copy_of = clbuf[::2].copy(queue) with copy_of.map(queue) as cpy: b = np.asarray(cpy) self.assertTrue(np.all(a[::2] == b)) copy_of = clbuf[1::2].copy(queue) with copy_of.map(queue) as cpy: b = np.asarray(cpy) self.assertTrue(np.all(a[1::2] == b)) copy_of = clbuf[1:-1].copy(queue) with copy_of.map(queue) as cpy: b = np.asarray(cpy) self.assertTrue(np.all(a[1:-1] == b))
def test_dim_reduce(self): queue = Queue(ctx, ctx.devices[0]) a = np.array([[1, 2], [3, 4], [5, 6]]) view = DeviceMemoryView.from_host(ctx, a) new_view = view[:, 0] self.assertEqual(new_view.ndim, a[:, 0].ndim) self.assertEqual(new_view.shape, a[:, 0].shape) self.assertEqual(new_view.offset_, 0) self.assertEqual(new_view.strides, a[:, 0].strides) with new_view.map(queue) as buf: b = np.asarray(buf) self.assertTrue(np.all(b == a[:, 0])) new_view = view[:, 1] with new_view.map(queue) as buf: b = np.asarray(buf) self.assertTrue(np.all(b == a[:, 1])) new_view = view[0, :] with new_view.map(queue) as buf: b = np.asarray(buf) self.assertTrue(np.all(b == a[0, :])) new_view = view[1, :] with new_view.map(queue) as buf: b = np.asarray(buf) self.assertTrue(np.all(b == a[1, :]))
def test_get_slice(self): queue = Queue(ctx, ctx.devices[0]) a = np.array([1, 2, 3, 4]) clbuf = DeviceMemoryView.from_host(ctx, a) self.assertEqual(clbuf._refcount, 1) new_buf = clbuf[::2] with new_buf.map(queue) as buf: b = np.asanyarray(buf) self.assertTrue(np.all(b == a[::2])) new_buf = clbuf[1::2] with new_buf.map(queue) as buf: b = np.asanyarray(buf) self.assertTrue(np.all(b == a[1::2])) new_buf = clbuf[::-1] with new_buf.map(queue) as buf: b = np.asanyarray(buf) self.assertTrue(np.all(b == a[::-1]))
def test_copy_2D(self): queue = Queue(ctx, ctx.devices[0]) a = np.arange(6 * 6).reshape([6, 6]) clbuf = DeviceMemoryView.from_host(ctx, a) slices = [ (slice(None, None, 2), slice(None, None, 2)), (slice(1, None, None), slice(1, None, None)), (slice(None, None, None), slice(1, None, None)), (slice(1, None, None), slice(None, None, None)), (slice(1, None, None), slice(0, None, 2)), (slice(None, None, 2), slice(1, None, 2)), (slice(1, None, 2), slice(None, None, 2)), ] for idx0, idx1 in slices: expected = a[idx0, idx1] sub_buf = clbuf[idx0, idx1] copy_of = sub_buf.copy(queue) with copy_of.map(queue) as cpy: b = np.asarray(cpy) expected = a[idx0, idx1] self.assertTrue(np.all(expected == b), (idx0, idx1))
def test_copy_2D_negative_stride(self): queue = Queue(ctx, ctx.devices[0]) a = np.arange(4 * 4).reshape([4, 4]) clbuf = DeviceMemoryView.from_host(ctx, a) slices = [(slice(None, None, -2), slice(None, None, -2)), (slice(1, None, -1), slice(1, None, -1)), (slice(None, None, None), slice(1, None, -1)), (slice(1, None, -1), slice(None, None, -1)), (slice(1, None, -2), slice(1, None, -2)), (slice(None, None, -2), slice(1, None, -2)), (slice(1, None, -2), slice(None, None, -2)), ] for idx0, idx1 in slices: copy_of = clbuf[idx0, idx1].copy(queue) with copy_of.map(queue) as cpy: b = np.asarray(cpy) expected = a[idx0, idx1] self.assertTrue(np.all(expected == b))
def test_from_host(self): a = np.array([[1, 2], [3, 4]]) view_a = memoryview(a) clmem = DeviceMemoryView.from_host(ctx, a) self.assertEqual(clmem.format, view_a.format) self.assertEqual(clmem.shape, view_a.shape) self.assertEqual(clmem.strides, view_a.strides)
def test_is_contiguous(self): a = np.array([[1, 2], [3, 4]]) clbuf = DeviceMemoryView.from_host(ctx, a) self.assertTrue(clbuf.is_contiguous) self.assertFalse(clbuf[:, 1:].is_contiguous) self.assertFalse(clbuf[::2, :].is_contiguous) self.assertFalse(clbuf[:, ::2].is_contiguous)
def test_copy_contig(self): queue = Queue(ctx, ctx.devices[0]) a = np.array([[1, 2], [3, 4]]) clbuf = DeviceMemoryView.from_host(ctx, a) copy_of = clbuf.copy(queue) queue.barrier() with copy_of.map(queue) as cpy: b = np.asarray(cpy) self.assertTrue(np.all(a == b))
def test_refcount(self): a = np.array([[1, 2], [3, 4]]) clbuf = DeviceMemoryView.from_host(ctx, a) self.assertEqual(clbuf._refcount, 1) new_buf = clbuf[:, :-1] self.assertEqual(clbuf._refcount, 2) del new_buf gc.collect() self.assertEqual(clbuf._refcount, 1) self.assertEqual(clbuf.base, None) #create sub_buffer new_buf = clbuf[1, :] self.assertEqual(clbuf._refcount, 2) del new_buf gc.collect() self.assertEqual(clbuf._refcount, 1) queue = Queue(ctx) with clbuf.map(queue) as host: self.assertEqual(clbuf._refcount, 1) self.assertEqual(clbuf._refcount, 2, "unmap increments the refcount") del host gc.collect() #GPU may not decrement the ref count #unless finish is called queue.finish() self.assertEqual(clbuf._refcount, 1) event = PyEvent() def callback(mem): event.set()
def test_read_write(self): a = np.array([[1, 2], [3, 4]]) clbuf = DeviceMemoryView.from_host(ctx, a) queue = Queue(ctx, ctx.devices[0]) out = np.zeros_like(a) clbuf.read(queue, out, blocking=True) self.assertTrue(np.all(out == a)) clbuf.write(queue, a + 1, blocking=True) clbuf.read(queue, out, blocking=True) self.assertTrue(np.all(out == a + 1))
def test_map(self): a = np.array([[1, 2], [3, 4]]) view_a = memoryview(a) clbuf = DeviceMemoryView.from_host(ctx, a) queue = Queue(ctx, ctx.devices[0]) self.assertEqual(clbuf._mapcount, 0) with clbuf.map(queue, writeable=False) as buf: self.assertEqual(clbuf._mapcount, 1) self.assertEqual(buf.format, view_a.format) self.assertEqual(buf.shape, view_a.shape) self.assertEqual(buf.strides, view_a.strides) b = np.asarray(buf) self.assertTrue(np.all(b == a)) self.assertTrue(buf.readonly) # self.assertEqual(clbuf._mapcount, 0) with clbuf.map(queue, readable=False) as buf: self.assertEqual(clbuf._mapcount, 1) b = np.asarray(buf) b[::] = a[::-1] self.assertFalse(buf.readonly) # self.assertEqual(clbuf._mapcount, 0) with clbuf.map(queue, writeable=False) as buf: self.assertEqual(clbuf._mapcount, 1) b = np.asarray(buf) self.assertTrue(np.all(b == a[::-1]))
def test_getitem(self): queue = Queue(ctx, ctx.devices[0]) a = np.array([[1, 2], [3, 4]]) clbuf = DeviceMemoryView.from_host(ctx, a) with self.assertRaises(IndexError): clbuf[1, 1, 1] self.assertEqual(clbuf._refcount, 1) new_buf = clbuf[:, :-1] self.assertEqual(clbuf._refcount, 2) mapp = new_buf.map(queue) with mapp as buf: b = np.asanyarray(buf) self.assertTrue(np.all(b == a[:, :-1])) del buf del new_buf gc.collect() new_buf = clbuf[:, 1:] with new_buf.map(queue) as buf: b = np.asanyarray(buf) self.assertTrue(np.all(b == a[:, 1:])) new_buf = clbuf[1:, :] with new_buf.map(queue) as buf: b = np.asanyarray(buf) self.assertTrue(np.all(b == a[1:, :]))
def test_copy_2D_negative_stride(self): queue = Queue(ctx, ctx.devices[0]) a = np.arange(4 * 4).reshape([4, 4]) clbuf = DeviceMemoryView.from_host(ctx, a) slices = [ (slice(None, None, -2), slice(None, None, -2)), (slice(1, None, -1), slice(1, None, -1)), (slice(None, None, None), slice(1, None, -1)), (slice(1, None, -1), slice(None, None, -1)), (slice(1, None, -2), slice(1, None, -2)), (slice(None, None, -2), slice(1, None, -2)), (slice(1, None, -2), slice(None, None, -2)), ] for idx0, idx1 in slices: copy_of = clbuf[idx0, idx1].copy(queue) with copy_of.map(queue) as cpy: b = np.asarray(cpy) expected = a[idx0, idx1] self.assertTrue(np.all(expected == b))