def test_invoke_kernel_arrays_float(self): """Test if a kernel is correctly invoked with copyin/copyout semantics for array arguments of dtype=float.""" a = numpy.arange(0.0, 4711.0 * 1024, dtype=float) b = numpy.arange(0.0, 4096.0, dtype=float) p = 1.5 s = 0.5 a_expect = a + p b_expect = b - s c_expect = (a.shape[0], b.shape[0]) device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() stream.invoke(library.test_offload_stream_kernel_arrays_float, a, b, a.size, b.size, p, s) stream.sync() self.assertTrue((a == a_expect).all(), "Wrong contents of array: " "{0} should be {1}".format(a, a_expect)) self.assertTrue((a == a_expect).all(), "Wrong contents of array: " "{0} should be {1}".format(b, b_expect))
def impl_test_dgemm(self, size): """Test an MKL dgemm with 256x256 matrices.""" device = pymic.devices[0] stream = device.get_default_stream() library = get_library(device, "libtests.so") m, n, k = size, size, size alpha, beta = 1.2, 1.2 numpy.random.seed(10) a = numpy.random.random(m * k).reshape((m, k)) b = numpy.random.random(k * n).reshape((k, n)) c = numpy.random.random(m * n).reshape((m, n)) A = numpy.matrix(a) B = numpy.matrix(b) C = numpy.matrix(c) expect = numpy.array(alpha * A * B + beta * C) offl_a = stream.bind(a) offl_b = stream.bind(b) offl_c = stream.bind(c) stream.invoke(library.test_kernel_dgemm, offl_a, offl_b, offl_c, m, n, k, alpha, beta) offl_c.update_host() stream.sync() r = offl_c.array self.assertEqualEpsilon(r.reshape((m * n,)), expect.reshape((m * n,)), "Array contains unexpected values: " "{0} should be {1}".format(r, expect))
def test_invoke_empty_kernel(self): """Test if invoke_kernel_string does successfully invoke a kernel function with no arguments.""" device = pymic.devices[0] stream = device.get_default_stream() library = get_library(device, "libtests.so") stream.invoke(library.test_offload_stream_empty) stream.sync()
def test_library_device_mismatch(self): """Test that kernel invocation fails if library has been loaded for another device.""" device1 = pymic.devices[0] device2 = pymic.devices[1] stream = device2.get_default_stream() library = get_library(device1, "libkernelnames.so") self.assertRaises(pymic.OffloadError, stream.invoke, library.kernel_underscores)
def test_invoke_kernel_nullptr_1(self): """Test if invoke kernel passes a None array as nullptr on the kernel side.""" device = pymic.devices[0] stream = device.get_default_stream() library = get_library(device, "libtests.so") result = numpy.empty((1,), dtype=int) result[0] = 1234567890 stream.invoke(library.test_offload_stream_nullptr_1, None, result) stream.sync() self.assertEqual(result[0], 0)
def test_invoke_kernel_nullptr_1(self): """Test if invoke kernel passes a None array as nullptr on the kernel side.""" device = pymic.devices[0] stream = device.get_default_stream() library = get_library(device, "libtests.so") result = numpy.empty((1, ), dtype=int) result[0] = 1234567890 stream.invoke(library.test_offload_stream_nullptr_1, None, result) stream.sync() self.assertEqual(result[0], 0)
def test_invalid_kernel_name(self): """Test if invoke_kernel_string throws an exception if a kernel's name does not exist in any loaded library.""" device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() try: self.assertRaises(pymic.OffloadError, library.this_kernel_does_not_exist_anywhere) except pymic.OffloadError: self.assertTrue(True) else: self.assertFalse(False)
def test_invoke_kernel_scalar(self): """Test if a kernel is correctly invoked with copyin/copyout semantics for scalar arguments.""" a = 42 b = 42.0 c = 0 d = 0.0 ival = numpy.empty((2, ), dtype=int) dval = numpy.empty((2, ), dtype=float) device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() stream.invoke(library.test_offload_stream_kernel_scalars, a, b, c, d, ival, dval) stream.sync() self.assertEqual((ival[0], ival[1]), (42, 0)) self.assertEqual((dval[0], dval[1]), (42.0, 0.0))
def test_bind_float(self): """Test proper functioning of the bind operation.""" device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() a = numpy.arange(0.0, 4711.0 * 1024, dtype=float) r = numpy.empty((1, ), dtype=float) offl_a = stream.bind(a) offl_r = stream.bind(r) stream.invoke(library.test_sum_float, offl_a, offl_a.size, offl_r) offl_r.update_host() stream.sync() self.assertEqual(r[0], sum(a)) self.assertEqual(offl_a.dtype, a.dtype) self.assertEqual(offl_a.shape, a.shape)
def test_invoke_kernel_scalar(self): """Test if a kernel is correctly invoked with copyin/copyout semantics for scalar arguments.""" a = 42 b = 42.0 c = 0 d = 0.0 ival = numpy.empty((2,), dtype=int) dval = numpy.empty((2,), dtype=float) device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() stream.invoke(library.test_offload_stream_kernel_scalars, a, b, c, d, ival, dval) stream.sync() self.assertEqual((ival[0], ival[1]), (42, 0)) self.assertEqual((dval[0], dval[1]), (42.0, 0.0))
def test_bind_float(self): """Test proper functioning of the bind operation.""" device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() a = numpy.arange(0.0, 4711.0 * 1024, dtype=float) r = numpy.empty((1,), dtype=float) offl_a = stream.bind(a) offl_r = stream.bind(r) stream.invoke(library.test_sum_float, offl_a, offl_a.size, offl_r) offl_r.update_host() stream.sync() self.assertEqual(r[0], sum(a)) self.assertEqual(offl_a.dtype, a.dtype) self.assertEqual(offl_a.shape, a.shape)
def test_kernel_names_available(self): """Test if all device kernels can be found with their correct function pointers.""" device = pymic.devices[0] stream = device.get_default_stream() library = get_library(device, "libkernelnames.so") pointers = numpy.empty((5,), dtype=numpy.int64) offl_pointers = stream.bind(pointers) stream.invoke(library.test_offload_library_get, offl_pointers) offl_pointers.update_host() stream.sync() kernels = [library.kernel_underscores, library.a, library.bb, library._bb, library.a123] for p, k in zip(pointers, kernels): self.assertEqual(p, k[1], "Kernel pointer does not match (0x{0:x} " "should be 0x{0:x}".format(k[1], p))
def test_bind_int(self): """Test proper functioning of the bind operation.""" pattern = int(0xdeadbeefabbaabba) device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() a = numpy.empty((4711 * 1024, ), dtype=int) r = numpy.empty((1, ), dtype=int) a[:] = pattern offl_a = stream.bind(a) offl_r = stream.bind(r) stream.invoke(library.test_check_pattern, offl_a, offl_a.size, offl_r, pattern) offl_r.update_host() stream.sync() self.assertEqual(r[0], a.shape[0]) self.assertEqual(offl_a.dtype, a.dtype) self.assertEqual(offl_a.shape, a.shape)
def test_bind_int(self): """Test proper functioning of the bind operation.""" pattern = int(0xdeadbeefabbaabba) device = pymic.devices[0] library = get_library(device, "libtests.so") stream = device.get_default_stream() a = numpy.empty((4711 * 1024,), dtype=int) r = numpy.empty((1,), dtype=int) a[:] = pattern offl_a = stream.bind(a) offl_r = stream.bind(r) stream.invoke(library.test_check_pattern, offl_a, offl_a.size, offl_r, pattern) offl_r.update_host() stream.sync() self.assertEqual(r[0], a.shape[0]) self.assertEqual(offl_a.dtype, a.dtype) self.assertEqual(offl_a.shape, a.shape)
def test_too_many_arguments(self): device = pymic.devices[0] stream = device.get_default_stream() library = get_library(device, "libtests.so") a0, a1 = numpy.array([0]), numpy.array([1]) a2, a3 = numpy.array([2]), numpy.array([3]) a4, a5 = numpy.array([4]), numpy.array([5]) a6, a7 = numpy.array([6]), numpy.array([7]) a8, a9 = numpy.array([8]), numpy.array([9]) aA, aB = numpy.array([10]), numpy.array([11]) aC, aD = numpy.array([12]), numpy.array([13]) aE, aF = numpy.array([14]), numpy.array([15]) aG = numpy.array([16]) try: stream.invoke(library.test_too_many_arguments, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, aA, aB, aC, aD, aE, aF, aG) except ValueError: self.assertTrue(True) else: self.assertTrue(False)