-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
cpu_interval.h
307 lines (260 loc) · 9.17 KB
/
cpu_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
/* 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.
*/
/* Simple CPU implementation
* Depends on Boost.Interval
*/
#ifndef CPU_INTERVAL_H
#define CPU_INTERVAL_H
#ifndef __USE_ISOC99
#define __USE_ISOC99
#endif
#include <iostream>
#include <vector>
#include <boost/numeric/interval.hpp>
//#include <iomanip>
#define UNPROTECTED 0
#define USE_RECURSION_CPU 1
using boost::numeric::interval;
using namespace boost::numeric;
template <class T, int N, int THREADS>
class global_stack_cpu {
private:
T *buf;
int free_index;
public:
// buf should point to an allocated global buffer of size N * THREADS *
// sizeof(T)
global_stack_cpu(T *buf, int thread_id) : buf(buf), free_index(thread_id) {}
void push(T const &v) {
buf[free_index] = v;
free_index += THREADS;
}
T pop() {
free_index -= THREADS;
return buf[free_index];
}
bool full() { return free_index >= N * THREADS; }
bool empty() { return free_index < THREADS; }
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 I>
I f_cpu(I const &x, int thread_id) {
typedef typename I::base_type T;
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 I>
I fd_cpu(I const &x, int thread_id) {
typedef typename I::base_type T;
T alpha = -T(thread_id) / T(THREADS);
return I(2) * x + I(alpha - 2);
}
// Is this interval small enough to stop iterating?
template <class I>
bool is_minimal_cpu(I const &x, int thread_id) {
typedef typename I::base_type T;
T const epsilon_x = 1e-6f;
T const epsilon_y = 1e-6f;
return !empty(x) && (width(x) <= epsilon_x * abs(median(x)) ||
width(f_cpu(x, thread_id)) <= epsilon_y);
}
// In some cases, Newton iterations converge slowly.
// Bisecting the interval accelerates convergence.
template <class I>
bool should_bisect_cpu(I const &x, I const &x1, I const &x2,
typename I::base_type alpha) {
typedef typename I::base_type T;
T wmax = alpha * width(x);
return width(x1) > wmax || width(x2) > wmax;
}
int const DEPTH_WORK = 128;
// 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 I, int THREADS, int DEPTH_RESULT>
void newton_interval_cpu(global_stack_cpu<I, DEPTH_RESULT, THREADS> &result,
I const &ix0, int thread_id) {
typedef typename I::base_type T;
T const alpha = .99f; // Threshold before switching to bisection
// Intervals to be processed
I local_buffer[DEPTH_WORK];
global_stack_cpu<I, DEPTH_WORK, 1> work(local_buffer, 0);
// 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_cpu(I(x), thread_id);
I id = fd_cpu(ix, thread_id);
bool has_part2;
I part1, part2;
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_cpu(part1, thread_id)) {
result.push(part1);
part1 = I::empty();
}
if (has_part2 && is_minimal_cpu(part2, thread_id)) {
result.push(part2);
part2 = I::empty();
}
if (should_bisect_cpu(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 ((part1.lower() <= part1.upper()) && !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
}
}
}
template <class I, int THREADS, int DEPTH_RESULT>
void newton_interval_rec_cpu(global_stack_cpu<I, DEPTH_RESULT, THREADS> &result,
I const &ix, int thread_id) {
typedef typename I::base_type T;
T const alpha = .99f; // Threshold before switching to bisection
if (is_minimal_cpu(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_cpu(I(x), thread_id);
I id = fd_cpu(ix, thread_id);
bool has_part2;
I part1, part2;
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_cpu(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 ((part1.lower() <= part1.upper()) && (!empty(part1))) {
newton_interval_rec_cpu<I, THREADS, DEPTH_RESULT>(result, part1, thread_id);
}
if (has_part2 && !empty(part2)) {
newton_interval_rec_cpu<I, THREADS, DEPTH_RESULT>(result, part2, thread_id);
}
}
template <class I>
void test_interval_newton_cpu(I *buffer, int *nresults, I i) {
typedef typename I::base_type T;
// Intervals to return
// std::vector<I> local_buffer(BLOCK_SIZE * GRID_SIZE * DEPTH_WORK);
for (int thread_id = 0; thread_id != BLOCK_SIZE * GRID_SIZE; ++thread_id) {
global_stack_cpu<I, DEPTH_RESULT, THREADS> result(buffer, thread_id);
#if USE_RECURSION_CPU
newton_interval_rec_cpu<I, THREADS>(result, i, thread_id);
#else
newton_interval_cpu<I, THREADS>(result, i, thread_id);
#endif
nresults[thread_id] = result.size();
}
}
typedef interval<T, interval_lib::policies<interval_lib::rounded_math<T>,
interval_lib::checking_base<T> > >
Ibase;
#if UNPROTECTED
typedef interval_lib::unprotect<Ibase>::type I_CPU;
Ibase::traits_type::rounding rnd;
#else
typedef Ibase I_CPU;
#endif
bool checkAgainstHost(int *h_nresults, int *h_nresults_cpu, I_CPU *h_result,
I_CPU *h_result_cpu) {
std::cout << "\nCheck against Host computation...\n\n";
int success = 1;
int success1 = 1;
int success2 = 1;
if (h_nresults_cpu[0] == h_nresults[0]) {
for (int i = 0; i != h_nresults[0]; ++i) {
TYPE diff1 = abs(h_result[THREADS * i + 0].lower() -
h_result_cpu[THREADS * i + 0].lower());
TYPE diff2 = abs(h_result[THREADS * i + 0].upper() -
h_result_cpu[THREADS * i + 0].upper());
if ((diff1 > 1.0e-6f) || (diff2 > 1.0e-6f)) {
success1 = 0;
break;
}
}
// in case the two intervals are reversed
for (int i = 0; i != h_nresults[0]; ++i) {
TYPE diff1 =
abs(h_result[THREADS * i + 0].lower() -
h_result_cpu[THREADS * (h_nresults[0] - i - 1) + 0].lower());
TYPE diff2 =
abs(h_result[THREADS * i + 0].upper() -
h_result_cpu[THREADS * (h_nresults[0] - i - 1) + 0].upper());
if ((diff1 > 1.0e-6f) || (diff2 > 1.0e-6f)) {
success2 = 0;
break;
}
}
success = success1 || success2;
} else
success = 0;
return (bool)success;
}
#endif