reduce_sum.cpp 2.8 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
11
#include <migraphx/gpu/device/reduce_sum.hpp>
#include <migraphx/gpu/device/launch.hpp>
#include <migraphx/gpu/device/visit.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
namespace device {

struct sum
{
Paul's avatar
Paul committed
12
    template <class T>
Paul's avatar
Paul committed
13
14
15
16
17
18
    MIGRAPHX_DEVICE_CONSTEXPR T operator()(T x, T y) const
    {
        return x + y;
    }
};

Paul's avatar
Paul committed
19
template <std::size_t N, class Op, class T, class F>
Paul's avatar
Paul committed
20
21
22
23
24
__device__ auto block_reduce(index idx, Op op, T init, std::size_t n, F f)
{
    using type = decltype(f(idx.local));
    MIGRAPHX_DEVICE_SHARED type buffer[N];
    type x = init;
Paul's avatar
Paul committed
25
    idx.local_stride(n, [&](auto i) { x = op(x, f(i)); });
Paul's avatar
Paul committed
26
27
28
    buffer[idx.local] = x;
    __syncthreads();

Paul's avatar
Paul committed
29
    for(std::size_t s = 1; s < idx.nlocal(); s *= 2)
Paul's avatar
Paul committed
30
31
    {
        const std::size_t index = 2 * s * idx.local;
Paul's avatar
Paul committed
32
        if(index < idx.nlocal())
Paul's avatar
Paul committed
33
34
35
36
37
38
39
40
        {
            buffer[index] = op(buffer[index], buffer[index + s]);
        }
        __syncthreads();
    }
    return buffer[0];
}

Paul's avatar
Paul committed
41
42
constexpr std::size_t compute_block_size(std::size_t n, std::size_t max_block_size)
{
Paul's avatar
Paul committed
43
    size_t block_size = 1;
Paul's avatar
Paul committed
44
45
46
47
48
    while(block_size < max_block_size and block_size < n)
        block_size *= 2;
    return block_size;
}

Paul's avatar
Paul committed
49
50
51
void reduce_sum(hipStream_t stream, const argument& result, const argument& arg)
{
    auto&& output_shape = result.get_shape();
Paul's avatar
Paul committed
52
    auto&& input_shape  = arg.get_shape();
Paul's avatar
Paul committed
53
    std::vector<std::size_t> reduce_lens;
Paul's avatar
Paul committed
54
55
56
57
58
59
60
61
62
63
    std::transform(output_shape.lens().begin(),
                   output_shape.lens().end(),
                   input_shape.lens().begin(),
                   std::back_inserter(reduce_lens),
                   [](auto x, auto y) -> std::size_t {
                       if(x == y)
                           return 1;
                       else
                           return y;
                   });
Paul's avatar
Paul committed
64
65
66
67
68
    shape reduce_slice{output_shape.type(), reduce_lens, input_shape.strides()};
    hip_visit_all(result, arg, reduce_slice)([&](auto output, auto input, auto reduce_shape) {
        auto nelements = result.get_shape().elements();
        auto relements = reduce_slice.elements();

Paul's avatar
Paul committed
69
        const std::size_t max_block_size = 1024;
Paul's avatar
Paul committed
70
        const std::size_t block_size     = compute_block_size(relements, max_block_size);
Paul's avatar
Paul committed
71
72
73
        gs_launch(stream, nelements * block_size, block_size)([=](auto i, auto idx) __device__ {
            auto base_idx = output.get_shape().multi(i / block_size);
            auto offset   = input.get_shape().index(base_idx);
Paul's avatar
Paul committed
74
            auto r = block_reduce<max_block_size>(idx, sum{}, 0, relements, [&](auto j) __device__ {
Paul's avatar
Paul committed
75
                return input.data()[reduce_shape.index(j) + offset];
Paul's avatar
Paul committed
76
            });
Paul's avatar
Paul committed
77
78
            if(idx.local == 0)
                output.data()[i / block_size] = r;
Paul's avatar
Paul committed
79
80
81
82
83
84
85
86
        });
    });
}

} // namespace device
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx