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 (254 lines) | stat: -rw-r--r-- 7,728 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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, z, h, eps, c):
    c[0] = cast(floor((x + eps) / h), "int")
    c[1] = cast(floor((y + eps) / h), "int")
    c[2] = cast(floor((z + eps) / h), "int")


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


@serial
@annotate
def count_bins(i, x, y, z, h, eps, qmax, rmax, keys, bin_counts,
               sort_offsets):
    c = declare('matrix(3, "int")')
    find_cell_id(x[i], y[i], z[i], h, eps, c)
    key = flatten(c[0], c[1], c[2], qmax, rmax)
    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, z, h, eps, qmax, rmax, indices, keys):
    c = declare('matrix(3, "int")')
    find_cell_id(x[i], y[i], z[i], h, eps, c)
    key = flatten(c[0], c[1], c[2], qmax, rmax)
    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, z, h, eps, qmax, rmax, start_indices,
                              sorted_indices, bin_counts, nbr_lengths,
                              max_key):
    d = h * h
    q_c = declare('matrix(3, "int")')
    find_cell_id(x[i], y[i], z[i], h, eps, q_c)

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

                key = flatten(cx, cy, cz, qmax, rmax)

                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]
                    zij = z[i] - z[j]
                    rij2 = xij * xij + yij * yij + zij * zij

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


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

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

                key = flatten(cx, cy, cz, qmax, rmax)

                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]
                    zij = z[i] - z[j]
                    rij2 = xij * xij + yij * yij + zij * zij

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


@annotate
def find_neighbor_lengths_periodic_knl(i, x, y, z, h, eps, xmax, ymax, zmax,
                                       pmax, qmax, rmax, start_indices,
                                       sorted_indices, bin_counts, nbr_lengths,
                                       max_key):
    d = h * h
    q_c = declare('matrix(3, "int")')
    xij, yij, zij = declare('double', 3)
    find_cell_id(x[i], y[i], z[i], h, eps, q_c)

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

                cx_f = cast(cx, "float")
                cy_f = cast(cy, "float")
                cz_f = cast(cz, "float")

                xoffset = cast(floor(cx_f / pmax), "int")
                yoffset = cast(floor(cy_f / qmax), "int")
                zoffset = cast(floor(cz_f / rmax), "int")

                cx -= xoffset * pmax
                cy -= yoffset * qmax
                cz -= zoffset * rmax

                key = flatten(cx, cy, cz, qmax, rmax)

                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 = abs(x[i] - x[j])
                    yij = abs(y[i] - y[j])
                    zij = abs(z[i] - z[j])
                    xij = min(xij, xmax - xij)
                    yij = min(yij, ymax - yij)
                    zij = min(zij, zmax - zij)
                    rij2 = xij * xij + yij * yij + zij * zij

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


@annotate
def find_neighbors_periodic_knl(i, x, y, z, h, eps, xmax, ymax, zmax,
                                pmax, qmax, rmax, start_indices, sorted_indices,
                                bin_counts, nbr_starts, nbrs, max_key):
    d = h * h
    q_c = declare('matrix(3, "int")')
    xij, yij, zij = declare('double', 3)
    find_cell_id(x[i], y[i], z[i], h, eps, q_c)
    length = 0
    nbr_start_idx = nbr_starts[i]

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

                cx_f = cast(cx, "float")
                cy_f = cast(cy, "float")
                cz_f = cast(cz, "float")

                xoffset = cast(floor(cx_f / pmax), "int")
                yoffset = cast(floor(cy_f / qmax), "int")
                zoffset = cast(floor(cz_f / rmax), "int")

                cx -= xoffset * pmax
                cy -= yoffset * qmax
                cz -= zoffset * rmax

                key = flatten(cx, cy, cz, qmax, rmax)

                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 = abs(x[i] - x[j])
                    yij = abs(y[i] - y[j])
                    zij = abs(z[i] - z[j])
                    xij = min(xij, xmax - xij)
                    yij = min(yij, ymax - yij)
                    zij = min(zij, zmax - zij)
                    rij2 = xij * xij + yij * yij + zij * zij

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