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

Make ReduceData::value safer #3421

Merged
merged 1 commit into from
Jul 13, 2023
Merged
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
30 changes: 22 additions & 8 deletions Src/Base/AMReX_Reduce.H
Original file line number Diff line number Diff line change
Expand Up @@ -666,25 +666,29 @@ public:
template <typename D>
typename D::Type value (D & reduce_data)
{
auto hp = reduce_data.hostPtr();

if (m_result_is_ready) {
return *hp;
}

using ReduceTuple = typename D::Type;
auto const& stream = Gpu::gpuStream();
auto hp = reduce_data.hostPtr();
auto dp = reduce_data.devicePtr();
auto const& nblocks = reduce_data.nBlocks();
#if defined(AMREX_USE_SYCL)
if (reduce_data.maxStreamIndex() == 0 && nblocks[0] <= 4096) {
const int N = nblocks[0];
if (N == 0) {
Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(*hp);
return *hp;
} else {
Gpu::PinnedVector<ReduceTuple> tmp(N);
Gpu::dtoh_memcpy_async(tmp.data(), dp, sizeof(ReduceTuple)*N);
Gpu::streamSynchronize();
for (int i = 1; i < N; ++i) {
Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(tmp[0], tmp[i]);
}
return tmp[0];
*hp = tmp[0];
}
} else
#endif
Expand Down Expand Up @@ -738,9 +742,14 @@ public:
});
#endif
Gpu::streamSynchronize();
return *hp;
}

m_result_is_ready = true;
return *hp;
}

private:
bool m_result_is_ready = false;
};

namespace Reduce {
Expand Down Expand Up @@ -1135,15 +1144,20 @@ public:
template <typename D>
typename D::Type value (D & reduce_data)
{
using ReduceTuple = typename D::Type;
auto& rrv = reduce_data.reference();
if (rrv.size() > 1) {
for (int i = 1, N = rrv.size(); i < N; ++i) {
Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(rrv[0], rrv[i]);
if (! m_result_is_ready) {
using ReduceTuple = typename D::Type;
if (rrv.size() > 1) {
for (int i = 1, N = rrv.size(); i < N; ++i) {
Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(rrv[0], rrv[i]);
}
}
m_result_is_ready = true;
}
return rrv[0];
}

bool m_result_is_ready = false;
};

namespace Reduce {
Expand Down
Loading