File: dot_impl.h

package info (click to toggle)
bagel 1.2.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 134,940 kB
  • sloc: cpp: 1,236,571; javascript: 15,383; python: 1,461; ansic: 674; makefile: 253; sh: 109
file content (345 lines) | stat: -rw-r--r-- 11,152 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef __BTAS_DOT_IMPL_H
#define __BTAS_DOT_IMPL_H 1

#include <algorithm>
#include <iterator>
#include <type_traits>

#include <btas/tensor_traits.h>
#include <btas/types.h>

#include <btas/generic/numeric_type.h>
#include <btas/generic/tensor_iterator_wrapper.h>

namespace btas {

template<typename _T>
struct __dot_result_type {
   typedef typename __dot_result_type<typename _T::value_type>::type type;
};

template<> struct __dot_result_type<float> { typedef float type; };

template<> struct __dot_result_type<double> { typedef double type; };

template<> struct __dot_result_type<std::complex<float>> { typedef std::complex<float> type; };

template<> struct __dot_result_type<std::complex<double>> { typedef std::complex<double> type; };

//  ================================================================================================

/// Dot with conjugation for general case
template<typename _T>
struct dotc_impl
{
   typedef typename __dot_result_type<_T>::type return_type;

   template<class _IteratorX, class _IteratorY>
   static return_type call (
      const unsigned long& Nsize,
            _IteratorX itrX, const typename std::iterator_traits<_IteratorX>::difference_type& incX,
            _IteratorY itrY, const typename std::iterator_traits<_IteratorY>::difference_type& incY)
   {
      return_type val = dotc(*itrX, *itrY);
      itrX += incX;
      itrY += incY;
      for (unsigned long i = 1; i < Nsize; ++i, itrX += incX, itrY += incY)
      {
         val += dotc(*itrX, *itrY);
      }
      return val;
   }
};

/// Dot without conjugation for general case
template<typename _T>
struct dotu_impl
{
   typedef typename __dot_result_type<_T>::type return_type;

   template<class _IteratorX, class _IteratorY>
   static return_type call (
      const unsigned long& Nsize,
            _IteratorX itrX, const typename std::iterator_traits<_IteratorX>::difference_type& incX,
            _IteratorY itrY, const typename std::iterator_traits<_IteratorY>::difference_type& incY)
   {
      // redirect to dotc, otherwise must be specialized
      return dotc_impl<_T>::call(Nsize, itrX, incX, itrY, incY);
   }
};

template<>
struct dotc_impl<float>
{
   typedef float return_type;

   static return_type call (
      const unsigned long& Nsize,
      const float* itrX, const typename std::iterator_traits<float*>::difference_type& incX,
      const float* itrY, const typename std::iterator_traits<float*>::difference_type& incY)
   {
#ifdef _HAS_CBLAS
      return cblas_sdot(Nsize, itrX, incX, itrY, incY);
#else
      return_type val = (*itrX) * (*itrY);
      itrX += incX;
      itrY += incY;
      for (unsigned long i = 1; i < Nsize; ++i, itrX += incX, itrY += incY)
      {
         val += (*itrX) * (*itrY);
      }
      return val;
#endif
   }
};

template<>
struct dotc_impl<double>
{
   typedef double return_type;

   static return_type call (
      const unsigned long& Nsize,
      const double* itrX, const typename std::iterator_traits<double*>::difference_type& incX,
      const double* itrY, const typename std::iterator_traits<double*>::difference_type& incY)
   {
#ifdef _HAS_CBLAS
      return cblas_ddot(Nsize, itrX, incX, itrY, incY);
#else
      return_type val = (*itrX) * (*itrY);
      itrX += incX;
      itrY += incY;
      for (unsigned long i = 1; i < Nsize; ++i, itrX += incX, itrY += incY)
      {
         val += (*itrX) * (*itrY);
      }
      return val;
#endif
   }
};

template<>
struct dotc_impl<std::complex<float>>
{
   typedef std::complex<float> return_type;

