"discover/gpu_info_oneapi.c" did not exist on "fd5971be0bb11d1b5903fc6778c329b4fd93d569"
reducer.h 3.76 KB
Newer Older
rusty1s's avatar
matmul  
rusty1s committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

#include <limits>
#include <map>

enum ReductionType { SUM, MEAN, MUL, DIV, MIN, MAX };

const std::map<std::string, ReductionType> reduce2REDUCE = {
    {"sum", SUM}, {"mean", MEAN}, {"mul", MUL},
    {"div", DIV}, {"min", MIN},   {"max", MAX},
};

#define AT_DISPATCH_REDUCTION_TYPES(reduce, ...)                               \
  [&] {                                                                        \
    switch (reduce2REDUCE.at(reduce)) {                                        \
    case SUM: {                                                                \
rusty1s's avatar
rusty1s committed
17
      static constexpr ReductionType REDUCE = SUM;                             \
rusty1s's avatar
matmul  
rusty1s committed
18
19
20
      return __VA_ARGS__();                                                    \
    }                                                                          \
    case MEAN: {                                                               \
rusty1s's avatar
rusty1s committed
21
      static constexpr ReductionType REDUCE = MEAN;                            \
rusty1s's avatar
matmul  
rusty1s committed
22
23
24
      return __VA_ARGS__();                                                    \
    }                                                                          \
    case MUL: {                                                                \
rusty1s's avatar
rusty1s committed
25
      static constexpr ReductionType REDUCE = MUL;                             \
rusty1s's avatar
matmul  
rusty1s committed
26
27
28
      return __VA_ARGS__();                                                    \
    }                                                                          \
    case DIV: {                                                                \
rusty1s's avatar
rusty1s committed
29
      static constexpr ReductionType REDUCE = DIV;                             \
rusty1s's avatar
matmul  
rusty1s committed
30
31
32
      return __VA_ARGS__();                                                    \
    }                                                                          \
    case MIN: {                                                                \
rusty1s's avatar
rusty1s committed
33
      static constexpr ReductionType REDUCE = MIN;                             \
rusty1s's avatar
matmul  
rusty1s committed
34
35
36
      return __VA_ARGS__();                                                    \
    }                                                                          \
    case MAX: {                                                                \
rusty1s's avatar
rusty1s committed
37
      static constexpr ReductionType REDUCE = MAX;                             \
rusty1s's avatar
matmul  
rusty1s committed
38
39
40
41
42
      return __VA_ARGS__();                                                    \
    }                                                                          \
    }                                                                          \
  }()

rusty1s's avatar
rusty1s committed
43
44
template <typename scalar_t, ReductionType REDUCE> struct Reducer {
  static inline scalar_t init() {
rusty1s's avatar
matmul  
rusty1s committed
45
46
47
48
49
50
51
52
53
54
    if (REDUCE == MUL || REDUCE == DIV)
      return (scalar_t)1;
    else if (REDUCE == MIN)
      return std::numeric_limits<scalar_t>::max();
    else if (REDUCE == MAX)
      return std::numeric_limits<scalar_t>::lowest();
    else
      return (scalar_t)0;
  }

rusty1s's avatar
rusty1s committed
55
56
  static inline void update(scalar_t *val, scalar_t new_val, int64_t *arg,
                            int64_t new_arg) {
rusty1s's avatar
matmul  
rusty1s committed
57
58
59
60
61
62
63
64
65
66
67
68
69
    if (REDUCE == SUM || REDUCE == MEAN)
      *val = *val + new_val;
    else if (REDUCE == MUL)
      *val = *val * new_val;
    else if (REDUCE == DIV)
      *val = *val / new_val;
    else if ((REDUCE == MIN && new_val < *val) ||
             (REDUCE == MAX && new_val > *val)) {
      *val = new_val;
      *arg = new_arg;
    }
  }

rusty1s's avatar
rusty1s committed
70
71
  static inline void write(scalar_t *address, scalar_t val,
                           int64_t *arg_address, int64_t arg, int count) {
rusty1s's avatar
matmul  
rusty1s committed
72
73
74
75
76
77
78
79
80
81
82
83
84
    if (REDUCE == SUM || REDUCE == MUL || REDUCE == DIV)
      *address = val;
    else if (REDUCE == MEAN)
      *address = val / (count > 0 ? count : (scalar_t)1);
    else if (REDUCE == MIN || REDUCE == MAX) {
      if (count > 0) {
        *address = val;
        *arg_address = arg;
      } else
        *address = (scalar_t)0;
    }
  }
};