1 #ifndef CAFFE_UTIL_MKL_ALTERNATE_H_
2 #define CAFFE_UTIL_MKL_ALTERNATE_H_
8 #else // If use MKL, simply include the MKL header
11 #include <Accelerate/Accelerate.h>
16 #endif // USE_ACCELERATE
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; } \
30 inline void vs##name( \
31 const int n, const float* a, float* y) { \
32 v##name<float>(n, a, y); \
34 inline void vd##name( \
35 const int n, const double* a, double* y) { \
36 v##name<double>(n, a, y); \
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]));
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; } \
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); \
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); \
61 DEFINE_VSL_UNARY_FUNC_WITH_PARAM(Powx, y[i] = pow(a[i], b));
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; } \
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); \
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); \
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]);
88 inline void cblas_saxpby(
const int N,
const float alpha,
const float* X,
89 const int incX,
const float beta,
float* Y,
91 cblas_sscal(N, beta, Y, incY);
92 cblas_saxpy(N, alpha, X, incX, Y, incY);
94 inline void cblas_daxpby(
const int N,
const double alpha,
const double* X,
95 const int incX,
const double beta,
double* Y,
97 cblas_dscal(N, beta, Y, incY);
98 cblas_daxpy(N, alpha, X, incX, Y, incY);
102 #endif // CAFFE_UTIL_MKL_ALTERNATE_H_