Skip to content

Commit

Permalink
Make reduce a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikvn committed Sep 13, 2021
1 parent c935c71 commit 6d581d8
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 50 deletions.
8 changes: 4 additions & 4 deletions common/cuda_hip/components/reduction.hpp.inc
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ __global__ __launch_bounds__(default_block_size) void reduce_add_array(
template <typename ValueType>
__global__
__launch_bounds__(default_block_size) void reduce_add_array_with_existing_value(
size_type size, const ValueType *__restrict__ source,
ValueType *__restrict__ result)
size_type size, const ValueType* __restrict__ source,
ValueType* __restrict__ result)
{
__shared__ UninitializedArray<ValueType, default_block_size> block_sum;
reduce_array(size, source, static_cast<ValueType *>(block_sum),
[](const ValueType &x, const ValueType &y) { return x + y; });
reduce_array(size, source, static_cast<ValueType*>(block_sum),
[](const ValueType& x, const ValueType& y) { return x + y; });

if (threadIdx.x == 0) {
result[blockIdx.x] += block_sum[0];
Expand Down
23 changes: 13 additions & 10 deletions core/base/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,23 @@ void Array<ValueType>::fill(const ValueType value)


template <typename ValueType>
void Array<ValueType>::reduce(ValueType *value) const
void reduce(const Array<ValueType>& input_arr, ValueType* value)
{
this->get_executor()->run(array::make_reduce_array(
this->get_const_data(), this->get_num_elems(), value));
auto exec = input_arr.get_executor();
exec->run(array::make_reduce_array(input_arr.get_const_data(),
input_arr.get_num_elems(), value));
}


template <typename ValueType>
ValueType Array<ValueType>::reduce(ValueType init_value) const
ValueType reduce(const Array<ValueType>& input_arr, const ValueType init_value)
{
auto value = Array<ValueType>(this->get_executor(), {init_value});
this->get_executor()->run(array::make_reduce_array(
this->get_const_data(), this->get_num_elems(), value.get_data()));
return this->get_executor()->copy_val_to_host(value.get_data());
auto exec = input_arr.get_executor();
auto value = Array<ValueType>(exec, {init_value});
exec->run(array::make_reduce_array(input_arr.get_const_data(),
input_arr.get_num_elems(),
value.get_data()));
return exec->copy_val_to_host(value.get_data());
}


Expand All @@ -118,13 +121,13 @@ GKO_INSTANTIATE_FOR_EACH_TEMPLATE_TYPE(GKO_DECLARE_ARRAY_FILL);


#define GKO_DECLARE_ARRAY_REDUCE(_type) \
void Array<_type>::reduce(_type *value) const
void reduce(const Array<_type>& arr, _type* value)

GKO_INSTANTIATE_FOR_EACH_TEMPLATE_TYPE(GKO_DECLARE_ARRAY_REDUCE);


#define GKO_DECLARE_ARRAY_REDUCE2(_type) \
_type Array<_type>::reduce(_type val) const
_type reduce(const Array<_type>& arr, const _type val)

GKO_INSTANTIATE_FOR_EACH_TEMPLATE_TYPE(GKO_DECLARE_ARRAY_REDUCE2);

Expand Down
4 changes: 2 additions & 2 deletions core/components/reduce_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ namespace kernels {

#define GKO_DECLARE_REDUCE_ARRAY_KERNEL(ValueType) \
void reduce_array(std::shared_ptr<const DefaultExecutor> exec, \
const ValueType *data, size_type num_entries, \
ValueType *val)
const ValueType* data, size_type num_entries, \
ValueType* val)


#define GKO_DECLARE_ALL_AS_TEMPLATES \
Expand Down
4 changes: 2 additions & 2 deletions cuda/components/reduce_array.cu
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ namespace components {
constexpr int default_block_size = 512;


#include "common/components/reduction.hpp.inc"
#include "common/cuda_hip/components/reduction.hpp.inc"


template <typename ValueType>
void reduce_array(std::shared_ptr<const DefaultExecutor> exec,
const ValueType *array, size_type size, ValueType *val)
const ValueType* array, size_type size, ValueType* val)
{
auto block_results_val = array;
size_type grid_dim = size;
Expand Down
24 changes: 24 additions & 0 deletions cuda/test/base/array.cu
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,27 @@ TYPED_TEST(Array, CanCopyBackTemporaryCloneOnDifferentExecutor)

this->assert_equal_to_original_x(this->x);
}


TYPED_TEST(Array, CanBeReduced)
{
using T = TypeParam;
auto cuda = gko::CudaExecutor::create(0, this->exec);
auto arr = gko::Array<TypeParam>(cuda, I<T>{T(4), T(6)});
auto out = gko::Array<TypeParam>(cuda, I<T>{T(2)});
gko::reduce(arr, out.get_data());

out.set_executor(cuda->get_master());
ASSERT_EQ(out.get_data()[0], TypeParam{12});
}


TYPED_TEST(Array, CanBeReduced2)
{
using T = TypeParam;
auto cuda = gko::CudaExecutor::create(0, this->exec);
auto arr = gko::Array<TypeParam>(cuda, I<T>{T(4), T(6)});
auto out = gko::reduce(arr, TypeParam{T(3)});

ASSERT_EQ(out, TypeParam{13});
}
11 changes: 2 additions & 9 deletions dpcpp/components/reduce_array.dp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,8 @@ namespace components {

template <typename ValueType>
void reduce_array(std::shared_ptr<const DefaultExecutor> exec,
const ValueType *array, size_type n, ValueType *val)
{
// exec->get_queue()->submit([&](sycl::handler &cgh) {
// cgh.parallel_for(sycl::range<1>{n}, [=](sycl::id<1> idx_id) {
// const auto idx = idx_id[0];
// array[idx] = val;
// });
// });
}
const ValueType* array, size_type n,
ValueType* val) GKO_NOT_IMPLEMENTED;

GKO_INSTANTIATE_FOR_EACH_TEMPLATE_TYPE(GKO_DECLARE_REDUCE_ARRAY_KERNEL);

Expand Down
4 changes: 2 additions & 2 deletions hip/components/reduce_array.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ namespace components {
constexpr int default_block_size = 512;


#include "common/components/reduction.hpp.inc"
#include "common/cuda_hip/components/reduction.hpp.inc"


template <typename ValueType>
void reduce_array(std::shared_ptr<const DefaultExecutor> exec,
const ValueType *array, size_type size, ValueType *val)
const ValueType* array, size_type size, ValueType* val)
{
auto block_results_val = array;
size_type grid_dim = size;
Expand Down
39 changes: 25 additions & 14 deletions include/ginkgo/core/base/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,20 +487,6 @@ class Array {
*/
void fill(const value_type value);

/**
* Reduce the values in the array
*
* @return the reduced value
*/
value_type reduce(value_type init_val = 0) const;

/**
* Reduce the values in the array
*
* @param the reduced value
*/
void reduce(value_type *value) const;

/**
* Returns the number of elements in the Array.
*
Expand Down Expand Up @@ -585,6 +571,31 @@ class Array {
};


/**
* Reduce the values in the array
*
* @tparam The type of the input data
*
* @param [in] input_arr the input array to be reduced
* @param [in] init_val the initial value
* @return the reduced value
*/
template <typename ValueType>
ValueType reduce(const Array<ValueType>& input_arr,
const ValueType init_val = 0);

/**
* Reduce the values in the array
*
* @tparam The type of the input data
*
* @param [in] input_arr the input array to be reduced
* @param [out,in] value the reduced value
*/
template <typename ValueType>
void reduce(const Array<ValueType>& input_arr, ValueType* value);


namespace detail {


Expand Down
2 changes: 1 addition & 1 deletion omp/components/reduce_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace components {

template <typename ValueType>
void reduce_array(std::shared_ptr<const DefaultExecutor> exec,
const ValueType *array, size_type n, ValueType *val)
const ValueType* array, size_type n, ValueType* val)
{
ValueType out = *val;
#pragma omp declare reduction(add:ValueType : omp_out = omp_out + omp_in)
Expand Down
2 changes: 1 addition & 1 deletion reference/components/reduce_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace components {

template <typename ValueType>
void reduce_array(std::shared_ptr<const DefaultExecutor> exec,
const ValueType *array, size_type n, ValueType *val)
const ValueType* array, size_type n, ValueType* val)
{
*val = std::accumulate(array, array + n, ValueType(*val));
}
Expand Down
10 changes: 5 additions & 5 deletions reference/test/base/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ TYPED_TEST(Array, CanBeFilledWithValue)

TYPED_TEST(Array, CanBeReduced)
{
TypeParam out = 0.0;
this->x.reduce(&out);
TypeParam out = 0.5;
gko::reduce(this->x, &out);

ASSERT_EQ(out, TypeParam{7});
ASSERT_EQ(out, TypeParam{7.5});
}


TYPED_TEST(Array, CanBeReduced2)
{
auto out = this->x.reduce();
auto out = gko::reduce(this->x, TypeParam{2});

ASSERT_EQ(out, TypeParam{7});
ASSERT_EQ(out, TypeParam{9});
}


Expand Down

0 comments on commit 6d581d8

Please sign in to comment.