Ejemplo n.º 1
0
nx_gpu = 120
nx_cpu = 80
#nx_gpu = nx_cpu = 100
ny, nz = 300, 64
tmax, tgap = 200, 10

# instances
gpu_devices = common_gpu.gpu_device_list(print_info=False)
context = cl.Context(gpu_devices)

mainf_list = [gpu.Fields(context, device, nx_gpu, ny, nz) for device in gpu_devices]
mainf_list.append( cpu.Fields(nx_cpu, ny, nz) )
nodef = NodeFields(mainf_list)
core = NodeCore(nodef)
exchange = NodeExchange(nodef)
pbc = NodePbc(nodef, 'y')
pbc = NodePbc(nodef, 'z')
pbc_x = NodePbc(nodef, 'x')

tfunc = lambda tstep: np.sin(0.1 * tstep)
#incident = NodeDirectIncident(nodef, 'ez', (0, 20, 0), (nodef.nx-1, 20, nz-1), tfunc) 
incident = NodeDirectIncident(nodef, 'ez', (20, 0, 0), (20, ny-1, nz-1), tfunc) 
getf = NodeGetFields(nodef, 'ez', (0, 0, 2), (nodef.nx-1, ny-1, 2))

# plot
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
plt.ion()
for anx in nodef.accum_nx_list[1:]:
	plt.plot((anx, anx), (0, ny), color='w', linewidth=0.2)
Ejemplo n.º 2
0
    def test_y_pbc_x_exchange(self):
        # instance
        nx, ny, nz = 40, 50, 60
        #nx, ny, nz = 3, 4, 5
        gpu_devices = common_gpu.gpu_device_list(print_info=False)
        context = cl.Context(gpu_devices)

        gpuf = gpu.Fields(context, gpu_devices[0], nx, ny, nz)
        cpuf = cpu.Fields(nx, ny, nz)
        mainf_list = [gpuf, cpuf]
        nodef = NodeFields(mainf_list)
        core = NodeCore(nodef)
        pbc = NodePbc(nodef, 'y')
        exchange = NodeExchange(nodef)
        
        # generate random source
        ehs_gpu = common_update.generate_random_ehs(nx, ny, nz, nodef.dtype)
        gpuf.set_eh_bufs(*ehs_gpu)
        ehs_gpu_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs_gpu) )

        ehs_cpu = common_update.generate_random_ehs(nx, ny, nz, nodef.dtype)
        cpuf.set_ehs(*ehs_cpu)
        ehs_cpu_dict = dict( zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs_cpu) )

        # verify
        for mainf in mainf_list:
            mainf.update_e()
        pbc.update_e()
        exchange.update_e()

        for mainf in mainf_list:
            mainf.update_h()
        pbc.update_h()
        exchange.update_h()

        mainf_list[-1].enqueue_barrier()
        getf0, getf1 = {}, {}

        # x-axis exchange
        getf0['e'] = gpu.GetFields(gpuf, ['ey', 'ez'], (nx-1, 0, 0), (nx-1, ny-2, nz-2))
        getf1['e'] = cpu.GetFields(cpuf, ['ey', 'ez'], (0, 0, 0), (0, ny-2, nz-2))

        getf0['h'] = gpu.GetFields(gpuf, ['hy', 'hz'], (nx-1, 1, 1), (nx-1, ny-1, nz-1))
        getf1['h'] = cpu.GetFields(cpuf, ['hy', 'hz'], (0, 1, 1), (0, ny-1, nz-1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            g0 = getf0[eh].get_fields()
            g1 = getf1[eh].get_fields()
            norm = np.linalg.norm(g0 - g1)
            self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'x-axis exchange', eh))

        # y-axis pbc gpu
        getf0['e'] = gpu.GetFields(gpuf, ['ex', 'ez'], (0, ny-1, 0), (nx-2, ny-1, nz-2))
        getf1['e'] = gpu.GetFields(gpuf, ['ex', 'ez'], (0, 0, 0), (nx-2, 0, nz-2))

        getf0['h'] = gpu.GetFields(gpuf, ['hx', 'hz'], (1, ny-1, 1), (nx-1, ny-1, nz-1))
        getf1['h'] = gpu.GetFields(gpuf, ['hx', 'hz'], (1, 0, 1), (nx-1, 0, nz-1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            g0 = getf0[eh].get_fields()
            g1 = getf1[eh].get_fields()
            norm = np.linalg.norm(g0 - g1)
            self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'y-axis pbc gpu', eh))

        # y-axis pbc cpu
        getf0['e'] = cpu.GetFields(cpuf, ['ex', 'ez'], (0, ny-1, 0), (nx-2, ny-1, nz-2))
        getf1['e'] = cpu.GetFields(cpuf, ['ex', 'ez'], (0, 0, 0), (nx-2, 0, nz-2))

        getf0['h'] = cpu.GetFields(cpuf, ['hx', 'hz'], (1, ny-1, 1), (nx-1, ny-1, nz-1))
        getf1['h'] = cpu.GetFields(cpuf, ['hx', 'hz'], (1, 0, 1), (nx-1, 0, nz-1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            g0 = getf0[eh].get_fields()
            g1 = getf1[eh].get_fields()
            norm = np.linalg.norm(g0 - g1)
            self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'y-axis pbc cpu', eh))
