"examples/python-dockerit/Modelfile" did not exist on "1363f537ce0331ab6c09238795960a01c8560d36"
functor.h 1.58 KB
Newer Older
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
71
72
73
74
75
76
/*!
 *  Copyright (c) 2019 by Contributors
 * \file kernel/cpu/functor.h
 * \brief Functors for template on CPU
 */
#ifndef DGL_KERNEL_CPU_FUNCTOR_H_
#define DGL_KERNEL_CPU_FUNCTOR_H_

#include <dmlc/omp.h>

#include <algorithm>

#include "../binary_reduce_common.h"

namespace dgl {
namespace kernel {

// Reducer functor specialization
template <typename DType>
struct ReduceSum<kDLCPU, DType> {
  static void Call(DType* addr, DType val) {
#pragma omp atomic
    *addr += val;
  }
  static DType BackwardCall(DType val, DType accum) {
    return 1;
  }
};

template <typename DType>
struct ReduceMax<kDLCPU, DType> {
  static void Call(DType* addr, DType val) {
#pragma omp critical
    *addr = std::max(*addr, val);
  }
  static DType BackwardCall(DType val, DType accum) {
    return static_cast<DType>(val == accum);
  }
};

template <typename DType>
struct ReduceMin<kDLCPU, DType> {
  static void Call(DType* addr, DType val) {
#pragma omp critical
    *addr = std::min(*addr, val);
  }
  static DType BackwardCall(DType val, DType accum) {
    return static_cast<DType>(val == accum);
  }
};

template <typename DType>
struct ReduceProd<kDLCPU, DType> {
  static void Call(DType* addr, DType val) {
#pragma omp atomic
    *addr *= val;
  }
  static DType BackwardCall(DType val, DType accum) {
    return accum / val;
  }
};

template <typename DType>
struct ReduceNone<kDLCPU, DType> {
  static void Call(DType* addr, DType val) {
    *addr = val;
  }
  static DType BackwardCall(DType val, DType accum) {
    return 1;
  }
};

}  // namespace kernel
}  // namespace dgl

#endif  // DGL_KERNEL_CPU_FUNCTOR_H_