def create_instance(self, fields, fields_pt0, fields_pt1, overlap_pt0, overlap_pt1): sx0, sy0, sz0 = fields_pt0 ox0, oy0, oz0 = overlap_pt0 ox1, oy1, oz1 = overlap_pt1 subf_pt0 = (ox0 - sx0, oy0 - sy0, oz0 - sz0) subf_pt1 = (ox1 - sx0, oy1 - sy0, oz1 - sz0) if self.is_array: x0, y0, z0 = self.pt0 svalue_pt0 = (ox0 - x0, oy0 - y0, oz0 - z0) svalue_pt1 = (ox1 - x0, oy1 - y0, oz1 - z0) dummied_shape = common.shape_two_points(self.pt0, self.pt1, is_dummy=True) dummied_shaped_svalue = self.spatial_value.reshape(dummied_shape) dummied_slices = [ slice(p0, p1 + 1) for p0, p1 in zip(svalue_pt0, svalue_pt1) ] subdomain_shape = common.shape_two_points(svalue_pt0, svalue_pt1) svalue_subdomain = dummied_shaped_svalue[dummied_slices].reshape( subdomain_shape).copy() else: svalue_subdomain = self.spatial_value getattr(self, fields.device_type).IncidentDirect( \ fields, self.str_f, subf_pt0, subf_pt1, self.tfunc, svalue_subdomain, self.is_overwrite)
def overlap_svalue(self, pt0, pt1, opt0, opt1, spatial_value, is_array): if is_array: dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) overlap_shape = common.shape_two_points(opt0, opt1) shift_slices = [slice(op0-p0, op1-p0+1) \ for op0, op1, p0 in zip(opt0, opt1, pt0)] reshaped_svalue = spatial_value.reshape(dummied_shape) dummied_svalue = reshaped_svalue[shift_slices] svalue = dummied_svalue.reshape(overlap_shape) else: svalue = spatial_value return svalue
def __init__(self, fields, str_f, pt0, pt1, is_array=False, is_overwrite=True): """ """ common.check_type("fields", fields, Fields) 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.slice_index_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 runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array, mpi_type = self.args slices = common.slice_index_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, mpi_type=mpi_type) tfunc = lambda tstep: np.sin(0.03 * tstep) incident = DirectIncident(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 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 __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) 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.slice_index_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, mpi_type = self.args slices = common.slice_index_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, mpi_type=mpi_type) tfunc = lambda tstep: np.sin(0.03*tstep) incident = DirectIncident(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 __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, 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_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 instance setf = SetFields(fields, str_f, pt0, pt1, is_array, is_overwrite) # global variables self.mainf = fields self.tfunc = tfunc self.setf = setf self.spatial_value = spatial_value 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): """ """ common.check_type('fields', fields, Fields) 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) dtype_str_list = fields.dtype_str_list 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)) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', 'OVERWRITE', \ 'DTYPE', 'PRAGMA_fp64'] values = common_gpu.macro_replace_list(pt0, pt1) + \ ['__global DTYPE *source', \ 'target[sub_idx]', 'source[idx]', '='] + \ dtype_str_list ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'copy.cl').read(), macros, values) program = cl.Program(fields.context, ksrc).build() # allocation source_bufs = [fields.get_buf(str_f) for str_f in str_fs] 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)) target_buf = cl.Buffer( \ fields.context, \ cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR, \ hostbuf=host_array) # global variables self.mainf = fields self.program = program self.source_bufs = source_bufs self.target_buf = target_buf self.host_array = host_array self.split_host_array_dict = split_host_array_dict
def __init__(self, fields, str_f, pt0, pt1): """ """ common.check_type('fields', fields, Fields) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) pt0 = list(common.convert_indices(fields.ns, pt0)) pt1 = list(common.convert_indices(fields.ns, pt1)) # local variables str_fs = common.convert_to_tuple(str_f) dtype_str_list = fields.dtype_str_list 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)) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', 'OVERWRITE', \ 'DTYPE'] values = common_gpu.macro_replace_list(pt0, pt1) + \ ['DTYPE *source', \ 'target[sub_idx]', 'source[idx]', '='] + \ dtype_str_list ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'copy.cu').read(), macros, values) program = SourceModule(ksrc) kernel_copy = program.get_function('copy') # allocation source_bufs = [fields.get_buf(str_f) for str_f in str_fs] shape = common.shape_two_points(pt0, pt1, len(str_fs)) host_array = np.zeros(shape, fields.dtype) split_host_array = np.split(host_array, len(str_fs)) split_host_array_dict = dict(zip(str_fs, split_host_array)) target_buf = cuda.to_device(host_array) # global variables self.mainf = fields self.kernel_copy = kernel_copy self.source_bufs = source_bufs self.target_buf = target_buf 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 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): """ """ common.check_type('fields', fields, Fields) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) pt0 = list( common.convert_indices(fields.ns, pt0) ) pt1 = list( common.convert_indices(fields.ns, pt1) ) # local variables str_fs = common.convert_to_tuple(str_f) dtype_str_list = fields.dtype_str_list 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)) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', 'OVERWRITE', \ 'DTYPE'] values = common_gpu.macro_replace_list(pt0, pt1) + \ ['DTYPE *source', \ 'target[sub_idx]', 'source[idx]', '='] + \ dtype_str_list ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'copy.cu').read(), macros, values) program = SourceModule(ksrc) kernel_copy = program.get_function('copy') # allocation source_bufs = [fields.get_buf(str_f) for str_f in str_fs] shape = common.shape_two_points(pt0, pt1, len(str_fs)) host_array = np.zeros(shape, fields.dtype) split_host_array = np.split(host_array, len(str_fs)) split_host_array_dict = dict( zip(str_fs, split_host_array) ) target_buf = cuda.to_device(host_array) # global variables self.mainf = fields self.kernel_copy = kernel_copy self.source_bufs = source_bufs self.target_buf = target_buf 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 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 set_savefields(self, str_f, pt0, pt1, tstep_range, dir_path, tag=0): common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('tstep_range', tstep_range, (list, tuple), int) common.check_type('dir_path', dir_path, str) common.check_type('tag', tag, int) pt0 = common.convert_indices(self.ns, pt0) pt1 = common.convert_indices(self.ns, pt1) t0 = common.convert_index(self.max_tstep, tstep_range[0]) t1 = common.convert_index(self.max_tstep, tstep_range[1]) + 1 tgap = tstep_range[2] tmax = self.max_tstep tag = tag if tag not in self.savef_tag_list else max(self.savef_tag_list)+1 self.savef_tag_list.append(tag) if is_mpi: overlap = common.overlap_two_regions(self.node_pt0, self.node_pt1, pt0, pt1) if overlap != None: sx0, sy0, sz0 = self.node_pt0 ox0, oy0, oz0 = overlap[0] ox1, oy1, oz1 = overlap[1] local_pt0 = (ox0-sx0, oy0-sy0, oz0-sz0) local_pt1 = (ox1-sx0, oy1-sy0, oz1-sz0) savef = node.SaveFields(self.nodef, str_f, local_pt0, local_pt1, dir_path, tag, tmax, True, rank) self.savef_list.append(savef) else: savef = node.SaveFields(self.nodef, str_f, pt0, pt1, dir_path, tag, tmax) self.savef_list.append(savef) # save the subdomain informations as pickle if self.is_master: if is_mpi: divide_info_dict = common_mpi.divide_info_dict(size, self.mpi_shape, pt0, pt1, self.asn_dict) else: divide_info_dict = { \ 'shape': common.shape_two_points(pt0, pt1), \ 'pt0': pt0, \ 'pt1': pt1, \ 'anx_list': self.nodef.accum_nx_list } divide_info_dict['str_fs'] = common.convert_to_tuple(str_f) divide_info_dict['tmax'] = tmax divide_info_dict['tgap'] = tgap pkl_file = open(dir_path + 'divide_info.pkl', 'wb') pickle.dump(divide_info_dict, pkl_file) pkl_file.close() self.t0 = t0 self.t1 = t1 self.tgap = tgap
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 e_or_h = str_f[0] 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) # create the SetFields instance setf = SetFields(fields, str_f, pt0, pt1, is_array, is_overwrite) # global variables self.mainf = fields self.dtype = dtype self.tfunc = tfunc self.setf = setf self.spatial_value = spatial_value self.tstep = 1 # global functions if e_or_h == 'e': self.update_e = self.update self.update_h = lambda : None elif e_or_h == 'h': self.update_e = lambda : None self.update_h = self.update # append to the update list self.priority_type = 'incident' fields.append_instance(self)
def create_instance(self, fields, fields_pt0, fields_pt1, overlap_pt0, overlap_pt1): sx0, sy0, sz0 = fields_pt0 ox0, oy0, oz0 = overlap_pt0 ox1, oy1, oz1 = overlap_pt1 subf_pt0 = (ox0-sx0, oy0-sy0, oz0-sz0) subf_pt1 = (ox1-sx0, oy1-sy0, oz1-sz0) if self.is_array: x0, y0, z0 = self.pt0 svalue_pt0 = (ox0-x0, oy0-y0, oz0-z0) svalue_pt1 = (ox1-x0, oy1-y0, oz1-z0) dummied_shape = common.shape_two_points(self.pt0, self.pt1, is_dummy=True) dummied_shaped_svalue = self.spatial_value.reshape(dummied_shape) dummied_slices = [slice(p0, p1+1) for p0, p1 in zip(svalue_pt0, svalue_pt1)] subdomain_shape = common.shape_two_points(svalue_pt0, svalue_pt1) svalue_subdomain = dummied_shaped_svalue[dummied_slices].reshape(subdomain_shape).copy() else: svalue_subdomain = self.spatial_value getattr(self, fields.device_type).IncidentDirect( \ fields, self.str_f, subf_pt0, subf_pt1, self.tfunc, svalue_subdomain, self.is_overwrite)
def __init__(self, fields, str_f, pt0, pt1): """ """ common.check_type("fields", fields, Fields) 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) dtype_str_list = fields.dtype_str_list 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)) # program macros = ["NMAX", "XID", "YID", "ZID", "ARGS", "TARGET", "SOURCE", "OVERWRITE", "DTYPE", "PRAGMA_fp64"] values = ( common_gpu.macro_replace_list(pt0, pt1) + ["__global DTYPE *source", "target[sub_idx]", "source[idx]", "="] + dtype_str_list ) ksrc = common.replace_template_code(open(common_gpu.src_path + "copy.cl").read(), macros, values) program = cl.Program(fields.context, ksrc).build() # allocation source_bufs = [fields.get_buf(str_f) for str_f in str_fs] 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)) target_buf = cl.Buffer(fields.context, cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR, hostbuf=host_array) # global variables self.mainf = fields self.program = program self.source_bufs = source_bufs self.target_buf = target_buf self.host_array = host_array self.split_host_array_dict = split_host_array_dict
def __init__(self, fields, str_f, pt0, pt1, source_is_array=False): """ """ common.check_type('fields', fields, Fields) 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('source_is_array', source_is_array, bool) self.mainf = mainf = fields str_fs = common.convert_to_tuple(str_f) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', \ 'DTYPE', 'PRAGMA_fp64'] if source_is_array: values = macro_replace_list(pt0, pt1) + \ ['__global DTYPE *source', \ 'target[idx]', 'source[sub_idx]'] + mainf.dtype_str_list[:2] else: values = macro_replace_list(pt0, pt1) + \ ['DTYPE source', \ 'target[idx]', 'source'] + mainf.dtype_str_list[:2] ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'subdomain.cl').read(), macros, values) self.program = cl.Program(mainf.context, ksrc).build() # allocation self.target_bufs = [mainf.get_buf(str_f) for str_f in str_fs] shape = list(common.shape_two_points(pt0, pt1)) shape[0] *= len(str_fs) if source_is_array: tmp_array = np.zeros(shape, dtype=mainf.dtype) self.source_buf = cl.Buffer( \ mainf.context, \ cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR, \ hostbuf=tmp_array) self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
def __init__(self, fields, str_f, pt0, pt1, source_is_array=False): """ """ common.check_type('fields', fields, Fields) 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('source_is_array', source_is_array, bool) self.mainf = mainf = fields str_fs = common.convert_to_tuple(str_f) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', \ 'DTYPE', 'PRAGMA_fp64'] if source_is_array: values = macro_replace_list(pt0, pt1) + \ ['__global DTYPE *source', \ 'target[idx]', 'source[sub_idx]'] + mainf.dtype_str_list[:2] else: values = macro_replace_list(pt0, pt1) + \ ['DTYPE source', \ 'target[idx]', 'source'] + mainf.dtype_str_list[:2] ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'subdomain.cl').read(), macros, values) self.program = cl.Program(mainf.context, ksrc).build() # allocation self.target_bufs = [mainf.get_buf(str_f) for str_f in str_fs] shape = list( common.shape_two_points(pt0, pt1) ) shape[0] *= len(str_fs) if source_is_array: tmp_array = np.zeros(shape, dtype=mainf.dtype) self.source_buf = cl.Buffer( \ mainf.context, \ cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR, \ hostbuf=tmp_array) self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
def runTest(self): if len(self.args) == 6: nx, ny, nz, str_f, pt0, pt1 = self.args src_is_array = False elif len(self.args) == 7: nx, ny, nz, str_f, pt0, pt1, src_is_array = self.args slidx = common.slice_index_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, '') setf = SetFields(fields, str_f, pt0, pt1, src_is_array) # generate random source if src_is_array: shape = list(common.shape_two_points(pt0, pt1)) shape[0] *= 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 eh_dict = {} for sf in str_fs: eh_dict[sf] = np.zeros(fields.ns, dtype=fields.dtype) gpu_eh = np.zeros(fields.ns, dtype=fields.dtype) # verify for str_f in str_fs: if src_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: cl.enqueue_copy(fields.queue, gpu_eh, fields.get_buf(str_f)) self.assertEqual( np.abs(eh_dict[str_f] - gpu_eh).max(), 0, self.args)
def runTest(self): if len(self.args) == 6: nx, ny, nz, str_f, pt0, pt1 = self.args src_is_array = False elif len(self.args) == 7: nx, ny, nz, str_f, pt0, pt1, src_is_array = self.args slidx = common.slice_index_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, '') setf = SetFields(fields, str_f, pt0, pt1, src_is_array) # generate random source if src_is_array: shape = list( common.shape_two_points(pt0, pt1) ) shape[0] *= 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 eh_dict = {} for sf in str_fs: eh_dict[sf] = np.zeros(fields.ns, dtype=fields.dtype) gpu_eh = np.zeros(fields.ns, dtype=fields.dtype) # verify for str_f in str_fs: if src_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: cl.enqueue_copy(fields.queue, gpu_eh, fields.get_buf(str_f)) self.assertEqual(np.abs(eh_dict[str_f] - gpu_eh).max(), 0, self.args)
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.slice_index_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 = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append(cpu.Fields(nx, ny, nz)) nodef = NodeFields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03 * tstep) incident = NodeDirectIncident(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = NodeGetFields(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.slice_index_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 = [gpu.Fields(context, device, nx, ny, nz) \ for device in gpu_devices] mainf_list.append( cpu.Fields(nx, ny, nz) ) nodef = NodeFields(mainf_list) dtype = nodef.dtype anx = nodef.accum_nx_list tfunc = lambda tstep: np.sin(0.03*tstep) incident = NodeDirectIncident(nodef, str_f, pt0, pt1, tfunc, value) # allocations for verify eh = np.zeros(nodef.ns, dtype) getf = NodeGetFields(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 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 __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, str_f, pt0, pt1, is_array=False, is_overwrite=True): """ """ common.check_type("fields", fields, Fields) 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) dtype_str_list = fields.dtype_str_list overwrite_str = {True: "=", False: "+="}[is_overwrite] 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)) # program macros = ["NMAX", "XID", "YID", "ZID", "ARGS", "TARGET", "SOURCE", "OVERWRITE", "DTYPE", "PRAGMA_fp64"] if is_array: values = ( common_gpu.macro_replace_list(pt0, pt1) + ["__global DTYPE *source", "target[idx]", "source[sub_idx]", overwrite_str] + dtype_str_list ) else: values = ( common_gpu.macro_replace_list(pt0, pt1) + ["DTYPE source", "target[idx]", "source", overwrite_str] + dtype_str_list ) ksrc = common.replace_template_code(open(common_gpu.src_path + "copy.cl").read(), macros, values) program = cl.Program(fields.context, ksrc).build() # allocation target_bufs = [fields.get_buf(str_f) for str_f in str_fs] shape = common.shape_two_points(pt0, pt1, len(str_fs)) if is_array: tmp_array = np.zeros(shape, dtype=fields.dtype) source_buf = cl.Buffer( fields.context, cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR, hostbuf=tmp_array ) # global variabels and functions self.mainf = fields self.program = program self.target_bufs = target_bufs self.shape = shape if is_array: self.source_buf = source_buf self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
def __init__(self, node_fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): """ """ common.check_type('node_fields', node_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 nodef = node_fields dtype = nodef.dtype is_array = True if isinstance(spatial_value, np.ndarray) else False mainf_list = nodef.mainf_list buffer_dict = nodef.buffer_dict anx = nodef.accum_nx_list nx, ny, nz = nodef.ns for axis, n, p0, p1 in zip(['x', 'y', 'z'], nodef.ns, pt0, pt1): start, end = 0, n if buffer_dict.has_key(axis + '+'): end = n + 1 if buffer_dict.has_key(axis + '-'): start = -1 common.check_value('pt0 %s' % axis, p0, range(start, end)) common.check_value('pt1 %s' % axis, p1, range(start, end)) 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 valriables self.str_f = str_f self.pt0 = pt0 self.pt1 = pt1 self.tfunc = tfunc self.spatial_value = spatial_value self.is_overwrite = is_overwrite self.is_array = is_array self.cpu = cpu if 'gpu' in [f.device_type for f in nodef.updatef_list]: from kemp.fdtd3d import gpu self.gpu = gpu # create IncidentDirect instance for i, mainf in enumerate(mainf_list): fields_pt0 = (anx[i], 0, 0) fields_pt1 = (anx[i + 1] - 1, ny - 1, nz - 1) overlap = common.overlap_two_regions(fields_pt0, fields_pt1, pt0, pt1) if overlap != None: self.create_instance(mainf, fields_pt0, fields_pt1, overlap[0], overlap[1]) # for buffer for direction, buffer in buffer_dict.items(): fields_pt0 = { \ 'x+': (anx[-1]-1, 0, 0), \ 'y+': (0, ny-2, 0), \ 'z+': (0, 0, nz-2), \ 'x-': (-1, 0, 0), \ 'y-': (0, -1, 0), \ 'z-': (0, 0, -1) }[direction] fields_pt1 = { \ 'x+': (anx[-1]+1, ny-1, nz-1), \ 'y+': (nx-1, ny, nz-1), \ 'z+': (nx-1, ny-1, nz), \ 'x-': (1, ny-1, nz-1), \ 'y-': (nx-1, 1, nz-1), \ 'z-': (nx-1, ny-1, 1) }[direction] overlap = common.overlap_two_regions(fields_pt0, fields_pt1, pt0, pt1) if overlap != None: self.create_instance(buffer, fields_pt0, fields_pt1, overlap[0], overlap[1])
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, str_f, pt0, pt1, is_array=False, is_overwrite=True): """ """ common.check_type('fields', fields, Fields) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('is_array', is_array, bool) 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 str_fs = common.convert_to_tuple(str_f) dtype_str_list = fields.dtype_str_list overwrite_str = {True: '=', False: '+='}[is_overwrite] 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)) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', 'OVERWRITE', \ 'DTYPE'] if is_array: values = common_gpu.macro_replace_list(pt0, pt1) + \ ['DTYPE *source', \ 'target[idx]', 'source[sub_idx]', overwrite_str] + \ dtype_str_list else: values = common_gpu.macro_replace_list(pt0, pt1) + \ ['DTYPE source', \ 'target[idx]', 'source', overwrite_str] + \ dtype_str_list ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'copy.cu').read(), macros, values) program = SourceModule(ksrc) kernel_copy = program.get_function('copy') # allocation target_bufs = [fields.get_buf(str_f) for str_f in str_fs] shape = common.shape_two_points(pt0, pt1, len(str_fs)) if is_array: tmp_array = np.zeros(shape, fields.dtype) source_buf = cuda.to_device(tmp_array) # global variabels and functions self.mainf = fields self.kernel_copy = kernel_copy self.target_bufs = target_bufs self.shape = shape if is_array: self.source_buf = source_buf self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
def set_savefields(self, str_f, pt0, pt1, tstep_range, dir_path, tag=0): common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('tstep_range', tstep_range, (list, tuple), int) common.check_type('dir_path', dir_path, str) common.check_type('tag', tag, int) pt0 = common.convert_indices(self.ns, pt0) pt1 = common.convert_indices(self.ns, pt1) t0 = common.convert_index(self.max_tstep, tstep_range[0]) t1 = common.convert_index(self.max_tstep, tstep_range[1]) + 1 tgap = tstep_range[2] tmax = self.max_tstep tag = tag if tag not in self.savef_tag_list else max( self.savef_tag_list) + 1 self.savef_tag_list.append(tag) if is_mpi: overlap = common.overlap_two_regions(self.node_pt0, self.node_pt1, pt0, pt1) if overlap != None: sx0, sy0, sz0 = self.node_pt0 ox0, oy0, oz0 = overlap[0] ox1, oy1, oz1 = overlap[1] local_pt0 = (ox0 - sx0, oy0 - sy0, oz0 - sz0) local_pt1 = (ox1 - sx0, oy1 - sy0, oz1 - sz0) savef = node.SaveFields(self.nodef, str_f, local_pt0, local_pt1, dir_path, tag, tmax, True, rank) self.savef_list.append(savef) else: savef = node.SaveFields(self.nodef, str_f, pt0, pt1, dir_path, tag, tmax) self.savef_list.append(savef) # save the subdomain informations as pickle if self.is_master: if is_mpi: divide_info_dict = common_mpi.divide_info_dict( size, self.mpi_shape, pt0, pt1, self.asn_dict) else: divide_info_dict = { \ 'shape': common.shape_two_points(pt0, pt1), \ 'pt0': pt0, \ 'pt1': pt1, \ 'anx_list': self.nodef.accum_nx_list } divide_info_dict['str_fs'] = common.convert_to_tuple(str_f) divide_info_dict['tmax'] = tmax divide_info_dict['tgap'] = tgap pkl_file = open(dir_path + 'divide_info.pkl', 'wb') pickle.dump(divide_info_dict, pkl_file) pkl_file.close() self.t0 = t0 self.t1 = t1 self.tgap = tgap
def __init__(self, fields, str_f, pt0, pt1): """ """ common.check_type('fields', fields, Fields) 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 nodef = fields str_fs = common.convert_to_tuple(str_f) mainf_list = nodef.mainf_list anx = nodef.accum_nx_list 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'], nodef.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)) dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) host_array = np.zeros(shape, dtype=nodef.dtype) split_host_array = np.split(host_array, len(str_fs)) split_host_array_dict = dict(zip(str_fs, split_host_array)) getf_list = [] slices_list = [] self.gpu, self.cpu = gpu, cpu for i, mainf in enumerate(mainf_list): nx0 = anx[i] nx1 = anx[i + 1] - 1 if i < len(mainf_list) - 1 else anx[i + 1] overlap = common.intersection_two_lines((nx0, nx1), (pt0[0], pt1[0])) if overlap != None: x0, y0, z0 = pt0 x1, y1, z1 = pt1 slice_pt0 = (overlap[0] - x0, 0, 0) slice_pt1 = (overlap[1] - x0, y1 - y0, z1 - z0) slices = [] for j, p0, p1 in zip([0, 1, 2], slice_pt0, slice_pt1): if dummied_shape[j] != 1: slices.append(slice(p0, p1 + 1)) slices_list.append(slices if slices != [] else [slice(0, 1)]) local_pt0 = (overlap[0] - nx0, y0, z0) local_pt1 = (overlap[1] - nx0, y1, z1) getf_list.append( getattr(self, mainf.device_type). \ GetFields(mainf, str_fs, local_pt0, local_pt1) ) # global variables self.str_fs = str_fs self.host_array = host_array self.split_host_array_dict = split_host_array_dict self.getf_list = getf_list self.slices_list = slices_list
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 e_or_h = str_f[0] 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) # create the SetFields instance func_dict = {} pts_dict = fields.split_points_dict(e_or_h, pt0, pt1) for part, pts in pts_dict.items(): if pts == None: func_dict[part] = lambda a='': None else: func_dict[part] = SetFields(fields, str_f, \ pts[0], pts[1], is_array, is_overwrite).set_fields if is_array: spatial_array_dict = {} for part, pts in pts_dict.items(): if pts == None: spatial_array_dict[part] = 0 else: slices0 = [slice(p0, p1+1) for p0, p1 in zip(pt0, pt1)] slices1 = [slice(p0, p1+1) for p0, p1 in zip(pts[0], pts[1])] overlap_slices = common.intersection_two_slices(fields.ns, slices0, slices1) shift_slices = [] for sl, p0 in zip(overlap_slices, pt0): s0, s1 = sl.start, sl.stop shift_slices.append( slice(s0-p0, s1-p0) ) dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) reshaped_value = spatial_value.reshape(dummied_shape) dummied_array = reshaped_value[shift_slices] overlap_shape = common.shape_two_points(pts[0], pts[1]) spatial_array_dict[part] = dummied_array.reshape(overlap_shape) # global variables and functions self.mainf = fields self.dtype = dtype self.tfunc = tfunc self.func_dict = func_dict self.e_or_h = e_or_h self.tstep = 1 if is_array: self.spatial_array_dict = spatial_array_dict self.update = self.update_spatial_value else: self.spatial_value = spatial_value self.update = self.update_single_value # append to the update list self.priority_type = 'incident' fields.append_instance(self)
def __init__(self, node_fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): """ """ common.check_type('node_fields', node_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 nodef = node_fields dtype = nodef.dtype is_array = True if isinstance(spatial_value, np.ndarray) else False mainf_list = nodef.mainf_list buffer_dict = nodef.buffer_dict anx = nodef.accum_nx_list nx, ny, nz = nodef.ns for axis, n, p0, p1 in zip(['x', 'y', 'z'], nodef.ns, pt0, pt1): start, end = 0, n if buffer_dict.has_key(axis+'+'): end = n + 1 if buffer_dict.has_key(axis+'-'): start = -1 common.check_value('pt0 %s' % axis, p0, range(start, end)) common.check_value('pt1 %s' % axis, p1, range(start, end)) 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 valriables self.str_f = str_f self.pt0 = pt0 self.pt1 = pt1 self.tfunc= tfunc self.spatial_value = spatial_value self.is_overwrite = is_overwrite self.is_array = is_array self.cpu = cpu if 'gpu' in [f.device_type for f in nodef.updatef_list]: from kemp.fdtd3d import gpu self.gpu = gpu # create IncidentDirect instance for i, mainf in enumerate(mainf_list): fields_pt0 = (anx[i], 0, 0) fields_pt1 = (anx[i+1]-1, ny-1, nz-1) overlap = common.overlap_two_regions(fields_pt0, fields_pt1, pt0, pt1) if overlap != None: self.create_instance(mainf, fields_pt0, fields_pt1, overlap[0], overlap[1]) # for buffer for direction, buffer in buffer_dict.items(): fields_pt0 = { \ 'x+': (anx[-1]-1, 0, 0), \ 'y+': (0, ny-2, 0), \ 'z+': (0, 0, nz-2), \ 'x-': (-1, 0, 0), \ 'y-': (0, -1, 0), \ 'z-': (0, 0, -1) }[direction] fields_pt1 = { \ 'x+': (anx[-1]+1, ny-1, nz-1), \ 'y+': (nx-1, ny, nz-1), \ 'z+': (nx-1, ny-1, nz), \ 'x-': (1, ny-1, nz-1), \ 'y-': (nx-1, 1, nz-1), \ 'z-': (nx-1, ny-1, 1) }[direction] overlap = common.overlap_two_regions(fields_pt0, fields_pt1, pt0, pt1) if overlap != None: self.create_instance(buffer, fields_pt0, fields_pt1, overlap[0], overlap[1])
def __init__(self, fields, str_f, pt0, pt1, is_array=False, is_overwrite=True): """ """ common.check_type('fields', fields, Fields) 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 nodef = fields str_fs = common.convert_to_tuple(str_f) mainf_list = nodef.mainf_list anx = nodef.accum_nx_list 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'], nodef.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) # allocation dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) setf_list = [] slices_list = [] self.gpu, self.cpu = gpu, cpu for i, mainf in enumerate(mainf_list): nx0 = anx[i] nx1 = anx[i + 1] overlap = common.intersection_two_lines((nx0, nx1), (pt0[0], pt1[0])) if overlap != None: x0, y0, z0 = pt0 x1, y1, z1 = pt1 slice_pt0 = (overlap[0] - x0, 0, 0) slice_pt1 = (overlap[1] - x0, y1 - y0, z1 - z0) slices = [] for j, p0, p1 in zip([0, 1, 2], slice_pt0, slice_pt1): if dummied_shape[j] != 1: slices.append(slice(p0, p1 + 1)) slices_list.append(slices if slices != [] else [slice(0, 1)]) local_pt0 = (overlap[0] - nx0, y0, z0) local_pt1 = (overlap[1] - nx0, y1, z1) setf_list.append( getattr(self, mainf.device_type). \ SetFields(mainf, str_fs, local_pt0, local_pt1, \ is_array, is_overwrite) ) # global variables and functions self.str_fs = str_fs self.dtype = nodef.dtype self.shape = common.shape_two_points(pt0, pt1, len(str_fs)) self.setf_list = setf_list self.slices_list = slices_list if is_array: self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
def __init__(self, node_fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): """ """ common.check_type('node_fields', node_fields, NodeFields) 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 nodef = node_fields dtype = nodef.dtype is_array = True if isinstance(spatial_value, np.ndarray) else False mainf_list = nodef.mainf_list anx = nodef.accum_nx_list for axis, n, p0, p1 in zip(['x', 'y', 'z'], nodef.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) # allocation dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) incident_list = [] reduced_slices = [] self.gpu, self.cpu = gpu, cpu for i, mainf in enumerate(mainf_list): nx0 = anx[i] nx1 = anx[i + 1] overlap = common.intersection_two_lines((nx0, nx1), (pt0[0], pt1[0])) if overlap != None: x0, y0, z0 = pt0 x1, y1, z1 = pt1 shift_pt0 = (overlap[0] - x0, y0 - y0, z0 - z0) shift_pt1 = (overlap[1] - x0, y1 - y0, z1 - z0) shift_slices = [ slice(p0, p1 + 1) for p0, p1 in zip(shift_pt0, shift_pt1) ] if is_array: reshaped_value = spatial_value.reshape(dummied_shape) dummied_array = reshaped_value[shift_slices] overlap_shape = common.shape_two_points( shift_pt0, shift_pt1) split_value = dummied_array.reshape(overlap_shape).copy() else: split_value = spatial_value local_pt0 = (overlap[0] - nx0, y0, z0) local_pt1 = (overlap[1] - nx0, y1, z1) incident_list.append( \ getattr(self, mainf.device_type). \ DirectIncident(mainf, str_f, local_pt0, local_pt1, \ tfunc, split_value, is_overwrite) ) # global variables self.incident_list = incident_list
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, is_array=False, is_overwrite=True): """ """ common.check_type('fields', fields, Fields) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('is_array', is_array, bool) 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 str_fs = common.convert_to_tuple(str_f) dtype_str_list = fields.dtype_str_list overwrite_str = {True: '=', False: '+='}[is_overwrite] 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)) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', 'OVERWRITE', \ 'DTYPE', 'PRAGMA_fp64'] nmax_str, xid_str, yid_str, zid_str = common_gpu.macro_replace_list(pt0, pt1) if is_array: values = [nmax_str, xid_str, yid_str, zid_str, \ '__global DTYPE *source', \ 'target[idx]', 'source[sub_idx]', overwrite_str] + \ dtype_str_list else: values = [nmax_str, xid_str, yid_str, zid_str, \ 'DTYPE source', \ 'target[idx]', 'source', overwrite_str] + \ dtype_str_list ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'copy.cl').read(), macros, values) program = cl.Program(fields.context, ksrc).build() # allocation target_bufs = [fields.get_buf(str_f) for str_f in str_fs] shape = common.shape_two_points(pt0, pt1, len(str_fs)) if is_array: tmp_array = np.zeros(shape, dtype=fields.dtype) source_buf = cl.Buffer( \ fields.context, \ cl.mem_flags.READ_WRITE | cl.mem_flags.COPY_HOST_PTR, \ hostbuf=tmp_array) # global variabels and functions self.mainf = fields self.program = program self.target_bufs = target_bufs self.shape = shape nmax = int(nmax_str) remainder = nmax % fields.ls self.gs = nmax if remainder == 0 else nmax - remainder + fields.ls if is_array: self.source_buf = source_buf self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
def __init__(self, fields, str_f, pt0, pt1, is_array=False, is_overwrite=True): """ """ common.check_type('fields', fields, Fields) 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 nodef = fields str_fs = common.convert_to_tuple(str_f) mainf_list = nodef.mainf_list anx = nodef.accum_nx_list 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'], nodef.ns, pt0, pt1): common.check_value('pt0 %s' % axis, p0, range(n)) common.check_value('pt1 %s' % axis, p1, range(n)) # allocation dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) setf_list = [] slices_list = [] self.gpu, self.cpu = gpu, cpu for i, mainf in enumerate(mainf_list): nx0 = anx[i] nx1 = anx[i+1] overlap = common.intersection_two_lines((nx0, nx1), (pt0[0], pt1[0])) if overlap != None: x0, y0, z0 = pt0 x1, y1, z1 = pt1 slice_pt0 = (overlap[0]-x0, 0, 0) slice_pt1 = (overlap[1]-x0, y1-y0, z1-z0) slices = [] for j, p0, p1 in zip([0, 1, 2], slice_pt0, slice_pt1): if dummied_shape[j] != 1: slices.append( slice(p0, p1+1) ) slices_list.append(slices if slices!=[] else [slice(0, 1)] ) local_pt0 = (overlap[0]-nx0, y0, z0) local_pt1 = (overlap[1]-nx0, y1, z1) setf_list.append( getattr(self, mainf.device_type). \ SetFields(mainf, str_fs, local_pt0, local_pt1, \ is_array, is_overwrite) ) # global variables and functions self.str_fs = str_fs self.dtype = nodef.dtype self.shape = common.shape_two_points(pt0, pt1, len(str_fs)) self.setf_list = setf_list self.slices_list = slices_list if is_array: self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
def __init__(self, fields, str_f, pt0, pt1): """ """ common.check_type('fields', fields, Fields) 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 nodef = fields str_fs = common.convert_to_tuple(str_f) mainf_list = nodef.mainf_list anx = nodef.accum_nx_list 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'], nodef.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)) dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) host_array = np.zeros(shape, dtype=nodef.dtype) split_host_array = np.split(host_array, len(str_fs)) split_host_array_dict = dict( zip(str_fs, split_host_array) ) getf_list = [] slices_list = [] self.gpu, self.cpu = gpu, cpu for i, mainf in enumerate(mainf_list): nx0 = anx[i] nx1 = anx[i+1]-1 if i < len(mainf_list)-1 else anx[i+1] overlap = common.intersection_two_lines((nx0, nx1), (pt0[0], pt1[0])) if overlap != None: x0, y0, z0 = pt0 x1, y1, z1 = pt1 slice_pt0 = (overlap[0]-x0, 0, 0) slice_pt1 = (overlap[1]-x0, y1-y0, z1-z0) slices = [] for j, p0, p1 in zip([0, 1, 2], slice_pt0, slice_pt1): if dummied_shape[j] != 1: slices.append( slice(p0, p1+1) ) slices_list.append(slices if slices!=[] else [slice(0, 1)] ) local_pt0 = (overlap[0]-nx0, y0, z0) local_pt1 = (overlap[1]-nx0, y1, z1) getf_list.append( getattr(self, mainf.device_type). \ GetFields(mainf, str_fs, local_pt0, local_pt1) ) # global variables self.str_fs = str_fs self.host_array = host_array self.split_host_array_dict = split_host_array_dict self.getf_list = getf_list self.slices_list = slices_list
def __init__(self, node_fields, str_f, pt0, pt1, tfunc, spatial_value=1., is_overwrite=False): """ """ common.check_type('node_fields', node_fields, NodeFields) 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 nodef = node_fields dtype = nodef.dtype is_array = True if isinstance(spatial_value, np.ndarray) else False mainf_list = nodef.mainf_list anx = nodef.accum_nx_list for axis, n, p0, p1 in zip(['x', 'y', 'z'], nodef.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) # allocation dummied_shape = common.shape_two_points(pt0, pt1, is_dummy=True) incident_list = [] reduced_slices = [] self.gpu, self.cpu = gpu, cpu for i, mainf in enumerate(mainf_list): nx0 = anx[i] nx1 = anx[i+1] overlap = common.intersection_two_lines((nx0, nx1), (pt0[0], pt1[0])) if overlap != None: x0, y0, z0 = pt0 x1, y1, z1 = pt1 shift_pt0 = (overlap[0]-x0, y0-y0, z0-z0) shift_pt1 = (overlap[1]-x0, y1-y0, z1-z0) shift_slices = [slice(p0, p1+1) for p0, p1 in zip(shift_pt0, shift_pt1)] if is_array: reshaped_value = spatial_value.reshape(dummied_shape) dummied_array = reshaped_value[shift_slices] overlap_shape = common.shape_two_points(shift_pt0, shift_pt1) split_value = dummied_array.reshape(overlap_shape).copy() else: split_value = spatial_value local_pt0 = (overlap[0]-nx0, y0, z0) local_pt1 = (overlap[1]-nx0, y1, z1) incident_list.append( \ getattr(self, mainf.device_type). \ DirectIncident(mainf, str_f, local_pt0, local_pt1, \ tfunc, split_value, is_overwrite) ) # global variables self.incident_list = incident_list
def __init__(self, fields, str_f, pt0, pt1, is_array=False, is_overwrite=True): """ """ common.check_type('fields', fields, Fields) common.check_type('str_f', str_f, (str, list, tuple), str) common.check_type('pt0', pt0, (list, tuple), (int, float)) common.check_type('pt1', pt1, (list, tuple), (int, float)) common.check_type('is_array', is_array, bool) 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 str_fs = common.convert_to_tuple(str_f) dtype_str_list = fields.dtype_str_list overwrite_str = {True: '=', False: '+='}[is_overwrite] 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)) # program macros = ['NMAX', 'XID', 'YID', 'ZID', \ 'ARGS', \ 'TARGET', 'SOURCE', 'OVERWRITE', \ 'DTYPE'] if is_array: values = common_gpu.macro_replace_list(pt0, pt1) + \ ['DTYPE *source', \ 'target[idx]', 'source[sub_idx]', overwrite_str] + \ dtype_str_list else: values = common_gpu.macro_replace_list(pt0, pt1) + \ ['DTYPE source', \ 'target[idx]', 'source', overwrite_str] + \ dtype_str_list ksrc = common.replace_template_code( \ open(common_gpu.src_path + 'copy.cu').read(), macros, values) program = SourceModule(ksrc) kernel_copy = program.get_function('copy') # allocation target_bufs = [fields.get_buf(str_f) for str_f in str_fs] shape = common.shape_two_points(pt0, pt1, len(str_fs)) if is_array: tmp_array = np.zeros(shape, fields.dtype) source_buf = cuda.to_device(tmp_array) # global variabels and functions self.mainf = fields self.kernel_copy = kernel_copy self.target_bufs = target_bufs self.shape = shape if is_array: self.source_buf = source_buf self.set_fields = self.set_fields_spatial_value else: self.set_fields = self.set_fields_single_value
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)