8#include <compile_time_options.h>
11#include <deal.II/base/tensor.h>
12#include <deal.II/base/utilities.h>
13#include <deal.II/base/vectorization.h>
34#define AssertThrowSIMD(variable, condition, exception) \
35 if constexpr (std::is_same< \
36 typename std::remove_const<decltype(variable)>::type, \
39 typename std::remove_const<decltype(variable)>::type, \
41 AssertThrow(condition(variable), exception); \
43 for (unsigned int k = 0; k < decltype(variable)::size(); ++k) { \
44 AssertThrow(condition((variable)[k]), exception); \
71 template <
typename T, std::
size_t w
idth>
87 template <
typename T, std::
size_t w
idth>
88 constexpr unsigned int get_stride_size<dealii::VectorizedArray<T, width>> =
96 template <
typename Functor,
size_t... Is>
97 auto generate_iterators_impl(Functor f, std::index_sequence<Is...>)
98 -> std::array<
decltype(f(0)),
sizeof...(Is)>
117 template <
unsigned int length,
typename Functor>
119 -> std::array<
decltype(f(0)), length>
121 return generate_iterators_impl<>(f, std::make_index_sequence<length>());
130 template <
typename T>
133 for (
auto &it : iterators)
148 template <
typename Number>
151 return std::max(Number(0.), number);
160 template <
typename Number>
163 return -std::min(Number(0.), number);
174 template <dealii::SIMDComparison predicate,
typename Number>
175 DEAL_II_HOST_DEVICE_ALWAYS_INLINE Number
178 const Number &true_value,
179 const Number &false_value)
181 static_assert(std::is_floating_point_v<Number>,
182 "Only scalar number types are allowed");
185 if constexpr (predicate == dealii::SIMDComparison::equal)
186 mask = (left == right);
187 else if constexpr (predicate == dealii::SIMDComparison::not_equal)
188 mask = (left != right);
189 else if constexpr (predicate == dealii::SIMDComparison::less_than)
190 mask = (left < right);
191 else if constexpr (predicate == dealii::SIMDComparison::less_than_or_equal)
192 mask = (left <= right);
193 else if constexpr (predicate == dealii::SIMDComparison::greater_than)
194 mask = (left > right);
196 mask = (left >= right);
198 return mask ? true_value : false_value;
208 template <dealii::SIMDComparison predicate,
typename T, std::
size_t w
idth>
209 DEAL_II_ALWAYS_INLINE
inline dealii::VectorizedArray<T, width>
211 const dealii::VectorizedArray<T, width> &right,
212 const dealii::VectorizedArray<T, width> &true_value,
213 const dealii::VectorizedArray<T, width> &false_value)
215 return dealii::compare_and_apply_mask<predicate>(
216 left, right, true_value, false_value);
227 template <
int N,
typename T>
230 return dealii::Utilities::fixed_power<N, T>(x);
239 template <
typename T>
240 DEAL_II_HOST_DEVICE T
pow(
const T x,
const T b);
248 template <
typename T, std::
size_t w
idth>
249 dealii::VectorizedArray<T, width>
250 pow(
const dealii::VectorizedArray<T, width> x,
const T b);
259 template <
typename T, std::
size_t w
idth>
260 dealii::VectorizedArray<T, width>
261 pow(
const dealii::VectorizedArray<T, width> x,
262 const dealii::VectorizedArray<T, width> b);
266 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
float pow(
const float x,
const float b)
268#ifdef RYUJIN_DEVICE_COMPILATION_PASS
270 return std::pow(x, b);
271#elif DEAL_II_COMPILER_VECTORIZATION_LEVEL >= 1 && defined(__SSE2__)
273 return pow(dealii::VectorizedArray<float, 4>(x), b)[0];
276 return std::pow(x, b);
282 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
double pow(
const double x,
const double b)
284#ifdef RYUJIN_DEVICE_COMPILATION_PASS
286 return std::pow(x, b);
287#elif DEAL_II_COMPILER_VECTORIZATION_LEVEL >= 1 && defined(__SSE2__)
289 return pow(dealii::VectorizedArray<double, 2>(x), b)[0];
292 return std::pow(x, b);
323 template <
typename T>
334 template <
typename T, std::
size_t w
idth>
335 dealii::VectorizedArray<T, width>
336 fast_pow(
const dealii::VectorizedArray<T, width> x,
347 template <
typename T, std::
size_t w
idth>
348 dealii::VectorizedArray<T, width>
349 fast_pow(
const dealii::VectorizedArray<T, width> x,
350 const dealii::VectorizedArray<T, width> b,
355 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
float
356 fast_pow(
const float x,
const float b, [[maybe_unused]]
const Bias bias)
358#ifdef RYUJIN_DEVICE_COMPILATION_PASS
360 return std::pow(x, b);
361#elif DEAL_II_COMPILER_VECTORIZATION_LEVEL >= 1 && defined(__SSE2__)
363 return fast_pow(dealii::VectorizedArray<float, 4>(x), b, bias)[0];
366 return std::pow(x, b);
372 DEAL_II_HOST_DEVICE_ALWAYS_INLINE
double
373 fast_pow(
const double x,
const double b, [[maybe_unused]]
const Bias bias)
375#ifdef RYUJIN_DEVICE_COMPILATION_PASS
377 return std::pow(
static_cast<float>(x),
static_cast<float>(b));
378#elif DEAL_II_COMPILER_VECTORIZATION_LEVEL >= 1 && defined(__SSE2__)
380 return fast_pow(dealii::VectorizedArray<double, 2>(x), b, bias)[0];
383 return std::pow(
static_cast<float>(x),
static_cast<float>(b));
399 template <
typename T,
typename V>
400 DEAL_II_ALWAYS_INLINE
inline T
read_entry(
const V &vector,
unsigned int i)
402 static_assert(std::is_same_v<typename get_value_type<T>::type,
403 typename V::value_type>,
407 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
409 result = vector.local_element(i);
412 result.load(vector.get_values() + i);
423 template <
typename T,
typename T2>
424 DEAL_II_ALWAYS_INLINE
inline T
read_entry(
const std::vector<T2> &vector,
427 if constexpr (std::is_same_v<typename get_value_type<T>::type, T2>) {
431 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
436 result.load(vector.data() + i);
443 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
447 for (
unsigned int k = 0; k < T::size(); ++k)
448 result[k] = vector[i + k];
461 template <
typename T,
typename V>
463 const unsigned int *js)
465 static_assert(std::is_same_v<typename get_value_type<T>::type,
466 typename V::value_type>,
470 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
472 result = vector.local_element(js[0]);
475 result.gather(vector.get_values(), js);
486 template <
typename T,
typename T2>
487 DEAL_II_ALWAYS_INLINE
inline T
read_entry(
const std::vector<T2> &vector,
488 const unsigned int *js)
490 static_assert(std::is_same_v<typename get_value_type<T>::type, T2>,
494 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
496 result = vector[js[0]];
499 result.load(vector.data(), js);
511 template <
typename T,
typename V>
512 DEAL_II_ALWAYS_INLINE
inline void
515 static_assert(std::is_same_v<typename get_value_type<T>::type,
516 typename V::value_type>,
519 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
521 vector.local_element(i) = values;
524 values.store(vector.get_values() + i);
533 template <
typename T,
typename T2>
534 DEAL_II_ALWAYS_INLINE
inline void
535 write_entry(std::vector<T2> &vector,
const T &values,
unsigned int i)
537 if constexpr (std::is_same_v<typename get_value_type<T>::type, T2>) {
540 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
545 values.store(vector.data() + i);
550 if constexpr (std::is_same_v<T, typename get_value_type<T>::type>) {
554 for (
unsigned int k = 0; k < T::size(); ++k)
555 vector[i + k] = values[k];
566 template <
int rank,
int dim, std::
size_t w
idth,
typename Number>
567 DEAL_II_ALWAYS_INLINE
inline dealii::Tensor<rank, dim, Number>
569 const dealii::Tensor<rank, dim, dealii::VectorizedArray<Number, width>>
571 const unsigned int k)
573 Assert(k < width, dealii::ExcMessage(
"Index past VectorizedArray width"));
574 dealii::Tensor<rank, dim, Number> result;
575 if constexpr (rank == 1) {
576 for (
unsigned int d = 0; d < dim; ++d)
577 result[d] = vectorized[d][k];
579 for (
unsigned int d = 0; d < dim; ++d)
592 template <
int rank,
int dim,
typename Number>
593 DEAL_II_ALWAYS_INLINE
inline dealii::Tensor<rank, dim, Number>
595 const unsigned int k [[maybe_unused]])
599 "The given index k must be zero for a serial tensor"));
609 template <
int rank,
int dim, std::
size_t w
idth,
typename Number>
611 dealii::Tensor<rank, dim, dealii::VectorizedArray<Number, width>> &result,
612 const dealii::Tensor<rank, dim, Number> &
serial,
613 const unsigned int k)
615 Assert(k < width, dealii::ExcMessage(
"Index past VectorizedArray width"));
616 if constexpr (rank == 1) {
617 for (
unsigned int d = 0; d < dim; ++d)
620 for (
unsigned int d = 0; d < dim; ++d)
632 template <
int rank,
int dim,
typename Number>
633 DEAL_II_ALWAYS_INLINE
inline void
635 const dealii::Tensor<rank, dim, Number> &
serial,
636 const unsigned int k [[maybe_unused]])
640 "The given index k must be zero for a serial tensor"));
DEAL_II_HOST_DEVICE T pow(const T x, const T b)
DEAL_II_ALWAYS_INLINE T read_entry(const V &vector, unsigned int i)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE Number positive_part(const Number number)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE Number negative_part(const Number number)
DEAL_II_ALWAYS_INLINE void assign_serial_tensor(dealii::Tensor< rank, dim, dealii::VectorizedArray< Number, width > > &result, const dealii::Tensor< rank, dim, Number > &serial, const unsigned int k)
DEAL_II_ALWAYS_INLINE auto generate_iterators(Functor f) -> std::array< auto, length >
DEAL_II_HOST_DEVICE T fast_pow(const T x, const T b, const Bias bias=Bias::none)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE T fixed_power(const T x)
constexpr unsigned int get_stride_size
DEAL_II_ALWAYS_INLINE void increment_iterators(T &iterators)
DEAL_II_ALWAYS_INLINE void write_entry(V &vector, const T &values, unsigned int i)
DEAL_II_HOST_DEVICE_ALWAYS_INLINE Number compare_and_apply_mask(const Number &left, const Number &right, const Number &true_value, const Number &false_value)
DEAL_II_ALWAYS_INLINE dealii::Tensor< rank, dim, Number > serialize_tensor(const dealii::Tensor< rank, dim, dealii::VectorizedArray< Number, width > > &vectorized, const unsigned int k)