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 operator()(
15  const int N,
16  const float alpha,
17  const T* x,
18  T* y,
19  Context* context) {
20  Scale(N, alpha, x, y, context);
21  }
22 };
23 
24 // Put light-weight implementations in .h file to enable inlining
25 template<typename T>
26 struct ScaleImpl<T, CPUContext, 1> {
27  inline void operator()(
28  const int N,
29  const float alpha,
30  const T* x,
31  T* y,
32  CPUContext* /*context*/) {
33  DCHECK_EQ(N, 1);
34  *y = *x * alpha;
35  }
36 };
37 
38 template<typename T, class Context, int FixedSize>
39 struct AxpyImpl {
40  inline void operator()(
41  const int N,
42  const float alpha,
43  const T* x,
44  T* y,
45  Context* context) {
46  Axpy(N, alpha, x, y, context);
47  }
48 };
49 
50 // Put light-weight implementations in .h file to enable inlining
51 template<typename T>
52 struct AxpyImpl<T, CPUContext, 1> {
53  inline void operator()(
54  const int N,
55  const float alpha,
56  const T* x,
57  T* y,
58  CPUContext* /*context*/) {
59  DCHECK_EQ(N, 1);
60  *y += *x * alpha;
61  }
62 };
63 
64 
65 } // namespace detail
66 
67 template <typename T, class Context, int FixedSize>
68 inline void ScaleFixedSize(
69  const int N,
70  const float alpha,
71  const T* x,
72  T* y,
73  Context* context) {
74  detail::ScaleImpl<T, Context, FixedSize>()(N, alpha, x, y, context);
75 }
76 
77 template <typename T, class Context, int FixedSize>
78 inline void AxpyFixedSize(
79  const int N,
80  const float alpha,
81  const T* x,
82  T* y,
83  Context* context) {
84  detail::AxpyImpl<T, Context, FixedSize>()(N, alpha, x, y, context);
85 }
86 
87 } // namespace math
88 } // namespace caffe2
89 
90 #endif // CAFFE2_UTILS_MATH_DETAIL_H_
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:40
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13