C++ examples#
A gallery of complete, self-contained eagle programs. Each lives under
docs/examples/ and is compiled and run by the exampletest gate
(docs/examples/run_examples.sh) in every mode it supports, so the sources
here are guaranteed to build and produce the output the C++ tutorials show.
Example |
Mode |
Modules |
What it shows |
|---|---|---|---|
|
CUDA |
graph |
Capture a launch, build a graph, instantiate a |
|
CUDA + C++ |
host |
One task per index through the OpenMP + SIMD |
|
CUDA + C++ |
reduce |
Sum an aether SoA array with the GPU or the OpenMP reduction. |
Full sources#
01_capture_replay.cu
1// Copyright 2026 Alessandro Masat
2// SPDX-License-Identifier: Apache-2.0
3
4// 01_capture_replay.cu
5//
6// The flagship EAGLE pattern: record a stream of kernel launches once, assemble
7// them into a CUDA graph, instantiate a Launcher, and replay the instantiated
8// graph many times with no per-launch API overhead.
9//
10// This is a CUDA-only example (graph capture has no pure-C++ analogue). Build:
11// nvcc -std=c++20 -arch=sm_61 -I<prefix>/include -DEAGLE_BLOCKSIZE=256 \
12// -DCUDA_API_PER_THREAD_DEFAULT_STREAM=1 -Xcompiler -fPIE \
13// 01_capture_replay.cu -o 01_capture_replay
14#include <cstdio>
15#include <vector>
16
17#include <eagle/eagle.h>
18
19// [cell:kernel]
20// A trivial elementwise kernel — the "work" we want to capture and replay.
21__global__ void addOne(int* buf, int n)
22{
23 const int tid = threadIdx.x + blockIdx.x * blockDim.x;
24 if (tid < n)
25 buf[tid] += 1;
26}
27// [cell:kernel:end]
28
29int main()
30{
31 constexpr int N = 64;
32 constexpr int REPLAYS = 10;
33
34 int* dBuf = nullptr;
35 if (cudaMalloc(&dBuf, N * sizeof(int)) != cudaSuccess)
36 return 1;
37 cudaMemset(dBuf, 0, N * sizeof(int));
38
39 // [cell:capture]
40 // A stream to record on, and a Graph that will own the captured work.
41 eagle::cuda::Stream stream;
42 eagle::cuda::Graph graph;
43 graph.stream(stream.cuda());
44
45 // Everything launched on the stream between begin() and end() is recorded
46 // into a cudaGraph_t instead of executing eagerly.
47 eagle::cuda::StreamCapturer capturer(stream.cuda());
48 capturer.begin();
49 addOne<<<1, N, 0, stream.cuda()>>>(dBuf, N);
50 graph.addNode(eagle::cuda::CapturedGraph{ capturer.end() });
51 // [cell:capture:end]
52
53 // [cell:replay]
54 // Instantiate the graph once, then replay it REPLAYS times. Each launch()
55 // is a single cudaGraphLaunch — no kernel-config marshalling per call.
56 eagle::cuda::Launcher launcher = graph.launcher();
57 for (int r = 0; r < REPLAYS; ++r)
58 launcher.launch();
59 launcher.synchronize();
60 // [cell:replay:end]
61
62 std::vector<int> host(N, -1);
63 cudaMemcpy(host.data(), dBuf, N * sizeof(int), cudaMemcpyDeviceToHost);
64 cudaFree(dBuf);
65
66 bool ok = true;
67 for (int i = 0; i < N; ++i)
68 ok &= (host[i] == REPLAYS);
69
70 std::printf("01_capture_replay: %d replays -> buf[0]=%d (expected %d) : %s\n",
71 REPLAYS, host[0], REPLAYS, ok ? "OK" : "FAIL");
72 return ok ? 0 : 1;
73}
02_host_dispatch.cu (CUDA-mode twin)
1// Copyright 2026 Alessandro Masat
2// SPDX-License-Identifier: Apache-2.0
3
4// 02_host_dispatch.cu
5//
6// The pure-C++ path: dispatch one task per index through EAGLE's OpenMP + SIMD
7// host launcher. `eagle::cpu::Host::launch` batches consecutive indices into
8// aether SIMD packets — the same call compiles and runs identically in CUDA
9// mode (this file) and in pure-C++ mode (the .cpp twin), which is EAGLE's
10// dual-mode promise.
11//
12// IMPORTANT: `launch` only WORK-SHARES across OpenMP threads when the caller
13// is already inside an existing `#pragma omp parallel` region; called bare,
14// as below, it runs serially (SIMD-vectorised, single-threaded). This
15// standalone example has no enclosing region, so it is a *correctness* demo,
16// not a *threading* demo — see `eagle::cpu::Host::launch`'s doc comment
17// (eagle/cpu/Host.h) for the full contract: wrapping several `launch` calls
18// in your own `#pragma omp parallel` region is what gets them threaded in
19// production code.
20#include <cstdio>
21#include <vector>
22
23#include <eagle/eagle.h>
24
25int main()
26{
27 constexpr eagle::idx_t N = 1024;
28 std::vector<double> x(N, 0.0);
29
30 // [cell:launch]
31 // One host task per index i; i.global() is this task's flat index. We write
32 // the sequence of odd numbers 1, 3, 5, ... whose prefix sums are N*N.
33 eagle::cpu::Host::launch(N, [&](const auto& i) {
34 const eagle::idx_t k = i.global();
35 x[k] = 2.0 * static_cast<double>(k) + 1.0;
36 });
37 // [cell:launch:end]
38
39 double sum = 0.0;
40 for (eagle::idx_t k = 0; k < N; ++k)
41 sum += x[k];
42
43 const double expected = static_cast<double>(N) * static_cast<double>(N);
44 const bool ok = (sum == expected);
45 std::printf("02_host_dispatch: N=%ld sum=%.0f (expected %.0f) : %s\n",
46 static_cast<long>(N), sum, expected, ok ? "OK" : "FAIL");
47 return ok ? 0 : 1;
48}
03_reduction.cu / 03_reduction.cpp
1// Copyright 2026 Alessandro Masat
2// SPDX-License-Identifier: Apache-2.0
3
4// 03_reduction.cu (CUDA mode)
5//
6// Sum an aether SoA array with EAGLE's multi-level GPU reduction. The pure-C++
7// twin (03_reduction.cpp) computes the same result with the cache-padded
8// OpenMP reduction — same array, same operator, one dual-mode API.
9#include <cstdio>
10
11#include <eagle/eagle.h>
12
13int main()
14{
15 using T = double;
16 using Sum = aether::SumOp<T>;
17 constexpr eagle::idx_t N = 1000;
18
19 // Fill a device-backed array with 1, 2, ..., N on the host, then upload.
20 auto arr = eagle::makeArray<T>(N);
21 {
22 auto a = arr.hostView();
23 for (eagle::idx_t i = 0; i < N; ++i)
24 a(i) = static_cast<T>(i + 1);
25 }
26 arr.upload();
27
28 cudaStream_t stream;
29 cudaStreamCreate(&stream);
30
31 // [cell:reduce]
32 // Blocking multi-level reduction on the GPU: returns the scalar sum.
33 const T total = eagle::cuda::Reduction::reduceBlocking<T, Sum>(arr, T(0), stream);
34 // [cell:reduce:end]
35
36 cudaStreamDestroy(stream);
37
38 const T expected = static_cast<T>(N) * static_cast<T>(N + 1) / T(2);
39 const bool ok = (total == expected);
40 std::printf("03_reduction (CUDA): sum(1..%ld)=%.0f (expected %.0f) : %s\n",
41 static_cast<long>(N), total, expected, ok ? "OK" : "FAIL");
42 return ok ? 0 : 1;
43}
1// Copyright 2026 Alessandro Masat
2// SPDX-License-Identifier: Apache-2.0
3
4// 03_reduction.cpp (pure-C++ / OpenMP mode)
5//
6// The C++-mode twin of 03_reduction.cu: the same aether SoA array reduced with
7// EAGLE's cache-padded OpenMP reduction. Built when eagle is configured with
8// EAGLE_CPP_MODE (which defines EAGLE_CPU_ONLY and removes the CUDA path).
9#include <cstdio>
10
11#include <eagle/eagle.h>
12
13int main()
14{
15 using T = double;
16 using Sum = aether::SumOp<T>;
17 constexpr eagle::idx_t N = 1000;
18
19 auto arr = eagle::makeArray<T>(N);
20 {
21 auto a = arr.hostView();
22 for (eagle::idx_t i = 0; i < N; ++i)
23 a(i) = static_cast<T>(i + 1);
24 }
25
26 // [cell:reduce]
27 // Cache-padded OpenMP reduction over the host-resident array.
28 const T total = eagle::cpu::Reduction<T, Sum>::reduce(arr.hostView().as_const(), T(0));
29 // [cell:reduce:end]
30
31 const T expected = static_cast<T>(N) * static_cast<T>(N + 1) / T(2);
32 const bool ok = (total == expected);
33 std::printf("03_reduction (C++): sum(1..%ld)=%.0f (expected %.0f) : %s\n",
34 static_cast<long>(N), total, expected, ok ? "OK" : "FAIL");
35 return ok ? 0 : 1;
36}
Running the gate#
# from the repo root, with eagle + aether installed into $CONDA_PREFIX
cd docs && make exampletest # or: CONDA_PREFIX=<env> ./examples/run_examples.sh
The gate compiles each *.cu with nvcc (CUDA mode) and each *.cpp
with g++ (pure-C++ / OpenMP mode), runs the resulting programs, and diffs
their stdout against the committed *.expected.txt files. Adding an example
is just dropping a new NN_topic.cu (and, for a dual-mode example, its
.cpp twin) plus the matching .expected.txt — the gate and this gallery
pick it up by its numeric prefix.
Embedding a compiled kernel#
The examples above are eagle’s own core facilities. The other side – loading a compiled plugin into your own C++/CUDA program through eagle’s binary ABI – is a separate, larger example: C4 Embed a kernel.