Skip to content

Commit

Permalink
Valarray extended (#391)
Browse files Browse the repository at this point in the history
* first commit: fix CMakeLists for macOS

* Extend quokka::valarray with more operators.

* Revert "first commit: fix CMakeLists for macOS"

This reverts commit 422cfe2.

* mod .gitignore

* small fixes and revert .gitignore

* set fillin to a member of valarray

* rename fillin to valarray_fillin

* define .fillin and .hasnan
  • Loading branch information
ChongChong He authored Sep 21, 2023
1 parent 1cebf4b commit 033e4f3
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/valarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,28 @@ template <typename T, int d> class valarray

[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr auto size() const -> size_t { return d; }

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void fillin(T const &scalar)
{
for (size_t i = 0; i < d; ++i) {
values[i] = scalar;
}
}

[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto hasnan() const -> bool
{
for (size_t i = 0; i < d; ++i) {
if (std::isnan(values[i])) { return true; }
}
return false;
}

private:
T values[d]; // NOLINT
static constexpr T default_value = 0;
};
} // namespace quokka

// array + array
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator+(quokka::valarray<T, d> const &a, quokka::valarray<T, d> const &b) -> quokka::valarray<T, d>
{
Expand All @@ -69,6 +85,7 @@ AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator+(quokka::valarray<T, d> c
return sum;
}

// array - array
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator-(quokka::valarray<T, d> const &a, quokka::valarray<T, d> const &b) -> quokka::valarray<T, d>
{
Expand All @@ -79,6 +96,7 @@ AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator-(quokka::valarray<T, d> c
return diff;
}

// array * array
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator*(quokka::valarray<T, d> const &a, quokka::valarray<T, d> const &b) -> quokka::valarray<T, d>
{
Expand All @@ -89,6 +107,7 @@ AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator*(quokka::valarray<T, d> c
return prod;
}

// array / array
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator/(quokka::valarray<T, d> const &a, quokka::valarray<T, d> const &b) -> quokka::valarray<T, d>
{
Expand All @@ -99,6 +118,7 @@ AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator/(quokka::valarray<T, d> c
return div;
}

// scalar * array
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator*(T const &scalar, quokka::valarray<T, d> const &v) -> quokka::valarray<T, d>
{
quokka::valarray<T, d> scalarprod;
Expand All @@ -108,6 +128,7 @@ template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto opera
return scalarprod;
}

// array * scalar
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator*(quokka::valarray<T, d> const &v, T const &scalar) -> quokka::valarray<T, d>
{
quokka::valarray<T, d> scalarprod;
Expand All @@ -117,13 +138,35 @@ template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto opera
return scalarprod;
}

// array *= scalar
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator*=(quokka::valarray<T, d> &v, T const &scalar)
{
for (size_t i = 0; i < v.size(); ++i) {
v[i] *= scalar;
}
}

// array += array
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator+=(quokka::valarray<T, d> &a, quokka::valarray<T, d> const &b)
{
for (size_t i = 0; i < a.size(); ++i) {
a[i] += b[i];
}
}

// array + scalar
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator+(quokka::valarray<T, d> const &v, T const &scalar) -> quokka::valarray<T, d>
{
quokka::valarray<T, d> scalarsum;
for (size_t i = 0; i < v.size(); ++i) {
scalarsum[i] = v[i] + scalar;
}
return scalarsum;
}

// array / scalar
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator/(quokka::valarray<T, d> const &v, T const &scalar) -> quokka::valarray<T, d>
{
quokka::valarray<T, d> scalardiv;
Expand All @@ -133,6 +176,15 @@ template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto opera
return scalardiv;
}

// array /= scalar
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void operator/=(quokka::valarray<T, d> &v, T const &scalar)
{
for (size_t i = 0; i < v.size(); ++i) {
v[i] /= scalar;
}
}

// abs(array)
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto abs(quokka::valarray<T, d> const &v) -> quokka::valarray<T, d>
{
quokka::valarray<T, d> abs_v;
Expand All @@ -142,6 +194,7 @@ template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto abs(q
return abs_v;
}

// min(array)
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto min(quokka::valarray<T, d> const &v) -> T
{
static_assert(d >= 1);
Expand All @@ -153,4 +206,70 @@ template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto min(q
return min_val;
}

// max(array)
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto max(quokka::valarray<T, d> const &v) -> T
{
static_assert(d >= 1);
T max_val = v[0]; // v must have at least 1 element

for (size_t i = 0; i < v.size(); ++i) {
max_val = std::max(max_val, v[i]);
}
return max_val;
}

// sum(array)
template <typename T, int d> AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto sum(quokka::valarray<T, d> const &v) -> T
{
T sum_val = 0;
for (size_t i = 0; i < v.size(); ++i) {
sum_val += v[i];
}
return sum_val;
}

// array > array
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator>(quokka::valarray<T, d> const &a, quokka::valarray<T, d> const &b) -> quokka::valarray<bool, d>
{
quokka::valarray<bool, d> comp;
for (size_t i = 0; i < a.size(); ++i) {
comp[i] = a[i] > b[i];
}
return comp;
}

// array > scalar
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator>(quokka::valarray<T, d> const &a, T const &scalar) -> quokka::valarray<bool, d>
{
quokka::valarray<bool, d> comp;
for (size_t i = 0; i < a.size(); ++i) {
comp[i] = a[i] > scalar;
}
return comp;
}

// array < array
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator<(quokka::valarray<T, d> const &a, quokka::valarray<T, d> const &b) -> quokka::valarray<bool, d>
{
quokka::valarray<bool, d> comp;
for (size_t i = 0; i < a.size(); ++i) {
comp[i] = a[i] < b[i];
}
return comp;
}

// array < scalar
template <typename T, int d>
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE auto operator<(quokka::valarray<T, d> const &a, T const &scalar) -> quokka::valarray<bool, d>
{
quokka::valarray<bool, d> comp;
for (size_t i = 0; i < a.size(); ++i) {
comp[i] = a[i] < scalar;
}
return comp;
}

#endif // VALARRAY_HPP_

0 comments on commit 033e4f3

Please sign in to comment.