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

Add support for Kokkos::fence in the fwd mode #1048

Merged
merged 1 commit into from
Aug 21, 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
5 changes: 5 additions & 0 deletions include/clad/Differentiator/KokkosBuiltins.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ inline void resize_pushforward(const I& arg, View& v, const size_t n0,
::Kokkos::resize(arg, d_v, n0, n1, n2, n3, n4, n5, n6, n7);
}

/// Fence
template <typename S> void fence_pushforward(const S& s, const S& /*d_s*/) {
::Kokkos::fence(s);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: variable 's' is not initialized [cppcoreguidelines-init-variables]

Suggested change
::Kokkos::fence(s);
::Kokkos::fence(s = 0);

Copy link
Owner

Choose a reason for hiding this comment

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

That seems broken, probably because my previous comment on kokkos and our CI.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah, I saw this, I don't think it's a valid suggestion either :)

Choose a reason for hiding this comment

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

Note, this is fine for forward mode but for the reverse mode we probably want d_s for the argument of the fence call to be able to identify the fences from the forward passes vs the fences of the reverse passes.

}

/// Parallel for
template <class... PolicyParams, class FunctorType> // range policy
void parallel_for_pushforward(
Expand Down
20 changes: 20 additions & 0 deletions unittests/Kokkos/ParallelFor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ template <typename View> struct Foo {
void operator()(const int i) const { res(i) = x * i; }
};

double parallel_for_functor_simplest_case_fence(double x) {
Kokkos::View<double[5], Kokkos::HostSpace> res("res");

// Kokkos::fence("named fence"); // Does not work on some versions of Kokkos.

Foo<Kokkos::View<double[5], Kokkos::HostSpace>> f(res, x);

f(0); // FIXME: this is a workaround to put Foo::operator() into the
// differentiation plan. This needs to be solved in clad.

Kokkos::parallel_for(5, f);
Kokkos::fence();

return res(3);
}

double parallel_for_functor_simplest_case_intpol(double x) {
Kokkos::View<double[5], Kokkos::HostSpace> res("res");

Expand Down Expand Up @@ -191,6 +207,10 @@ double parallel_for_functor_simplest_case_mdpol_space_and_anon(double x) {
TEST(ParallelFor, FunctorSimplestCases) {
const double eps = 1e-8;

auto df0 = clad::differentiate(parallel_for_functor_simplest_case_fence, 0);
for (double x = 3; x <= 5; x += 1)
EXPECT_NEAR(df0.execute(x), 3, eps);

auto df1 = clad::differentiate(parallel_for_functor_simplest_case_intpol, 0);
for (double x = 3; x <= 5; x += 1)
EXPECT_NEAR(df1.execute(x), 3, eps);
Expand Down
Loading