def verify(s, pt0, pt1): print('pt0 = %s, pt1 = %s' % (pt0, pt1)) slidx = s.get_slidx(pt0, pt1) for strf in s.strf_list: # non-spatial fset = SetFields(s.fdtd, strf, pt0, pt1) value = np.random.rand() fset.set_fields(value) fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() copy = fget.get_fields(strf) assert np.linalg.norm(value - 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(*fset.rplist['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 verify(s, pt0, pt1): print('pt0 = %s, pt1 = %s' % (pt0, pt1)) slidx = s.get_slidx(pt0, pt1) for strf in s.strf_list: # non-spatial fset = SetFields(s.fdtd, strf, pt0, pt1) value = np.random.rand() fset.set_fields(value) fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() copy = fget.get_fields(strf) assert np.linalg.norm(value - 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(*fset.rplist['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 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 = s.get_slidx(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 = s.get_slidx(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 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 = s.get_slidx(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 = s.get_slidx(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 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 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()
class ExchangeMpiSplit2(): def __init__(self, gpuf, core, direction): common.check_type('gpuf', gpuf, Fields) common.check_value('direction', direction, ('+', '-', '+-')) if '+' in direction: self.gf_h = gf_h = GetFields(gpuf, ['hy', 'hz'], (-1, 0, 0), (-1, -1, -1)) self.sf_e = SetFields(gpuf, ['ey', 'ez'], (-1, 0, 0), (-1, -1, -1), True) target = 0 if rank == size-1 else rank+1 self.req_send_h = comm.Send_init(gf_h.host_array, target, tag=0) self.tmp_recv_e = np.zeros(gf_h.host_array.shape, gpuf.dtype) self.req_recv_e = comm.Recv_init(self.tmp_recv_e, target, tag=1) if '-' in direction: self.gf_e = gf_e = GetFields(gpuf, ['ey', 'ez'], (0, 0, 0), (0, -1, -1)) self.sf_h = SetFields(gpuf, ['hy', 'hz'], (0, 0, 0), (0, -1, -1), True) target = size-1 if rank == 0 else rank-1 self.req_send_e = comm.Send_init(gf_e.host_array, target, tag=1) self.tmp_recv_h = np.zeros(gf_e.host_array.shape, gpuf.dtype) self.req_recv_h = comm.Recv_init(self.tmp_recv_h, target, tag=0) # global variables self.core = core self.direction = direction def update_e(self): self.core.update_e('pre') if '-' in self.direction: self.gf_e.get_event().wait() self.req_send_e.Start() if '+' in self.direction: self.req_recv_e.Start() self.core.update_e('post') if '-' in self.direction: self.req_send_e.Wait() if '+' in self.direction: self.req_recv_e.Wait() self.sf_e.set_fields(self.tmp_recv_e) def update_h(self): self.core.update_h('pre') if '+' in self.direction: self.gf_h.get_event().wait() self.req_send_h.Start() if '-' in self.direction: self.req_recv_h.Start() self.core.update_h('post') if '+' in self.direction: self.req_send_h.Wait() if '-' in self.direction: self.req_recv_h.Wait() self.sf_h.set_fields(self.tmp_recv_h)
class ExchangeMpiSplit3(): def __init__(self, gpuf, core, direction, tmax): common.check_type('gpuf', gpuf, Fields) common.check_value('direction', direction, ('+', '-', '+-')) if '+' in direction: self.gf_h = gf_h = GetFields(gpuf, ['hy', 'hz'], (-2, 0, 0), (-2, -1, -1)) self.sf_e = SetFields(gpuf, ['ey', 'ez'], (-1, 0, 0), (-1, -1, -1), True) self.req_send_h = comm.Send_init(gf_h.host_array, rank + 1, tag=0) self.tmp_recv_e_list = [ np.zeros(gf_h.host_array.shape, gpuf.dtype) for i in range(2) ] self.req_recv_e_list = [ comm.Recv_init(tmp_recv_e, rank + 1, tag=1) for tmp_recv_e in self.tmp_recv_e_list ] self.switch_e = 0 if '-' in direction: self.gf_e = gf_e = GetFields(gpuf, ['ey', 'ez'], (1, 0, 0), (1, -1, -1)) self.sf_h = SetFields(gpuf, ['hy', 'hz'], (0, 0, 0), (0, -1, -1), True) self.req_send_e = comm.Send_init(gf_e.host_array, rank - 1, tag=1) self.tmp_recv_h_list = [ np.zeros(gf_e.host_array.shape, gpuf.dtype) for i in range(2) ] self.req_recv_h_list = [ comm.Recv_init(tmp_recv_h, rank - 1, tag=0) for tmp_recv_h in self.tmp_recv_h_list ] self.switch_h = 0 # global variables self.core = core self.direction = direction self.tmax = tmax self.tstep = 1 def update_e(self): if self.direction == '+': self.core.update_e() else: # update e self.core.update_e('pre') # mpi send e if self.tstep > 1: self.req_send_e.Wait() self.gf_e.get_event().wait() self.req_send_e.Start() if self.tstep == self.tmax: self.req_send_e.Wait() # update e self.core.update_e('mid') # mpi recv h if self.tstep > 1: self.req_recv_h_list[self.switch_h].Wait() self.sf_h.set_fields(self.tmp_recv_h_list[self.switch_h]) self.switch_h = 1 if self.switch_h == 0 else 0 if self.tstep < self.tmax: self.req_recv_h_list[self.switch_h].Start() # update e self.core.update_e('post') def update_h(self): if self.direction == '-': self.core.update_h() else: # update h self.core.update_h('pre') # mpi send h if self.tstep > 1: self.req_send_h.Wait() if self.tstep < self.tmax: self.gf_h.get_event().wait() self.req_send_h.Start() # update h self.core.update_h('mid') # mpi recv e if self.tstep == 1: self.req_recv_e_list[self.switch_e].Start() self.req_recv_e_list[self.switch_e].Wait() self.sf_e.set_fields(self.tmp_recv_e_list[self.switch_e]) self.switch_e = 1 if self.switch_e == 0 else 0 if self.tstep < self.tmax: self.req_recv_e_list[self.switch_e].Start() # update h self.core.update_h('post') self.tstep += 1
class ExchangeMpiSplit(): def __init__(self, gpuf, core, direction, tmax): common.check_type('gpuf', gpuf, Fields) common.check_value('direction', direction, ('+', '-', '+-')) if '+' in direction: self.gf_h = gf_h = GetFields(gpuf, ['hy', 'hz'], (-1, 0, 0), (-1, -1, -1)) self.sf_e = SetFields(gpuf, ['ey', 'ez'], (-1, 0, 0), (-1, -1, -1), True) self.req_send_h = comm.Send_init(gf_h.host_array, rank+1, tag=0) self.tmp_recv_e_list = [np.zeros(gf_h.host_array.shape, gpuf.dtype) for i in range(2)] self.req_recv_e_list = [comm.Recv_init(tmp_recv_e, rank+1, tag=1) for tmp_recv_e in self.tmp_recv_e_list] self.switch_e = 0 if '-' in direction: self.gf_e = gf_e = GetFields(gpuf, ['ey', 'ez'], (0, 0, 0), (0, -1, -1)) self.sf_h = SetFields(gpuf, ['hy', 'hz'], (0, 0, 0), (0, -1, -1), True) self.req_send_e = comm.Send_init(gf_e.host_array, rank-1, tag=1) self.tmp_recv_h_list = [np.zeros(gf_e.host_array.shape, gpuf.dtype) for i in range(2)] self.req_recv_h_list = [comm.Recv_init(tmp_recv_h, rank-1, tag=0) for tmp_recv_h in self.tmp_recv_h_list] self.switch_h = 0 # global variables self.gpuf = gpuf self.core = core self.direction = direction self.tmax = tmax self.tstep = 1 def update_e(self): # update e self.core.update_e('pre') if '-' in self.direction: # mpi send e if self.tstep > 1: self.req_send_e.Wait() if self.tstep < self.tmax: self.gf_e.get_event().wait() self.req_send_e.Start() if '+' in self.direction: # mpi recv e if self.tstep > 1: self.req_recv_e_list[self.switch_e].Wait() self.sf_e.set_fields(self.tmp_recv_e_list[self.switch_e]) self.switch_e = 1 if self.switch_e == 0 else 0 if self.tstep < self.tmax: self.req_recv_e_list[self.switch_e].Start() # update h self.core.update_h('post') # update e self.core.update_e('mid') def update_h(self): # update h self.core.update_h('pre') if '+' in self.direction: # mpi send h if self.tstep > 1: self.req_send_h.Wait() if self.tstep < self.tmax: self.gf_h.get_event().wait() self.req_send_h.Start() if '-' in self.direction: # mpi recv h if self.tstep > 1: self.req_recv_h_list[self.switch_h].Wait() self.sf_h.set_fields(self.tmp_recv_h_list[self.switch_h]) self.switch_h = 1 if self.switch_h == 0 else 0 if self.tstep < self.tmax: self.req_recv_h_list[self.switch_h].Start() # update e self.core.update_e('post') # update h self.core.update_h('mid') self.tstep += 1
class ExchangeMpiSplit2(): def __init__(self, gpuf, core, direction): common.check_type('gpuf', gpuf, Fields) common.check_value('direction', direction, ('+', '-', '+-')) if '+' in direction: self.gf_h = gf_h = GetFields(gpuf, ['hy', 'hz'], (-1, 0, 0), (-1, -1, -1)) self.sf_e = SetFields(gpuf, ['ey', 'ez'], (-1, 0, 0), (-1, -1, -1), True) target = 0 if rank == size - 1 else rank + 1 self.req_send_h = comm.Send_init(gf_h.host_array, target, tag=0) self.tmp_recv_e = np.zeros(gf_h.host_array.shape, gpuf.dtype) self.req_recv_e = comm.Recv_init(self.tmp_recv_e, target, tag=1) if '-' in direction: self.gf_e = gf_e = GetFields(gpuf, ['ey', 'ez'], (0, 0, 0), (0, -1, -1)) self.sf_h = SetFields(gpuf, ['hy', 'hz'], (0, 0, 0), (0, -1, -1), True) target = size - 1 if rank == 0 else rank - 1 self.req_send_e = comm.Send_init(gf_e.host_array, target, tag=1) self.tmp_recv_h = np.zeros(gf_e.host_array.shape, gpuf.dtype) self.req_recv_h = comm.Recv_init(self.tmp_recv_h, target, tag=0) # global variables self.core = core self.direction = direction def update_e(self): self.core.update_e('pre') if '-' in self.direction: self.gf_e.get_event().wait() self.req_send_e.Start() if '+' in self.direction: self.req_recv_e.Start() self.core.update_e('post') if '-' in self.direction: self.req_send_e.Wait() if '+' in self.direction: self.req_recv_e.Wait() self.sf_e.set_fields(self.tmp_recv_e) def update_h(self): self.core.update_h('pre') if '+' in self.direction: self.gf_h.get_event().wait() self.req_send_h.Start() if '-' in self.direction: self.req_recv_h.Start() self.core.update_h('post') if '+' in self.direction: self.req_send_h.Wait() if '-' in self.direction: self.req_recv_h.Wait() self.sf_h.set_fields(self.tmp_recv_h)