   static return_type call (
      const unsigned long& Nsize,
      const std::complex<float>* itrX, const typename std::iterator_traits<std::complex<float>*>::difference_type& incX,
      const std::complex<float>* itrY, const typename std::iterator_traits<std::complex<float>*>::difference_type& incY)
   {
      return_type val;
#ifdef _HAS_CBLAS
      cblas_cdotc_sub(Nsize, itrX, incX, itrY, incY, &val);
#else
      val = std::conj(*itrX) * (*itrY);
      itrX += incX;
      itrY += incY;
      for (unsigned long i = 1; i < Nsize; ++i, itrX += incX, itrY += incY)
      {
         val += std::conj(*itrX) * (*itrY);
      }
#endif
      return val;
   }
};

template<>
struct dotu_impl<std::complex<float>>
{
   typedef std::complex<float> return_type;

   static return_type call (
      const unsigned long& Nsize,
      const std::complex<float>* itrX, const typename std::iterator_traits<std::complex<float>*>::difference_type& incX,
      const std::complex<float>* itrY, const typename std::iterator_traits<std::complex<float>*>::difference_type& incY)
   {
      return_type val;
#ifdef _HAS_CBLAS
      cblas_cdotu_sub(Nsize, itrX, incX, itrY, incY, &val);
#else
      val = (*itrX) * (*itrY);
      itrX += incX;
      itrY += incY;
      for (unsigned long i = 1; i < Nsize; ++i, itrX += incX, itrY += incY)
      {
         val += *itrX * (*itrY);
      }
#endif
      return val;
   }
};

template<>
struct dotc_impl<std::complex<double>>
{
   typedef std::complex<double> return_type;

   static return_type call (
      const unsigned long& Nsize,
      const std::complex<double>* itrX, const typename std::iterator_traits<std::complex<double>*>::difference_type& incX,
      const std::complex<double>* itrY, const typename std::iterator_traits<std::complex<double>*>::difference_type& incY)
   {
      return_type val;
#ifdef _HAS_CBLAS
      cblas_zdotc_sub(Nsize, itrX, incX, itrY, incY, &val);
#else
      val = std::conj(*itrX) * (*itrY);
      itrX += incX;
      itrY += incY;
      for (unsigned long i = 1; i < Nsize; ++i, itrX += incX, itrY += incY)
      {
         val += std::conj(*itrX) * (*itrY);
      }
#endif
      return val;
   }
};

template<>
struct dotu_impl<std::complex<double>>
{
   typedef std::complex<double> return_type;

