File: simd.hh

package info (click to toggle)
dune-common 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,048 kB
  • sloc: cpp: 54,403; python: 4,136; sh: 1,657; makefile: 17
file content (503 lines) | stat: -rw-r--r-- 14,306 bytes parent folder | download | duplicates (2)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_SIMD_HH
#define DUNE_COMMON_SIMD_HH

#warning dune/common/simd.hh is deprecated.
#warning Use the new infrastructure from dune/common/simd/simd.h instead.

/**
   \file

   \brief Abstractions for support of dedicated SIMD data types

   Libraries like Vc (https://github.com/VcDevel/Vc) add high-level
   data types for SIMD (or vectorization) support in C++.  Most of
   these operations mimic the behavior of a numerical data type. Some
   boolean operations can not be implemented in a compatible way to
   trivial data types.

   This header contains additional abstractions to help writing code
   that works with trivial numerical data types (like double) and Vc
   vectorization data types.

   See also the conditional.hh and range_utils.hh headers.

   \deprecated Use the newer simd architecture from dune/common/simd/simd.hh
               instead.
 */

#include <cassert>
#include <cstddef>
#include <type_traits>
#include <utility>

#include <dune/common/conditional.hh>
#include <dune/common/debugalign.hh>
#include <dune/common/rangeutilities.hh>
#if HAVE_VC
// include Vc part of new simd interface to provide compatibility for
// functionality that has been switched over.
#include <dune/common/simd/vc.hh>
#endif
#include <dune/common/typetraits.hh>
#include <dune/common/vc.hh>

namespace Dune
{

#if HAVE_VC
  namespace VcImpl {
    //! A reference-like proxy for elements of random-access vectors.
    /**
     * This is necessary because Vc's lane-access operation return a proxy
     * that cannot constructed by non-Vc code (i.e. code that isn't
     * explicitly declared `friend`).  This means in particular that there
     * is no copy/move constructor, meaning we cannot return such proxies
     * from our own functions, such as `lane()`.  To work around this, we
     * define our own proxy class which internally holds a reference to the
     * vector and a lane index.
     */
    template<class V>
    class Proxy
    {
      static_assert(std::is_same<V, std::decay_t<V> >::value, "Class Proxy "
                    "may only be instantiated with unqualified types");
    public:
      using value_type = typename V::value_type;

    private:
      static_assert(std::is_arithmetic<value_type>::value,
                    "Only arithmetic types are supported");
      V &vec_;
      std::size_t idx_;

    public:
      Proxy(std::size_t idx, V &vec)
        : vec_(vec), idx_(idx)
      { }

      operator value_type() const { return vec_[idx_]; }

      // postfix operators

      template<class T = value_type,
               class = std::enable_if_t<!std::is_same<T, bool>::value> >
      value_type operator++(int) { return vec_[idx_]++; }
      template<class T = value_type,
               class = std::enable_if_t<!std::is_same<T, bool>::value> >
      value_type operator--(int) { return vec_[idx_]--; }

      // unary (prefix) operators
      template<class T = value_type,
               class = std::enable_if_t<!std::is_same<T, bool>::value> >
      Proxy &operator++() { ++(vec_[idx_]); return *this; }
      template<class T = value_type,
               class = std::enable_if_t<!std::is_same<T, bool>::value> >
      Proxy &operator--() { --(vec_[idx_]); return *this; }
      decltype(auto) operator!() const { return !(vec_[idx_]); }
      decltype(auto) operator+() const { return +(vec_[idx_]); }
      decltype(auto) operator-() const { return -(vec_[idx_]); }
      template<class T = value_type,
               class = std::enable_if_t<std::is_integral<T>::value> >
      decltype(auto) operator~() const { return ~(vec_[idx_]); }

      // binary operators
#define DUNE_SIMD_VC_BINARY_OP(OP)                                      \
      template<class T>                                                 \
      auto operator OP(T &&o) const                                     \
        -> decltype(vec_[idx_] OP valueCast(std::forward<T>(o)))        \
      {                                                                 \
        return vec_[idx_] OP valueCast(std::forward<T>(o));             \
      }                                                                 \
      static_assert(true, "Require semicolon to unconfuse editors")

