File: parallel_target_teams_reduction_min.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,999,616 kB
  • sloc: cpp: 6,951,724; ansic: 1,486,157; asm: 913,598; python: 232,059; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,079; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,430; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (70 lines) | stat: -rw-r--r-- 2,157 bytes parent folder | download | duplicates (6)
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
// RUN: %libomptarget-compilexx-and-run-generic
// RUN: %libomptarget-compileoptxx-and-run-generic

// FIXME: This is a bug in host offload, this should run fine.
// REQUIRES: gpu

// This test validates that the OpenMP target reductions to find a minimum work
// as indended for a few common data types.

#include <algorithm>
#include <cassert>
#include <limits>
#include <vector>

template <class Tp> void test_min_idx_reduction() {
  const Tp length = 1000;
  const Tp nminimas = 8;
  std::vector<float> a(length, 3.0f);
  const Tp step = length / nminimas;
  for (Tp i = 0; i < nminimas; i++) {
    a[i * step] -= 1.0f;
  }
  for (Tp i = 0; i < nminimas; i++) {
    Tp idx = a.size();
    float *b = a.data();
#pragma omp target teams distribute parallel for reduction(min : idx)          \
    map(always, to : b[0 : length])
    for (Tp j = 0; j < length - 1; j++) {
      if (b[j] < b[j + 1]) {
        idx = std::min(idx, j);
      }
    }
    assert(idx == i * step &&
           "#pragma omp target teams distribute parallel for "
           "reduction(min:<identifier list>) does not work as intended.");
    a[idx] += 1.0f;
  }
}

template <class Tp> void test_min_val_reduction() {
  const int length = 1000;
  const int half = length / 2;
  std::vector<Tp> a(length, (Tp)3);
  a[half] -= (Tp)1;
  Tp min_val = std::numeric_limits<Tp>::max();
  Tp *b = a.data();
#pragma omp target teams distribute parallel for reduction(min : min_val)      \
    map(always, to : b[0 : length])
  for (int i = 0; i < length; i++) {
    min_val = std::min(min_val, b[i]);
  }
  assert(std::abs(((double)a[half + 1]) - ((double)min_val) - 1.0) < 1e-6 &&
         "#pragma omp target teams distribute parallel for "
         "reduction(min:<identifier list>) does not work as intended.");
}

int main() {
  // Reducing over indices
  test_min_idx_reduction<int>();
  test_min_idx_reduction<unsigned int>();
  test_min_idx_reduction<long>();

  // Reducing over values
  test_min_val_reduction<int>();
  test_min_val_reduction<unsigned int>();
  test_min_val_reduction<long>();
  test_min_val_reduction<float>();
  test_min_val_reduction<double>();
  return 0;
}