def _gen_uniform_dataset(n, h, seed=None): if seed is not None: np.random.seed(seed) u = np.random.uniform pa = get_particle_array(x=u(size=n), y=u(size=n), z=u(size=n), h=h) h = DeviceHelper(pa, backend='opencl') pa.set_device_helper(h) return pa
def test_extract_particles(self, backend): check_import(backend) self.setup() # Given pa = get_particle_array(name='f', x=[0.0, 1.0, 2.0, 3.0], m=1.0, rho=2.0) h = DeviceHelper(pa, backend=backend) pa.set_device_helper(h) # When indices = np.array([1, 2], dtype=np.uint32) indices = array.to_device(indices, backend=backend) result_pa = h.extract_particles(indices) # Then assert result_pa.gpu.get_number_of_particles() == 2
def test_push_correctly_sets_values_with_no_args(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) pa.x[:] = 1.0 pa.rho[:] = 1.0 pa.m[:] = 1.0 pa.tag[:] = [1, 2] h.push() # Then assert np.allclose(pa.x, h.x.get()) assert np.allclose(pa.y, h.y.get()) assert np.allclose(pa.m, h.m.get()) assert np.allclose(pa.rho, h.rho.get()) assert np.allclose(pa.tag, h.tag.get())
def test_push_correctly_sets_values_with_args(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) assert pa.tag[0] == 0 # When pa.set_device_helper(h) pa.x[:] = [2.0, 3.0] pa.rho[0] = 1.0 pa.tag[:] = 1 h.push('x', 'rho', 'tag') # Then assert np.allclose(pa.x, h.x.get()) assert np.allclose(pa.y, h.y.get()) assert np.allclose(pa.m, h.m.get()) assert np.allclose(pa.rho, h.rho.get()) assert np.allclose(pa.tag, h.tag.get())
def test_resize_works(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) pa.extend(2) pa.align_particles() h.resize(4) # Then assert np.allclose(pa.x[:2], h.x[:2].get()) assert np.allclose(pa.m[:2], h.m[:2].get()) assert np.allclose(pa.rho[:2], h.rho[:2].get()) assert np.allclose(pa.tag[:2], h.tag[:2].get()) # When pa.remove_particles([2, 3]) pa.align_particles() old_x = h.x.data h.resize(2) # Then assert old_x == h.x.data assert np.allclose(pa.x, h.x.get()) assert np.allclose(pa.y, h.y.get()) assert np.allclose(pa.m, h.m.get()) assert np.allclose(pa.rho, h.rho.get()) assert np.allclose(pa.tag, h.tag.get())
def test_align(self, backend): check_import(backend) self.setup() # Given pa = self.pa pa.add_property('force', stride=3) h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) n = 5 h.resize(n) h.x.set(np.array([2.0, 3.0, 4.0, 5.0, 6.0], h.x.dtype)) h.force.set(np.arange(n * 3, dtype=h.force.dtype)) indices = array.arange(4, -1, -1, dtype=np.int32, backend=backend) h.align(indices) # Then assert np.all(h.x.get() == np.array([6., 5., 4., 3., 2.])) x = np.arange(n * 3) x.shape = (n, 3) expect = x[::-1, :].ravel() assert np.all(h.force.get() == expect)
def _setup_arrays_on_device(self): pas = self.object.particle_arrays array_map = {} array_index = {} for idx, pa in enumerate(pas): if pa.gpu is None: pa.set_device_helper(DeviceHelper(pa, backend=self.backend)) array_map[pa.name] = pa array_index[pa.name] = idx self._array_map = array_map self._array_index = array_index self._setup_structs_on_device()
def test_simple(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) # Then assert np.allclose(pa.x, h.x.get()) assert np.allclose(pa.y, h.y.get()) assert np.allclose(pa.m, h.m.get()) assert np.allclose(pa.rho, h.rho.get()) assert np.allclose(pa.tag, h.tag.get())
def test_get_number_of_particles(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) h.resize(5) h.x.set(np.array([2.0, 3.0, 4.0, 5.0, 6.0], h.x.dtype)) h.tag.set(np.array([0, 0, 1, 0, 1], h.tag.dtype)) h.align_particles() # Then assert h.get_number_of_particles() == 5 assert h.get_number_of_particles(real=True) == 3
def test_that_adding_removing_prop_to_array_updates_gpu(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) pa.add_property('test', data=[3.0, 4.0]) # Then assert np.allclose(pa.test, h.test.get()) # When pa.remove_property('test') # Then assert not hasattr(h, 'test') assert 'test' not in h._data assert 'test' not in h.properties
def test_remove_tagged_particles(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) h.resize(5) h.x.set(np.array([2.0, 3.0, 4.0, 5.0, 6.0], h.x.dtype)) h.tag.set(np.array([0, 0, 1, 0, 1], h.tag.dtype)) h.remove_tagged_particles(1) # Then assert np.all(np.sort(h.x.get()) == np.array([2., 3., 5.]))
def test_align(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) h.resize(5) h.x.set(np.array([2.0, 3.0, 4.0, 5.0, 6.0], h.x.dtype)) indices = array.arange(4, -1, -1, dtype=np.int32, backend=backend) h.align(indices) # Then assert np.all(h.x.get() == np.array([6., 5., 4., 3., 2.]))
def test_remove_particles(self, backend): check_import(backend) self.setup() # Given pa = self.pa h = DeviceHelper(pa, backend=backend) # When pa.set_device_helper(h) h.resize(4) h.x.set(np.array([2.0, 3.0, 4.0, 5.0], h.x.dtype)) indices = np.array([1, 2], dtype=np.uint32) indices = array.to_device(indices, backend=backend) h.remove_particles(indices) # Then assert np.all(np.sort(h.x.get()) == np.array([2., 5.]))
def test_update_minmax_cl(self): backend = 'opencl' check_import(backend) self.setup() # Given x = [0.0, -1.0, 2.0, 3.0] y = [0.0, 1.0, -2.0, 3.0] z = [0.0, 1.0, 2.0, -3.0] h = [4.0, 1.0, 2.0, 3.0] pa = get_particle_array(x=x, y=y, z=z, h=h) h = DeviceHelper(pa, backend=backend) pa.set_device_helper(h) # When h.update_minmax_cl(['x', 'y', 'z', 'h']) # Then assert h.x.minimum == -1.0 assert h.x.maximum == 3.0 assert h.y.minimum == -2.0 assert h.y.maximum == 3.0 assert h.z.minimum == -3.0 assert h.z.maximum == 2.0 assert h.h.minimum == 1.0 assert h.h.maximum == 4.0 # When h.x.maximum, h.x.minimum = 100., 100. h.y.maximum, h.y.minimum = 100., 100. h.z.maximum, h.z.minimum = 100., 100. h.h.maximum, h.h.minimum = 100., 100. h.update_minmax_cl(['x', 'y', 'z', 'h'], only_min=True) # Then assert h.x.minimum == -1.0 assert h.x.maximum == 100.0 assert h.y.minimum == -2.0 assert h.y.maximum == 100.0 assert h.z.minimum == -3.0 assert h.z.maximum == 100.0 assert h.h.minimum == 1.0 assert h.h.maximum == 100.0 # When h.x.maximum, h.x.minimum = 100., 100. h.y.maximum, h.y.minimum = 100., 100. h.z.maximum, h.z.minimum = 100., 100. h.h.maximum, h.h.minimum = 100., 100. h.update_minmax_cl(['x', 'y', 'z', 'h'], only_max=True) # Then assert h.x.minimum == 100.0 assert h.x.maximum == 3.0 assert h.y.minimum == 100.0 assert h.y.maximum == 3.0 assert h.z.minimum == 100.0 assert h.z.maximum == 2.0 assert h.h.minimum == 100.0 assert h.h.maximum == 4.0