spla
Loading...
Searching...
No Matches
cl_sort_by_key.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/JetBrains-Research/spla */
4/**********************************************************************************/
5/* MIT License */
6/* */
7/* Copyright (c) 2023 SparseLinearAlgebra */
8/* */
9/* Permission is hereby granted, free of charge, to any person obtaining a copy */
10/* of this software and associated documentation files (the "Software"), to deal */
11/* in the Software without restriction, including without limitation the rights */
12/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13/* copies of the Software, and to permit persons to whom the Software is */
14/* furnished to do so, subject to the following conditions: */
15/* */
16/* The above copyright notice and this permission notice shall be included in all */
17/* copies or substantial portions of the Software. */
18/* */
19/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25/* SOFTWARE. */
26/**********************************************************************************/
27
28#ifndef SPLA_CL_SORT_BY_KEY_HPP
29#define SPLA_CL_SORT_BY_KEY_HPP
30
32#include <opencl/cl_alloc.hpp>
38
39#include <cmath>
40
41namespace spla {
42
43 template<typename T>
44 bool cl_sort_by_key_bitonic(cl::CommandQueue& queue, cl::Buffer& keys, cl::Buffer& values, uint size) {
45 if (size <= 1) {
46 LOG_MSG(Status::Ok, "nothing to do");
47 return true;
48 }
49
50 auto* acc = get_acc_cl();
51
52 const uint pair_size = sizeof(uint) + sizeof(T);
53 const uint local_size = floor_to_pow2(acc->get_max_local_mem() / pair_size);
54 const uint max_treads_per_block = std::min(acc->get_max_wgs(), local_size / 2u);
55
56 assert(local_size > 2);
57
58 CLProgramBuilder builder;
59 builder.set_name("sort_bitonic")
60 .add_type("TYPE", get_ttype<T>().template as<Type>())
61 .add_define("BLOCK_SIZE", local_size)
62 .set_source(source_sort_bitonic)
63 .acquire();
64
65 const uint local_limit = capped_group_size(builder, acc, "bitonic_sort_local");
66 const uint global_limit = capped_group_size(builder, acc, "bitonic_sort_global");
67 if (local_limit == 0 || global_limit == 0) {
68 return false;
69 }
70
71 auto kernel_local = builder.make_kernel("bitonic_sort_local");
72 kernel_local.setArg(0, keys);
73 kernel_local.setArg(1, values);
74 kernel_local.setArg(2, size);
75
76 if (size <= local_size) {
77 const uint wave_size = acc->get_wave_size();
78 const uint n_threads = std::min(align(std::min(size, max_treads_per_block), wave_size), local_limit);
79
80 cl::NDRange global(n_threads);
81 cl::NDRange local(n_threads);
82 queue.enqueueNDRangeKernel(kernel_local, cl::NDRange(), global, local);
83 return true;
84 }
85
86 const uint n_groups = div_up(size, local_size);
87 const uint block_threads = std::min(max_treads_per_block, local_limit);
88
89 cl::NDRange step_pre_sort_global(block_threads * n_groups);
90 cl::NDRange step_pre_sort_local(block_threads);
91 queue.enqueueNDRangeKernel(kernel_local, cl::NDRange(), step_pre_sort_global, step_pre_sort_local);
92
93 auto kernel_global = builder.make_kernel("bitonic_sort_global");
94 kernel_global.setArg(0, keys);
95 kernel_global.setArg(1, values);
96 kernel_global.setArg(2, size);
97 kernel_global.setArg(3, uint(local_size * 2));
98
99 cl::NDRange step_final_global(global_limit);
100 cl::NDRange step_final_local(global_limit);
101 queue.enqueueNDRangeKernel(kernel_global, cl::NDRange(), step_final_global, step_final_local);
102 return true;
103 }
104
105 template<typename T>
106 void cl_sort_by_key_radix(cl::CommandQueue& queue, cl::Buffer& keys, cl::Buffer& values, uint n, CLAlloc* tmp_alloc, uint max_key = 0xffffffff) {
107 if (n <= 1) {
108 LOG_MSG(Status::Ok, "nothing to do");
109 return;
110 }
111
112 const uint BITS_COUNT = 4;
113 const uint BITS_VALS = 1 << BITS_COUNT;
114 const uint BITS_MASK = BITS_VALS - 1;
115
116 auto* cl_acc = get_acc_cl();
117 const uint block_size = cl_acc->get_default_wgs();
118
119 CLProgramBuilder builder;
120 builder.set_name("radix_sort")
121 .add_define("BLOCK_SIZE", block_size)
122 .add_define("BITS_VALS", BITS_VALS)
123 .add_define("BITS_MASK", BITS_MASK)
124 .add_type("TYPE", get_ttype<T>().template as<Type>())
125 .set_source(source_sort_radix)
126 .acquire();
127
128 const uint n_treads_total = align(n, block_size);
129 const uint n_groups = div_up(n, block_size);
130 const uint n_blocks_sizes = n_groups * BITS_VALS;
131
132 cl::Buffer cl_temp_keys;
133 cl::Buffer cl_temp_values;
134 cl_acc->get_alloc_general()->alloc_paired(sizeof(uint) * n, sizeof(T) * n, cl_temp_keys, cl_temp_values);
135
136 cl::Buffer cl_offsets = tmp_alloc->alloc(sizeof(uint) * n);
137 cl::Buffer cl_blocks_size = tmp_alloc->alloc(sizeof(uint) * n_blocks_sizes);
138
139 auto kernel_local = builder.make_kernel("radix_sort_local");
140 auto kernel_scatter = builder.make_kernel("radix_sort_scatter");
141
142 const uint bits_in_max_key = static_cast<uint>(std::floor(std::log2(float(max_key)))) + 1;
143 const uint bits_aligned = align(bits_in_max_key, BITS_COUNT);
144 const uint max_bits = std::min(32u, bits_aligned);
145
146 cl::Buffer in_keys = keys;
147 cl::Buffer in_values = values;
148 cl::Buffer out_keys = cl_temp_keys;
149 cl::Buffer out_values = cl_temp_values;
150
151 for (uint shift = 0; shift <= max_bits - BITS_COUNT; shift += BITS_COUNT) {
152 cl::NDRange global(n_treads_total);
153 cl::NDRange local(block_size);
154
155 kernel_local.setArg(0, in_keys);
156 kernel_local.setArg(1, cl_offsets);
157 kernel_local.setArg(2, cl_blocks_size);
158 kernel_local.setArg(3, n);
159 kernel_local.setArg(4, shift);
160 queue.enqueueNDRangeKernel(kernel_local, cl::NDRange(), global, local);
161
162 cl_exclusive_scan<uint>(queue, cl_blocks_size, n_blocks_sizes, PLUS_UINT.template cast_safe<TOpBinary<uint, uint, uint>>(), tmp_alloc);
163
164 kernel_scatter.setArg(0, in_keys);
165 kernel_scatter.setArg(1, in_values);
166 kernel_scatter.setArg(2, out_keys);
167 kernel_scatter.setArg(3, out_values);
168 kernel_scatter.setArg(4, cl_offsets);
169 kernel_scatter.setArg(5, cl_blocks_size);
170 kernel_scatter.setArg(6, n);
171 kernel_scatter.setArg(7, shift);
172 queue.enqueueNDRangeKernel(kernel_scatter, cl::NDRange(), global, local);
173
174 std::swap(in_keys, out_keys);
175 std::swap(in_values, out_values);
176 }
177
178 keys = in_keys;
179 values = in_values;
180 }
181
182 template<typename T>
183 void cl_sort_by_key(cl::CommandQueue& queue, cl::Buffer& keys, cl::Buffer& values, uint n, CLAlloc* tmp_alloc, uint max_key = 0xffffffff) {
184 if (n <= 1) {
185 LOG_MSG(Status::Ok, "nothing to do");
186 return;
187 }
188
189 const uint sort_switch = 2u << 14u;
190
191 if (n <= sort_switch && cl_sort_by_key_bitonic<T>(queue, keys, values, n)) {
192 return;
193 }
194 cl_sort_by_key_radix<T>(queue, keys, values, n, tmp_alloc, max_key);
195 }
196
197}// namespace spla
198
199#endif//SPLA_CL_SORT_BY_KEY_HPP
Base class for any device-local opencl buffer allocator.
Definition cl_alloc.hpp:39
virtual cl::Buffer alloc(std::size_t size)=0
Runtime opencl program builder.
Definition cl_program_builder.hpp:55
CLProgramBuilder & add_define(const char *define, int value)
Definition cl_program_builder.cpp:42
CLProgramBuilder & set_name(const char *name)
Definition cl_program_builder.cpp:38
CLProgramBuilder & add_type(const char *alias, const ref_ptr< Type > &type)
Definition cl_program_builder.cpp:46
cl::Kernel make_kernel(const char *name)
Definition cl_program_builder.hpp:67
CLProgramBuilder & set_source(const char *source)
Definition cl_program_builder.cpp:62
void acquire()
Definition cl_program_builder.cpp:86
Definition top.hpp:174
uint capped_group_size(CLProgramBuilder &builder, CLAccelerator *acc, const char *kernel_name)
Definition cl_program_builder.cpp:79
ref_ptr< OpBinary > PLUS_UINT
Definition op.cpp:76
std::uint32_t uint
Library index and size type.
Definition config.hpp:56
#define LOG_MSG(status, msg)
Definition logger.hpp:66
Definition algorithm.hpp:37
void cl_sort_by_key(cl::CommandQueue &queue, cl::Buffer &keys, cl::Buffer &values, uint n, CLAlloc *tmp_alloc, uint max_key=0xffffffff)
Definition cl_sort_by_key.hpp:183
void cl_sort_by_key_radix(cl::CommandQueue &queue, cl::Buffer &keys, cl::Buffer &values, uint n, CLAlloc *tmp_alloc, uint max_key=0xffffffff)
Definition cl_sort_by_key.hpp:106
void cl_exclusive_scan(cl::CommandQueue &queue, cl::Buffer &values, uint n, const ref_ptr< TOpBinary< T, T, T > > &op, CLAlloc *tmp_alloc)
Definition cl_prefix_sum.hpp:39
bool cl_sort_by_key_bitonic(cl::CommandQueue &queue, cl::Buffer &keys, cl::Buffer &values, uint size)
Definition cl_sort_by_key.hpp:44