Ejemplo n.º 3
0
    def runTest(self):
        nx, ny, nz = self.args

        # 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

        # buffer instance
        nodef.append_buffer_fields(cpu.Fields(3, ny, nz, mpi_type='x-'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, nz, mpi_type='y+'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, nz, mpi_type='y-'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, ny, mpi_type='z+'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, ny, mpi_type='z-'))

        exchange = NodeExchange(nodef)
        
        # generate random source
        for f in mainf_list[:-1]:
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_eh_bufs(*ehs)

        for f in nodef.cpuf_dict.values():
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_ehs(*ehs)

        # verify
        exchange.update_e()
        exchange.update_h()
        getf0, getf1 = {}, {}

        # mainf list
        self.gpu, self.cpu = gpu, cpu
        for f0, f1 in zip(mainf_list[:-1], mainf_list[1:]):
            getf0['e'] = getattr(self, f0.device_type).GetFields(f0, ['ey', 'ez'], \
                    (f0.nx-1, 0, 0), (f0.nx-1, f0.ny-2, f0.nz-2))
            getf1['e'] = getattr(self, f1.device_type).GetFields(f1, ['ey', 'ez'], \
                    (0, 0, 0), (0, f1.ny-2, f1.nz-2))

            getf0['h'] = getattr(self, f0.device_type).GetFields(f0, ['hy', 'hz'], \
                    (f0.nx-1, 1, 1), (f0.nx-1, f0.ny-1, f0.nz-1))
            getf1['h'] = getattr(self, f1.device_type).GetFields(f1, ['hy', 'hz'], \
                    (0, 1, 1), (0, f1.ny-1, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm(getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%s, %g, %s, %s, %s' % \
                        (self.args, norm, 'mainf', \
                        getf0[eh].mainf.device_type, getf1[eh].mainf.device_type) )

        # buffer 'x-'
        f0, f1 = nodef.cpuf_dict['x-'], mainf_list[0]
        getf0['e'] = cpu.GetFields(f0, ['ey', 'ez'], \
                (f0.nx-1, 0, 0), (f0.nx-1, f0.ny-2, f0.nz-2))
        getf1['e'] = gpu.GetFields(f1, ['ey', 'ez'], \
                (1, 0, 0), (1, f1.ny-2, f1.nz-2))

        getf0['h'] = cpu.GetFields(f0, ['hy', 'hz'], \
                (f0.nx-2, 1, 1), (f0.nx-2, f0.ny-1, f0.nz-1))
        getf1['h'] = gpu.GetFields(f1, ['hy', 'hz'], \
                (0, 1, 1), (0, f1.ny-1, f1.nz-1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            norm = np.linalg.norm( \
                    getf0[eh].get_fields() - getf1[eh].get_fields())
            self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'x-', eh) )

        # buffer 'y+'
        anx_list = nodef.accum_nx_list

        f1 = nodef.cpuf_dict['y+']
        for f0, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = getattr(self, f0.device_type).GetFields(f0, ['ex', 'ez'], \
                    (0, f0.ny-1, 0), (f0.nx-2, f0.ny-1, f0.nz-2))
            getf1['e'] = cpu.GetFields(f1, ['ey', 'ez'], \
                    (1, anx0, 0), (1, anx1-1, f1.nz-2))

            getf0['h'] = getattr(self, f0.device_type).GetFields(f0, ['hx', 'hz'], \
                    (1, f0.ny-2, 1), (f0.nx-1, f0.ny-2, f0.nz-1))
            getf1['h'] = cpu.GetFields(f1, ['hy', 'hz'], \
                    (0, anx0+1, 1), (0, anx1, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'y+', eh) )

        # buffer 'y-'
        f0 = nodef.cpuf_dict['y-']
        for f1, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = cpu.GetFields(f0, ['ey', 'ez'], \
                    (f0.nx-1, anx0, 0), (f0.nx-1, anx1-1, f0.nz-2))
            getf1['e'] = getattr(self, f1.device_type).GetFields(f1, ['ex', 'ez'], \
                    (0, 1, 0), (f1.nx-2, 1, f1.nz-2))

            getf0['h'] = cpu.GetFields(f0, ['hy', 'hz'], \
                    (f0.nx-2, anx0+1, 1), (f0.nx-2, anx1, f0.nz-1))
            getf1['h'] = getattr(self, f1.device_type).GetFields(f1, ['hx', 'hz'], \
                    (1, 0, 1), (f1.nx-1, 0, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'y-', eh) )

        # buffer 'z+'
        f1 = nodef.cpuf_dict['z+']
        for f0, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = getattr(self, f0.device_type).GetFields(f0, ['ex', 'ey'], \
                    (0, 0, f0.nz-1), (f0.nx-2, f0.ny-2, f0.nz-1))
            getf1['e'] = cpu.GetFields(f1, ['ey', 'ez'], \
                    (1, anx0, 0), (1, anx1-1, f1.nz-2))

            getf0['h'] = getattr(self, f0.device_type).GetFields(f0, ['hx', 'hy'], \
                    (1, 1, f0.nz-2), (f0.nx-1, f0.ny-1, f0.nz-2))
            getf1['h'] = cpu.GetFields(f1, ['hy', 'hz'], \
                    (0, anx0+1, 1), (0, anx1, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'z+', eh) )

        # buffer 'z-'
        f0 = nodef.cpuf_dict['z-']
        for f1, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = cpu.GetFields(f0, ['ey', 'ez'], \
                    (f0.nx-1, anx0, 0), (f0.nx-1, anx1-1, f0.nz-2))
            getf1['e'] = getattr(self, f1.device_type).GetFields(f1, ['ex', 'ey'], \
                    (0, 0, 1), (f1.nx-2, f1.ny-2, 1))

            getf0['h'] = cpu.GetFields(f0, ['hy', 'hz'], \
                    (f0.nx-2, anx0+1, 1), (f0.nx-2, anx1, f0.nz-1))
            getf1['h'] = getattr(self, f1.device_type).GetFields(f1, ['hx', 'hy'], \
                    (1, 1, 0), (f1.nx-1, f1.ny-1, 0))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'z-', eh) )
Ejemplo n.º 4
0
    def test_y_pbc_x_exchange(self):
        # instance
        nx, ny, nz = 40, 50, 60
        #nx, ny, nz = 3, 4, 5
        gpu_devices = common_gpu.gpu_device_list(print_info=False)
        context = cl.Context(gpu_devices)

        gpuf = gpu.Fields(context, gpu_devices[0], nx, ny, nz)
        cpuf = cpu.Fields(nx, ny, nz)
        mainf_list = [gpuf, cpuf]
        nodef = NodeFields(mainf_list)
        core = NodeCore(nodef)
        pbc = NodePbc(nodef, 'y')
        exchange = NodeExchange(nodef)

        # generate random source
        ehs_gpu = common_update.generate_random_ehs(nx, ny, nz, nodef.dtype)
        gpuf.set_eh_bufs(*ehs_gpu)
        ehs_gpu_dict = dict(zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs_gpu))

        ehs_cpu = common_update.generate_random_ehs(nx, ny, nz, nodef.dtype)
        cpuf.set_ehs(*ehs_cpu)
        ehs_cpu_dict = dict(zip(['ex', 'ey', 'ez', 'hx', 'hy', 'hz'], ehs_cpu))

        # verify
        for mainf in mainf_list:
            mainf.update_e()
        pbc.update_e()
        exchange.update_e()

        for mainf in mainf_list:
            mainf.update_h()
        pbc.update_h()
        exchange.update_h()

        mainf_list[-1].enqueue_barrier()
        getf0, getf1 = {}, {}

        # x-axis exchange
        getf0['e'] = gpu.GetFields(gpuf, ['ey', 'ez'], (nx - 1, 0, 0),
                                   (nx - 1, ny - 2, nz - 2))
        getf1['e'] = cpu.GetFields(cpuf, ['ey', 'ez'], (0, 0, 0),
                                   (0, ny - 2, nz - 2))

        getf0['h'] = gpu.GetFields(gpuf, ['hy', 'hz'], (nx - 1, 1, 1),
                                   (nx - 1, ny - 1, nz - 1))
        getf1['h'] = cpu.GetFields(cpuf, ['hy', 'hz'], (0, 1, 1),
                                   (0, ny - 1, nz - 1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            g0 = getf0[eh].get_fields()
            g1 = getf1[eh].get_fields()
            norm = np.linalg.norm(g0 - g1)
            self.assertEqual(norm, 0,
                             '%g, %s, %s' % (norm, 'x-axis exchange', eh))

        # y-axis pbc gpu
        getf0['e'] = gpu.GetFields(gpuf, ['ex', 'ez'], (0, ny - 1, 0),
                                   (nx - 2, ny - 1, nz - 2))
        getf1['e'] = gpu.GetFields(gpuf, ['ex', 'ez'], (0, 0, 0),
                                   (nx - 2, 0, nz - 2))

        getf0['h'] = gpu.GetFields(gpuf, ['hx', 'hz'], (1, ny - 1, 1),
                                   (nx - 1, ny - 1, nz - 1))
        getf1['h'] = gpu.GetFields(gpuf, ['hx', 'hz'], (1, 0, 1),
                                   (nx - 1, 0, nz - 1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            g0 = getf0[eh].get_fields()
            g1 = getf1[eh].get_fields()
            norm = np.linalg.norm(g0 - g1)
            self.assertEqual(norm, 0,
                             '%g, %s, %s' % (norm, 'y-axis pbc gpu', eh))

        # y-axis pbc cpu
        getf0['e'] = cpu.GetFields(cpuf, ['ex', 'ez'], (0, ny - 1, 0),
                                   (nx - 2, ny - 1, nz - 2))
        getf1['e'] = cpu.GetFields(cpuf, ['ex', 'ez'], (0, 0, 0),
                                   (nx - 2, 0, nz - 2))

        getf0['h'] = cpu.GetFields(cpuf, ['hx', 'hz'], (1, ny - 1, 1),
                                   (nx - 1, ny - 1, nz - 1))
        getf1['h'] = cpu.GetFields(cpuf, ['hx', 'hz'], (1, 0, 1),
                                   (nx - 1, 0, nz - 1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            g0 = getf0[eh].get_fields()
            g1 = getf1[eh].get_fields()
            norm = np.linalg.norm(g0 - g1)
            self.assertEqual(norm, 0,
                             '%g, %s, %s' % (norm, 'y-axis pbc cpu', eh))
Ejemplo n.º 5
0
    def runTest(self):
        axis, nx, ny, nz = self.args
        self.gpu, self.cpu = gpu, cpu

        # 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

        pbc = NodePbc(nodef, axis)
        exchange = NodeExchange(nodef)

        # generate random source
        for f in mainf_list[:-1]:
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_eh_bufs(*ehs)

        for f in nodef.cpuf_dict.values():
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_ehs(*ehs)

        # verify
        for mainf in mainf_list:
            mainf.update_e()
        pbc.update_e()
        exchange.update_e()

        for mainf in mainf_list:
            mainf.update_h()
        pbc.update_h()
        exchange.update_h()

        mainf_list[-1].enqueue_barrier()

        getf0, getf1 = {}, {}

        if axis == 'x':
            f0, f1 = mainf_list[0], mainf_list[-1]
            getf0['e'] = getattr(self, f0.device_type).GetFields(f0, ['ey', 'ez'], \
                    (0, 0, 0), (0, f0.ny-2, f0.nz-2))
            getf1['e'] = getattr(self, f1.device_type).GetFields(f1, ['ey', 'ez'], \
                    (f1.nx-1, 0, 0), (f1.nx-1, f1.ny-2, f1.nz-2))

            getf0['h'] = getattr(self, f0.device_type).GetFields(f0, ['hy', 'hz'], \
                    (0, 1, 1), (0, f0.ny-1, f0.nz-1))
            getf1['h'] = getattr(self, f1.device_type).GetFields(f1, ['hy', 'hz'], \
                    (f1.nx-1, 1, 1), (f1.nx-1, f1.ny-1, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm(getf0[eh].get_fields() -
                                      getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'x', eh))

        elif axis == 'y':
            for f in mainf_list:
                getf0['e'] = getattr(self, f.device_type).GetFields(f, ['ex', 'ez'], \
                        (0, 0, 0), (f.nx-2, 0, f.nz-2))
                getf1['e'] = getattr(self, f.device_type).GetFields(f, ['ex', 'ez'], \
                        (0, f.ny-1, 0), (f.nx-2, f.ny-1, f.nz-2))

                getf0['h'] = getattr(self, f.device_type).GetFields(f, ['hx', 'hz'], \
                        (1, 0, 1), (f.nx-1, 0, f.nz-1))
                getf1['h'] = getattr(self, f.device_type).GetFields(f, ['hx', 'hz'], \
                        (1, f.ny-1, 1), (f.nx-1, f.ny-1, f.nz-1))

                for getf in getf0.values() + getf1.values():
                    getf.get_event().wait()

                for eh in ['e', 'h']:
                    norm = np.linalg.norm( \
                            getf0[eh].get_fields() - getf1[eh].get_fields())
                    self.assertEqual(
                        norm, 0,
                        '%g, %s, %s, %s' % (norm, 'y', eh, f.device_type))

        elif axis == 'z':
            for f in mainf_list:
                getf0['e'] = getattr(self, f.device_type).GetFields(f, ['ex', 'ey'], \
                        (0, 0, f.nz-1), (f.nx-2, f.ny-2, f.nz-1))
                getf1['e'] = getattr(self, f.device_type).GetFields(f, ['ex', 'ey'], \
                        (0, 0, 0), (f.nx-2, f.ny-2, 0))

                getf0['h'] = getattr(self, f.device_type).GetFields(f, ['hx', 'hy'], \
                        (1, 1, f.nz-1), (f.nx-1, f.ny-1, f.nz-1))
                getf1['h'] = getattr(self, f.device_type).GetFields(f, ['hx', 'hy'], \
                        (1, 1, 0), (f.nx-1, f.ny-1, 0))

                for getf in getf0.values() + getf1.values():
                    getf.get_event().wait()

                for eh in ['e', 'h']:
                    norm = np.linalg.norm( \
                            getf0[eh].get_fields() - getf1[eh].get_fields())
                    self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'z', eh))
Ejemplo n.º 6
0
    def runTest(self):
        nx, ny, nz = self.args

        # 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

        # buffer instance
        nodef.append_buffer_fields(cpu.Fields(3, ny, nz, mpi_type='x-'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, nz, mpi_type='y+'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, nz, mpi_type='y-'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, ny, mpi_type='z+'))
        nodef.append_buffer_fields(cpu.Fields(3, nodef.nx, ny, mpi_type='z-'))

        exchange = NodeExchange(nodef)

        # generate random source
        for f in mainf_list[:-1]:
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_eh_bufs(*ehs)

        for f in nodef.cpuf_dict.values():
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_ehs(*ehs)

        # verify
        exchange.update_e()
        exchange.update_h()
        getf0, getf1 = {}, {}

        # mainf list
        self.gpu, self.cpu = gpu, cpu
        for f0, f1 in zip(mainf_list[:-1], mainf_list[1:]):
            getf0['e'] = getattr(self, f0.device_type).GetFields(f0, ['ey', 'ez'], \
                    (f0.nx-1, 0, 0), (f0.nx-1, f0.ny-2, f0.nz-2))
            getf1['e'] = getattr(self, f1.device_type).GetFields(f1, ['ey', 'ez'], \
                    (0, 0, 0), (0, f1.ny-2, f1.nz-2))

            getf0['h'] = getattr(self, f0.device_type).GetFields(f0, ['hy', 'hz'], \
                    (f0.nx-1, 1, 1), (f0.nx-1, f0.ny-1, f0.nz-1))
            getf1['h'] = getattr(self, f1.device_type).GetFields(f1, ['hy', 'hz'], \
                    (0, 1, 1), (0, f1.ny-1, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm(getf0[eh].get_fields() -
                                      getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%s, %g, %s, %s, %s' % \
                        (self.args, norm, 'mainf', \
                        getf0[eh].mainf.device_type, getf1[eh].mainf.device_type) )

        # buffer 'x-'
        f0, f1 = nodef.cpuf_dict['x-'], mainf_list[0]
        getf0['e'] = cpu.GetFields(f0, ['ey', 'ez'], \
                (f0.nx-1, 0, 0), (f0.nx-1, f0.ny-2, f0.nz-2))
        getf1['e'] = gpu.GetFields(f1, ['ey', 'ez'], \
                (1, 0, 0), (1, f1.ny-2, f1.nz-2))

        getf0['h'] = cpu.GetFields(f0, ['hy', 'hz'], \
                (f0.nx-2, 1, 1), (f0.nx-2, f0.ny-1, f0.nz-1))
        getf1['h'] = gpu.GetFields(f1, ['hy', 'hz'], \
                (0, 1, 1), (0, f1.ny-1, f1.nz-1))

        for getf in getf0.values() + getf1.values():
            getf.get_event().wait()

        for eh in ['e', 'h']:
            norm = np.linalg.norm( \
                    getf0[eh].get_fields() - getf1[eh].get_fields())
            self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'x-', eh))

        # buffer 'y+'
        anx_list = nodef.accum_nx_list

        f1 = nodef.cpuf_dict['y+']
        for f0, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = getattr(self, f0.device_type).GetFields(f0, ['ex', 'ez'], \
                    (0, f0.ny-1, 0), (f0.nx-2, f0.ny-1, f0.nz-2))
            getf1['e'] = cpu.GetFields(f1, ['ey', 'ez'], \
                    (1, anx0, 0), (1, anx1-1, f1.nz-2))

            getf0['h'] = getattr(self, f0.device_type).GetFields(f0, ['hx', 'hz'], \
                    (1, f0.ny-2, 1), (f0.nx-1, f0.ny-2, f0.nz-1))
            getf1['h'] = cpu.GetFields(f1, ['hy', 'hz'], \
                    (0, anx0+1, 1), (0, anx1, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'y+', eh))

        # buffer 'y-'
        f0 = nodef.cpuf_dict['y-']
        for f1, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = cpu.GetFields(f0, ['ey', 'ez'], \
                    (f0.nx-1, anx0, 0), (f0.nx-1, anx1-1, f0.nz-2))
            getf1['e'] = getattr(self, f1.device_type).GetFields(f1, ['ex', 'ez'], \
                    (0, 1, 0), (f1.nx-2, 1, f1.nz-2))

            getf0['h'] = cpu.GetFields(f0, ['hy', 'hz'], \
                    (f0.nx-2, anx0+1, 1), (f0.nx-2, anx1, f0.nz-1))
            getf1['h'] = getattr(self, f1.device_type).GetFields(f1, ['hx', 'hz'], \
                    (1, 0, 1), (f1.nx-1, 0, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'y-', eh))

        # buffer 'z+'
        f1 = nodef.cpuf_dict['z+']
        for f0, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = getattr(self, f0.device_type).GetFields(f0, ['ex', 'ey'], \
                    (0, 0, f0.nz-1), (f0.nx-2, f0.ny-2, f0.nz-1))
            getf1['e'] = cpu.GetFields(f1, ['ey', 'ez'], \
                    (1, anx0, 0), (1, anx1-1, f1.nz-2))

            getf0['h'] = getattr(self, f0.device_type).GetFields(f0, ['hx', 'hy'], \
                    (1, 1, f0.nz-2), (f0.nx-1, f0.ny-1, f0.nz-2))
            getf1['h'] = cpu.GetFields(f1, ['hy', 'hz'], \
                    (0, anx0+1, 1), (0, anx1, f1.nz-1))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'z+', eh))

        # buffer 'z-'
        f0 = nodef.cpuf_dict['z-']
        for f1, anx0, anx1 in zip(mainf_list, anx_list[:-1], anx_list[1:]):
            getf0['e'] = cpu.GetFields(f0, ['ey', 'ez'], \
                    (f0.nx-1, anx0, 0), (f0.nx-1, anx1-1, f0.nz-2))
            getf1['e'] = getattr(self, f1.device_type).GetFields(f1, ['ex', 'ey'], \
                    (0, 0, 1), (f1.nx-2, f1.ny-2, 1))

            getf0['h'] = cpu.GetFields(f0, ['hy', 'hz'], \
                    (f0.nx-2, anx0+1, 1), (f0.nx-2, anx1, f0.nz-1))
            getf1['h'] = getattr(self, f1.device_type).GetFields(f1, ['hx', 'hy'], \
                    (1, 1, 0), (f1.nx-1, f1.ny-1, 0))

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ['e', 'h']:
                norm = np.linalg.norm( \
                        getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, '%g, %s, %s' % (norm, 'z-', eh))
Ejemplo n.º 7
0
    def runTest(self):
        axis, nx, ny, nz = self.args
        self.gpu, self.cpu = gpu, cpu

        # 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

        pbc = NodePbc(nodef, axis)
        exchange = NodeExchange(nodef)

        # generate random source
        for f in mainf_list[:-1]:
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_eh_bufs(*ehs)

        for f in nodef.cpuf_dict.values():
            nx, ny, nz = f.ns
            ehs = common_update.generate_random_ehs(nx, ny, nz, dtype)
            f.set_ehs(*ehs)

        # verify
        for mainf in mainf_list:
            mainf.update_e()
        pbc.update_e()
        exchange.update_e()

        for mainf in mainf_list:
            mainf.update_h()
        pbc.update_h()
        exchange.update_h()

        mainf_list[-1].enqueue_barrier()

        getf0, getf1 = {}, {}

        if axis == "x":
            f0, f1 = mainf_list[0], mainf_list[-1]
            getf0["e"] = getattr(self, f0.device_type).GetFields(f0, ["ey", "ez"], (0, 0, 0), (0, f0.ny - 2, f0.nz - 2))
            getf1["e"] = getattr(self, f1.device_type).GetFields(
                f1, ["ey", "ez"], (f1.nx - 1, 0, 0), (f1.nx - 1, f1.ny - 2, f1.nz - 2)
            )

            getf0["h"] = getattr(self, f0.device_type).GetFields(f0, ["hy", "hz"], (0, 1, 1), (0, f0.ny - 1, f0.nz - 1))
            getf1["h"] = getattr(self, f1.device_type).GetFields(
                f1, ["hy", "hz"], (f1.nx - 1, 1, 1), (f1.nx - 1, f1.ny - 1, f1.nz - 1)
            )

            for getf in getf0.values() + getf1.values():
                getf.get_event().wait()

            for eh in ["e", "h"]:
                norm = np.linalg.norm(getf0[eh].get_fields() - getf1[eh].get_fields())
                self.assertEqual(norm, 0, "%g, %s, %s" % (norm, "x", eh))

        elif axis == "y":
            for f in mainf_list:
                getf0["e"] = getattr(self, f.device_type).GetFields(f, ["ex", "ez"], (0, 0, 0), (f.nx - 2, 0, f.nz - 2))
                getf1["e"] = getattr(self, f.device_type).GetFields(
                    f, ["ex", "ez"], (0, f.ny - 1, 0), (f.nx - 2, f.ny - 1, f.nz - 2)
                )

                getf0["h"] = getattr(self, f.device_type).GetFields(f, ["hx", "hz"], (1, 0, 1), (f.nx - 1, 0, f.nz - 1))
                getf1["h"] = getattr(self, f.device_type).GetFields(
                    f, ["hx", "hz"], (1, f.ny - 1, 1), (f.nx - 1, f.ny - 1, f.nz - 1)
                )

                for getf in getf0.values() + getf1.values():
                    getf.get_event().wait()

                for eh in ["e", "h"]:
                    norm = np.linalg.norm(getf0[eh].get_fields() - getf1[eh].get_fields())
                    self.assertEqual(norm, 0, "%g, %s, %s, %s" % (norm, "y", eh, f.device_type))

        elif axis == "z":
            for f in mainf_list:
                getf0["e"] = getattr(self, f.device_type).GetFields(
                    f, ["ex", "ey"], (0, 0, f.nz - 1), (f.nx - 2, f.ny - 2, f.nz - 1)
                )
                getf1["e"] = getattr(self, f.device_type).GetFields(f, ["ex", "ey"], (0, 0, 0), (f.nx - 2, f.ny - 2, 0))

                getf0["h"] = getattr(self, f.device_type).GetFields(
                    f, ["hx", "hy"], (1, 1, f.nz - 1), (f.nx - 1, f.ny - 1, f.nz - 1)
                )
                getf1["h"] = getattr(self, f.device_type).GetFields(f, ["hx", "hy"], (1, 1, 0), (f.nx - 1, f.ny - 1, 0))

                for getf in getf0.values() + getf1.values():
                    getf.get_event().wait()

                for eh in ["e", "h"]:
                    norm = np.linalg.norm(getf0[eh].get_fields() - getf1[eh].get_fields())
                    self.assertEqual(norm, 0, "%g, %s, %s" % (norm, "z", eh))