Caffe
mkl_alternate.hpp
1 #ifndef CAFFE_UTIL_MKL_ALTERNATE_H_
2 #define CAFFE_UTIL_MKL_ALTERNATE_H_
3 
4 #ifdef USE_MKL
5 
6 #include <mkl.h>
7 
8 #else // If use MKL, simply include the MKL header
9 
10 #ifdef USE_ACCELERATE
11 #include <Accelerate/Accelerate.h>
12 #else
13 extern "C" {
14 #include <cblas.h>
15 }
16 #endif // USE_ACCELERATE
17 
18 #include <math.h>
19 
20 // Functions that caffe uses but are not present if MKL is not linked.
21 
22 // A simple way to define the vsl unary functions. The operation should
23 // be in the form e.g. y[i] = sqrt(a[i])
24 #define DEFINE_VSL_UNARY_FUNC(name, operation) \
25  template<typename Dtype> \
26  void v##name(const int n, const Dtype* a, Dtype* y) { \
27  CHECK_GT(n, 0); CHECK(a); CHECK(y); \
28  for (int i = 0; i < n; ++i) { operation; } \
29  } \
30  inline void vs##name( \
31  const int n, const float* a, float* y) { \
32  v##name<float>(n, a, y); \
33  } \
34  inline void vd##name( \
35  const int n, const double* a, double* y) { \
36  v##name<double>(n, a, y); \
37  }
38 
39 DEFINE_VSL_UNARY_FUNC(Sqr, y[i] = a[i] * a[i]);
40 DEFINE_VSL_UNARY_FUNC(Exp, y[i] = exp(a[i]));
41 DEFINE_VSL_UNARY_FUNC(Ln, y[i] = log(a[i]));
42 DEFINE_VSL_UNARY_FUNC(Abs, y[i] = fabs(a[i]));
43 
44 // A simple way to define the vsl unary functions with singular parameter b.
45 // The operation should be in the form e.g. y[i] = pow(a[i], b)
46 #define DEFINE_VSL_UNARY_FUNC_WITH_PARAM(name, operation) \
47  template<typename Dtype> \
48  void v##name(const int n, const Dtype* a, const Dtype b, Dtype* y) { \
49  CHECK_GT(n, 0); CHECK(a); CHECK(y); \
50  for (int i = 0; i < n; ++i) { operation; } \
51  } \
52  inline void vs##name( \
53  const int n, const float* a, const float b, float* y) { \
54  v##name<float>(n, a, b, y); \
55  } \
56  inline void vd##name( \
57  const int n, const double* a, const float b, double* y) { \
58  v##name<double>(n, a, b, y); \
59  }
60 
61 DEFINE_VSL_UNARY_FUNC_WITH_PARAM(Powx, y[i] = pow(a[i], b));
62 
63 // A simple way to define the vsl binary functions. The operation should
64 // be in the form e.g. y[i] = a[i] + b[i]
65 #define DEFINE_VSL_BINARY_FUNC(name, operation) \
66  template<typename Dtype> \
67  void v##name(const int n, const Dtype* a, const Dtype* b, Dtype* y) { \
68  CHECK_GT(n, 0); CHECK(a); CHECK(b); CHECK(y); \
69  for (int i = 0; i < n; ++i) { operation; } \
70  } \
71  inline void vs##name( \
72  const int n, const float* a, const float* b, float* y) { \
73  v##name<float>(n, a, b, y); \
74  } \
75  inline void vd##name( \
76  const int n, const double* a, const double* b, double* y) { \
77  v##name<double>(n, a, b, y); \
78  }
79 
80 DEFINE_VSL_BINARY_FUNC(Add, y[i] = a[i] + b[i]);
81 DEFINE_VSL_BINARY_FUNC(Sub, y[i] = a[i] - b[i]);
82 DEFINE_VSL_BINARY_FUNC(Mul, y[i] = a[i] * b[i]);
83 DEFINE_VSL_BINARY_FUNC(Div, y[i] = a[i] / b[i]);
84 
85 // In addition, MKL comes with an additional function axpby that is not present
86 // in standard blas. We will simply use a two-step (inefficient, of course) way
87 // to mimic that.
88 inline void cblas_saxpby(const int N, const float alpha, const float* X,
89  const int incX, const float beta, float* Y,
90  const int incY) {
91  cblas_sscal(N, beta, Y, incY);
92  cblas_saxpy(N, alpha, X, incX, Y, incY);
93 }
94 inline void cblas_daxpby(const int N, const double alpha, const double* X,
95  const int incX, const double beta, double* Y,
96  const int incY) {
97  cblas_dscal(N, beta, Y, incY);
98  cblas_daxpy(N, alpha, X, incX, Y, incY);
99 }
100 
101 #endif // USE_MKL
102 #endif // CAFFE_UTIL_MKL_ALTERNATE_H_