      DUNE_SIMD_VC_BINARY_OP(*);
      DUNE_SIMD_VC_BINARY_OP(/);
      DUNE_SIMD_VC_BINARY_OP(%);

      DUNE_SIMD_VC_BINARY_OP(+);
      DUNE_SIMD_VC_BINARY_OP(-);

      DUNE_SIMD_VC_BINARY_OP(<<);
      DUNE_SIMD_VC_BINARY_OP(>>);

      DUNE_SIMD_VC_BINARY_OP(<);
      DUNE_SIMD_VC_BINARY_OP(>);
      DUNE_SIMD_VC_BINARY_OP(<=);
      DUNE_SIMD_VC_BINARY_OP(>=);

      DUNE_SIMD_VC_BINARY_OP(==);
      DUNE_SIMD_VC_BINARY_OP(!=);

      DUNE_SIMD_VC_BINARY_OP(&);
      DUNE_SIMD_VC_BINARY_OP(^);
      DUNE_SIMD_VC_BINARY_OP(|);

      DUNE_SIMD_VC_BINARY_OP(&&);
      DUNE_SIMD_VC_BINARY_OP(||);
#undef DUNE_SIMD_VC_BINARY_OP

#define DUNE_SIMD_VC_ASSIGNMENT(OP)                                   \
      template<class T>                                               \
      auto operator OP(T &&o)                                         \
        -> std::enable_if_t<AlwaysTrue<decltype(                      \
                 vec_[idx_] OP valueCast(std::forward<T>(o))          \
               )>::value, Proxy&>                                     \
      {                                                               \
        vec_[idx_] OP valueCast(std::forward<T>(o));                  \
        return *this;                                                 \
      }                                                               \
      static_assert(true, "Require semicolon to unconfuse editors")

      DUNE_SIMD_VC_ASSIGNMENT(=);
      DUNE_SIMD_VC_ASSIGNMENT(*=);
      DUNE_SIMD_VC_ASSIGNMENT(/=);
      DUNE_SIMD_VC_ASSIGNMENT(%=);
      DUNE_SIMD_VC_ASSIGNMENT(+=);
      DUNE_SIMD_VC_ASSIGNMENT(-=);
      DUNE_SIMD_VC_ASSIGNMENT(<<=);
      DUNE_SIMD_VC_ASSIGNMENT(>>=);
      DUNE_SIMD_VC_ASSIGNMENT(&=);
      DUNE_SIMD_VC_ASSIGNMENT(^=);
      DUNE_SIMD_VC_ASSIGNMENT(|=);
#undef DUNE_SIMD_VC_ASSIGNMENT

      // swap on proxies swaps the proxied vector entries.  As such, it
      // applies to rvalues of proxies too, not just lvalues
      template<class V1, class V2>
      friend void swap(Proxy<V1>, Proxy<V2>);

      template<class T>
      friend void swap(Proxy p1, T& s2)
      {
        // don't use swap() ourselves -- not supported by Vc 1.3.0 (but is
        // supported by Vc 1.3.2)
        T tmp = p1.vec_[p1.idx_];
        p1.vec_[p1.idx_] = s2;
        s2 = tmp;
      }

      template<class T>
      friend void swap(T& s1, Proxy p2)
      {
        T tmp = s1;
        s1 = p2.vec_[p2.idx_];
        p2.vec_[p2.idx_] = tmp;
      }
    };

    template<class V1, class V2>
    void swap(Proxy<V1> p1, Proxy<V2> p2)
    {
      typename V1::value_type tmp = p1.vec_[p1.idx_];
      p1.vec_[p1.idx_] = p2.vec_[p2.idx_];
      p2.vec_[p2.idx_] = tmp;
    }
  } //  namespace VcImpl
#endif // HAVE_VC

  template<typename T>
  struct SimdScalarTypeTraits
  {
    using type = T;
  };

  template<typename T>
  using SimdScalar = typename SimdScalarTypeTraits<T>::type;

#if HAVE_VC
  /*
    Add Vc specializations for the SimdScalarTypeTraits trais class
   */
  template<typename T, typename A>
  struct SimdScalarTypeTraits< Vc::Vector<T,A> >
  {
    using type = T;
  };

