Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default constructor of Pack to init to 0 #318

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions src/ekat/ekat_pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,77 +174,68 @@ struct Pack {
typedef typename std::remove_const<ScalarType>::type scalar;

KOKKOS_FORCEINLINE_FUNCTION
Pack () {
vector_simd for (int i = 0; i < n; ++i) {
d[i] = ScalarTraits<scalar>::invalid();
}
constexpr Pack ()
: Pack (scalar(0))
{
// Nothing to do here
}

// Init all slots to scalar v.
KOKKOS_FORCEINLINE_FUNCTION
Pack (const scalar& v) {
vector_simd for (int i = 0; i < n; ++i) d[i] = v;
constexpr Pack (const scalar& v) {
*this = v;
}

// Init this Pack from another one.
template <typename T>
KOKKOS_FORCEINLINE_FUNCTION explicit
Pack (const Pack<T,n>& v) {
constexpr Pack (const Pack<T,n>& v) {
vector_simd for (int i = 0; i < n; ++i) d[i] = v[i];
}

// Init this Pack from another one.
KOKKOS_FORCEINLINE_FUNCTION
Pack (const Pack& src) {
constexpr Pack (const Pack& src) {
vector_simd for (int i = 0; i < n; ++i) d[i] = src[i];
}

// Init this Pack from another one.
KOKKOS_FORCEINLINE_FUNCTION
Pack (const volatile Pack& src) {
constexpr Pack (const volatile Pack& src) {
vector_simd for (int i = 0; i < n; ++i) d[i] = src.d[i];
}

// Init this Pack from a scalar, but only where Mask is true; otherwise
// init to default value.
// Init this Pack from a scalar, but only where Mask is true
template <typename T>
KOKKOS_FORCEINLINE_FUNCTION
explicit Pack (const Mask<n>& m, const T p) {
vector_simd for (int i = 0; i < n; ++i) {
d[i] = m[i] ? p : ScalarTraits<scalar>::invalid();
}
explicit Pack (const Mask<n>& m, const T p)
: Pack (m,p,T(0))
{
// Nothing to do here
}

// Init this Pack from two scalars, according to a given mask:
// if mask is true, set first value, otherwise the other.
template <typename T, typename S>
KOKKOS_FORCEINLINE_FUNCTION
explicit Pack (const Mask<n>& m, const T v_true, const S v_false) {
vector_simd
for (int i = 0; i < n; ++i) {
d[i] = m[i] ? v_true : v_false;
}
set (m,v_true,v_false);
}

// Init this Pack from a scalar, but only where Mask is true; otherwise
// init to default value.
template <typename T>
KOKKOS_FORCEINLINE_FUNCTION
explicit Pack (const Mask<n>& m, const Pack<T,n>& p) {
vector_simd for (int i = 0; i < n; ++i) {
d[i] = m[i] ? p[i] : ScalarTraits<scalar>::invalid();
}
set (m,p,Pack());
}

// Init this Pack from two other packs, according to a given mask:
// if mask is true, set first pack's value, otherwise the other's.
template <typename T, typename S>
KOKKOS_FORCEINLINE_FUNCTION
explicit Pack (const Mask<n>& m, const Pack<T,n>& p_true, const Pack<S,n>& p_false) {
vector_simd
for (int i = 0; i < n; ++i) {
d[i] = m[i] ? p_true[i] : p_false[i];
}
set (m,p_true,p_false);
}

KOKKOS_FORCEINLINE_FUNCTION const scalar& operator[] (const int& i) const { return d[i]; }
Expand Down
10 changes: 5 additions & 5 deletions tests/algorithm/lin_interp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,9 @@ TEST_CASE("lin_interp_monotone", "lin_interp") {

// Generic lambda, to get min-max of a scalarized subview
// Must use with rank-1 scalar views only
auto minmax = [](const auto& v) -> std::pair<Real,Real> {
auto minmax = [](const auto& v, const int sz) -> std::pair<Real,Real> {
std::pair<Real,Real> minmax {v[0],v[0]};
for (int i=1; i<v.extent_int(0); ++i) {
for (int i=1; i<sz; ++i) {
minmax.first = std::min(minmax.first,v[i]);
minmax.second = std::max(minmax.second,v[i]);
}
Expand Down Expand Up @@ -623,7 +623,7 @@ TEST_CASE("lin_interp_monotone", "lin_interp") {
populate_array (km1,get_col(y1_h,i).data(),generator,y_dist,false);

// Generate x2 in a way that guarantees its range is contained in that of x1
auto mm1 = minmax(get_col(x1_h,i));
auto mm1 = minmax(get_col(x1_h,i),km1);
auto delta = mm1.second-mm1.first;
real_pdf x2_dist(mm1.first+delta/1000,mm1.second-delta/1000);
populate_array (km2,get_col(x2_h,i).data(),generator,x2_dist,true);
Expand Down Expand Up @@ -653,8 +653,8 @@ TEST_CASE("lin_interp_monotone", "lin_interp") {
auto y2_h_s = ekat::scalarize(y2_h);
auto y1_h_s = ekat::scalarize(y1_h);
for (int i = 0; i < ncol; ++i) {
auto mm1 = minmax(ekat::subview(y1_h_s,i));
auto mm2 = minmax(ekat::subview(y2_h_s,i));
auto mm1 = minmax(ekat::subview(y1_h_s,i),km1);
auto mm2 = minmax(ekat::subview(y2_h_s,i),km2);
REQUIRE ( (mm2.first>=mm1.first && mm2.second<=mm1.second) );
}
}
Expand Down
11 changes: 6 additions & 5 deletions tests/pack/pack_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct TestPack {
REQUIRE(p2[i] == p_ref[i]);
}

// Masked ctor to create a pack with [3 invalid 3 invalid ...]
// Masked ctor to create a pack with [3 0 3 0 ...]
// We can do this with scalars or packs
Pack p3(m,3);
Pack p4(m,three);
Expand All @@ -187,8 +187,9 @@ struct TestPack {
REQUIRE(p3[i] == p_ref[i]);
REQUIRE(p4[i] == p_ref[i]);
} else {
REQUIRE(ekat::is_invalid(p3[i]));
REQUIRE(ekat::is_invalid(p4[i]));
// Non-masked values should keep the default ctor value (which is 0)
REQUIRE(p3[i]==0);
REQUIRE(p4[i]==0);
}
}
}
Expand Down Expand Up @@ -478,8 +479,8 @@ TEST_CASE("isnan", "ekat::pack") {
mvt mzero("",1), mnan("",1);
Kokkos::parallel_for(Kokkos::RangePolicy<>(0,1),
KOKKOS_LAMBDA(int) {
zero(0) = pt(0); // Ctor inits pack to 0
nan(0) = pt(); // Ctor inits pack to nan
zero(0) = pt(); // Ctor inits pack to 0
nan(0) = ScalarTraits<Real>::invalid();

const pt& z = zero(0);
const pt& n = nan(0);
Expand Down