Limbo 3.5.4
Loading...
Searching...
No Matches
Numerical.h
Go to the documentation of this file.
1
7#ifndef LIMBO_SOLVERS_NUMERICAL_H
8#define LIMBO_SOLVERS_NUMERICAL_H
9
11#if MKL == 1
12#include <mkl.h>
13#endif
14
16namespace limbo
17{
19namespace solvers
20{
21
22#if MKL == 1
29template <typename T>
30inline void mklaxpy(unsigned int n, T a, T const* x, T* y);
36template <>
37inline void mklaxpy<float>(unsigned int n, float a, float const* x, float* y)
38{
39 cblas_saxpy(n, a, x, 1, y, 1);
40}
46template <>
47inline 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
60template <typename T, typename MatrixType>
61inline void mklAxPlusy(T a, MatrixType const& A, T const* x, T b, T* y);
68template <>
69inline 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}
79template <>
80inline 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
93template <typename T, typename MatrixType>
94inline void mklATxPlusy(T a, MatrixType const& A, T const* x, T b, T* y);
100template <>
101inline 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}
110template <>
111inline 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}
121template <typename T>
122inline T mkldot(unsigned int n, T const* x, T const* y);
128template <>
129inline float mkldot(unsigned int n, float const* x, float const* y)
130{
131 return cblas_sdot(n, x, 1, y, 1);
132}
138template <>
139inline double mkldot(unsigned int n, double const* x, double const* y)
140{
141 return cblas_ddot(n, x, 1, y, 1);
142}
143
149template <typename T>
150inline void mklcopy(unsigned int n, T const* x, T* y);
155template <>
156inline void mklcopy(unsigned int n, float const* x, float* y)
157{
158 cblas_scopy(n, x, 1, y, 1);
159}
164template <>
165inline void mklcopy(unsigned int n, double const* x, double* y)
166{
167 cblas_dcopy(n, x, 1, y, 1);
168}
169#endif
170
178template <typename T, typename V>
179inline 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
197template <typename T, typename V, typename MatrixType>
198inline 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}
217
225template <typename T, typename V, typename MatrixType>
226inline 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}
245
251template <typename T>
252inline 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}
263
268template <typename T>
269inline 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
#define limboAssert(condition)
custom assertion without message
Definition AssertMsg.h:36
Basic utilities such as variables and linear expressions in solvers.
namespace for Limbo.Solvers
void vcopy(unsigned int n, T const *x, T *y)
copy vector
Definition Numerical.h:269
void axpy(unsigned int n, T a, V const *x, T *y)
Definition Numerical.h:179
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
void ATxPlusy(T a, MatrixType const &A, V const *x, T *y)
Definition Numerical.h:226
namespace for Limbo
Compressed sparse row (CSR) matrix.
Definition Solvers.h:1716