-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrle_arithmetic_template.j2
215 lines (175 loc) · 5.32 KB
/
rle_arithmetic_template.j2
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
# cython: infer_types=True
import numpy as np
cimport cython
from numpy import nan
{# cdef float NAN = float("NaN") #}
from libc.math cimport copysign, isfinite, INFINITY, NAN
cdef float inf = INFINITY
{# cdef float = INFINITY #}
{% for config_add_sub in configs_add_sub %}
# s/boundscheck(True/boundscheck(False
# s/boundscheck(False/boundscheck(True
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
cpdef {{config_add_sub.operation}}_rles(long [::1] runs1, {{config_add_sub.value_dtype}} [::1] values1, long [::1] runs2, {{config_add_sub.value_dtype}} [::1] values2):
cdef int x1 = 0
cdef int x2 = 0
cdef int xn = 0
cdef int nr = 0
cdef {{config_add_sub.value_dtype}} nv = 0
cdef {{config_add_sub.value_dtype}} diff = 0
cdef int l1 = len(runs1)
cdef int l2 = len(runs2)
cdef {{config_add_sub.run_dtype}} r1 = runs1[x1]
cdef {{config_add_sub.run_dtype}} r2 = runs2[x2]
nrs_arr = np.zeros(len(runs1) + len(runs2), dtype=np.{{config_add_sub.run_dtype}})
nvs_arr = np.zeros(len(runs1) + len(runs2), dtype=np.{{config_add_sub.value_dtype}})
cdef {{config_add_sub.run_dtype}}[::1] nrs
cdef {{config_add_sub.value_dtype}}[::1] nvs
nrs = nrs_arr
nvs = nvs_arr
while(x1 < l1 and x2 < l2):
diff = r1 - r2
nv = values1[x1] {{config_add_sub.op}} values2[x2]
if diff < 0:
nr = r1
r2 = r2 - r1
x1 += 1
if x1 < l1:
r1 = runs1[x1]
elif diff > 0:
nr = r2
r1 = r1 - r2
x2 += 1
if x2 < l2:
r2 = runs2[x2]
else:
nr = r2
x1 += 1
x2 += 1
if x1 < l1:
r1 = runs1[x1]
if x2 < l2:
r2 = runs2[x2]
# if the new value is the same as the old, merge the runs
if xn > 0 and nv == nvs[xn - 1]:
nrs[xn - 1] += nr
else:
nrs[xn] = nr
nvs[xn] = nv
xn += 1
# Must use resize because initial guess for array was likely way too large
nrs_arr.resize(xn, refcheck=False)
nvs_arr.resize(xn, refcheck=False)
return nrs_arr, nvs_arr
{% endfor %}
{% for config_div in configs_div %}
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
cpdef div_rles_{{config_div.name}}(long [::1] runs1, double [::1] values1, long [::1] runs2, double [::1] values2):
cdef int x1 = 0
cdef int x2 = 0
cdef int xn = 0
cdef int nr = 0
cdef double nv = 0
cdef double diff = 0
cdef double sign = 0
cdef int l1 = len(runs1)
cdef int l2 = len(runs2)
cdef long r1 = runs1[x1]
cdef long r2 = runs2[x2]
nrs_arr = np.zeros(len(runs1) + len(runs2), dtype=np.int)
nvs_arr = np.zeros(len(runs1) + len(runs2), dtype=np.double)
cdef long[::1] nrs
cdef double[::1] nvs
nrs = nrs_arr
nvs = nvs_arr
while(x1 < l1 and x2 < l2):
diff = r1 - r2
{{config_div.nv}}
if diff < 0:
nr = r1
r2 = r2 - r1
x1 += 1
if x1 < l1:
r1 = runs1[x1]
elif diff > 0:
nr = r2
r1 = r1 - r2
x2 += 1
if x2 < l2:
r2 = runs2[x2]
else:
nr = r2
x1 += 1
x2 += 1
if x1 < l1:
r1 = runs1[x1]
if x2 < l2:
r2 = runs2[x2]
# if the new value is the same as the old, merge the runs
if xn > 0 and nv == nvs[xn - 1]:
nrs[xn - 1] += nr
else:
nrs[xn] = nr
nvs[xn] = nv
xn += 1
# Must use resize because initial guess for array was likely way too large
nrs_arr.resize(xn, refcheck=False)
nvs_arr.resize(xn, refcheck=False)
return nrs_arr, nvs_arr
{% endfor %}
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
cpdef mul_rles(long [::1] runs1, double [::1] values1, long [::1] runs2, double [::1] values2):
cdef int x1 = 0
cdef int x2 = 0
cdef int xn = 0
cdef int nr = 0
cdef double nv = 0
cdef double diff = 0
cdef int l1 = len(runs1)
cdef int l2 = len(runs2)
cdef long r1 = runs1[x1]
cdef long r2 = runs2[x2]
nrs_arr = np.zeros(len(runs1) + len(runs2), dtype=np.int)
nvs_arr = np.zeros(len(runs1) + len(runs2), dtype=np.double)
cdef long[::1] nrs
cdef double[::1] nvs
nrs = nrs_arr
nvs = nvs_arr
while(x1 < l1 and x2 < l2):
diff = r1 - r2
nv = values1[x1] * values2[x2]
if diff < 0:
nr = r1
r2 = r2 - r1
x1 += 1
if x1 < l1:
r1 = runs1[x1]
elif diff > 0:
nr = r2
r1 = r1 - r2
x2 += 1
if x2 < l2:
r2 = runs2[x2]
else:
nr = r2
x1 += 1
x2 += 1
if x1 < l1:
r1 = runs1[x1]
if x2 < l2:
r2 = runs2[x2]
# if the new value is the same as the old, merge the runs
if xn > 0 and nv == nvs[xn - 1]:
nrs[xn - 1] += nr
else:
nrs[xn] = nr
nvs[xn] = nv
xn += 1
return nrs_arr, nvs_arr