Пример #1
0
def profile_kernel(kernel, name, backend=None):
    """For profiling raw PyCUDA/PyOpenCL kernels or cython functions
    """
    from compyle.array import get_backend
    backend = get_backend(backend)

    def _profile_knl(*args, **kwargs):
        if backend == 'opencl':
            start = time.time()
            event = kernel(*args, **kwargs)
            event.wait()
            end = time.time()
            _record_profile(name, end - start)
            return event
        elif backend == 'cuda':
            exec_time = kernel(*args, **kwargs, time_kernel=True)
            _record_profile(name, exec_time)
            return exec_time
        else:
            start = time.time()
            kernel(*args, **kwargs)
            end = time.time()
            _record_profile(name, end - start)

    if get_config().profile:
        wgi = getattr(kernel, 'get_work_group_info', None)
        if wgi is not None:
            _profile_knl.get_work_group_info = wgi
        return _profile_knl
    else:
        return kernel
Пример #2
0
def named_profile(name, backend=None):
    """Decorator for profiling raw PyOpenCL/PyCUDA kernels or cython functions.
    This can be used on a function that returns a raw PyCUDA/PyOpenCL kernel

    For example::

    @named_profile('prefix_sum')
    def _get_prefix_sum(ctx):
        return GenericScanKernel(ctx, np.int32,
                                 arguments="__global int *ary",
                                 input_expr="ary[i]",
                                 scan_expr="a+b", neutral="0",
                                 output_statement="ary[i] = prev_item")
    """
    from compyle.array import get_backend
    backend = get_backend(backend)

    def _decorator(f):
        if name is None:
            n = f.__name__
        else:
            n = name

        def _profiled_kernel_generator(*args, **kwargs):
            kernel = f(*args, **kwargs)
            return profile_kernel(kernel, n, backend=backend)

        return _profiled_kernel_generator

    return _decorator
Пример #3
0
    def __init__(self,
                 xmin=-1000.,
                 xmax=1000.,
                 ymin=0.,
                 ymax=0.,
                 zmin=0.,
                 zmax=0.,
                 periodic_in_x=False,
                 periodic_in_y=False,
                 periodic_in_z=False,
                 n_layers=2.0,
                 backend=None,
                 props=None):
        """Constructor"""
        DomainManagerBase.__init__(self,
                                   xmin=xmin,
                                   xmax=xmax,
                                   ymin=ymin,
                                   ymax=ymax,
                                   zmin=zmin,
                                   zmax=zmax,
                                   periodic_in_x=periodic_in_x,
                                   periodic_in_y=periodic_in_y,
                                   periodic_in_z=periodic_in_z,
                                   n_layers=n_layers,
                                   props=props)

        self.use_double = get_config().use_double
        self.dtype = np.float64 if self.use_double else np.float32

        self.dtype_max = np.finfo(self.dtype).max
        self.backend = get_backend(backend)

        self.ghosts = None
Пример #4
0
    def __init__(self, particle_array, backend=None):
        self.backend = get_backend(backend)
        self._particle_array = pa = particle_array
        self.use_double = get_config().use_double
        self._dtype = np.float64 if self.use_double else np.float32
        self.num_real_particles = pa.num_real_particles
        self._data = {}
        self.properties = []
        self.constants = []

        for prop, ary in pa.properties.items():
            self.add_prop(prop, ary)
        for prop, ary in pa.constants.items():
            self.add_const(prop, ary)
Пример #5
0
 def __init__(self, num_particles, x=None, y=None, vx=None, vy=None,
              xmax=100., ymax=100., dx=1.5, init_T=0.,
              backend=None):
     self.backend = get_backend(backend)
     self.num_particles = num_particles
     self.xmin, self.xmax = 0., xmax
     self.ymin, self.ymax = 0., ymax
     self.m = 1.
     if x is None and y is None:
         self.x, self.y = self.setup_positions(num_particles, dx)
     if vx is None and vy is None:
         self.vx, self.vy = self.setup_velocities(init_T, num_particles)
     self.fx = carr.zeros_like(self.x, backend=self.backend)
     self.fy = carr.zeros_like(self.x, backend=self.backend)
     self.pe = carr.zeros_like(self.x, backend=self.backend)
     self.energy_calc = Reduction("a+b", map_func=calculate_energy,
                                  backend=self.backend)
Пример #6
0
 def __init__(self,
              nx=10,
              ny=10,
              xmin=0.,
              xmax=1.,
              ymin=0.,
              ymax=1.,
              bc=lambda x: 0,
              backend=None):
     self.backend = get_backend(backend)
     self.xmin, self.xmax, self.ymin, self.ymax = xmin, xmax, ymin, ymax
     self.nx, self.ny = nx, ny
     self.dx = (xmax - xmin) / (nx - 1)
     self.dy = (ymax - ymin) / (ny - 1)
     self.x = np.arange(self.xmin, self.xmax + self.dx * 0.5, self.dx)
     self.y = np.arange(self.ymin, self.ymax + self.dy * 0.5, self.dy)
     self.bc = bc
     self.setup()
Пример #7
0
 def __init__(self, grid, backend=None):
     self.grid = grid
     self.backend = get_backend(backend)
     self.step_method = Elementwise(laplace_step, backend=self.backend)
     self.res = self.grid.u.copy()