def test_sliceset_computations(self): ''' macroparticles per slice, particles_within_cuts require a sliceset as a parameter Check that CPU/GPU functions yield the same result (if both exist) No complete tracking, only bare functions. ''' fname = ['particles_within_cuts', 'macroparticles_per_slice'] pm.update_active_dict(pm._CPU_numpy_func_dict) np.random.seed(0) n = 999 b = self.create_gaussian_bunch(n) b.sort_for('z') slicer = UniformBinSlicer(n_slices=20, n_sigma_z=2) s_set = b.get_slices(slicer) z_cpu = b.z.copy() z_gpu = pycuda.gpuarray.to_gpu(z_cpu) sliceset_cpu = s_set sliceset_gpu = copy.deepcopy(s_set) sliceset_gpu.slice_index_of_particle = pycuda.gpuarray.to_gpu( s_set.slice_index_of_particle) params_cpu = [sliceset_cpu] params_gpu = [sliceset_gpu] for f in fname: res_cpu = pm._CPU_numpy_func_dict[f](*params_cpu) res_gpu = pm._GPU_func_dict[f](*params_gpu) self.assertTrue( np.allclose(res_cpu, res_gpu.get()), 'CPU/GPU version of ' + f + ' dont yield the same result')
def test_set_GPU(self): pm.update_active_dict(pm._GPU_func_dict) self.assertTrue( set(self.available_GPU).issubset(set(pm.__dict__.keys())), 'Setting the active dict to GPU fails. Not all GPU functions ' + 'were spilled to pm.globals()') self.assertFalse( set(self.available_CPU).issubset(set(pm.__dict__.keys())), 'Setting the active dict to GPU fails. Not all CPU functions ' + 'were deleted from pm.globals() when switching to GPU.')
def __exit__(self, exc_type, exc_value, traceback): ''' Move all data back to the CPU (and un-patch the methods?) Reestablish state of everything as it was before entering Remove slice records from bunch. ''' self.bunch.clean_slices() for coord in self.to_move: obj = getattr(self.bunch, coord, None) if isinstance(obj, pycuda.gpuarray.GPUArray): setattr(self.bunch, coord, obj.get()) pm.update_active_dict(pm._CPU_numpy_func_dict)
def __enter__(self): ''' Move all data to the GPU (and monkey patch methods?) Returns self (eg. to provide info about gpu/status/...) Remove slice records from bunch. ''' self.bunch.clean_slices() for coord in self.to_move: obj = getattr(self.bunch, coord, None) if isinstance(obj, np.ndarray): setattr(self.bunch, coord, gpuarray.to_gpu(obj, gpu_utils.memory_pool.allocate)) # replace functions in general.math.py pm.update_active_dict(pm._GPU_func_dict) return self
def test_set_CPU(self): pm.update_active_dict(pm._CPU_numpy_func_dict) self.assertTrue( set(self.available_CPU).issubset(set(pm.__dict__.keys())), 'Setting the active dict to CPU fails. Not all CPU functions ' + 'were spilled to pm.globals()')