mirror of
https://github.com/siemens-mobile-hacks/llvm-c166.git
synced 2026-11-25 21:45:57 +00:00
Regular cross-team reductions have two phases: the intra-team reduction and the inter-team reduction. Atomic cross-team reductions replace the second phase with a atomic instruction which is used by the main thread of each team to directly fold the result of the intra-team reduction into the final result. Since this requires a combination of "data type" and "combine operation" for which an atomic instruction is available, only some (but very common) reductions can be transformed to atomic reductions. In cases where multiple reductions are performed on the same construct, the atomic path is only taken if all reductions can be transformed. Otherwise, we fall back to the regular cross-team reduction using a buffer with per-team slots. This is not strictly necessary, but hybrid reductions would induce more complexity with questionable benefit. Selecting an atomic path might not be the best option for every situation, which is why it is not enabled by default. Instead, it can be enabled via `-fopenmp-target-atomic-reduction`. Note that enabling the atomic path will not *force* atomic reductions. They will only be applied if possible, as described above. The performance (measured with https://github.com/ro-i/xteam-test @ c71339705091500f731e2a39f247d2660bacbdce, array size 177,777,777) is up to +15% faster (aka, more throughput) for supported reductions on a gfx942, with no noticeable regressions. Example: - sum reduction, type double: +10.22% faster - sum reduction, type uint: +15.57% faster - sum reduction, type ulong: +13.31% faster On a gfx90a, there is little to negative benefit: - sum reduction, type double: -4.32% faster (aka, slower) - sum reduction, type uint: +3.08% faster - sum reduction, type ulong: +1.68% faster Claude assisted with this patch.
64 lines
2.3 KiB
C++
64 lines
2.3 KiB
C++
// Validate the atomic cross-team reduction fast path under unified shared
|
|
// memory.
|
|
//
|
|
// RUN: %libomptarget-compilexx-generic -fopenmp-force-usm \
|
|
// RUN: -fopenmp-target-atomic-reduction && env HSA_XNACK=1 \
|
|
// RUN: %libomptarget-run-generic | %fcheck-generic
|
|
// RUN: %libomptarget-compileoptxx-generic -fopenmp-force-usm \
|
|
// RUN: -fopenmp-target-atomic-reduction && env HSA_XNACK=1 \
|
|
// RUN: %libomptarget-run-generic | %fcheck-generic
|
|
//
|
|
// REQUIRES: amdgpu, unified_shared_memory
|
|
|
|
#include <cassert>
|
|
#include <climits>
|
|
#include <cstdio>
|
|
#include <omp.h>
|
|
|
|
int main() {
|
|
const int N = 1 << 16;
|
|
|
|
long *sum = (long *)omp_alloc(sizeof(long), llvm_omp_target_shared_mem_alloc);
|
|
*sum = 7;
|
|
#pragma omp target teams distribute parallel for reduction(+ : sum[0])
|
|
for (int i = 0; i < N; ++i)
|
|
sum[0] += i;
|
|
assert(sum[0] == 7 + (long)(N - 1) * N / 2 && "atomic + reduction incorrect");
|
|
omp_free(sum, llvm_omp_target_shared_mem_alloc);
|
|
|
|
double *fsum =
|
|
(double *)omp_alloc(sizeof(double), llvm_omp_target_shared_mem_alloc);
|
|
*fsum = 0.5;
|
|
#pragma omp target teams distribute parallel for reduction(+ : fsum[0])
|
|
for (int i = 0; i < N; ++i)
|
|
fsum[0] += 1.0;
|
|
assert(fsum[0] == 0.5 + (double)N && "atomic fp + reduction incorrect");
|
|
omp_free(fsum, llvm_omp_target_shared_mem_alloc);
|
|
|
|
int *mx = (int *)omp_alloc(sizeof(int), llvm_omp_target_shared_mem_alloc);
|
|
int *mn = (int *)omp_alloc(sizeof(int), llvm_omp_target_shared_mem_alloc);
|
|
*mx = INT_MIN;
|
|
*mn = INT_MAX;
|
|
#pragma omp target teams distribute parallel for reduction(max : mx[0]) \
|
|
reduction(min : mn[0])
|
|
for (int i = 0; i < N; ++i) {
|
|
mx[0] = i > mx[0] ? i : mx[0];
|
|
mn[0] = i < mn[0] ? i : mn[0];
|
|
}
|
|
assert(mx[0] == N - 1 && mn[0] == 0 && "atomic min/max reduction incorrect");
|
|
omp_free(mx, llvm_omp_target_shared_mem_alloc);
|
|
omp_free(mn, llvm_omp_target_shared_mem_alloc);
|
|
|
|
unsigned *bits =
|
|
(unsigned *)omp_alloc(sizeof(unsigned), llvm_omp_target_shared_mem_alloc);
|
|
*bits = 0;
|
|
#pragma omp target teams distribute parallel for reduction(| : bits[0])
|
|
for (int i = 0; i < N; ++i)
|
|
bits[0] |= (unsigned)i;
|
|
assert(bits[0] == (unsigned)(N - 1) && "atomic | reduction incorrect");
|
|
omp_free(bits, llvm_omp_target_shared_mem_alloc);
|
|
|
|
printf("SUCCESS\n");
|
|
// CHECK: SUCCESS
|
|
return 0;
|
|
}
|