File: benchmark_block_sort.parallel.hpp

package info (click to toggle)
rocprim 6.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,428 kB
  • sloc: cpp: 153,383; python: 1,397; sh: 404; xml: 217; makefile: 119
file content (296 lines) | stat: -rw-r--r-- 11,880 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// MIT License
//
// Copyright (c) 2019-2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef ROCPRIM_BENCHMARK_BLOCK_SORT_PARALLEL_HPP_
#define ROCPRIM_BENCHMARK_BLOCK_SORT_PARALLEL_HPP_

#include "benchmark_utils.hpp"

// Google Benchmark
#include <benchmark/benchmark.h>

// HIP API
#include <hip/hip_runtime.h>

// rocPRIM
#include <rocprim/block/block_load_func.hpp>
#include <rocprim/block/block_sort.hpp>
#include <rocprim/block/block_store_func.hpp>
#include <rocprim/type_traits.hpp>

#include <string>
#include <vector>

#include <cstddef>

template<class KeyType,
         class ValueType,
         unsigned int                  BlockSize,
         unsigned int                  ItemsPerThread,
         rocprim::block_sort_algorithm block_sort_algorithm,
         std::enable_if_t<std::is_same<ValueType, rocprim::empty_type>::value, bool> = true>
__global__ __launch_bounds__(BlockSize) void sort_kernel(const KeyType* input, KeyType* output)
{
    const unsigned int lid          = threadIdx.x;
    const unsigned int block_offset = blockIdx.x * ItemsPerThread * BlockSize;

    KeyType keys[ItemsPerThread];
    rocprim::block_load_direct_striped<BlockSize>(lid, input + block_offset, keys);

    rocprim::block_sort<KeyType, BlockSize, ItemsPerThread, ValueType, block_sort_algorithm> bsort;
    bsort.sort(keys);

    rocprim::block_store_direct_blocked(lid, output + block_offset, keys);
}

template<class KeyType,
         class ValueType,
         unsigned int                  BlockSize,
         unsigned int                  ItemsPerThread,
         rocprim::block_sort_algorithm block_sort_algorithm,
         std::enable_if_t<!std::is_same<ValueType, rocprim::empty_type>::value, bool> = true>
__global__ __launch_bounds__(BlockSize) void sort_kernel(const KeyType* input, KeyType* output)
{
    const unsigned int lid          = threadIdx.x;
    const unsigned int block_offset = blockIdx.x * ItemsPerThread * BlockSize;

    KeyType   keys[ItemsPerThread];
    ValueType values[ItemsPerThread];
    rocprim::block_load_direct_striped<BlockSize>(lid, input + block_offset, keys);

    ROCPRIM_UNROLL
    for(unsigned int item = 0; item < ItemsPerThread; ++item)
    {
        values[item] = block_offset + lid * ItemsPerThread + item;
    }

    rocprim::block_sort<KeyType, BlockSize, ItemsPerThread, ValueType, block_sort_algorithm> bsort;
    bsort.sort(keys, values);

    ROCPRIM_UNROLL
    for(unsigned int item = 0; item < ItemsPerThread; ++item)
    {
        keys[item] = keys[item] + static_cast<KeyType>(values[item]);
    }

    rocprim::block_store_direct_blocked(lid, output + block_offset, keys);
}

template<class KeyType,
         class ValueType,
         unsigned int                  BlockSize,
         unsigned int                  ItemsPerThread,
         rocprim::block_sort_algorithm block_sort_algorithm>
__global__ __launch_bounds__(BlockSize) void stable_sort_kernel(const KeyType* input,
                                                                KeyType*       output)
{
    const unsigned int lid          = threadIdx.x;
    const unsigned int block_offset = blockIdx.x * ItemsPerThread * BlockSize;

    KeyType keys[ItemsPerThread];
    rocprim::block_load_direct_striped<BlockSize>(lid, input + block_offset, keys);

    using stable_key_type = rocprim::tuple<KeyType, unsigned int>;
    stable_key_type stable_keys[ItemsPerThread];

    ROCPRIM_UNROLL
    for(unsigned int item = 0; item < ItemsPerThread; ++item)
    {
        stable_keys[item] = rocprim::make_tuple(keys[item], ItemsPerThread * lid + item);
    }

    // Special comparison that preserves relative order of equal keys
    auto stable_compare_function
        = [](const stable_key_type& a, const stable_key_type& b) mutable -> bool
    {
        const bool ab = rocprim::less<KeyType>{}(rocprim::get<0>(a), rocprim::get<0>(b));
        return ab
               || (!rocprim::less<KeyType>{}(rocprim::get<0>(b), rocprim::get<0>(a))
                   && (rocprim::get<1>(a) < rocprim::get<1>(b)));
    };

    rocprim::block_sort<stable_key_type,
                        BlockSize,
                        ItemsPerThread,
                        rocprim::empty_type,
                        block_sort_algorithm>
        bsort;
    bsort.sort(stable_keys, stable_compare_function);

    ROCPRIM_UNROLL
    for(unsigned int item = 0; item < ItemsPerThread; ++item)
    {
        keys[item] = rocprim::get<0>(stable_keys[item]);
    }

    rocprim::block_store_direct_blocked(lid, output + block_offset, keys);
}

template<class KeyType,
         class ValueType,
         unsigned int                  BlockSize,
         unsigned int                  ItemsPerThread,
         rocprim::block_sort_algorithm block_sort_algorithm,
         const bool                    stable = false>
