Caffe2 - C++ API
A deep learning, cross platform ML framework
math-detail.h
1 #ifndef CAFFE2_UTILS_MATH_DETAIL_H_
2 #define CAFFE2_UTILS_MATH_DETAIL_H_
3 namespace caffe2 {
4 
5 class CPUContext;
6 
7 namespace math {
8 namespace detail {
9 
10 // proxy to a class because of partial specialization limitations for functions
11 
12 template<typename T, class Context, int FixedSize>
13 struct ScaleImpl {
14  inline void
15  operator()(const int N, const T alpha, const T* x, T* y, Context* context) {
16  Scale(N, alpha, x, y, context);
17  }
18 };
19 
20 // Put light-weight implementations in .h file to enable inlining
21 template<typename T>
22 struct ScaleImpl<T, CPUContext, 1> {
23  inline void operator()(
24  const int N,
25  const T alpha,
26  const T* x,
27  T* y,
28  CPUContext* context) {
29  DCHECK_EQ(N, 1);
30  *y = *x * alpha;
31  }
32 };
33 
34 template<typename T, class Context, int FixedSize>
35 struct AxpyImpl {
36  inline void
37  operator()(const int N, const T alpha, const T* x, T* y, Context* context) {
38  Axpy(N, alpha, x, y, context);
39  }
40 };
41 
42 // Put light-weight implementations in .h file to enable inlining
43 template<typename T>
44 struct AxpyImpl<T, CPUContext, 1> {
45  inline void operator()(
46  const int N,
47  const T alpha,
48  const T* x,
49  T* y,
50  CPUContext* context) {
51  DCHECK_EQ(N, 1);
52  *y += *x * alpha;
53  }
54 };
55 
56 
57 } // namespace detail
58 
59 template <typename T, class Context, int FixedSize>
60 inline void
61 ScaleFixedSize(const int N, const T alpha, const T* x, T* y, Context* context) {
62  detail::ScaleImpl<T, Context, FixedSize>()(N, alpha, x, y, context);
63 }
64 
65 template <typename T, class Context, int FixedSize>
66 inline void
67 AxpyFixedSize(const int N, const T alpha, const T* x, T* y, Context* context) {
68  detail::AxpyImpl<T, Context, FixedSize>()(N, alpha, x, y, context);
69 }
70 
71 } // namespace math
72 } // namespace caffe2
73 
74 #endif // CAFFE2_UTILS_MATH_DETAIL_H_
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:105