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 runTest(self): nx, ny, nz, str_f, pt0, pt1 = 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, '', 'single') getf = GetFields(fields, str_f, pt0, pt1) # host allocations ehs = common_update.generate_random_ehs(nx, ny, nz, fields.dtype) eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) ) fields.set_eh_bufs(*ehs) # verify getf.get_event().wait() for str_f in str_fs: original = eh_dict[str_f][slidx] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def runTest(self): nx, ny, nz, str_f, pt0, pt1 = self.args slidx = common.slices_two_points(pt0, pt1) str_fs = common.convert_to_tuple(str_f) # instance gpu_devices = common_gpu.gpu_device_list(print_info=False) context = cl.Context(gpu_devices) device = gpu_devices[0] fields = Fields(context, device, nx, ny, nz, '', 'single') getf = GetFields(fields, str_f, pt0, pt1) # host allocations ehs = common_random.generate_ehs(nx, ny, nz, fields.dtype) eh_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs) ) fields.set_eh_bufs(*ehs) # verify getf.get_event().wait() for str_f in str_fs: original = eh_dict[str_f][slidx] copy = getf.get_fields(str_f) norm = np.linalg.norm(original - copy) self.assertEqual(norm, 0, '%s, %g' % (self.args, norm))
def 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 verify(s, pt0, pt1): print('pt0 = %s, pt1 = %s' % (pt0, pt1)) slidx = s.get_slidx(pt0, pt1) for strf in s.strf_list: fget = GetFields(s.fdtd, strf, pt0, pt1) fget.get_event().wait() original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) #print original, copy assert np.linalg.norm(original - copy) == 0
def test_boundary(s): print('\n-- test boundary (two fields) --') print('E fields') str_fs_dict = {'x': ['ey', 'ez'], 'y': ['ex', 'ez'], 'z': ['ex', 'ey']} 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) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() for strf in str_fs: original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) assert np.linalg.norm(original - copy) == 0 print('H fields') str_fs_dict = {'x': ['hy', 'hz'], 'y': ['hx', 'hz'], 'z': ['hx', 'hy']} 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) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() for strf in str_fs: original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) assert np.linalg.norm(original - 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) --') print('E fields') str_fs_dict = {'x':['ey','ez'], 'y':['ex','ez'], 'z':['ex','ey']} 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) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() for strf in str_fs: original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) assert np.linalg.norm(original - copy) == 0 print('H fields') str_fs_dict = {'x':['hy','hz'], 'y':['hx','hz'], 'z':['hx','hy']} 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) fget = GetFields(s.fdtd, str_fs, pt0, pt1) fget.get_event().wait() for strf in str_fs: original = s.fhosts[strf][slidx] copy = fget.get_fields(strf) assert np.linalg.norm(original - copy) == 0
# Plot import matplotlib.pyplot as plt plt.ion() imag = plt.imshow(output.get_fields('ez').T, cmap=plt.cm.hot, origin='lower', vmin=0, vmax=0.05) plt.colorbar() # Main loop from datetime import datetime t0 = datetime.now() for tstep in xrange(1, tmax+1): fdtd.update_h() fdtd.update_e() src.update(tstep) if tstep % tgap == 0: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)), sys.stdout.flush() output.get_event().wait() f = output.get_fields('ez') imag.set_array(f.T**2 ) #plt.savefig('./simple.png') plt.draw() #print('[%s] %d/%d (%d %%)' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)) print('')
plt.colorbar() ''' # main loop from datetime import datetime from time import time t0 = datetime.now() t00 = time() gtmp = GetFields(fields, 'ez', (0, 0, 0), (0, 0, 0)) for tstep in xrange(1, tmax + 1): fields.update_e() fields.update_h() ''' if tstep % tgap == 0: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)), sys.stdout.flush() getf.get_event().wait() imag.set_array( getf.get_fields().T ) #plt.savefig('./png/%.6d.png' % tstep) plt.draw() ''' gtmp.get_event().wait() dt = time() - t00 print dt print('[%s] %d/%d (%d %%) %f Mpoint/s' % (datetime.now() - t0, tstep, tmax, float(tstep) / tmax * 100, nx * ny * nz * tmax / dt / 1e6))
vmin=0, vmax=0.05) plt.colorbar() # Main loop from datetime import datetime t0 = datetime.now() for tstep in xrange(1, tmax + 1): fdtd.update_e() src.update(tstep) pbc.update_e() fdtd.update_h() pbc.update_h() if tstep % tgap == 0: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep) / tmax * 100)), sys.stdout.flush() output.get_event().wait() f = output.get_fields() imag.set_array(f.T**2) #plt.savefig('./simple.png') plt.draw() #output.get_event().wait() #print('[%s] %d/%d (%d %%)' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)) print('')
import pyopencl as cl from kemp.fdtd3d import common_gpu from kemp.fdtd3d.gpu import Fields, GetFields, Npml nx, ny, nz = 100, 110, 128 gpu_id = 0 gpu_devices = common_gpu.get_gpu_devices() context = cl.Context(gpu_devices) device = gpu_devices[gpu_id] fdtd = Fields(context, device, nx, ny, nz, coeff_use='') fhosts = {} pml = Npml(fdtd, -0.428571428571, 0.714285714286, 0.6, 0.2, 0.6, 0.2) fhost = np.random.rand(nx, nz).astype(fdtd.dtype) cl.enqueue_write_buffer(fdtd.queue, pml.pex, fhost) pml.update_h() fget = GetFields(fdtd, ['hz','hx'], (0, ny-1, 0), (nx-1, ny-1, nz-1)) fget.get_event().wait() hz = fget.get_fields('hz') hx = fget.get_fields('hx') print fhost print hz print fhost.shape print hz.shape assert np.linalg.norm(fhost - hz) == 0
from datetime import datetime from time import time, sleep t0 = datetime.now() t00 = time() # main loop for tstep in xrange(1, tmax + 1): fields.update_e() exch.update_e() fields.update_h() exch.update_h() if tstep % 10 == 0 and is_plot: getf.get_event().wait() np.save('rank%d_%d' % (rank, tstep), getf.get_fields()) if is_master: print( '[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep) / tmax * 100)), sys.stdout.flush() for i in range(size): load_fail = True while load_fail: try: arr[i * nx:(i + 1) * nx, :] = np.load('rank%d_%d.npy' % (i, tstep)) load_fail = False
imag = plt.imshow(np.zeros((nx, ny), fields.dtype).T, interpolation='nearest', origin='lower', vmin=-1.1, vmax=1.1) plt.colorbar() ''' # main loop from datetime import datetime from time import time t0 = datetime.now() t00 = time() gtmp = GetFields(fields, 'ez', (0, 0, 0), (0, 0, 0)) for tstep in xrange(1, tmax+1): fields.update_e() fields.update_h() ''' if tstep % tgap == 0: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)), sys.stdout.flush() getf.get_event().wait() imag.set_array( getf.get_fields().T ) #plt.savefig('./png/%.6d.png' % tstep) plt.draw() ''' gtmp.get_event().wait() dt = time() - t00 print dt print('[%s] %d/%d (%d %%) %f Mpoint/s' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100, nx*ny*nz*tmax/dt/1e6) )
from datetime import datetime from time import time, sleep t0 = datetime.now() t00 = time() # main loop for tstep in xrange(1, tmax+1): fields.update_e() exch.update_e() fields.update_h() exch.update_h() if tstep % 10 == 0 and is_plot: getf.get_event().wait() np.save('rank%d_%d' % (rank, tstep), getf.get_fields()) if is_master: print('[%s] %d/%d (%d %%)\r' % (datetime.now() - t0, tstep, tmax, float(tstep)/tmax*100)), sys.stdout.flush() for i in range(size): load_fail = True while load_fail: try: arr[i*nx:(i+1)*nx,:] = np.load('rank%d_%d.npy' % (i, tstep)) load_fail = False except: sleep(0.1)
import pyopencl as cl from kemp.fdtd3d import common_gpu from kemp.fdtd3d.gpu import Fields, GetFields, Npml nx, ny, nz = 100, 110, 128 gpu_id = 0 gpu_devices = common_gpu.get_gpu_devices() context = cl.Context(gpu_devices) device = gpu_devices[gpu_id] fdtd = Fields(context, device, nx, ny, nz, coeff_use='') fhosts = {} pml = Npml(fdtd, -0.428571428571, 0.714285714286, 0.6, 0.2, 0.6, 0.2) fhost = np.random.rand(nx, nz).astype(fdtd.dtype) cl.enqueue_write_buffer(fdtd.queue, pml.pex, fhost) pml.update_h() fget = GetFields(fdtd, ['hz', 'hx'], (0, ny - 1, 0), (nx - 1, ny - 1, nz - 1)) fget.get_event().wait() hz = fget.get_fields('hz') hx = fget.get_fields('hx') print fhost print hz print fhost.shape print hz.shape assert np.linalg.norm(fhost - hz) == 0