  template<typename T, std::size_t N, typename V, std::size_t M>
  struct SimdScalarTypeTraits< Vc::SimdArray<T,N,V,M> >
  {
    using type = T;
  };
#endif // HAVE_VC

  //! deduce the underlying scalar data type of an AlignedNumber
  template<typename T, std::size_t align>
  struct SimdScalarTypeTraits< AlignedNumber<T,align> >
  {
    using type = T;
  };

  template<typename V, typename = void>
  struct SimdIndexTypeTraits {
    using type = std::size_t;
  };

  //! An simd vector of indices corresponding to a simd vector V
  /**
   * lanes(T()) == lanes(SimdIndex<T>()) holds.
   *
   * \note The size of the elements of a SimdIndex isn't very well-defined.
   *       Be careful.
   */
  template<typename V>
  using SimdIndex = typename SimdIndexTypeTraits<V>::type;

#if HAVE_VC
  template<typename T, typename A>
  struct SimdIndexTypeTraits<Vc::Vector<T, A> > {
    using type = typename Vc::Vector<T, A>::index_type;
  };

  template<typename T, std::size_t n, typename V>
  struct SimdIndexTypeTraits<Vc::SimdArray<T, n, V> > {
    using type = typename Vc::SimdArray<T, n, V>::index_type;
  };
#endif // HAVE_VC

  template<typename V, typename = void>
  struct SimdMaskTypeTraits {
    using type = bool;
  };

  //! A simd vector of truth values corresponding to a simd vector V
  /**
   * lanes(T()) == lanes(SimdMask<T>()) holds.
   */
  template<typename V>
  using SimdMask = typename SimdMaskTypeTraits<V>::type;

#if HAVE_VC
  template<typename T, typename A>
  struct SimdMaskTypeTraits<Vc::Vector<T, A> > {
    using type = typename Vc::Vector<T, A>::mask_type;
  };

  template<typename T, std::size_t n, typename V>
  struct SimdMaskTypeTraits<Vc::SimdArray<T, n, V> > {
    using type = typename Vc::SimdArray<T, n, V>::mask_type;
  };
#endif // HAVE_VC

#if HAVE_VC
  /*
    Add Vc specializations for cond(), see conditional.hh
   */
  template<typename T, typename A>
  Vc::Vector<T,A> cond(const Vc::Mask<T,A> & b,
    const Vc::Vector<T,A> & v1,
    const Vc::Vector<T,A> & v2)
  {
    return std::move(Vc::iif(b, v1, v2));
  }

  template<typename T, std::size_t N, typename V, std::size_t M>
  Vc::SimdArray<T,N,V,M> cond(const typename Vc::SimdArray<T,N,V,M>::mask_type & b,
    const Vc::SimdArray<T,N,V,M> & v1,
    const Vc::SimdArray<T,N,V,M> & v2)
  {
    return std::move(Vc::iif(b, v1, v2));
  }
#endif // HAVE_VC

#if HAVE_VC
  /*
    Add Vc specializations for several boolean operations, see rangeutitlities.hh:

    max_value, min_value, any_true, all_true
   */
  template<typename T, typename A>
  T max_value(const Vc::Vector<T,A> & v)
  {
    return v.max();
  }

  template<typename T, std::size_t N, typename V, std::size_t M>
  double max_value(const Vc::SimdArray<T,N,V,M> & v)
  {
    return v.max();
  }

  template<typename T, typename A>
  T min_value(const Vc::Vector<T,A> & v)
  {
    return v.min();
  }

  template<typename T, std::size_t N, typename V, std::size_t M>
  double min_value(const Vc::SimdArray<T,N,V,M> & v)
  {
    return v.min();
  }

  template<typename T, typename A>
  bool any_true(const Vc::Mask<T,A> & v)
  {
    return Vc::any_of(v);
  }

  template<typename T, std::size_t N, typename V, std::size_t M>
  bool any_true(const Vc::SimdMaskArray<T,N,V,M> & v)
  {
    return Vc::any_of(v);
  }

  template<typename T, typename A>
  bool all_true(const Vc::Mask<T,A> & v)
  {
    return Vc::all_of(v);
  }