struct block_sort_benchmark : public config_autotune_interface
{
private:
    static constexpr bool with_values = !std::is_same<ValueType, rocprim::empty_type>::value;
    static constexpr unsigned int items_per_block = BlockSize * ItemsPerThread;

    static const char* get_block_sort_method_name(rocprim::block_sort_algorithm alg)
    {
        switch(alg)
        {
            case rocprim::block_sort_algorithm::merge_sort: return "merge_sort";
            case rocprim::block_sort_algorithm::stable_merge_sort: return "stable_merge_sort";
            case rocprim::block_sort_algorithm::bitonic_sort:
                return "bitonic_sort";
                // Not using `default: ...` because it kills effectiveness of -Wswitch
        }
        return "unknown_algorithm";
    }

public:
    std::string sort_key() const override
    {
        using namespace std::string_literals;
        return std::string((with_values ? "_pairs"s : "_keys"s) + (stable ? "_stable"s : ""s)
                           + pad_string(std::to_string(items_per_block), 5) + ", " + name());
    }

    std::string name() const override
    {
        return bench_naming::format_name(
            "{lvl:block,algo:sort,key_type:" + std::string(Traits<KeyType>::name()) + ",value_type:"
            + std::string(Traits<ValueType>::name()) + ",stable:" + (stable ? "true" : "false")
            + ",cfg:{bs:" + std::to_string(BlockSize) + ",ipt:" + std::to_string(ItemsPerThread)
            + ",method:" + std::string(get_block_sort_method_name(block_sort_algorithm)) + "}}");
    }

    static constexpr unsigned int batch_size        = 10;
    static constexpr unsigned int warmup_size       = 5;
    static constexpr bool         debug_synchronous = false;

    static auto dispatch_block_sort(std::false_type /*stable_sort*/,
                             size_t            size,
                             const hipStream_t stream,
                             KeyType*          d_input,
                             KeyType*          d_output)
    {
        hipLaunchKernelGGL(
            HIP_KERNEL_NAME(
                sort_kernel<KeyType, ValueType, BlockSize, ItemsPerThread, block_sort_algorithm>),
            dim3(size / items_per_block),
            dim3(BlockSize),
            0,
            stream,
            d_input,
            d_output);
    }

    static auto dispatch_block_sort(std::true_type /*stable_sort*/,
                             size_t            size,
                             const hipStream_t stream,
                             KeyType*          d_input,
                             KeyType*          d_output)
    {
        hipLaunchKernelGGL(HIP_KERNEL_NAME(stable_sort_kernel<KeyType,
                                                              ValueType,
                                                              BlockSize,
                                                              ItemsPerThread,
                                                              block_sort_algorithm>),
                           dim3(size / items_per_block),
                           dim3(BlockSize),
                           0,
                           stream,
                           d_input,
                           d_output);
    }

    void run(benchmark::State&   state,
             size_t              bytes,
             const managed_seed& seed,
             hipStream_t         stream) const override
    {
        // Calculate the number of elements N
        size_t N = bytes / sizeof(KeyType);

        const auto size = items_per_block * ((N + items_per_block - 1) / items_per_block);

        std::vector<KeyType> input = get_random_data<KeyType>(size,
                                                              generate_limits<KeyType>::min(),
                                                              generate_limits<KeyType>::max(),
                                                              seed.get_0());

        KeyType* d_input;
        KeyType* d_output;
        HIP_CHECK(hipMalloc(reinterpret_cast<void**>(&d_input), size * sizeof(KeyType)));
        HIP_CHECK(hipMalloc(reinterpret_cast<void**>(&d_output), size * sizeof(KeyType)));
        HIP_CHECK(hipMemcpy(d_input, input.data(), size * sizeof(KeyType), hipMemcpyHostToDevice));
        HIP_CHECK(hipDeviceSynchronize());

        static constexpr auto stable_tag = rocprim::detail::bool_constant<stable>{};

        // HIP events creation
        hipEvent_t start, stop;
        HIP_CHECK(hipEventCreate(&start));
        HIP_CHECK(hipEventCreate(&stop));

        // Run
        for(auto _ : state)
        {
            // Record start event
            HIP_CHECK(hipEventRecord(start, stream));

            for(size_t i = 0; i < batch_size; i++)
            {
                dispatch_block_sort(stable_tag, size, stream, d_input, d_output);
            }
            HIP_CHECK(hipGetLastError());

            // Record stop event and wait until it completes
            HIP_CHECK(hipEventRecord(stop, stream));
            HIP_CHECK(hipEventSynchronize(stop));

            float elapsed_mseconds;
            HIP_CHECK(hipEventElapsedTime(&elapsed_mseconds, start, stop));
            state.SetIterationTime(elapsed_mseconds / 1000);
        }

        // Destroy HIP events
        HIP_CHECK(hipEventDestroy(start));
        HIP_CHECK(hipEventDestroy(stop));

        state.SetBytesProcessed(state.iterations() * batch_size * size * sizeof(KeyType));
        state.SetItemsProcessed(state.iterations() * batch_size * size);

        state.counters["sorted_size"] = benchmark::Counter(BlockSize * ItemsPerThread,
                                                           benchmark::Counter::kDefaults,
                                                           benchmark::Counter::OneK::kIs1024);

        HIP_CHECK(hipFree(d_input));
        HIP_CHECK(hipFree(d_output));
    }
};

#endif // ROCPRIM_BENCHMARK_BLOCK_SORT_PARALLEL_HPP_