Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Numerical.h
Go to the documentation of this file.
1 
7 #ifndef LIMBO_SOLVERS_NUMERICAL_H
8 #define LIMBO_SOLVERS_NUMERICAL_H
9 
10 #include <limbo/solvers/Solvers.h>
11 #if MKL == 1
12 #include <mkl.h>
13 #endif
14 
16 namespace limbo
17 {
19 namespace solvers
20 {
21 
22 #if MKL == 1
23 template <typename T>
30 inline void mklaxpy(unsigned int n, T a, T const* x, T* y);
36 template <>
37 inline void mklaxpy<float>(unsigned int n, float a, float const* x, float* y)
38 {
39  cblas_saxpy(n, a, x, 1, y, 1);
40 }
46 template <>
47 inline void mklaxpy<double>(unsigned int n, double a, double const* x, double* y)
48 {
49  cblas_daxpy(n, a, x, 1, y, 1);
50 }
51 
60 template <typename T, typename MatrixType>
61 inline void mklAxPlusy(T a, MatrixType const& A, T const* x, T b, T* y);
68 template <>
69 inline void mklAxPlusy<float, MatrixCSR<float, int, 1> >(float a, MatrixCSR<float, int, 1> const& A, float const* x, float b, float* y)
70 {
71  mkl_scsrmv("N", &A.numRows, &A.numColumns, &a, "G", A.vElement, A.vColumn, A.vRowBeginIndex, A.vRowBeginIndex+1, x, &b, y);
72 }
79 template <>
80 inline void mklAxPlusy<double, MatrixCSR<double, int, 1> >(double a, MatrixCSR<double, int, 1> const& A, double const* x, double b, double* y)
81 {
82  mkl_dcsrmv("N", &A.numRows, &A.numColumns, &a, "G", A.vElement, A.vColumn, A.vRowBeginIndex, A.vRowBeginIndex+1, x, &b, y);
83 }
84 
93 template <typename T, typename MatrixType>
94 inline void mklATxPlusy(T a, MatrixType const& A, T const* x, T b, T* y);
100 template <>
101 inline void mklATxPlusy<float, MatrixCSR<float, int, 1> >(float a, MatrixCSR<float, int, 1> const& A, float const* x, float b, float* y)
102 {
103  mkl_scsrmv("T", &A.numRows, &A.numColumns, &a, "G", A.vElement, A.vColumn, A.vRowBeginIndex, A.vRowBeginIndex+1, x, &b, y);
104 }
110 template <>
111 inline void mklATxPlusy<double, MatrixCSR<double, int, 1> >(double a, MatrixCSR<double, int, 1> const& A, double const* x, double b, double* y)
112 {
113  mkl_dcsrmv("T", &A.numRows, &A.numColumns, &a, "G", A.vElement, A.vColumn, A.vRowBeginIndex, A.vRowBeginIndex+1, x, &b, y);
114 }
121 template <typename T>
122 inline T mkldot(unsigned int n, T const* x, T const* y);
128 template <>
129 inline float mkldot(unsigned int n, float const* x, float const* y)
130 {
131  return cblas_sdot(n, x, 1, y, 1);
132 }
138 template <>
139 inline double mkldot(unsigned int n, double const* x, double const* y)
140 {
141  return cblas_ddot(n, x, 1, y, 1);
142 }
143 
149 template <typename T>
150 inline void mklcopy(unsigned int n, T const* x, T* y);
155 template <>
156 inline void mklcopy(unsigned int n, float const* x, float* y)
157 {
158  cblas_scopy(n, x, 1, y, 1);
159 }
164 template <>
165 inline void mklcopy(unsigned int n, double const* x, double* y)
166 {
167  cblas_dcopy(n, x, 1, y, 1);
168 }
169 #endif
170 
178 template <typename T, typename V>
179 inline void axpy(unsigned int n, T a, V const* x, T* y)
180 {
181 #if MKL == 1
182  mklaxpy(n, a, x, y);
183 #else
184  for (unsigned int i = 0; i < n; ++i)
185  y[i] += a*x[i];
186 #endif
187 }
188 
197 template <typename T, typename V, typename MatrixType>
198 inline void AxPlusy(T a, MatrixType const& A, V const* x, T* y)
199 {
200  // y = a A x + y
201 #if MKL == 1
202  mklAxPlusy(a, A, x, (T)1, y);
203 #else
204  for (typename MatrixType::index_type i = 0; i < A.numRows; ++i)
205  {
206  for (typename MatrixType::index_type k = A.vRowBeginIndex[i]; k < A.vRowBeginIndex[i+1]; ++k)
207  {
208  typename MatrixType::index_type kk = k-MatrixType::s_startingIndex;
209 #ifdef DEBUG_NUMERICAL
210  limboAssert(A.vColumn[kk]-MatrixType::s_startingIndex < A.numColumns && kk < A.numElements);
211 #endif
212  y[i] += a*A.vElement[kk]*x[A.vColumn[kk]-MatrixType::s_startingIndex];
213  }
214  }
215 #endif
216 }
225 template <typename T, typename V, typename MatrixType>
226 inline void ATxPlusy(T a, MatrixType const& A, V const* x, T* y)
227 {
228  // y = a A^T x + y
229 #if MKL == 1
230  mklATxPlusy(a, A, x, (T)1, y);
231 #else
232  for (typename MatrixType::index_type i = 0; i < A.numRows; ++i)
233  {
234  for (typename MatrixType::index_type k = A.vRowBeginIndex[i]; k < A.vRowBeginIndex[i+1]; ++k)
235  {
236  typename MatrixType::index_type kk = k-MatrixType::s_startingIndex;
237 #ifdef DEBUG_NUMERICAL
238  limboAssert(A.vColumn[kk]-MatrixType::s_startingIndex < A.numColumns && kk < A.numElements);
239 #endif
240  y[A.vColumn[kk]-MatrixType::s_startingIndex] += a*A.vElement[kk]*x[i];
241  }
242  }
243 #endif
244 }
251 template <typename T>
252 inline T dot(unsigned int n, T const* x, T const* y)
253 {
254  T result = 0;
255 #if MKL == 1
256  result = mkldot(n, x, y);
257 #else
258  for (unsigned int i = 0; i < n; ++i)
259  result += x[i]*y[i];
260 #endif
261  return result;
262 }
268 template <typename T>
269 inline void vcopy(unsigned int n, T const* x, T* y)
270 {
271  // y = x
272 #if MKL == 1
273  mklcopy(n, x, y);
274 #else
275  std::copy(x, x+n, y);
276 #endif
277 }
278 
279 } // namespace solvers
280 } // namespace limbo
281 
282 #endif
Basic utilities such as variables and linear expressions in solvers.
void ATxPlusy(T a, MatrixType const &A, V const *x, T *y)
Definition: Numerical.h:226
void axpy(unsigned int n, T a, V const *x, T *y)
Definition: Numerical.h:179
namespace for Limbo
Definition: GraphUtility.h:22
void vcopy(unsigned int n, T const *x, T *y)
copy vector
Definition: Numerical.h:269
#define limboAssert(condition)
custom assertion without message
Definition: AssertMsg.h:64
void AxPlusy(T a, MatrixType const &A, V const *x, T *y)
Definition: Numerical.h:198
T dot(unsigned int n, T const *x, T const *y)
compute dot product
Definition: Numerical.h:252