Esempio n. 1
0
 def __init__(self,
              num_particles,
              x=None,
              y=None,
              z=None,
              vx=None,
              vy=None,
              vz=None,
              xmax=100.,
              ymax=100.,
              zmax=100.,
              dx=2.,
              init_T=0.,
              backend=None):
     super().__init__(num_particles,
                      x=x,
                      y=y,
                      z=z,
                      vx=vx,
                      vy=vy,
                      vz=vz,
                      xmax=xmax,
                      ymax=ymax,
                      zmax=zmax,
                      dx=dx,
                      init_T=init_T,
                      backend=backend)
     self.init_forces = Elementwise(calculate_force, backend=self.backend)
     self.step1 = Elementwise(step_method1, backend=self.backend)
     self.step2 = Elementwise(step_method2, backend=self.backend)
Esempio n. 2
0
 def setup_kernels(self):
     # neighbor kernels
     self.find_neighbor_lengths = Elementwise(find_neighbor_lengths_knl,
                                              backend=self.backend)
     self.find_neighbors = Elementwise(find_neighbors_knl,
                                       backend=self.backend)
     self.scan_start_indices = Scan(input=input_start_indices,
                                    output=output_start_indices,
                                    scan_expr="a+b",
                                    dtype=np.int32,
                                    backend=self.backend)
Esempio n. 3
0
    def __init__(self, x, y, h, xmax, ymax, backend=None):
        super().__init__(x, y, h, xmax, ymax, backend=backend)
        self.max_bits = np.ceil(np.log2(self.max_key))

        # sort kernels
        self.fill_keys = Elementwise(fill_keys, backend=self.backend)
        self.fill_bin_counts = Elementwise(fill_bin_counts,
                                           backend=self.backend)
        self.scan_keys = Scan(input=input_scan_keys,
                              output=output_scan_keys,
                              scan_expr="a+b", dtype=np.int32,
                              backend=self.backend)
Esempio n. 4
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, use_count_sort=False):
     super().__init__(num_particles, x=x, y=y, vx=vx, vy=vy,
                      xmax=xmax, ymax=ymax, dx=dx, init_T=init_T,
                      backend=backend)
     self.init_forces = Elementwise(calculate_force, backend=self.backend)
     self.step1 = Elementwise(step_method1, backend=self.backend)
     self.step2 = Elementwise(step_method2, backend=self.backend)
     self.nnps_algorithm = NNPSCountingSort \
         if use_count_sort else NNPSRadixSort
     self.nnps = self.nnps_algorithm(self.x, self.y, 3., self.xmax,
                                     self.ymax, backend=self.backend)
Esempio n. 5
0
    def _get_remove_particles_bool_kernels(self):
        @annotate(i='int', if_remove='gintp', return_='int')
        def remove_input_expr(i, if_remove):
            return if_remove[i]

        @annotate(int='i, item, last_item, num_particles',
                  gintp='if_remove, num_removed_particles',
                  new_indices='guintp')
        def remove_output_expr(i, item, last_item, if_remove, new_indices,
                               num_removed_particles, num_particles):
            if not if_remove[i]:
                new_indices[i - item] = i
            if i == num_particles - 1:
                num_removed_particles[0] = last_item

        remove_knl = Scan(remove_input_expr,
                          remove_output_expr,
                          'a+b',
                          dtype=np.int32,
                          backend=self.backend)

        @annotate(int='i, size, stride', guintp='indices, new_indices')
        def stride_knl_elwise(i, indices, new_indices, size, stride):
            tmp_idx, j_s = declare('unsigned int', 2)
            for j_s in range(stride):
                tmp_idx = i * stride + j_s
                if tmp_idx < size:
                    new_indices[tmp_idx] = indices[i] * stride + j_s

        stride_knl = Elementwise(stride_knl_elwise, backend=self.backend)

        return remove_knl, stride_knl
Esempio n. 6
0
def run(nv, backend):
    e = Elementwise(velocity, backend=backend)
    args = make_vortices(nv, backend)
    t1 = time.time()
    e(*args)
    print(time.time() - t1)
    u = args[-3]
    u.pull()
    return e, args
Esempio n. 7
0
    def _setup_strided_indices_kernel(self):
        @annotate(int='i, stride', gintp='indices, dest')
        def set_indices(i, indices, dest, stride):
            j = declare('int')
            for j in range(stride):
                dest[i * stride + j] = indices[i] * stride + j

        knl = Elementwise(set_indices, backend=self.backend)
        self._strided_indices_knl = knl
Esempio n. 8
0
    def _get_remove_particles_kernel(self):
        @annotate(int='i, size', indices='guintp', if_remove='gintp')
        def fill_if_remove_elwise(i, indices, if_remove, size):
            if indices[i] < size:
                if_remove[indices[i]] = 1

        fill_if_remove_knl = Elementwise(fill_if_remove_elwise,
                                         backend=self.backend)

        return fill_if_remove_knl
