def __init__(self, buffer_fields, max_tstep): common.check_type('buffer_fields', buffer_fields, BufferFields) # local variables mainf = buffer_fields nx, ny, nz = mainf.ns dtype = mainf.dtype direction = mainf.direction target_rank = mainf.target_rank # create instances (getf, setf and mpi requests) if '+' in direction: # split h getf = GetFields(mainf, ['hy', 'hz'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['ey', 'ez'], (2, 0, 0), (2, ny - 1, nz - 1), True) if rank < target_rank: tag_send, tag_recv = 0, 1 elif rank > target_rank: # pbc tag_send, tag_recv = 2, 3 elif '-' in direction: # split e getf = GetFields(mainf, ['ey', 'ez'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['hy', 'hz'], (0, 0, 0), (0, ny - 1, nz - 1), True) if rank > target_rank: tag_send, tag_recv = 1, 0 elif rank < target_rank: # pbc tag_send, tag_recv = 3, 2 req_send = comm.Send_init(getf.host_array, target_rank, tag=tag_send) tmp_recv_list = [ np.zeros(getf.host_array.shape, dtype) for i in range(2) ] req_recv_list = [ comm.Recv_init(tmp_recv, target_rank, tag=tag_recv) for tmp_recv in tmp_recv_list ] # global variables and functions self.getf = getf self.setf = setf self.req_send = req_send self.req_recv_list = req_recv_list self.tmp_recv_list = tmp_recv_list self.switch = 0 self.max_tstep = max_tstep self.tstep = 1 # append to the update list self.priority_type = 'mpi' mainf.append_instance(self)
def __init__(self, buffer_fields, target_rank): common.check_type('buffer_fields', buffer_fields, BufferFields) common.check_type('target_rank', target_rank, int) # local variables mainf = buffer_fields nx, ny, nz = mainf.ns dtype = mainf.dtype direction = mainf.direction assert rank != target_rank, 'The target_rank %d is same as the my_rank %d.' % ( target_rank, rank) # create instances (getf, setf and mpi requests) if '+' in direction: # split h getf = GetFields(mainf, ['hy', 'hz'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['ey', 'ez'], (2, 0, 0), (2, ny - 1, nz - 1), True) if rank < target_rank: tag_send, tag_recv = 0, 1 elif rank > target_rank: # pbc tag_send, tag_recv = 2, 3 elif '-' in direction: # split e getf = GetFields(mainf, ['ey', 'ez'], (1, 0, 0), (1, ny - 1, nz - 1)) setf = SetFields(mainf, ['hy', 'hz'], (0, 0, 0), (0, ny - 1, nz - 1), True) if rank > target_rank: tag_send, tag_recv = 1, 0 elif rank < target_rank: # pbc tag_send, tag_recv = 3, 2 # global variables and functions self.mainf = mainf self.target_rank = target_rank self.getf = getf self.setf = setf self.tag_send = tag_send self.tag_recv = tag_recv self.tmp_recv = np.zeros(getf.host_array.shape, dtype) # global functions self.update_e = self.recv if '+' in direction else self.send self.update_h = self.send if '+' in direction else self.recv # append to the update list self.priority_type = 'mpi' mainf.append_instance(self)
def verify(s, pt0, pt1): print('pt0 = %s, pt1 = %s' % (pt0, pt1)) slidx = common.get_slice_index(pt0, pt1) shape = common.get_shape(pt0, pt1) for strf in s.strf_list: # non-spatial fset = SetFields(s.fdtd, strf, pt0, pt1) values = np.random.rand(*shape).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() copy = fget.get_fields(strf) assert np.linalg.norm(values - copy) == 0 if pt0 != pt1: for strf in s.strf_list: # spatial fset = SetFields(s.fdtd, strf, pt0, pt1, np.ndarray) values = np.random.rand(*shape).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() copy = fget.get_fields(strf) assert np.linalg.norm(values - copy) == 0
def __init__(self, fields, target_rank, tmax): common.check_type('fields', fields, Fields) # local variables nx, ny, nz = fields.ns dtype = fields.dtype mpi_type = fields.mpi_type common.check_value('mpi_type', mpi_type, \ ['x+', 'x-', 'y+', 'y-', 'z+', 'z-']) # create instances (getf, setf and mpi requests) if '+' in mpi_type: # split h getf = GetFields(fields, ['hy', 'hz'], \ (1, 1, 1), (1, ny-1, nz-1)) setf = SetFields(fields, ['ey', 'ez'], \ (nx-1, 0, 0), (nx-1, ny-2, nz-2), True) req_send = comm.Send_init(getf.host_array, target_rank, tag=1) tmp_recv = np.zeros(getf.host_array.shape, dtype) req_recv = comm.Recv_init(tmp_recv, target_rank, tag=2) elif '-' in mpi_type: # split e getf = GetFields(fields, ['ey', 'ez'], \ (nx-2, 0, 0), (nx-2, ny-2, nz-2)) setf = SetFields(fields, ['hy', 'hz'], \ (0, 1, 1), (0, ny-1, nz-1), True) req_send = comm.Send_init(getf.host_array, target_rank, tag=2) tmp_recv = np.zeros(getf.host_array.shape, dtype) req_recv = comm.Recv_init(tmp_recv, target_rank, tag=1) # global variables and functions self.mainf = fields self.getf = getf self.setf = setf self.tmp_recv = tmp_recv self.req_send = req_send self.req_recv = req_recv self.tmax = tmax self.tstep = 1 # append to the update list self.priority_type = 'mpi' self.mainf.append_instance(self)
def test_boundary(s): print('\n-- test boundary (two fields) --') shape_dict = { 'x': (s.ny * 2, s.nz), 'y': (s.nx * 2, s.nz), 'z': (s.nx * 2, s.ny) } print('E fields') str_fs_dict = {'x': ['ey', 'ez'], 'y': ['ex', 'ez'], 'z': ['ex', 'ey']} pt0_dict = { 'x': (s.nx - 1, 0, 0), 'y': (0, s.ny - 1, 0), 'z': (0, 0, s.nz - 1) } pt1 = (s.nx - 1, s.ny - 1, s.nz - 1) for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt0 = pt0_dict[axis] slidx = common.get_slice_index(pt0, pt1) fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray) values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() copy = fget.get_fields() assert np.linalg.norm(values - copy) == 0 print('H fields') str_fs_dict = {'x': ['hy', 'hz'], 'y': ['hx', 'hz'], 'z': ['hx', 'hy']} pt0 = (0, 0, 0) pt1_dict = { 'x': (0, s.ny - 1, s.nz - 1), 'y': (s.nx - 1, 0, s.nz - 1), 'z': (s.nx - 1, s.ny - 1, 0) } for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt1 = pt1_dict[axis] slidx = common.get_slice_index(pt0, pt1) fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray) values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() copy = fget.get_fields() assert np.linalg.norm(values - copy) == 0
def runTest(self): nx, ny, nz, str_f, pt0, pt1, is_array = self.args slice_xyz = common.slice_index_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance fields = Fields(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) ) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slice_xyz] = split_value_dict[str_f] else: eh_dict[str_f][slice_xyz] = value setf.set_fields(value) fields.enqueue_barrier() for str_f in str_fs: original = eh_dict[str_f] copy = fields.get(str_f)[:,:,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 slice_xyz = common.slice_index_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance fields = Fields(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)) # verify for str_f in str_fs: if is_array: eh_dict[str_f][slice_xyz] = split_value_dict[str_f] else: eh_dict[str_f][slice_xyz] = value setf.set_fields(value) fields.enqueue_barrier() for str_f in str_fs: original = eh_dict[str_f] copy = fields.get(str_f)[:, :, fields.slice_z] norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def test_boundary(s): print('\n-- test boundary (two fields) --') shape_dict = {'x':(s.ny*2, s.nz), 'y':(s.nx*2, s.nz), 'z':(s.nx*2, s.ny)} print('E fields') str_fs_dict = {'x':['ey','ez'], 'y':['ex','ez'], 'z':['ex','ey']} pt0_dict = {'x':(s.nx-1, 0, 0), 'y':(0, s.ny-1, 0), 'z':(0, 0, s.nz-1)} pt1 = (s.nx-1, s.ny-1, s.nz-1) for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt0 = pt0_dict[axis] slidx = common.get_slice_index(pt0, pt1) fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray) values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() copy = fget.get_fields() assert np.linalg.norm(values - copy) == 0 print('H fields') str_fs_dict = {'x':['hy','hz'], 'y':['hx','hz'], 'z':['hx','hy']} pt0 = (0, 0, 0) pt1_dict = {'x':(0, s.ny-1, s.nz-1), 'y':(s.nx-1, 0, s.nz-1), 'z':(s.nx-1, s.ny-1, 0)} for axis in str_fs_dict.keys(): print('direction : %s' % axis) str_fs = str_fs_dict[axis] pt1 = pt1_dict[axis] slidx = common.get_slice_index(pt0, pt1) fset = SetFields(s.fdtd, str_fs, pt0, pt1, np.ndarray) values = np.random.rand(*shape_dict[axis]).astype(s.fdtd.dtype) fset.set_fields(values) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() copy = fget.get_fields() assert np.linalg.norm(values - copy) == 0
def __init__(s, fields, str_f, pt0, pt1, tfunc, spatial_arr=1.): s.emf = fields s.tfunc = tfunc s.spatial_arr = spatial_arr s.setf = SetFields(s.emf, str_f, pt0, pt1, type(spatial_arr))
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)