Skip to content

[SYCL] Support lambda functions passed to reduction #2190

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

Merged
merged 5 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 24 additions & 19 deletions sycl/include/CL/sycl/intel/reduction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,17 @@ using IsKnownIdentityOp =
template <typename T, class BinaryOperation, typename Subst = void>
class reducer {
public:
reducer(const T &Identity) : MValue(Identity), MIdentity(Identity) {}
void combine(const T &Partial) {
BinaryOperation BOp;
MValue = BOp(MValue, Partial);
}
reducer(const T &Identity, BinaryOperation BOp)
: MValue(Identity), MIdentity(Identity), MBinaryOp(BOp) {}
void combine(const T &Partial) { MValue = MBinaryOp(MValue, Partial); }

T getIdentity() const { return MIdentity; }

T MValue;

private:
const T MIdentity;
BinaryOperation MBinaryOp;
};

/// Specialization of the generic class 'reducer'. It is used for reductions
Expand All @@ -183,7 +182,7 @@ class reducer<T, BinaryOperation,
enable_if_t<IsKnownIdentityOp<T, BinaryOperation>::value>> {
public:
reducer() : MValue(getIdentity()) {}
reducer(const T &) : MValue(getIdentity()) {}
reducer(const T &, BinaryOperation) : MValue(getIdentity()) {}

void combine(const T &Partial) {
BinaryOperation BOp;
Expand Down Expand Up @@ -405,7 +404,7 @@ class reduction_impl {
template <
typename _T = T, class _BinaryOperation = BinaryOperation,
enable_if_t<IsKnownIdentityOp<_T, _BinaryOperation>::value> * = nullptr>
reduction_impl(accessor_type &Acc, const T &Identity)
reduction_impl(accessor_type &Acc, const T &Identity, BinaryOperation)
: MAcc(shared_ptr_class<accessor_type>(shared_ptr_class<accessor_type>{},
&Acc)),
MIdentity(getIdentity()) {
Expand All @@ -431,10 +430,10 @@ class reduction_impl {
template <
typename _T = T, class _BinaryOperation = BinaryOperation,
enable_if_t<!IsKnownIdentityOp<_T, _BinaryOperation>::value> * = nullptr>
reduction_impl(accessor_type &Acc, const T &Identity)
reduction_impl(accessor_type &Acc, const T &Identity, BinaryOperation BOp)
: MAcc(shared_ptr_class<accessor_type>(shared_ptr_class<accessor_type>{},
&Acc)),
MIdentity(Identity) {
MIdentity(Identity), MBinaryOp(BOp) {
assert(Acc.get_count() == 1 &&
"Only scalar/1-element reductions are supported now.");
}
Expand All @@ -456,7 +455,7 @@ class reduction_impl {
template <
typename _T = T, class _BinaryOperation = BinaryOperation,
enable_if_t<IsKnownIdentityOp<_T, _BinaryOperation>::value> * = nullptr>
reduction_impl(T *VarPtr, const T &Identity)
reduction_impl(T *VarPtr, const T &Identity, BinaryOperation)
: MIdentity(Identity), MUSMPointer(VarPtr) {
// For now the implementation ignores the identity value given by user
// when the implementation knows the identity.
Expand All @@ -478,8 +477,8 @@ class reduction_impl {
template <
typename _T = T, class _BinaryOperation = BinaryOperation,
enable_if_t<!IsKnownIdentityOp<_T, _BinaryOperation>::value> * = nullptr>
reduction_impl(T *VarPtr, const T &Identity)
: MIdentity(Identity), MUSMPointer(VarPtr) {}
reduction_impl(T *VarPtr, const T &Identity, BinaryOperation BOp)
: MIdentity(Identity), MUSMPointer(VarPtr), MBinaryOp(BOp) {}

/// Associates reduction accessor with the given handler and saves reduction
/// buffer so that it is alive until the command group finishes the work.
Expand Down Expand Up @@ -563,6 +562,9 @@ class reduction_impl {
return OutPtr;
}

/// Returns the binary operation associated with the reduction.
BinaryOperation getBinaryOperation() const { return MBinaryOp; }

private:
/// Identity of the BinaryOperation.
/// The result of BinaryOperation(X, MIdentity) is equal to X for any X.
Expand All @@ -576,6 +578,8 @@ class reduction_impl {
/// USM pointer referencing the memory to where the result of the reduction
/// must be written. Applicable/used only for USM reductions.
T *MUSMPointer = nullptr;

BinaryOperation MBinaryOp;
};

/// These are the forward declaration for the classes that help to create
Expand Down Expand Up @@ -794,9 +798,10 @@ reduCGFuncImpl(handler &CGH, KernelType KernelFunc, const nd_range<Dims> &Range,
typename Reduction::result_type ReduIdentity = Redu.getIdentity();
using Name = typename get_reduction_main_kernel_name_t<
KernelName, KernelType, Reduction::is_usm, UniformPow2WG, OutputT>::name;
auto BOp = Redu.getBinaryOperation();
CGH.parallel_for<Name>(Range, [=](nd_item<Dims> NDIt) {
// Call user's functions. Reducer.MValue gets initialized there.
typename Reduction::reducer_type Reducer(ReduIdentity);
typename Reduction::reducer_type Reducer(ReduIdentity, BOp);
KernelFunc(NDIt, Reducer);

size_t WGSize = NDIt.get_local_range().size();
Expand All @@ -811,7 +816,6 @@ reduCGFuncImpl(handler &CGH, KernelType KernelFunc, const nd_range<Dims> &Range,
// Tree-reduction: reduce the local array LocalReds[:] to LocalReds[0]
// LocalReds[WGSize] accumulates last/odd elements when the step
// of tree-reduction loop is not even.
typename Reduction::binary_operation BOp;
size_t PrevStep = WGSize;
for (size_t CurStep = PrevStep >> 1; CurStep > 0; CurStep >>= 1) {
if (LID < CurStep)
Expand Down Expand Up @@ -925,6 +929,7 @@ reduAuxCGFuncImpl(handler &CGH, size_t NWorkItems, size_t NWorkGroups,
auto LocalReds = Redu.getReadWriteLocalAcc(NumLocalElements, CGH);

auto ReduIdentity = Redu.getIdentity();
auto BOp = Redu.getBinaryOperation();
using Name = typename get_reduction_aux_kernel_name_t<
KernelName, KernelType, Reduction::is_usm, UniformPow2WG, OutputT>::name;
nd_range<1> Range{range<1>(NWorkItems), range<1>(WGSize)};
Expand All @@ -943,7 +948,6 @@ reduAuxCGFuncImpl(handler &CGH, size_t NWorkItems, size_t NWorkGroups,
// Tree-reduction: reduce the local array LocalReds[:] to LocalReds[0]
// LocalReds[WGSize] accumulates last/odd elements when the step
// of tree-reduction loop is not even.
typename Reduction::binary_operation BOp;
size_t PrevStep = WGSize;
for (size_t CurStep = PrevStep >> 1; CurStep > 0; CurStep >>= 1) {
if (LID < CurStep)
Expand Down Expand Up @@ -1022,10 +1026,10 @@ template <typename T, class BinaryOperation, int Dims, access::mode AccMode,
access::placeholder IsPH>
detail::reduction_impl<T, BinaryOperation, Dims, false, AccMode, IsPH>
reduction(accessor<T, Dims, AccMode, access::target::global_buffer, IsPH> &Acc,
const T &Identity, BinaryOperation) {
const T &Identity, BinaryOperation BOp) {
// The Combiner argument was needed only to define the BinaryOperation param.
return detail::reduction_impl<T, BinaryOperation, Dims, false, AccMode, IsPH>(
Acc, Identity);
Acc, Identity, BOp);
}

/// Creates and returns an object implementing the reduction functionality.
Expand All @@ -1050,9 +1054,10 @@ reduction(accessor<T, Dims, AccMode, access::target::global_buffer, IsPH> &Acc,
/// \param Identity, and the binary operation used in the reduction.
template <typename T, class BinaryOperation>
detail::reduction_impl<T, BinaryOperation, 0, true, access::mode::read_write>
reduction(T *VarPtr, const T &Identity, BinaryOperation) {
reduction(T *VarPtr, const T &Identity, BinaryOperation BOp) {
return detail::reduction_impl<T, BinaryOperation, 0, true,
access::mode::read_write>(VarPtr, Identity);
access::mode::read_write>(VarPtr, Identity,
BOp);
}

/// Creates and returns an object implementing the reduction functionality.
Expand Down
71 changes: 71 additions & 0 deletions sycl/test/reduction/reduction_nd_lambda.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// UNSUPPORTED: cuda
// OpenCL C 2.x alike work-group functions not yet supported by CUDA.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to see a bit more clarification here. Are block reductions missing from CUDA itself or CUDA plugin does not expose them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Alex, I updated the comment by copying it from here: https://github.com/intel/llvm/blame/sycl/sycl/test/reduction/reduction_nd_s0_dw.cpp#L2 and adding a note about intel::reduce() that is not supported yet by CUDA.
I don't have any other details regarding CUDA support for that feature.
Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have any other details regarding CUDA support for that feature.

It's implementable, but not in the plugin yet. CUDA doesn't have native support for equivalents to everything in the GroupAlgorithms extension, but it did recently add support for reductions as part of its cooperative group functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the link. The updated comment is explanatory enough, I think:
// Reductions use work-group builtins (e.g. intel::reduce()) not yet supported
// by CUDA.

Copy link
Contributor

Choose a reason for hiding this comment

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

I completely agree. I was just providing some more background for @alexbatashev.

//
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out
// RUNx: env SYCL_DEVICE_TYPE=HOST %t.out
// RUN: %CPU_RUN_PLACEHOLDER %t.out
// RUN: %GPU_RUN_PLACEHOLDER %t.out
// RUN: %ACC_RUN_PLACEHOLDER %t.out

// This test performs basic checks of parallel_for(nd_range, reduction, lambda)

#include "reduction_utils.hpp"
#include <CL/sycl.hpp>
#include <cassert>

using namespace cl::sycl;

template <class KernelName, typename T, class BinaryOperation>
void test(T Identity, BinaryOperation BOp, size_t WGSize, size_t NWItems) {
buffer<T, 1> InBuf(NWItems);
buffer<T, 1> OutBuf(1);

// Initialize.
T CorrectOut;
initInputData(InBuf, CorrectOut, Identity, BOp, NWItems);

// Compute.
queue Q;
Q.submit([&](handler &CGH) {
auto In = InBuf.template get_access<access::mode::read>(CGH);
auto Out = OutBuf.template get_access<access::mode::discard_write>(CGH);
auto Redu = intel::reduction(Out, Identity, BOp);

range<1> GlobalRange(NWItems);
range<1> LocalRange(WGSize);
nd_range<1> NDRange(GlobalRange, LocalRange);
CGH.parallel_for<KernelName>(NDRange, Redu,
[=](nd_item<1> NDIt, auto &Sum) {
Sum.combine(In[NDIt.get_global_linear_id()]);
});
});

// Check correctness.
auto Out = OutBuf.template get_access<access::mode::read>();
T ComputedOut = *(Out.get_pointer());
if (ComputedOut != CorrectOut) {
std::cout << "NWItems = " << NWItems << ", WGSize = " << WGSize << "\n";
std::cout << "Computed value: " << ComputedOut
<< ", Expected value: " << CorrectOut << "\n";
assert(0 && "Wrong value.");
}
}

int main() {
test<class AddTestName, int>(
0, [](auto x, auto y) { return (x + y); }, 8, 32);
test<class MulTestName, int>(
0, [](auto x, auto y) { return (x * y); }, 8, 32);

// Check with CUSTOM type.
test<class CustomAddTestname, CustomVec<long long>>(
CustomVec<long long>(0),
[](auto x, auto y) {
CustomVecPlus<long long> BOp;
return BOp(x, y);
},
4, 64);

std::cout << "Test passed\n";
return 0;
}