def __init__(self, fields, str_f, pt0, pt1): """ """ common.check_type('fields', fields, (Fields, BufferFields)) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), int) common.check_type('pt1', pt1, (list, tuple), int) # local variables str_fs = common.convert_to_tuple(str_f) for strf in str_fs: strf_list = ['ex', 'ey', 'ez', 'hx', 'hy', 'hz'] common.check_value('str_f', strf, strf_list) for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) # allocation shape = common.shape_two_points(pt0, pt1, len(str_fs)) host_array = np.zeros(shape, dtype=fields.dtype) split_host_array = np.split(host_array, len(str_fs)) split_host_array_dict = dict( zip(str_fs, split_host_array) ) # global variables self.mainf = fields self.str_fs = str_fs self.slice_xyz = common.slices_two_points(pt0, pt1) self.host_array = host_array self.split_host_array_dict = split_host_array_dict
def __init__(self, fields, str_f, pt0, pt1, is_array=False, is_overwrite=True): """ """ common.check_type('fields', fields, (Fields, BufferFields)) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), int) common.check_type('pt1', pt1, (list, tuple), int) common.check_type('is_array', is_array, bool) common.check_type('is_overwrite', is_overwrite, bool) # local variables str_fs = common.convert_to_tuple(str_f) for strf in str_fs: strf_list = ['ex', 'ey', 'ez', 'hx', 'hy', 'hz'] common.check_value('str_f', strf, strf_list) for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) # global variables and functions self.mainf = fields self.str_fs = str_fs self.slice_xyz = common.slices_two_points(pt0, pt1) self.shape = common.shape_two_points(pt0, pt1, len(str_fs)) self.is_overwrite = is_overwrite if is_array: self.func = self.set_fields_spatial_value else: self.func = self.set_fields_single_value
def __init__(self, fields, str_f, pt0, pt1): """ """ common.check_type('fields', fields, (Fields, BufferFields)) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), int) common.check_type('pt1', pt1, (list, tuple), int) # local variables str_fs = common.convert_to_tuple(str_f) for strf in str_fs: strf_list = ['ex', 'ey', 'ez', 'hx', 'hy', 'hz'] common.check_value('str_f', strf, strf_list) for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) # allocation shape = common.shape_two_points(pt0, pt1, len(str_fs)) host_array = np.zeros(shape, dtype=fields.dtype) split_host_array = np.split(host_array, len(str_fs)) split_host_array_dict = dict(zip(str_fs, split_host_array)) # global variables self.mainf = fields self.str_fs = str_fs self.slice_xyz = common.slices_two_points(pt0, pt1) self.host_array = host_array self.split_host_array_dict = split_host_array_dict
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slice_xyz = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance fields = Fields(0, nx, ny, nz, '', 'single') tfunc = lambda tstep: np.sin(0.03*tstep) incident = IncidentDirect(fields, str_f, pt0, pt1, tfunc, value) # host allocations eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) # verify eh[slice_xyz] = fields.dtype(value) * fields.dtype(tfunc(1)) fields.update_e() fields.update_h() copy_eh_buf = fields.get_buf(str_f) copy_eh = np.zeros_like(eh) cuda.memcpy_dtoh(copy_eh, copy_eh_buf) original = eh[slice_xyz] copy = copy_eh[slice_xyz] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm)) fields.context_pop()
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slice_xyz = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance fields = Fields(0, nx, ny, nz, '', 'single') tfunc = lambda tstep: np.sin(0.03 * tstep) incident = IncidentDirect(fields, str_f, pt0, pt1, tfunc, value) # host allocations eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) # verify eh[slice_xyz] = fields.dtype(value) * fields.dtype(tfunc(1)) fields.update_e() fields.update_h() copy_eh_buf = fields.get_buf(str_f) copy_eh = np.zeros_like(eh) cuda.memcpy_dtoh(copy_eh, copy_eh_buf) original = eh[slice_xyz] copy = copy_eh[slice_xyz] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm)) fields.context_pop()
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slices = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance fields = Fields(nx, ny, nz, '', 'single', 0) tfunc = lambda tstep: np.sin(0.03 * tstep) incident = IncidentDirect(fields, str_f, pt0, pt1, tfunc, value) # host allocations eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) getf = GetFields(fields, str_f, pt0, pt1) # verify eh[slices] = fields.dtype(value) * fields.dtype(tfunc(1)) fields.update_e() fields.update_h() fields.enqueue_barrier() original = eh[slices] getf.get_event().wait() copy = getf.get_fields() norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slices = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance fields = Fields(nx, ny, nz, '', 'single', 0) tfunc = lambda tstep: np.sin(0.03*tstep) incident = IncidentDirect(fields, str_f, pt0, pt1, tfunc, value) # host allocations eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) getf = GetFields(fields, str_f, pt0, pt1) # verify eh[slices] = fields.dtype(value) * fields.dtype(tfunc(1)) fields.update_e() fields.update_h() fields.enqueue_barrier() original = eh[slices] getf.get_event().wait() copy = getf.get_fields() norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1 = self.args slidx = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) device = gpu_devices[0] fields = Fields(context, device, nx, ny, nz, '', 'single') getf = GetFields(fields, str_f, pt0, pt1) # host allocations ehs = common_random.generate_ehs(nx, ny, nz, fields.dtype) eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) ) fields.set_eh_bufs(*ehs) # verify getf.get_event().wait() for str_f in str_fs: original = eh_dict[str_f][slidx] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slices = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append(cpu.Fields(nx, ny, nz)) nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list getf = GetFields(nodef, str_f, (0, 0, 0), (nodef.nx - 1, ny - 1, nz - 1)) setf = SetFields(nodef, str_f, pt0, pt1, is_array) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1, len(str_fs)) value = np.random.rand(*shape).astype(nodef.dtype) split_value = np.split(value, len(str_fs)) split_value_dict = dict(zip(str_fs, split_value)) else: value = np.random.ranf() # host allocations global_ehs = [np.zeros(nodef.ns, dtype) for i in range(6)] eh_dict = dict(zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], global_ehs)) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slices] = split_value_dict[str_f] else: eh_dict[str_f][slices] = value setf.set_fields(value) gpu_getf = gpu.GetFields(mainf_list[0], str_fs, (0, 0, 0), (nx - 1, ny - 1, nz - 1)) gpu_getf.get_event().wait() getf.wait() for str_f in str_fs: original = eh_dict[str_f] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) #if norm != 0: #print '\ngpu getf\n', gpu_getf.get_fields(str_f) #print original[slices] #print copy[slices] self.assertEqual(norm, 0, '%s, %g, %s' % (self.args, norm, str_f))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slices = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append( cpu.Fields(nx, ny, nz) ) nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list getf = GetFields(nodef, str_f, (0, 0, 0), (nodef.nx-1, ny-1, nz-1)) setf = SetFields(nodef, str_f, pt0, pt1, is_array) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1, len(str_fs)) value = np.random.rand(*shape).astype(nodef.dtype) split_value = np.split(value, len(str_fs)) split_value_dict = dict( zip(str_fs, split_value) ) else: value = np.random.ranf() # host allocations global_ehs = [np.zeros(nodef.ns, dtype) for i in range(6)] eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], global_ehs) ) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slices] = split_value_dict[str_f] else: eh_dict[str_f][slices] = value setf.set_fields(value) gpu_getf = gpu.GetFields(mainf_list[0], str_fs, (0, 0, 0), (nx-1, ny-1, nz-1)) gpu_getf.get_event().wait() getf.wait() for str_f in str_fs: original = eh_dict[str_f] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) #if norm != 0: #print '\ngpu getf\n', gpu_getf.get_fields(str_f) #print original[slices] #print copy[slices] self.assertEqual(norm, 0, '%s, %g, %s' % (self.args, norm, str_f))
def __init__(self, fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): common.check_type('fields', fields, Fields) common.check_value('str_f', str_f, ('ex', 'ey', 'ez', 'hx', 'hy', 'hz')) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('tfunc', tfunc, types.FunctionType) common.check_type('spatial_value', spatial_value, \ (np.ndarray, np.number, types.FloatType, types.IntType) ) common.check_type('is_overwrite', is_overwrite, bool) # local variables pt0 = common.convert_indices(fields.ns, pt0) pt1 = common.convert_indices(fields.ns, pt1) dtype = fields.dtype is_array = True if isinstance(spatial_value, np.ndarray) else False for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) if is_array: shape = common.shape_two_points(pt0, pt1) assert shape == spatial_value.shape, \ 'shape mismatch : %s, %s' % (shape, spatial_value.shape) assert dtype == spatial_value.dtype, \ 'dtype mismatch : %s, %s' % (dtype, spatial_value.dtype) else: spatial_value = dtype(spatial_value) # global variables self.mainf = fields self.str_f = str_f self.slices = common.slices_two_points(pt0, pt1) self.tfunc = tfunc self.spatial_value = spatial_value self.is_overwrite = is_overwrite self.e_or_h = str_f[0] self.tstep = 1 # append to the update list self.priority_type = 'incident' fields.append_instance(self)
def runTest(self): nx, ny, nz, str_f, pt0, pt1 = self.args slices = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append( cpu.Fields(nx, ny, nz) ) nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list getf = GetFields(nodef, str_f, pt0, pt1) # generate random source global_ehs = [np.zeros(nodef.ns, dtype) for i in range(6)] eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], global_ehs) ) for i, f in enumerate(mainf_list[:-1]): nx, ny, nz = f.ns ehs = common_random.generate_ehs(nx, ny, nz, dtype) f.set_eh_bufs(*ehs) for eh, geh in zip(ehs, global_ehs): geh[anx[i]:anx[i+1],:,:] = eh[:-1,:,:] f = mainf_list[-1] nx, ny, nz = f.ns ehs = common_random.generate_ehs(nx, ny, nz, dtype) f.set_ehs(*ehs) for eh, geh in zip(ehs, global_ehs): geh[anx[-2]:anx[-1]+1,:,:] = eh[:] # verify getf.wait() for str_f in str_fs: original = eh_dict[str_f][slices] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1 = self.args slices = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append(cpu.Fields(nx, ny, nz)) nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list getf = GetFields(nodef, str_f, pt0, pt1) # generate random source global_ehs = [np.zeros(nodef.ns, dtype) for i in range(6)] eh_dict = dict(zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], global_ehs)) for i, f in enumerate(mainf_list[:-1]): nx, ny, nz = f.ns ehs = common_random.generate_ehs(nx, ny, nz, dtype) f.set_eh_bufs(*ehs) for eh, geh in zip(ehs, global_ehs): geh[anx[i]:anx[i + 1], :, :] = eh[:-1, :, :] f = mainf_list[-1] nx, ny, nz = f.ns ehs = common_random.generate_ehs(nx, ny, nz, dtype) f.set_ehs(*ehs) for eh, geh in zip(ehs, global_ehs): geh[anx[-2]:anx[-1] + 1, :, :] = eh[:] # verify getf.wait() for str_f in str_fs: original = eh_dict[str_f][slices] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slidx = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) device = gpu_devices[0] qtask = QueueTask() fields = Fields(context, device, qtask, nx, ny, nz, '', 'single') setf = SetFields(fields, str_f, pt0, pt1, is_array) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1, len(str_fs)) value = np.random.rand(*shape).astype(fields.dtype) split_value = np.split(value, len(str_fs)) split_value_dict = dict( zip(str_fs, split_value) ) else: value = np.random.ranf() # host allocations ehs = [np.zeros(fields.ns, dtype=fields.dtype) for i in range(6)] eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) ) gpu_eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slidx] = split_value_dict[str_f] else: eh_dict[str_f][slidx] = value setf.set_fields(value) setf.mainf.enqueue_barrier() for str_f in str_fs: cl.enqueue_copy(fields.queue, gpu_eh, fields.get_buf(str_f)) original = eh_dict[str_f] copy = gpu_eh[:,:,fields.slice_z] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slices = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [cpu.Fields(nx, ny, nz)] mainf_list += [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03 * tstep) incident = IncidentDirect(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = GetFields(nodef, str_f, pt0, pt1) # verify eh[slices] = dtype(value) * dtype(tfunc(1)) e_or_h = str_f[0] nodef.update_e() nodef.update_h() getf.wait() original = eh[slices] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slices = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) mainf_list = [ cpu.Fields(nx, ny, nz) ] mainf_list += [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] nodef = Fields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03*tstep) incident = IncidentDirect(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = GetFields(nodef, str_f, pt0, pt1) # verify eh[slices] = dtype(value) * dtype(tfunc(1)) e_or_h = str_f[0] nodef.update_e() nodef.update_h() getf.wait() original = eh[slices] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def __init__(self, fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): common.check_type('fields', fields, Fields) common.check_value('str_f', str_f, ('ex', 'ey', 'ez', 'hx', 'hy', 'hz')) common.check_type('pt0', pt0, (list, tuple), int) common.check_type('pt1', pt1, (list, tuple), int) common.check_type('tfunc', tfunc, types.FunctionType) common.check_type('spatial_value', spatial_value, \ (np.ndarray, np.number, types.FloatType, types.IntType) ) common.check_type('is_overwrite', is_overwrite, bool) # local variables dtype = fields.dtype is_array = True if isinstance(spatial_value, np.ndarray) else False for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) if is_array: shape = common.shape_two_points(pt0, pt1) assert shape == spatial_value.shape, \ 'shape mismatch : %s, %s' % (shape, spatial_value.shape) assert dtype == spatial_value.dtype, \ 'dtype mismatch : %s, %s' % (dtype, spatial_value.dtype) else: spatial_value = dtype(spatial_value) # global variables self.mainf = fields self.str_f = str_f self.slices = common.slices_two_points(pt0, pt1) self.tfunc = tfunc self.spatial_value = spatial_value self.is_overwrite = is_overwrite self.e_or_h = str_f[0] self.tstep = 1 # append to the update list self.priority_type = 'incident' fields.append_instance(self)
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slidx = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance fields = Fields(0, nx, ny, nz, '', 'single') setf = SetFields(fields, str_f, pt0, pt1, is_array) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1, len(str_fs)) value = np.random.rand(*shape).astype(fields.dtype) split_value = np.split(value, len(str_fs)) split_value_dict = dict( zip(str_fs, split_value) ) else: value = np.random.ranf() # host allocations ehs = [np.zeros(fields.ns, dtype=fields.dtype) for i in range(6)] eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) ) gpu_eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slidx] = split_value_dict[str_f] else: eh_dict[str_f][slidx] = value setf.set_fields(value) for str_f in str_fs: cuda.memcpy_dtoh(gpu_eh, fields.get_buf(str_f)) original = eh_dict[str_f] copy = gpu_eh[:,:,fields.slice_z] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm)) fields.context_pop()
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slice_xyz = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) device = gpu_devices[0] qtask = QueueTask() fields = Fields(context, device, qtask, nx, ny, nz, '', 'single') tfunc = lambda tstep: np.sin(0.03*tstep) incident = IncidentDirect(fields, str_f, pt0, pt1, tfunc, value) # host allocations eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) # verify eh[slice_xyz] = fields.dtype(value) * fields.dtype(tfunc(1)) fields.update_e() fields.update_h() fields.enqueue_barrier() copy_eh_buf = fields.get_buf(str_f) copy_eh = np.zeros_like(eh) cl.enqueue_copy(fields.queue, copy_eh, copy_eh_buf) original = eh[slice_xyz] copy = copy_eh[slice_xyz] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slice_xyz = common.slices_two_points(pt0, pt1) # generate random source if is_array: shape = common.shape_two_points(pt0, pt1) value = np.random.rand(*shape).astype(np.float32) else: value = np.random.ranf() # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) device = gpu_devices[0] qtask = QueueTask() fields = Fields(context, device, qtask, nx, ny, nz, '', 'single') tfunc = lambda tstep: np.sin(0.03 * tstep) incident = IncidentDirect(fields, str_f, pt0, pt1, tfunc, value) # host allocations eh = np.zeros(fields.ns_pitch, dtype=fields.dtype) # verify eh[slice_xyz] = fields.dtype(value) * fields.dtype(tfunc(1)) fields.update_e() fields.update_h() fields.enqueue_barrier() copy_eh_buf = fields.get_buf(str_f) copy_eh = np.zeros_like(eh) cl.enqueue_copy(fields.queue, copy_eh, copy_eh_buf) original = eh[slice_xyz] copy = copy_eh[slice_xyz] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1 = self.args slice_xyz = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance fields = Fields(nx, ny, nz, '', 'single') getf = GetFields(fields, str_f, pt0, pt1) # host allocations ehs = common_random.generate_ehs(nx, ny, nz, fields.dtype) eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) ) fields.set_ehs(*ehs) # verify getf.get_event().wait() for str_f in str_fs: original = eh_dict[str_f][slice_xyz] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1 = self.args slice_xyz = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance fields = Fields(nx, ny, nz, '', 'single') getf = GetFields(fields, str_f, pt0, pt1) # host allocations ehs = common_random.generate_ehs(nx, ny, nz, fields.dtype) eh_dict = dict(zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs)) fields.set_ehs(*ehs) # verify getf.get_event().wait() for str_f in str_fs: original = eh_dict[str_f][slice_xyz] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def __init__(self, fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): """ """ common.check_type('fields', fields, Fields) common.check_value('str_f', str_f, ('ex', 'ey', 'ez', 'hx', 'hy', 'hz')) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('tfunc', tfunc, types.FunctionType) common.check_type('spatial_value', spatial_value, \ (np.ndarray, np.number, types.FloatType, types.IntType) ) common.check_type('is_overwrite', is_overwrite, bool) pt0 = list( common.convert_indices(fields.ns, pt0) ) pt1 = list( common.convert_indices(fields.ns, pt1) ) # local variables e_or_h = str_f[0] dtype = fields.dtype is_buffer = True if isinstance(fields, BufferFields) else False is_array = True if isinstance(spatial_value, np.ndarray) else False for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) if is_array: shape = common.shape_two_points(pt0, pt1) assert shape == spatial_value.shape, \ 'shape mismatch : %s, %s' % (shape, spatial_value.shape) assert dtype == spatial_value.dtype, \ 'dtype mismatch : %s, %s' % (dtype, spatial_value.dtype) else: spatial_value = dtype(spatial_value) # create the SetFields instances is_update_dict = {} setf_dict = {} svalue_dict = {} if is_buffer: for part in ['', 'pre', 'post']: sl0 = common.slices_two_points(pt0, pt1) sl1 = common_buffer.slice_dict[e_or_h][part] overlap = common.overlap_two_slices(fields.ns, sl0, sl1) if overlap == None: setf_dict[part] = None else: opt0, opt1 = common.two_points_slices(fields.ns, overlap) setf_dict[part] = SetFields(fields, str_f, opt0, opt1, is_array, is_overwrite) svalue_dict[part] = self.overlap_svalue(pt0, pt1, opt0, opt1, spatial_value, is_array) else: setf_dict[''] = SetFields(fields, str_f, pt0, pt1, is_array, is_overwrite) svalue_dict[''] = spatial_value # global variables self.mainf = fields self.tfunc = tfunc self.setf_dict = setf_dict self.svalue_dict = svalue_dict self.e_or_h = e_or_h self.tstep = 1 # append to the update list self.priority_type = 'incident' fields.append_instance(self)
def __init__(self, fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): """ """ common.check_type('fields', fields, (Fields, BufferFields)) common.check_value('str_f', str_f, ('ex', 'ey', 'ez', 'hx', 'hy', 'hz')) common.check_type('pt0', pt0, (list, tuple), int) common.check_type('pt1', pt1, (list, tuple), int) common.check_type('tfunc', tfunc, types.FunctionType) common.check_type('spatial_value', spatial_value, \ (np.ndarray, np.number, types.FloatType, types.IntType) ) common.check_type('is_overwrite', is_overwrite, bool) # local variables e_or_h = str_f[0] dtype = fields.dtype is_buffer = True if isinstance(fields, BufferFields) else False is_array = True if isinstance(spatial_value, np.ndarray) else False for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) if is_array: shape = common.shape_two_points(pt0, pt1) assert shape == spatial_value.shape, \ 'shape mismatch : %s, %s' % (shape, spatial_value.shape) assert dtype == spatial_value.dtype, \ 'dtype mismatch : %s, %s' % (dtype, spatial_value.dtype) else: spatial_value = dtype(spatial_value) # create the SetFields instances is_update_dict = {} setf_dict = {} svalue_dict = {} if is_buffer: for part in ['', 'pre', 'post']: sl0 = common.slices_two_points(pt0, pt1) sl1 = common_buffer.slice_dict[e_or_h][part] overlap = common.overlap_two_slices(fields.ns, sl0, sl1) if overlap == None: setf_dict[part] = None else: opt0, opt1 = common.two_points_slices(fields.ns, overlap) setf_dict[part] = SetFields(fields, str_f, opt0, opt1, is_array, is_overwrite) svalue_dict[part] = self.overlap_svalue( pt0, pt1, opt0, opt1, spatial_value, is_array) else: setf_dict[''] = SetFields(fields, str_f, pt0, pt1, is_array, is_overwrite) svalue_dict[''] = spatial_value # global variables self.mainf = fields self.tfunc = tfunc self.setf_dict = setf_dict self.svalue_dict = svalue_dict self.e_or_h = e_or_h self.tstep = 1 # append to the update list self.priority_type = 'incident' fields.append_instance(self)
def __init__(self, fields, pt0, pt1, ep_inf, drude_freq, gamma, mask_arrays=(1, 1, 1)): common.check_type('fields', fields, Fields) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('ep_inf', ep_inf, (int, float)) common.check_type('drude_freq', drude_freq, (int, float)) common.check_type('gamma', gamma, (int, float)) common.check_type('mask_arrays', mask_arrays, (list, tuple), (np.ndarray, types.IntType)) # local variables pt0 = common.convert_indices(fields.ns, pt0) pt1 = common.convert_indices(fields.ns, pt1) dtype = fields.dtype for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) for mask_array in mask_arrays: if isinstance(mask_array, np.ndarray): assert common.shape_two_points(pt0, pt1) == mask_array.shape, \ 'shape mismatch : %s, %s' % (shape, mask_array.shape) # allocations shape = common.shape_two_points(pt0, pt1, is_dummy=True) psis = [np.zeros(shape, dtype) for i in range(3)] dt = fields.dt aa = (2 - gamma * dt) / (2 + gamma * dt) bb = drude_freq**2 * dt / (2 + gamma * dt) comm = 2 * ep_inf + bb * dt ca = 2 * dt / comm cb = -(aa + 3) * bb * dt / comm cc = -(aa + 1) * dt / comm cas = [ca * mask for mask in mask_arrays] cbs = [cb * mask for mask in mask_arrays] ccs = [cc * mask for mask in mask_arrays] # modify ce arrays slices = common.slices_two_points(pt0, pt1) for ce, ca in zip(fields.get_ces(), cas): ce[slices] = ca # global variables self.mainf = fields self.psis = psis self.cbs = cbs self.ccs = ccs self.pcs = aa, (aa + 1) * bb self.slices = slices # append to the update list self.priority_type = 'material' fields.append_instance(self)
def __init__(self, fields, pt0, pt1, ep_inf, drude_freq, gamma, mask_arrays=(1, 1, 1)): common.check_type('fields', fields, Fields) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('ep_inf', ep_inf, (int, float)) common.check_type('drude_freq', drude_freq, (int, float)) common.check_type('gamma', gamma, (int, float)) common.check_type('mask_arrays', mask_arrays, (list, tuple), (np.ndarray, int)) # local variables pt0 = common.convert_indices(fields.ns, pt0) pt1 = common.convert_indices(fields.ns, pt1) context = fields.context queue = fields.queue dtype = fields.dtype shape = common.shape_two_points(pt0, pt1, is_dummy=True) for axis, n, p0, p1 in zip(['x', 'y', 'z'], fields.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) for mask_array in mask_arrays: if isinstance(mask_array, np.ndarray): assert common.shape_two_points(pt0, pt1) == mask_array.shape, \ 'shape mismatch : %s, %s' % (shape, mask_array.shape) # allocations psis = [np.zeros(shape, dtype) for i in range(3)] psi_bufs = [ cl.Buffer(context, cl.mem_flags.READ_WRITE, psi.nbytes) for psi in psis ] for psi_buf, psi in zip(psi_bufs, psis): cl.enqueue_copy(queue, psi_buf, psi) dt = fields.dt aa = (2 - gamma * dt) / (2 + gamma * dt) bb = drude_freq**2 * dt / (2 + gamma * dt) comm = 2 * ep_inf + bb * dt ca = 2 * dt / comm cb = -(aa + 3) * bb * dt / comm cc = -(aa + 1) * dt / comm cas = [ca * mask for mask in mask_arrays] shape = common.shape_two_points(pt0, pt1, is_dummy=True) f = np.zeros(shape, dtype) psi_bufs = [ cl.Buffer(context, cl.mem_flags.READ_WRITE, f.nbytes) for i in range(3) ] for psi_buf in psi_bufs: cl.enqueue_copy(queue, psi_buf, f) cf = np.ones(shape, dtype) mask_bufs = [ cl.Buffer(context, cl.mem_flags.READ_ONLY, cf.nbytes) for i in range(3) ] for mask_buf, mask in zip(mask_bufs, mask_arrays): cl.enqueue_copy(queue, mask_buf, cf * mask) # modify ce arrays slices = common.slices_two_points(pt0, pt1) for ce, ca in zip(fields.get_ces(), cas): ce[slices] = ca * mask + ce[slices] * mask.__invert__() # program nmax_str, xid_str, yid_str, zid_str = common_gpu.macro_replace_list( pt0, pt1) macros = ['NMAX', 'XID', 'YID', 'ZID', 'DX', 'DTYPE', 'PRAGMA_fp64'] values = [nmax_str, xid_str, yid_str, zid_str, str(fields.ls)] + fields.dtype_str_list ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'drude.cl').read(), macros, values) program = cl.Program(fields.context, ksrc).build() # arguments pca = aa pcb = (aa + 1) * bb args = fields.ns + [dtype(cb), dtype(cc), dtype(pca), dtype(pcb)] \ + fields.eh_bufs[:3] + psi_bufs + mask_bufs # global variables self.mainf = fields self.program = program self.args = args nx, ny, nz = fields.ns nmax = int(nmax_str) remainder = nmax % fields.ls self.gs = nmax if remainder == 0 else nmax - remainder + fields.ls # append to the update list self.priority_type = 'material' fields.append_instance(self)