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

OpenMPTarget: Fix reduction bug in parallel_reduce for TeamPolicy. #4491

Merged

Conversation

rgayatri23
Copy link
Contributor

The PR fixes a bug in the OpenMPTarget backend for array reduction.

@rgayatri23 rgayatri23 self-assigned this Nov 1, 2021
dalg24
dalg24 previously approved these changes Nov 2, 2021
Copy link
Member

@dalg24 dalg24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the issue was both declaration of the result as an array and copy back to the host?
@lucbv please confirm that this fixes #4410

@lucbv
Copy link
Contributor

lucbv commented Nov 2, 2021

@rgayatri23 @dalg24
No there is still a similar issue in the implementation of execute_array (line 197 onward).

@lucbv
Copy link
Contributor

lucbv commented Nov 2, 2021

Below is the piece of code that triggers it in Kokkos Kernels, for a reproducer I suggest adding void main() that creates a 2D view filled with increasing values in each columns so you can analytically compute the result of the nrm1 operation.

template <class RV, class XMV, class SizeType = typename XMV::size_type>
struct MV_Nrm1_Right_FunctorVector {
  typedef typename XMV::execution_space execution_space;
  typedef SizeType size_type;
  typedef typename XMV::non_const_value_type xvalue_type;
  typedef Kokkos::ArithTraits<xvalue_type> XAT;
  typedef Kokkos::ArithTraits<typename XAT::mag_type> MAT;
  typedef typename XAT::mag_type value_type[];

  size_type value_count;
  typename XMV::const_type m_x;

  MV_Nrm1_Right_FunctorVector(const XMV& x) : value_count(x.extent(1)), m_x(x) {
    static_assert(Kokkos::Impl::is_view<RV>::value,
                  "KokkosBlas::Impl::MV_Nrm1_Right_FunctorVector: "
                  "R is not a Kokkos::View.");
    static_assert(Kokkos::Impl::is_view<XMV>::value,
                  "KokkosBlas::Impl::MV_Nrm1_Right_FunctorVector: "
                  "X is not a Kokkos::View.");
    static_assert(std::is_same<typename RV::value_type,
                               typename RV::non_const_value_type>::value,
                  "KokkosBlas::Impl::MV_Nrm1_Right_FunctorVector: "
                  "R is const.  It must be nonconst, because it is an output "
                  "argument (we must be able to write to its entries).");
    static_assert(RV::rank == 1 && XMV::rank == 2,
                  "KokkosBlas::Impl::MV_Nrm1_Right_FunctorVector: "
                  "RV must have rank 1 and XMV must have rank 2.");
  }

  KOKKOS_INLINE_FUNCTION void operator()(const size_type i,
                                         value_type sum) const {
    const size_type numVecs = value_count;
#ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
#pragma ivdep
#endif
#ifdef KOKKOS_ENABLE_PRAGMA_VECTOR
#pragma vector always
#endif
    for (size_type j = 0; j < numVecs; ++j) {
      xvalue_type val = m_x(i, j);
      sum[j] += MAT::abs(XAT::real(val)) + MAT::abs(XAT::imag(val));
    }
  }

  KOKKOS_INLINE_FUNCTION void join(volatile value_type update,
                                   const volatile value_type source) const {
    const size_type numVecs = value_count;
#ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
#pragma ivdep
#endif
#ifdef KOKKOS_ENABLE_PRAGMA_VECTOR
#pragma vector always
#endif
    for (size_type j = 0; j < numVecs; ++j) {
      update[j] += source[j];
    }
  }

  KOKKOS_INLINE_FUNCTION void join(value_type update,
                                   const value_type source) const {
    const size_type numVecs = value_count;
#ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
#pragma ivdep
#endif
#ifdef KOKKOS_ENABLE_PRAGMA_VECTOR
#pragma vector always
#endif
    for (size_type j = 0; j < numVecs; ++j) {
      update[j] += source[j];
    }
  }
};

template <class RV, class XMV, class SizeType>
void MV_Nrm1_Invoke(const RV& r, const XMV& X) {
  typedef typename XMV::execution_space execution_space;
  const SizeType numRows = static_cast<SizeType>(X.extent(0));
  Kokkos::RangePolicy<execution_space, SizeType> policy(0, numRows);

  // If the input multivector (2-D View) has only one column, invoke
  // the single-vector version of the kernel.
  if (X.extent(1) > 1) {
    typedef MV_Nrm1_Right_FunctorVector<RV, XMV, SizeType> functor_type;
    functor_type op(X);
    Kokkos::parallel_reduce("KokkosBlas1::Nrm1::S1", policy, op, r);
  }
}

@lucbv
Copy link
Contributor

lucbv commented Nov 2, 2021

In general I would suggest looking at what tests are disabled in Kokkos Core when KOKKOS_ENABLE_OPENMPTARGET is defined. I am pretty sure that the reproducer I show above is already covered, it is just not enabled at this time.

@dalg24 dalg24 dismissed their stale review November 2, 2021 16:35

Issue is not resolved and no new unit test is enabled

@rgayatri23
Copy link
Contributor Author

@rgayatri23 @dalg24 No there is still a similar issue in the implementation of execute_array (line 197 onward).

Yes I need to do the similar changes for the RangePolicy version too. I am working on the unit tests now, enabling the one that tests these features.

@rgayatri23
Copy link
Contributor Author

Fixed the bug in RangePolicy parallel_reduce for array reductions and enabled corresponding tests.

@dalg24
Copy link
Member

dalg24 commented Nov 3, 2021

@lucbv please review the PR update

Copy link
Contributor

@lucbv lucbv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rgayatri23 for the changes. It fixes some issues but I now have more build problem with the execute_init_join method. I have to comment it's implementation to build without failures in Kokkos Kernels. We can tackle that in another PR in my opinion.

@rgayatri23
Copy link
Contributor Author

@lucbv What issues are you facing in the execute_init_join ? The only change there is that we now initialize zero length reductions to be consistent with Kokkos "should" do.

@lucbv
Copy link
Contributor

lucbv commented Nov 3, 2021

@rgayatri23
I have opened another issue regarding these, see issue #4500

@dalg24 dalg24 merged commit c38853a into kokkos:develop Nov 4, 2021
@rgayatri23 rgayatri23 deleted the OpenMPTarget_array_reduction_bug_fix branch March 24, 2023 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants