def test_call(self): expected = np.zeros([10], dtype=[('x', np.float32), ('y', np.float32)]) expected['x'] = np.arange(10) expected['y'] = np.sin(expected['x'] / 10) program = Program(ctx, source=source) program.build() generate_sin = program.kernel('generate_sin') generate_sin.argtypes = [global_memory(), ctypes.c_float] buf = empty(ctx, [10], ctype=cl.cl_float2) queue = Queue(ctx, ctx.devices[0]) size = [buf.size] with self.assertRaises(TypeError): generate_sin(queue, buf, 1.0) generate_sin(queue, buf, 1.0, global_work_size=size) with buf.map(queue) as host: self.assertTrue(np.all(expected['x'] == np.asarray(host)[:, 0])) self.assertTrue(np.allclose(expected['y'], np.asarray(host)[:, 1])) generate_sin.global_work_size = lambda a, scale: [a.size] generate_sin(queue, buf, 1.0) with buf.map(queue) as host: self.assertTrue(np.all(expected['x'] == np.asarray(host)[:, 0])) self.assertTrue(np.allclose(expected['y'], np.asarray(host)[:, 1]))
def test_name(self): program = Program(ctx, source=source) program.build() generate_sin = program.kernel('generate_sin') self.assertEqual(generate_sin.name, 'generate_sin')
def test_argtypes(self): program = Program(ctx, source=source) program.build() generate_sin = program.kernel('generate_sin') generate_sin.argtypes = [DeviceMemoryView, ctypes.c_float] with self.assertRaises(TypeError): generate_sin.argtypes = [DeviceMemoryView, ctypes.c_float, ctypes.c_float]
def test_binaries(self): program = Program(ctx, source=source) self.assertEqual(program.binaries, dict.fromkeys(ctx.devices)) program.build() binaries = program.binaries self.assertIsNotNone(binaries[ctx.devices[0]]) self.assertEqual(len(binaries[ctx.devices[0]]), program.binary_sizes[0]) program2 = Program(ctx, binaries=binaries) self.assertIsNone(program2.source) self.assertEqual(program2.binaries, binaries)
def test_set_args(self): program = Program(ctx, source=source) program.build() generate_sin = program.kernel('generate_sin') generate_sin.argtypes = [global_memory(), ctypes.c_float] buf = empty(ctx, [10], ctype=cl.cl_float2) queue = Queue(ctx, ctx.devices[0]) generate_sin.set_args(buf, 1.0) queue.enqueue_nd_range_kernel(generate_sin, 1, global_work_size=[buf.size]) expected = np.zeros([10], dtype=[('x', np.float32), ('y', np.float32)]) expected['x'] = np.arange(10) expected['y'] = np.sin(expected['x'] / 10) with buf.map(queue) as host: self.assertTrue(np.all(expected['x'] == np.asarray(host)[:, 0])) self.assertTrue(np.allclose(expected['y'], np.asarray(host)[:, 1])) generate_sin.argnames = ['a', 'scale'] generate_sin.set_args(a=buf, scale=1.0) queue.enqueue_nd_range_kernel(generate_sin, 1, global_work_size=[buf.size]) with buf.map(queue) as host: self.assertTrue(np.all(expected['x'] == np.asarray(host)[:, 0])) self.assertTrue(np.allclose(expected['y'], np.asarray(host)[:, 1])) with self.assertRaises(TypeError): generate_sin.set_args(a=buf) generate_sin.__defaults__ = [1.0] generate_sin.set_args(a=buf) queue.enqueue_nd_range_kernel(generate_sin, 1, global_work_size=[buf.size]) with buf.map(queue) as host: self.assertTrue(np.all(expected['x'] == np.asarray(host)[:, 0])) self.assertTrue(np.allclose(expected['y'], np.asarray(host)[:, 1]))
def test_devices(self): program = Program(ctx, source=source) program.build()
def test_program(self): program = Program(ctx, source=source) program.build()