   static return_type call (
      const unsigned long& Nsize,
      const std::complex<double>* itrX, const typename std::iterator_traits<std::complex<double>*>::difference_type& incX,
      const std::complex<double>* itrY, const typename std::iterator_traits<std::complex<double>*>::difference_type& incY)
   {
      return_type val;
#ifdef _HAS_CBLAS
      cblas_zdotu_sub(Nsize, itrX, incX, itrY, incY, &val);
#else
      val = (*itrX) * (*itrY);
      itrX += incX;
      itrY += incY;
      for (unsigned long i = 1; i < Nsize; ++i, itrX += incX, itrY += incY)
      {
         val += *itrX * (*itrY);
      }
#endif
      return val;
   }
};

//  ================================================================================================

/// Generic implementation of BLAS DOT in terms of C++ iterator
template<class _IteratorX, class _IteratorY>
typename __dot_result_type<typename std::iterator_traits<_IteratorX>::value_type>::type
dotc (
   const unsigned long& Nsize,
         _IteratorX itrX, const typename std::iterator_traits<_IteratorX>::difference_type& incX,
         _IteratorY itrY, const typename std::iterator_traits<_IteratorY>::difference_type& incY)
{
   typedef std::iterator_traits<_IteratorX> __traits_X;
   typedef std::iterator_traits<_IteratorY> __traits_Y;

   static_assert(std::is_same<typename __traits_X::value_type, typename __traits_Y::value_type>::value, "value type of Y must be the same as that of X");
   static_assert(std::is_same<typename __traits_X::iterator_category, std::random_access_iterator_tag>::value, "iterator X must be a random access iterator");
   static_assert(std::is_same<typename __traits_Y::iterator_category, std::random_access_iterator_tag>::value, "iterator Y must be a random access iterator");

   return dotc_impl<typename __traits_X::value_type>::call(Nsize, itrX, incX, itrY, incY);
}

/// Generic implementation of BLAS DOT in terms of C++ iterator
template<class _IteratorX, class _IteratorY>
typename __dot_result_type<typename std::iterator_traits<_IteratorX>::value_type>::type
dotu (
   const unsigned long& Nsize,
         _IteratorX itrX, const typename std::iterator_traits<_IteratorX>::difference_type& incX,
         _IteratorY itrY, const typename std::iterator_traits<_IteratorY>::difference_type& incY)
{
   typedef std::iterator_traits<_IteratorX> __traits_X;
   typedef std::iterator_traits<_IteratorY> __traits_Y;

   static_assert(std::is_same<typename __traits_X::value_type, typename __traits_Y::value_type>::value, "value type of Y must be the same as that of X");
   static_assert(std::is_same<typename __traits_X::iterator_category, std::random_access_iterator_tag>::value, "iterator X must be a random access iterator");
   static_assert(std::is_same<typename __traits_Y::iterator_category, std::random_access_iterator_tag>::value, "iterator Y must be a random access iterator");

   return dotu_impl<typename __traits_X::value_type>::call(Nsize, itrX, incX, itrY, incY);
}

/// Generic implementation of BLAS DOT in terms of C++ iterator
template<class _IteratorX, class _IteratorY>
typename __dot_result_type<typename std::iterator_traits<_IteratorX>::value_type>::type
dot (
   const unsigned long& Nsize,
         _IteratorX itrX, const typename std::iterator_traits<_IteratorX>::difference_type& incX,
         _IteratorY itrY, const typename std::iterator_traits<_IteratorY>::difference_type& incY)
{
   return dotc(Nsize, itrX, incX, itrY, incY);
}

//  ================================================================================================

/// Convenient wrapper to call BLAS DOT-C from tensor objects
template<
   class _TensorX,
   class _TensorY,
   class = typename std::enable_if<
      is_tensor<_TensorX>::value &
      is_tensor<_TensorY>::value
   >::type
>
typename __dot_result_type<typename _TensorX::value_type>::type
dotc (const _TensorX& X, const _TensorY& Y)
{
   typedef typename _TensorX::value_type value_type;
   static_assert(std::is_same<value_type, typename _TensorY::value_type>::value, "value type of Y must be the same as that of X");

   if (X.empty() || Y.empty())
   {
      return 0;
   }

   auto itrX = tbegin(X);
   auto itrY = tbegin(Y);

   return dotc(X.size(), itrX, 1, itrY, 1);
}

/// Convenient wrapper to call BLAS DOT-U from tensor objects
template<
   class _TensorX,
   class _TensorY,
   class = typename std::enable_if<
      is_tensor<_TensorX>::value &
      is_tensor<_TensorY>::value
   >::type
>
typename __dot_result_type<typename _TensorX::value_type>::type
dotu (const _TensorX& X, const _TensorY& Y)
{
   typedef typename _TensorX::value_type value_type;
   static_assert(std::is_same<value_type, typename _TensorY::value_type>::value, "value type of Y must be the same as that of X");

   if (X.empty() || Y.empty())
   {
      return 0;
   }

   auto itrX = tbegin(X);
   auto itrY = tbegin(Y);

   return dotu(X.size(), itrX, 1, itrY, 1);
}

/// Convenient wrapper to call BLAS DOT from tensor objects
template<
   class _TensorX,
   class _TensorY,
   class = typename std::enable_if<
      is_tensor<_TensorX>::value &
      is_tensor<_TensorY>::value
   >::type
>
typename __dot_result_type<typename _TensorX::value_type>::type
dot (const _TensorX& X, const _TensorY& Y)
{
   return dotc(X, Y);
}

} // namespace btas

#endif // __BTAS_DOT_IMPL_H