Esempio n. 9
0
    def extract_particles(self,
                          indices,
                          dest_array=None,
                          align=True,
                          props=None):
        """Create new particle array for particles with given indices

        Parameters
        ----------

        indices : Array
            indices of particles to be extracted.

        props : list
            the list of properties to extract, if None all properties
            are extracted.

        """
        if not dest_array:
            dest_array = self.empty_clone(props=props)

        if props is None:
            prop_names = self.properties
        else:
            prop_names = props

        if len(indices) == 0:
            return dest_array

        start_idx = dest_array.get_number_of_particles()

        dest_array.gpu.extend(len(indices))

        args_list = [indices, start_idx]

        for prop in prop_names:
            stride = self._particle_array.stride.get(prop, 1)
            src_arr = self._data[prop]
            dst_arr = dest_array.gpu.get_device_array(prop)

            args_list.append(stride)
            args_list.append(dst_arr)
            args_list.append(src_arr)

        extract_particles_knl = ExtractParticles('extract_particles_knl',
                                                 prop_names).function
        extract_particles_elwise = Elementwise(extract_particles_knl,
                                               backend=self.backend)
        extract_particles_elwise(*args_list)

        if align:
            dest_array.gpu.align_particles()

        return dest_array
Esempio n. 10
0
    def __init__(self, x, y, h, xmax, ymax, backend=None):
        self.backend = backend
        self.num_particles = x.length
        self.x, self.y = x, y
        self.h = h

        cmax = np.array([floor(xmax / h), floor(ymax / h)], dtype=np.int32)
        self.max_key = 1 + flatten(cmax[0], cmax[1], 1 + cmax[1])
        self.qmax = 1 + cmax[1]

        # neighbor kernels
        self.find_neighbor_lengths = Elementwise(find_neighbor_lengths_knl,
                                                 backend=self.backend)
        self.find_neighbors = Elementwise(find_neighbors_knl,
                                          backend=self.backend)
        self.scan_start_indices = Scan(input=input_start_indices,
                                       output=output_start_indices,
                                       scan_expr="a+b", dtype=np.int32,
                                       backend=self.backend)
        self.init_arrays()
Esempio n. 11
0
    def _get_copy_indices_kernel(self):
        @annotate(int='i, size, stride', guintp='indices, new_indices')
        def fill_indices_elwise(i, indices, new_indices, size, stride):
            tmp_idx, j_s = declare('unsigned int', 2)
            for j_s in range(stride):
                tmp_idx = i * stride + j_s
                if tmp_idx < size:
                    new_indices[tmp_idx] = indices[i] * stride + j_s

        fill_indices_knl = Elementwise(fill_indices_elwise,
                                       backend=self.backend)

        return fill_indices_knl
Esempio n. 12
0
    def _get_translate_kernel(self):
        @annotate
        def translate(i, x, y, z, tag, xtranslate, ytranslate, ztranslate,
                      masks):
            xmask, ymask, zmask, mask = declare('int', 4)
            mask = masks[i]

            zmask = mask % 3
            mask /= 3
            ymask = mask % 3
            mask /= 3
            xmask = mask % 3

            x[i] = x[i] + (xmask - 1) * xtranslate
            y[i] = y[i] + (ymask - 1) * ytranslate
            z[i] = z[i] + (zmask - 1) * ztranslate

            tag[i] = 2

        return Elementwise(translate, backend=self.backend)
Esempio n. 13
0
    def _get_box_wrap_kernel(self):
        @annotate
        def box_wrap(i, x, y, z, xmin, ymin, zmin, xmax, ymax, zmax,
                     xtranslate, ytranslate, ztranslate, periodic_in_x,
                     periodic_in_y, periodic_in_z):
            if periodic_in_x:
                if x[i] < xmin:
                    x[i] = x[i] + xtranslate
                if x[i] > xmax:
                    x[i] = x[i] - xtranslate

            if periodic_in_y:
                if y[i] < ymin:
                    y[i] = y[i] + ytranslate
                if y[i] > ymax:
                    y[i] = y[i] - ytranslate

            if periodic_in_z:
                if z[i] < zmin:
                    z[i] = z[i] + ztranslate
                if z[i] > zmax:
                    z[i] = z[i] - ztranslate

        return Elementwise(box_wrap, backend=self.backend)
Esempio n. 14
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()
Esempio n. 15
0
 def __init__(self, x, y, h, xmax, ymax, backend=None):
     super().__init__(x, y, h, xmax, ymax, backend=backend)
     # sort kernels
     self.count_bins = Elementwise(count_bins, backend=self.backend)
     self.sort_indices = Elementwise(sort_indices, backend=self.backend)