Repository navigation
Expand file tree
/
Copy pathexample.cpp
More file actions
61 lines (51 loc) · 2.09 KB
/
Copy pathexample.cpp
File metadata and controls
61 lines (51 loc) · 2.09 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
// Copyright (c) 2026, Lilo Huang <kuso.cc@gmail.com>
// SPDX-License-Identifier: BSD-3-Clause
/**
* @file example.cpp
* @brief Minimal usage example for adn::sum and adn::cumsum.
*
* Sums data that defeats naive floating-point summation (large values
* cancelling around a small signal) and shows that the result is
* bit-identical no matter how the input is ordered.
*/
#include <sycl/sycl.hpp>
#include <algorithm>
#include <cstdio>
#include <random>
#include <vector>
#include "repro_sum.hpp"
int main() {
sycl::queue q{sycl::gpu_selector_v};
std::printf("Device: %s\n",
q.get_device().get_info<sycl::info::device::name>().c_str());
// 1e16 +/- pairs cancel exactly; the true sum is the 10000 ones.
std::vector<double> data;
for (int i = 0; i < 5; ++i) {
data.push_back(1e16);
data.push_back(-1e16);
}
for (int i = 0; i < 10000; ++i) {
data.push_back(1.0);
}
// Host pointer overload (copies to device internally):
double result = adn::sum(q, data.data(), data.size());
std::printf("adn::sum = %.17g (expected 10000)\n", result);
// Same data, shuffled - the reproducible sum is bit-identical:
std::mt19937 rng(42);
std::shuffle(data.begin(), data.end(), rng);
double shuffled = adn::sum(q, data.data(), data.size());
std::printf("shuffled input = %.17g (%s)\n", shuffled,
shuffled == result ? "bit-identical" : "MISMATCH");
// Every element is the reproducible sum of the input prefix ending there.
std::vector<double> prefixes(data.size());
adn::cumsum(q, data.data(), prefixes.data(), data.size());
std::printf("last prefix = %.17g (%s)\n", prefixes.back(),
prefixes.back() == shuffled ? "matches sum" : "MISMATCH");
// Device-pointer overload with more folds for higher accuracy:
double *d_arr = sycl::malloc_device<double>(data.size(), q);
q.memcpy(d_arr, data.data(), data.size() * sizeof(double)).wait();
double k6 = adn::sum<6>(q, d_arr, data.size());
std::printf("K=6 folds = %.17g\n", k6);
sycl::free(d_arr, q);
return shuffled == result && prefixes.back() == shuffled ? 0 : 1;
}