Pointwise Step Function

Issue #142 new
Nils Deppe created an issue

Hi Klaus!

I have another feature request: a pointwise step function. I'm not sure how good of an idea this is because the definition at H(x=0) varies. We use H(x=0)=1.0. Here's what I've implemented for doubles:

namespace blaze {
template <typename T>
BLAZE_ALWAYS_INLINE SIMDdouble step_function(const SIMDf64<T>& v) noexcept
#if BLAZE_AVX512F_MODE || BLAZE_MIC_MODE
{
  return _mm512_set_pd((~v)[7] < 0.0 ? 0.0 : 1.0, (~v)[6] < 0.0 ? 0.0 : 1.0,
                       (~v)[5] < 0.0 ? 0.0 : 1.0, (~v)[4] < 0.0 ? 0.0 : 1.0,
                       (~v)[3] < 0.0 ? 0.0 : 1.0, (~v)[2] < 0.0 ? 0.0 : 1.0,
                       (~v)[1] < 0.0 ? 0.0 : 1.0, (~v)[0] < 0.0 ? 0.0 : 1.0);
}
#elif BLAZE_AVX_MODE
{
  return _mm256_set_pd((~v)[3] < 0.0 ? 0.0 : 1.0, (~v)[2] < 0.0 ? 0.0 : 1.0,
                       (~v)[1] < 0.0 ? 0.0 : 1.0, (~v)[0] < 0.0 ? 0.0 : 1.0);
}
#elif BLAZE_SSE2_MODE
{
  return _mm_set_pd((~v)[1] < 0.0 ? 0.0 : 1.0, (~v)[0] < 0.0 ? 0.0 : 1.0);
}
#else
{
  return SIMDdouble{(~v).value < 0.0 ? 0.0 : 1.0};
}
#endif

BLAZE_ALWAYS_INLINE double step_function(const double& v) noexcept {
  return v < 0.0 ? 0.0 : 1.0;
}

struct StepFunction {
  explicit inline StepFunction() {}

  template <typename T>
  BLAZE_ALWAYS_INLINE decltype(auto) operator()(const T& a) const noexcept {
    return step_function(a);
  }

  template <typename T>
  BLAZE_ALWAYS_INLINE decltype(auto) load(const T& a) const noexcept {
    BLAZE_CONSTRAINT_MUST_BE_SIMD_PACK(T);
    return step_function(a);
  }
};
}  // namespace blaze

template <typename VT, bool TF>
BLAZE_ALWAYS_INLINE decltype(auto) step_function(
    const blaze::DenseVector<VT, TF>& vec) noexcept {
  return blaze::map(~vec, blaze::StepFunction{});
}

For the AVX512 case it may be more efficient to user masked vectorized operations, but I haven't studied this.

If because of the ambiguity at x=0 you don't want this in Blaze that's totally understandable :)

Cheers,

Nils

Comments (1)

  1. Klaus Iglberger

    Hi Nils!

    Thanks for the proposal. I agree that a pointwise step function would be useful for many applications. I will consider it for one of the next releases. Also thanks a lot for the source code, this makes it easier.

    Best regards,

    Klaus!

  2. Log in to comment