compute-acceleration
GEMM and sum reduction across serial, OpenMP and CUDA
Loading...
Searching...
No Matches
reduction_omp.h
1// Copyright (c) 2025 yanghuafang
2// SPDX-License-Identifier: MIT
3
4#ifndef ACCEL_OMP_REDUCTION_OMP_H_
5#define ACCEL_OMP_REDUCTION_OMP_H_
6
7#include "core/span.h"
8
9namespace accel {
10
11// Thread-parallel sum via an OpenMP `reduction(+ : ...)` clause.
12//
13// Each thread accumulates a private partial over a static slice, and the
14// runtime combines them at the end of the region. That is the same two-stage
15// shape the CUDA kernel uses — private accumulation, then a tree combine —
16// with the runtime supplying the tree.
17//
18// Thread-safety: safe to call concurrently, but see OpenmpSetThreads()
19// regarding nested regions; this function is intended to be entered from the
20// serial region.
21//
22// The combine order is chosen by the runtime and is **not** fixed across runs
23// or thread counts. Two calls on identical input may differ in the last bits.
24// Compare against a relative tolerance; SumBlocked() is the variant to use
25// when reproducibility matters.
26//
27// Precondition: A non-null pointer whenever the span is non-empty.
28//
29// Postcondition: Returns the total; the operand is not modified.
30double SumOmp(Span<const float> input) noexcept;
31
32} // namespace accel
33
34#endif // ACCEL_OMP_REDUCTION_OMP_H_