vSMC
vSMC: Scalable Monte Carlo
cblas.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/math/cblas.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_MATH_CBLAS_HPP
33 #define VSMC_MATH_CBLAS_HPP
34 
35 #include <vsmc/internal/config.hpp>
36 #include <cmath>
37 #include <numeric>
38 
39 #if VSMC_USE_MKL_CBLAS
40 #include <mkl.h>
41 #define VSMC_CBLAS_INT MKL_INT
42 #elif VSMC_USE_VECLIB_CBLAS
43 #include <Accelerate/Accelerate.h>
44 #define VSMC_CBLAS_INT int
45 #endif
46 
50 #ifndef VSMC_CBLAS_THRESHOLD
51 #define VSMC_CBLAS_THRESHOLD 1000
52 #endif
53 
54 namespace vsmc {
55 
56 namespace math {
57 
60 template <typename T>
61 inline T asum (std::size_t n, const T *x)
62 {
63  using std::fabs;
64 
65  T sum = 0;
66  for (std::size_t i = 0; i != n; ++i)
67  sum += fabs(x[i]);
68 
69  return sum;
70 }
71 
74 template <typename T>
75 inline T dot (std::size_t n, const T *x, const T *y)
76 {return std::inner_product(x, x + n, y, static_cast<T>(0));}
77 
79 template <typename T>
80 inline void scal (std::size_t n, T a, T *x)
81 {
82  for (std::size_t i = 0; i != n; ++i)
83  x[i] *= a;
84 }
85 
86 } // namespace vsmc::math
87 
88 } // namespace vsmc
89 
90 #ifdef VSMC_CBLAS_INT
91 
92 #define VSMC_DEFINE_MATH_CBLAS_S1(name, sname, dname) \
93 inline float name (std::size_t n, const float *x) \
94 { \
95  return n < VSMC_CBLAS_THRESHOLD ? \
96  name<float>(n, x): \
97  ::cblas_##sname(static_cast<VSMC_CBLAS_INT>(n), x, 1); \
98 } \
99 inline double name (std::size_t n, const double *x) \
100 { \
101  return n < VSMC_CBLAS_THRESHOLD ? \
102  name<double>(n, x): \
103  ::cblas_##dname(static_cast<VSMC_CBLAS_INT>(n), x, 1); \
104 }
105 
106 #define VSMC_DEFINE_MATH_CBLAS_S2(name, sname, dname) \
107 inline float name (std::size_t n, const float *x, const float *y) \
108 { \
109  return n < VSMC_CBLAS_THRESHOLD ? \
110  name<float>(n, x, y): \
111  ::cblas_##sname(static_cast<VSMC_CBLAS_INT>(n), x, 1, y, 1); \
112 } \
113 inline double name (std::size_t n, const double *x, const double *y) \
114 { \
115  return n < VSMC_CBLAS_THRESHOLD ? \
116  name<double>(n, x, y): \
117  ::cblas_##dname(static_cast<VSMC_CBLAS_INT>(n), x, 1, y, 1); \
118 }
119 
120 #define VSMC_DEFINE_MATH_CBLAS_SV(name, sname, dname) \
121 inline void name (std::size_t n, float a, float *x) \
122 { \
123  n < VSMC_CBLAS_THRESHOLD ? \
124  name<float>(n, a, x): \
125  ::cblas_##sname(static_cast<VSMC_CBLAS_INT>(n), a, x, 1); \
126 } \
127 inline void name (std::size_t n, double a, double *x) \
128 { \
129  n < VSMC_CBLAS_THRESHOLD ? \
130  name<double>(n, a, x): \
131  ::cblas_##dname(static_cast<VSMC_CBLAS_INT>(n), a, x, 1); \
132 }
133 
134 namespace vsmc { namespace math {
135 
139 
140 } } // namespace vsmc::math
141 
142 #endif // VSMC_CBLAS_INT
143 
144 #endif // VSMC_MATH_CBLAS_HPP
Definition: adapter.hpp:37
T dot(std::size_t n, const T *x, const T *y)
The dot product.
Definition: cblas.hpp:75
void scal(std::size_t n, T a, T *x)
Scale a vector.
Definition: cblas.hpp:80
#define VSMC_DEFINE_MATH_CBLAS_SV(name, sname, dname)
Definition: cblas.hpp:120
T asum(std::size_t n, const T *x)
Sum of vector magnitudes.
Definition: cblas.hpp:61
#define VSMC_DEFINE_MATH_CBLAS_S2(name, sname, dname)
Definition: cblas.hpp:106
#define VSMC_DEFINE_MATH_CBLAS_S1(name, sname, dname)
Definition: cblas.hpp:92