-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcuda.cu
More file actions
224 lines (183 loc) · 5.71 KB
/
cuda.cu
File metadata and controls
224 lines (183 loc) · 5.71 KB
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
// Copyright 2026 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <iostream>
#include <vector>
#include <random>
#include <limits>
#include <boost/safe_numbers/unsigned_integers.hpp>
#include <boost/safe_numbers/bit.hpp>
#include <boost/safe_numbers/integer_utilities.hpp>
#include <boost/safe_numbers/numeric.hpp>
#include <boost/safe_numbers/charconv.hpp>
#include <boost/safe_numbers/cuda_error_reporting.hpp>
#include <cuda_runtime.h>
using test_type = boost::safe_numbers::u32;
using basis_type = test_type::basis_type;
// All safe_numbers types and free functions are annotated with __host__ __device__,
// so they work identically on both host and device.
__global__ void arithmetic_kernel(const test_type* a, const test_type* b, test_type* out, int n)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < n)
{
// Basic arithmetic with overflow detection works on device
out[i] = a[i] + b[i];
}
}
__global__ void bit_kernel(const test_type* in, int* out, int n)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < n)
{
// All <bit> free functions work on device
out[i] = boost::safe_numbers::popcount(in[i]);
}
}
__global__ void utility_kernel(const test_type* in, test_type* out, int n)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < n)
{
// Integer utilities work on device
out[i] = boost::safe_numbers::isqrt(in[i]);
}
}
__global__ void numeric_kernel(const test_type* a, const test_type* b, test_type* out, int n)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < n)
{
// gcd, lcm, midpoint work on device
out[i] = boost::safe_numbers::gcd(a[i], b[i]);
}
}
__global__ void charconv_kernel(const test_type* in, test_type* out, int n)
{
int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < n)
{
// charconv round-trip on device
char buf[16] {};
auto tc = boost::charconv::to_chars(buf, buf + sizeof(buf), in[i]);
test_type parsed {};
boost::charconv::from_chars(buf, tc.ptr, parsed);
out[i] = parsed;
}
}
// Helper: allocate CUDA managed memory
void allocate(void** ptr, std::size_t bytes)
{
cudaError_t err = cudaMallocManaged(ptr, bytes);
if (err != cudaSuccess)
{
throw std::runtime_error(cudaGetErrorString(err));
}
cudaDeviceSynchronize();
}
template <typename T>
void cleanup(T** ptr)
{
if (*ptr != nullptr)
{
cudaFree(*ptr);
*ptr = nullptr;
}
}
int main()
{
const int n = 10000;
const int threadsPerBlock = 256;
const int blocksPerGrid = (n + threadsPerBlock - 1) / threadsPerBlock;
std::mt19937_64 rng {42};
std::uniform_int_distribution<basis_type> dist {basis_type{1}, (std::numeric_limits<basis_type>::max)() / basis_type{2}};
// --- Allocate managed arrays ---
test_type* a = nullptr;
test_type* b = nullptr;
test_type* out_tt = nullptr;
int* out_int = nullptr;
allocate(reinterpret_cast<void**>(&a), n * sizeof(test_type));
allocate(reinterpret_cast<void**>(&b), n * sizeof(test_type));
allocate(reinterpret_cast<void**>(&out_tt), n * sizeof(test_type));
allocate(reinterpret_cast<void**>(&out_int), n * sizeof(int));
for (int i = 0; i < n; ++i)
{
a[i] = test_type{dist(rng)};
b[i] = test_type{dist(rng)};
}
// The device_error_context captures any overflow/underflow errors
// reported from device code and rethrows them on the host.
boost::safe_numbers::device_error_context ctx;
// --- Test 1: Arithmetic (a + b, using half-range to avoid overflow) ---
arithmetic_kernel<<<blocksPerGrid, threadsPerBlock>>>(a, b, out_tt, n);
ctx.synchronize();
bool pass = true;
for (int i = 0; i < n; ++i)
{
if (out_tt[i] != a[i] + b[i])
{
pass = false;
break;
}
}
std::cout << "Arithmetic (add): " << (pass ? "PASSED" : "FAILED") << '\n';
// --- Test 2: Bit functions (popcount) ---
bit_kernel<<<blocksPerGrid, threadsPerBlock>>>(a, out_int, n);
ctx.synchronize();
pass = true;
for (int i = 0; i < n; ++i)
{
if (out_int[i] != boost::safe_numbers::popcount(a[i]))
{
pass = false;
break;
}
}
std::cout << "Bit (popcount): " << (pass ? "PASSED" : "FAILED") << '\n';
// --- Test 3: Integer utilities (isqrt) ---
utility_kernel<<<blocksPerGrid, threadsPerBlock>>>(a, out_tt, n);
ctx.synchronize();
pass = true;
for (int i = 0; i < n; ++i)
{
if (out_tt[i] != boost::safe_numbers::isqrt(a[i]))
{
pass = false;
break;
}
}
std::cout << "Utility (isqrt): " << (pass ? "PASSED" : "FAILED") << '\n';
// --- Test 4: Numeric (gcd) ---
numeric_kernel<<<blocksPerGrid, threadsPerBlock>>>(a, b, out_tt, n);
ctx.synchronize();
pass = true;
for (int i = 0; i < n; ++i)
{
if (out_tt[i] != boost::safe_numbers::gcd(a[i], b[i]))
{
pass = false;
break;
}
}
std::cout << "Numeric (gcd): " << (pass ? "PASSED" : "FAILED") << '\n';
// --- Test 5: Charconv round-trip ---
charconv_kernel<<<blocksPerGrid, threadsPerBlock>>>(a, out_tt, n);
ctx.synchronize();
pass = true;
for (int i = 0; i < n; ++i)
{
if (out_tt[i] != a[i])
{
pass = false;
break;
}
}
std::cout << "Charconv (rt): " << (pass ? "PASSED" : "FAILED") << '\n';
// --- Cleanup ---
cleanup(&a);
cleanup(&b);
cleanup(&out_tt);
cleanup(&out_int);
cudaDeviceReset();
return 0;
}