  template<typename T, std::size_t N, typename V, std::size_t M>
  bool all_true(const Vc::SimdMaskArray<T,N,V,M> & v)
  {
    return Vc::all_of(v);
  }
#endif // HAVE_VC

  //! get the number of lanes of a simd vector (scalar version)
  template<class T>
  std::size_t lanes(const T &) { return 1; }

  //! access a lane of a simd vector (scalar version)
  template<class T>
  T lane(std::size_t l, const T &v)
  {
    assert(l == 0);
    return v;
  }

  //! access a lane of a simd vector (scalar version)
  template<class T>
  T &lane(std::size_t l, T &v)
  {
    assert(l == 0);
    return v;
  }

#if HAVE_VC
  template<class T, class A>
  std::size_t lanes(const Vc::Vector<T, A> &)
  {
    return Vc::Vector<T, A>::size();
  }

  template<class T, class A>
  T lane(std::size_t l, const Vc::Vector<T, A> &v)
  {
    assert(l < lanes(v));
    return v[l];
  }

  template<class T, class A>
  auto lane(std::size_t l, Vc::Vector<T, A> &v)
  {
    assert(l < lanes(v));
    return VcImpl::Proxy<Vc::Vector<T, A> >{l, v};
  }

  template<class T, std::size_t n, class V>
  std::size_t lanes(const Vc::SimdArray<T, n, V> &)
  {
    return n;
  }

  template<class T, std::size_t n, class V>
  T lane(std::size_t l, const Vc::SimdArray<T, n, V> &v)
  {
    assert(l < n);
    return v[l];
  }

  template<class T, std::size_t n, class V>
  auto lane(std::size_t l, Vc::SimdArray<T, n, V> &v)
  {
    assert(l < n);
    return VcImpl::Proxy<Vc::SimdArray<T, n, V> >{l, v};
  }

  template<class T, std::size_t n, class V>
  std::size_t lanes(const Vc::SimdMaskArray<T, n, V> &)
  {
    return n;
  }

  template<class T, std::size_t n, class V>
  bool lane(std::size_t l, const Vc::SimdMaskArray<T, n, V> &v)
  {
    assert(l < n);
    return v[l];
  }

  template<class T, std::size_t n, class V>
  auto lane(std::size_t l, Vc::SimdMaskArray<T, n, V> &v)
  {
    assert(l < n);
    return VcImpl::Proxy<Vc::SimdMaskArray<T, n, V> >{l, v};
  }
#endif // HAVE_VC

  //! masked Simd assignment (scalar version)
  /**
   * Assign \c src to \c dest for those lanes where \c mask is true.
   */
  template<class T>
  void assign(T &dst, const T &src, bool mask)
  {
    if(mask) dst = src;
  }

#if HAVE_VC
  /*
    Add Vc specializations for masked assignment
  */
  template<class T, class A>
  void assign(Vc::Vector<T, A> &dst, const Vc::Vector<T, A> &src,
              typename Vc::Vector<T, A>::mask_type mask)
  {
    dst(mask) = src;
  }

  template<class T, std::size_t n, class V>
  void assign(Vc::SimdArray<T, n, V> &dst, const Vc::SimdArray<T, n, V> &src,
              typename Vc::SimdArray<T, n, V>::mask_type mask)
  {
    dst(mask) = src;
  }
#endif // HAVE_VC

  template<class T>
  void swap(T &v1, T &v2, bool mask)
  {
    using std::swap;
    if(mask) swap(v1, v2);
  }

#if HAVE_VC
  /*
    Add Vc specializations for masked swap
  */
  template<class T, class A>
  void swap(Vc::Vector<T, A> &v1, Vc::Vector<T, A> &v2,
            typename Vc::Vector<T, A>::mask_type mask)
  {
    auto tmp = v1;
    v1(mask) = v2;
    v2(mask) = tmp;
  }

  template<class T, std::size_t n, class V>
  void swap(Vc::SimdArray<T, n, V> &v1, Vc::SimdArray<T, n, V> &v2,
            typename Vc::SimdArray<T, n, V>::mask_type mask)
  {
    auto tmp = v1;
    v1(mask) = v2;
    v2(mask) = tmp;
  }
#endif // HAVE_VC

} // end namespace Dune

#endif // DUNE_COMMON_SIMD_HH