File: nnps_kernels.py

package info (click to toggle)
compyle 0.8.1-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,100 kB
  • sloc: python: 12,337; makefile: 21
file content (142 lines) | stat: -rw-r--r-- 3,578 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from compyle.api import declare, annotate
from compyle.parallel import serial
from compyle.low_level import atomic_inc, cast
from math import floor
import numpy as np


@annotate
def find_cell_id(x, y, h, c):
    c[0] = cast(floor((x) / h), "int")
    c[1] = cast(floor((y) / h), "int")


@annotate
def flatten(p, q, qmax):
    return p * qmax + q


@serial
@annotate
def count_bins(i, x, y, h, cmax, keys, bin_counts,
               sort_offsets):
    c = declare('matrix(2, "int")')
    find_cell_id(x[i], y[i], h, c)
    key = flatten(c[0], c[1], cmax)
    keys[i] = key
    idx = atomic_inc(bin_counts[key])
    sort_offsets[i] = idx


@annotate
def sort_indices(i, keys, sort_offsets, start_indices, sorted_indices):
    key = keys[i]
    offset = sort_offsets[i]
    start_idx = start_indices[key]
    sorted_indices[start_idx + offset] = i


@annotate
def input_start_indices(i, counts):
    return 0 if i == 0 else counts[i - 1]


@annotate
def output_start_indices(i, item, indices):
    indices[i] = item


@annotate
def fill_keys(i, x, y, h, cmax, indices, keys):
    c = declare('matrix(2, "int")')
    find_cell_id(x[i], y[i], h, c)
    key = flatten(c[0], c[1], cmax)
    keys[i] = key
    indices[i] = i


@annotate
def input_scan_keys(i, keys):
    return 1 if i == 0 or keys[i] != keys[i - 1] else 0


@annotate
def output_scan_keys(i, item, prev_item, keys, start_indices):
    key = keys[i]
    if item != prev_item:
        start_indices[key] = i


@annotate
def fill_bin_counts(i, keys, start_indices, bin_counts, num_particles):
    if i == num_particles - 1:
        last_key = keys[num_particles - 1]
        bin_counts[last_key] = num_particles - start_indices[last_key]
    if i == 0 or keys[i] == keys[i - 1]:
        return
    key = keys[i]
    prev_key = keys[i - 1]
    bin_counts[prev_key] = start_indices[key] - start_indices[prev_key]


@annotate
def find_neighbor_lengths_knl(i, x, y, h, cmax, start_indices, sorted_indices,
                              bin_counts, nbr_lengths, max_key):
    d = h * h
    q_c = declare('matrix(2, "int")')
    find_cell_id(x[i], y[i], h, q_c)

    for p in range(-1, 2):
        for q in range(-1, 2):
            cx = q_c[0] + p
            cy = q_c[1] + q

            key = flatten(cx, cy, cmax)

            if key >= max_key or key < 0:
                continue

            start_idx = start_indices[key]
            np = bin_counts[key]

            for k in range(np):
                j = sorted_indices[start_idx + k]
                xij = x[i] - x[j]
                yij = y[i] - y[j]
                rij2 = xij * xij + yij * yij

                if rij2 < d:
                    nbr_lengths[i] += 1


@annotate
def find_neighbors_knl(i, x, y, h, cmax, start_indices, sorted_indices,
                       bin_counts, nbr_starts, nbrs, max_key):
    d = h * h
    q_c = declare('matrix(2, "int")')
    find_cell_id(x[i], y[i], h, q_c)
    length = 0
    nbr_start_idx = nbr_starts[i]

    for p in range(-1, 2):
        for q in range(-1, 2):
            cx = q_c[0] + p
            cy = q_c[1] + q

            key = flatten(cx, cy, cmax)

            if key >= max_key or key < 0:
                continue

            start_idx = start_indices[key]
            np = bin_counts[key]

            for k in range(np):
                j = sorted_indices[start_idx + k]
                xij = x[i] - x[j]
                yij = y[i] - y[j]
                rij2 = xij * xij + yij * yij

                if rij2 < d:
                    nbrs[nbr_start_idx + length] = j
                    length += 1