-
Notifications
You must be signed in to change notification settings - Fork 0
/
splines.py
171 lines (140 loc) · 4.08 KB
/
splines.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 11 23:20:11 2017
@author: hkohr
"""
import mako
import numpy as np
import pygpu
from pygpu.tools import ScalarArg, ArrayArg
ctx = pygpu.init('cuda')
pygpu.set_default_context(ctx)
DTYPE_TO_CTYPE = {v: k
for k, v in pygpu.dtypes.NAME_TO_DTYPE.items()
if k.startswith('ga')}
# --- Kernel code definition --- #
kernel_tpl = mako.template.Template("""
/*************************************************************************/
/*
bspline3_norm_coord(x)
B-spline of order 3 in normalized coordinates (grid step 1).
Parameters
----------
x : float
Evaluation point.
Returns
-------
spline_val : float
B-spline value at x.
*/
WITHIN_KERNEL ${float}
bspline3_norm_coord(${float} x) {
const ${float} two_m_x = 2.0 - x;
if (x < 1.0)
return -0.5 * x * x * two_m_x + 2.0 / 3.0 * x;
else if (x < 2.0)
return two_m_x * two_m_x * two_m_x / 6.0;
else
return 0.0;
}
/*************************************************************************/
/*
bspline_weights(lam, &a0, &a1, &a2, &a3)
Compute the filter weights of a B-spline at cell fraction lam.
Parameters
----------
lam : float
Normalized position (0 <= lam < 1) in a cell where the B-spline
should be evaluated.
&a0, &a1, &a2, &a3 : float_ptr
Pointers to float values where the weights should be stored.
Returns
-------
None
*/
WITHIN_KERNEL void
bspline_weights(${float} lam,
${float} *a0,
${float} *a1,
${float} *a2,
${float} *a3
){
const ${float} one_m_lam = 1.0 - lam;
const ${float} lam_sq = lam * lam;
const ${float} one_m_lam_sq = one_m_lam * one_m_lam;
*a0 = one_m_lam_sq * one_m_lam / 6.0;
*a1 = - 0.5 * lam_sq * (2.0 - lam) + 2.0 / 3.0;
*a2 = - 0.5 * one_m_lam_sq * lam + 2.0 / 3.0 ;
*a3 = lam_sq * lam / 6.0;
return;
}
/*************************************************************************/
/*
eval_bspline(x, gmin, gstep, gn)
Evaluate a 3rd order interpolating B-spline for the given grid points at
the point x.
Parameters
----------
x : float
Point in which to evaluate the spline.
gmin : float
Minimum grid point.
gstep : positive float
Step between two consecutive grid points.
gn : unsigned int
Number of grid points.
Returns
-------
bspline_val : float
The B-spline value at x.
*/
WITHIN_KERNEL ${float}
bspline_value(const ${float} x,
const ${float} gmin,
const ${float} gstep,
const unsigned int gn
){
// Compute coordinates of x in the grid
const ${float} x_idx_f = (x - gmin) / gstep;
const int x_idx = (int) ${floor}(x_idx_f);
const ${float} lam = x_idx_f - x_idx;
// Determine weights
${float} a0, a1, a2, a3;
bspline_weights(lam, &a0, &a1, &a2, &a3);
// TO BE CONTINUED
}
/*************************************************************************/
KERNEL void
test_kernel(GLOBAL_MEM ${float} *out,
const ${float} *x,
const unsigned int n){
unsigned int i;
for (i=0; i<n; i++)
out[i] = bspline3_norm_coord(x[i]);
return;
}
""")
# --- Kernel test --- #
x = pygpu.gpuarray.array([0, 0.5, 1.0, 1.5, 2.0, 2.5])
src = kernel_tpl.render(float=DTYPE_TO_CTYPE[x.dtype],
floor='floor')
out = x._empty_like_me()
args = [ArrayArg(out.dtype, 'out'), ArrayArg(x.dtype, 'x'),
ScalarArg(np.dtype('uint32'), 'n')]
spec = [pygpu.gpuarray.GpuArray, pygpu.gpuarray.GpuArray, 'uint32']
have_small = False
have_double = False
have_complex = False
for arg in args:
if arg.dtype.itemsize < 4 and type(arg) == ArrayArg:
have_small = True
if arg.dtype in [np.float64, np.complex128]:
have_double = True
if arg.dtype in [np.complex64, np.complex128]:
have_complex = True
flags = dict(have_small=have_small, have_double=have_double,
have_complex=have_complex)
kernel = pygpu.gpuarray.GpuKernel(
src, 'test_kernel', spec, context=out.context, cluda=True, **flags)
kernel(out, x, x.size, n=x.size)