def _setup_structs_on_device(self): if self.backend == 'opencl': import pyopencl as cl import pyopencl.array # noqa: 401 import pyopencl.tools # noqa: 401 gpu = self._gpu_structs cpu = self._cpu_structs for k, v in cpu.items(): if v is None: gpu[k] = v else: g_struct, code = cl.tools.match_dtype_to_c_struct( self._ctx.devices[0], "dummy", v.dtype) g_v = v.astype(g_struct) gpu[k] = cl.array.to_device(self._queue, g_v) if k in self._equations: self._equations[k]._gpu = gpu[k] else: from pycuda import gpuarray from compyle.cuda import match_dtype_to_c_struct gpu = self._gpu_structs cpu = self._cpu_structs for k, v in cpu.items(): if v is None: gpu[k] = v else: g_struct, code = match_dtype_to_c_struct( None, "junk", v.dtype) g_v = v.astype(g_struct) gpu[k] = gpuarray.to_gpu(g_v) if k in self._equations: self._equations[k]._gpu = gpu[k]
def test_cuda_struct_mapping(self): from compyle.cuda import match_dtype_to_c_struct from pycuda import gpuarray # Given dtype = np.dtype([('l', np.int64), ('i', np.uint8), ('x', np.float32)]) a = np.empty(1, dtype) a['l'] = 1.0 a['i'] = 2 a['x'] = 1.23 # When gs1, code1 = match_dtype_to_c_struct(None, "junk", a.dtype) a_ga = a.astype(gs1) ga = gpuarray.to_gpu(a_ga) # Then result = ga.get() np.testing.assert_almost_equal(result.tolist(), a.tolist()) self.assertFalse(a.dtype.fields == gs1.fields)