forked from NVIDIA/cuda-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuda_interval.h
331 lines (281 loc) · 9.88 KB
/
cuda_interval.h
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CUDA_INTERVAL_H
#define CUDA_INTERVAL_H
#include "interval.h"
#include "cuda_interval_lib.h"
// Stack in local memory. Managed independently for each thread.
template <class T, int N>
class local_stack {
private:
T buf[N];
int tos;
public:
__device__ local_stack() : tos(-1) {}
__device__ T const &top() const { return buf[tos]; }
__device__ T &top() { return buf[tos]; }
__device__ void push(T const &v) { buf[++tos] = v; }
__device__ T pop() { return buf[tos--]; }
__device__ bool full() { return tos == (N - 1); }
__device__ bool empty() { return tos == -1; }
};
// Stacks in global memory.
// Same function as local_stack, but accessible from the host.
// Interleaved between threads by blocks of THREADS elements.
// Independent stack for each thread, no sharing of data between threads.
template <class T, int N, int THREADS>
class global_stack {
private:
T *buf;
int free_index;
public:
// buf should point to an allocated global buffer of
// size N * THREADS * sizeof(T)
__device__ global_stack(T *buf, int thread_id)
: buf(buf), free_index(thread_id) {}
__device__ void push(T const &v) {
buf[free_index] = v;
free_index += THREADS;
}
__device__ T pop() {
free_index -= THREADS;
return buf[free_index];
}
__device__ bool full() { return free_index >= N * THREADS; }
__device__ bool empty() { return free_index < THREADS; }
__device__ int size() { return free_index / THREADS; }
};
// The function F of which we want to find roots, defined on intervals
// Should typically depend on thread_id (indexing an array of coefficients...)
template <class T>
__device__ interval_gpu<T> f(interval_gpu<T> const &x, int thread_id) {
typedef interval_gpu<T> I;
T alpha = -T(thread_id) / T(THREADS);
return square(x - I(1)) + I(alpha) * x;
}
// First derivative of F, also defined on intervals
template <class T>
__device__ interval_gpu<T> fd(interval_gpu<T> const &x, int thread_id) {
typedef interval_gpu<T> I;
T alpha = -T(thread_id) / T(THREADS);
return I(2) * x + I(alpha - 2);
}
// Is this interval small enough to stop iterating?
template <class T>
__device__ bool is_minimal(interval_gpu<T> const &x, int thread_id) {
T const epsilon_x = 1e-6f;
T const epsilon_y = 1e-6f;
return !empty(x) && (width(x) <= epsilon_x * abs(median(x)) ||
width(f(x, thread_id)) <= epsilon_y);
}
// In some cases, Newton iterations converge slowly.
// Bisecting the interval accelerates convergence.
template <class T>
__device__ bool should_bisect(interval_gpu<T> const &x,
interval_gpu<T> const &x1,
interval_gpu<T> const &x2, T alpha) {
T wmax = alpha * width(x);
return (!empty(x1) && width(x1) > wmax) || (!empty(x2) && width(x2) > wmax);
}
// Main interval Newton loop.
// Keep refining a list of intervals stored in a stack.
// Always keep the next interval to work on in registers
// (avoids excessive spilling to local mem)
template <class T, int THREADS, int DEPTH_RESULT>
__device__ void newton_interval(
global_stack<interval_gpu<T>, DEPTH_RESULT, THREADS> &result,
interval_gpu<T> const &ix0, int thread_id) {
typedef interval_gpu<T> I;
int const DEPTH_WORK = 128;
T const alpha = .99f; // Threshold before switching to bisection
// Intervals to be processed
local_stack<I, DEPTH_WORK> work;
// We start with the whole domain
I ix = ix0;
while (true) {
// Compute (x - F({x})/F'(ix)) inter ix
// -> may yield 0, 1 or 2 intervals
T x = median(ix);
I iq = f(I(x), thread_id);
I id = fd(ix, thread_id);
bool has_part2;
I part1, part2 = I::empty();
part1 = division_part1(iq, id, has_part2);
part1 = intersect(I(x) - part1, ix);
if (has_part2) {
part2 = division_part2(iq, id);
part2 = intersect(I(x) - part2, ix);
}
// Do we have small-enough intervals?
if (is_minimal(part1, thread_id)) {
result.push(part1);
part1 = I::empty();
}
if (has_part2 && is_minimal(part2, thread_id)) {
result.push(part2);
part2 = I::empty();
}
if (should_bisect(ix, part1, part2, alpha)) {
// Not so good improvement
// Switch to bisection method for this step
part1 = I(ix.lower(), x);
part2 = I(x, ix.upper());
has_part2 = true;
}
if (!empty(part1)) {
// At least 1 solution
// We will compute part1 next
ix = part1;
if (has_part2 && !empty(part2)) {
// 2 solutions
// Save the second solution for later
work.push(part2);
}
} else if (has_part2 && !empty(part2)) {
// 1 solution
// Work on that next
ix = part2;
} else {
// No solution
// Do we still have work to do in the stack?
if (work.empty()) // If not, we are done
break;
else
ix = work.pop(); // Otherwise, pick an interval to work on
}
}
}
// Recursive implementation
template <class T, int THREADS, int DEPTH_RESULT>
__device__ void newton_interval_rec(
global_stack<interval_gpu<T>, DEPTH_RESULT, THREADS> &result,
interval_gpu<T> const &ix, int thread_id) {
typedef interval_gpu<T> I;
T const alpha = .99f; // Threshold before switching to bisection
if (is_minimal(ix, thread_id)) {
result.push(ix);
return;
}
// Compute (x - F({x})/F'(ix)) inter ix
// -> may yield 0, 1 or 2 intervals
T x = median(ix);
I iq = f(I(x), thread_id);
I id = fd(ix, thread_id);
bool has_part2;
I part1, part2 = I::empty();
part1 = division_part1(iq, id, has_part2);
part1 = intersect(I(x) - part1, ix);
if (has_part2) {
part2 = division_part2(iq, id);
part2 = intersect(I(x) - part2, ix);
}
if (should_bisect(ix, part1, part2, alpha)) {
// Not so good improvement
// Switch to bisection method for this step
part1 = I(ix.lower(), x);
part2 = I(x, ix.upper());
has_part2 = true;
}
if (has_part2 && !empty(part2)) {
newton_interval_rec<T, THREADS, DEPTH_RESULT>(result, part2, thread_id);
}
if (!empty(part1)) {
newton_interval_rec<T, THREADS, DEPTH_RESULT>(result, part1, thread_id);
}
}
// Naive implementation, no attempt to keep the top of the stack in registers
template <class T, int THREADS, int DEPTH_RESULT>
__device__ void newton_interval_naive(
global_stack<interval_gpu<T>, DEPTH_RESULT, THREADS> &result,
interval_gpu<T> const &ix0, int thread_id) {
typedef interval_gpu<T> I;
int const DEPTH_WORK = 128;
T const alpha = .99f; // Threshold before switching to bisection
// Intervals to be processed
local_stack<I, DEPTH_WORK> work;
// We start with the whole domain
work.push(ix0);
while (!work.empty()) {
I ix = work.pop();
if (is_minimal(ix, thread_id)) {
result.push(ix);
} else {
// Compute (x - F({x})/F'(ix)) inter ix
// -> may yield 0, 1 or 2 intervals
T x = median(ix);
I iq = f(I(x), thread_id);
I id = fd(ix, thread_id);
bool has_part2;
I part1, part2 = I::empty();
part1 = division_part1(iq, id, has_part2);
part1 = intersect(I(x) - part1, ix);
if (has_part2) {
part2 = division_part2(iq, id);
part2 = intersect(I(x) - part2, ix);
}
if (should_bisect(ix, part1, part2, alpha)) {
// Not so good improvement
// Switch to bisection method for this step
part1 = I(ix.lower(), x);
part2 = I(x, ix.upper());
has_part2 = true;
}
if (!empty(part1)) {
work.push(part1);
}
if (has_part2 && !empty(part2)) {
work.push(part2);
}
}
}
}
template <class T>
__global__ void test_interval_newton(interval_gpu<T> *buffer, int *nresults,
interval_gpu<T> i,
int implementation_choice) {
int thread_id = blockIdx.x * BLOCK_SIZE + threadIdx.x;
typedef interval_gpu<T> I;
// Intervals to return
global_stack<I, DEPTH_RESULT, THREADS> result(buffer, thread_id);
switch (implementation_choice) {
case 0:
newton_interval_naive<T, THREADS>(result, i, thread_id);
break;
case 1:
newton_interval<T, THREADS>(result, i, thread_id);
break;
#if (__CUDA_ARCH__ >= 200)
case 2:
newton_interval_rec<T, THREADS>(result, i, thread_id);
break;
#endif
default:
newton_interval_naive<T, THREADS>(result, i, thread_id);
}
nresults[thread_id] = result.size();
}
#endif