|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
4 | 4 |
|
| 5 | +from typing import TYPE_CHECKING, Tuple, Union |
| 6 | + |
5 | 7 | import numba |
6 | 8 |
|
7 | 9 | from cuda.cooperative.experimental._common import ( |
| 10 | + CUB_BLOCK_SCAN_ALGOS, |
| 11 | + CudaSharedMemConfig, |
| 12 | + dim3, |
8 | 13 | make_binary_tempfile, |
| 14 | + normalize_dim_param, |
9 | 15 | normalize_dtype_param, |
10 | 16 | ) |
11 | 17 | from cuda.cooperative.experimental._types import ( |
|
18 | 24 | Value, |
19 | 25 | ) |
20 | 26 |
|
| 27 | +if TYPE_CHECKING: |
| 28 | + import numpy as np |
| 29 | + |
| 30 | + |
| 31 | +TEMPLATE_PARAMETERS = [ |
| 32 | + TemplateParameter("KeyT"), |
| 33 | + TemplateParameter("BLOCK_DIM_X"), |
| 34 | + TemplateParameter("ITEMS_PER_THREAD"), |
| 35 | + TemplateParameter("ValueT"), |
| 36 | + TemplateParameter("RADIX_BITS"), |
| 37 | + TemplateParameter("MEMOIZE_OUTER_SCAN"), |
| 38 | + TemplateParameter("INNER_SCAN_ALGORITHM"), |
| 39 | + TemplateParameter("SMEM_CONFIG"), |
| 40 | + TemplateParameter("BLOCK_DIM_Y"), |
| 41 | + TemplateParameter("BLOCK_DIM_Z"), |
| 42 | +] |
| 43 | + |
| 44 | + |
| 45 | +METHOD_PARAMETERS_VARIANTS = [ |
| 46 | + [ |
| 47 | + Pointer(numba.uint8), |
| 48 | + DependentArray(Dependency("KeyT"), Dependency("ITEMS_PER_THREAD")), |
| 49 | + ], |
| 50 | + [ |
| 51 | + Pointer(numba.uint8), |
| 52 | + DependentArray(Dependency("KeyT"), Dependency("ITEMS_PER_THREAD")), |
| 53 | + Value(numba.int32), |
| 54 | + Value(numba.int32), |
| 55 | + ], |
| 56 | +] |
| 57 | + |
| 58 | + |
| 59 | +# N.B. In order to support multi-dimensional block dimensions, we have to |
| 60 | +# defaults for all the template parameters preceding the final Y and |
| 61 | +# Z dimensions. This will be improved in the future, allowing users |
| 62 | +# to provide overrides for the default values. |
| 63 | + |
| 64 | +TEMPLATE_PARAMETER_DEFAULTS = { |
| 65 | + "ValueT": "::cub::NullType", # Indicates keys-only sort |
| 66 | + "RADIX_BITS": 4, |
| 67 | + "MEMOIZE_OUTER_SCAN": "true", |
| 68 | + "INNER_SCAN_ALGORITHM": CUB_BLOCK_SCAN_ALGOS["warp_scans"], |
| 69 | + "SMEM_CONFIG": str(CudaSharedMemConfig.BankSizeFourByte), |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +def _get_template_parameter_specializations( |
| 74 | + dtype: numba.types.Type, dim: dim3, items_per_thread: int |
| 75 | +) -> dict: |
| 76 | + """ |
| 77 | + Returns a dictionary of template parameter specializations for the block |
| 78 | + radix sort algorithm. |
| 79 | +
|
| 80 | + Args: |
| 81 | + dtype: Supplies the Numba data type. |
| 82 | +
|
| 83 | + dim: Supplies the block dimensions. |
| 84 | +
|
| 85 | + items_per_thread: Supplies the number of items each thread owns. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + A dictionary of template parameter specializations. |
| 89 | + """ |
| 90 | + specialization = { |
| 91 | + "KeyT": dtype, |
| 92 | + "BLOCK_DIM_X": dim[0], |
| 93 | + "ITEMS_PER_THREAD": items_per_thread, |
| 94 | + "BLOCK_DIM_Y": dim[1], |
| 95 | + "BLOCK_DIM_Z": dim[2], |
| 96 | + } |
| 97 | + |
| 98 | + specialization.update(TEMPLATE_PARAMETER_DEFAULTS) |
| 99 | + |
| 100 | + return specialization |
| 101 | + |
| 102 | + |
| 103 | +def _radix_sort( |
| 104 | + dtype: Union[str, type, "np.dtype", "numba.types.Type"], |
| 105 | + threads_per_block: Union[int, Tuple[int, int], Tuple[int, int, int], dim3], |
| 106 | + items_per_thread: int, |
| 107 | + descending: bool, |
| 108 | +) -> Invocable: |
| 109 | + dim = normalize_dim_param(threads_per_block) |
| 110 | + dtype = normalize_dtype_param(dtype) |
| 111 | + |
| 112 | + method_name = "SortDescending" if descending else "Sort" |
| 113 | + template = Algorithm( |
| 114 | + "BlockRadixSort", |
| 115 | + method_name, |
| 116 | + "block_radix_sort", |
| 117 | + ["cub/block/block_radix_sort.cuh"], |
| 118 | + TEMPLATE_PARAMETERS, |
| 119 | + METHOD_PARAMETERS_VARIANTS, |
| 120 | + ) |
| 121 | + specialization = template.specialize( |
| 122 | + _get_template_parameter_specializations(dtype, dim, items_per_thread) |
| 123 | + ) |
| 124 | + return Invocable( |
| 125 | + temp_files=[ |
| 126 | + make_binary_tempfile(ltoir, ".ltoir") |
| 127 | + for ltoir in specialization.get_lto_ir() |
| 128 | + ], |
| 129 | + temp_storage_bytes=specialization.get_temp_storage_bytes(), |
| 130 | + algorithm=specialization, |
| 131 | + ) |
| 132 | + |
21 | 133 |
|
22 | 134 | def radix_sort_keys(dtype, threads_per_block, items_per_thread): |
23 | 135 | """Performs an ascending block-wide radix sort over a :ref:`blocked arrangement <flexible-data-arrangement>` of keys. |
@@ -47,54 +159,17 @@ def radix_sort_keys(dtype, threads_per_block, items_per_thread): |
47 | 159 | ``{ [0, 1, 2, 3], [4, 5, 6, 7], ..., [508, 509, 510, 511] }``. |
48 | 160 |
|
49 | 161 | Args: |
50 | | - dtype: Numba data type of the keys to be sorted |
51 | | - threads_per_block: The number of threads in a block |
| 162 | + dtype: Data type of the keys to be sorted |
| 163 | +
|
| 164 | + threads_per_block: The number of threads in a block, either an integer |
| 165 | + or a tuple of 2 or 3 integers |
| 166 | +
|
52 | 167 | items_per_thread: The number of items each thread owns |
53 | 168 |
|
54 | 169 | Returns: |
55 | 170 | A callable object that can be linked to and invoked from a CUDA kernel |
56 | 171 | """ |
57 | | - # Normalize the dtype parameter. |
58 | | - dtype = normalize_dtype_param(dtype) |
59 | | - |
60 | | - template = Algorithm( |
61 | | - "BlockRadixSort", |
62 | | - "Sort", |
63 | | - "block_radix_sort", |
64 | | - ["cub/block/block_radix_sort.cuh"], |
65 | | - [ |
66 | | - TemplateParameter("KeyT"), |
67 | | - TemplateParameter("BLOCK_DIM_X"), |
68 | | - TemplateParameter("ITEMS_PER_THREAD"), |
69 | | - ], |
70 | | - [ |
71 | | - [ |
72 | | - Pointer(numba.uint8), |
73 | | - DependentArray(Dependency("KeyT"), Dependency("ITEMS_PER_THREAD")), |
74 | | - ], |
75 | | - [ |
76 | | - Pointer(numba.uint8), |
77 | | - DependentArray(Dependency("KeyT"), Dependency("ITEMS_PER_THREAD")), |
78 | | - Value(numba.int32), |
79 | | - Value(numba.int32), |
80 | | - ], |
81 | | - ], |
82 | | - ) |
83 | | - specialization = template.specialize( |
84 | | - { |
85 | | - "KeyT": dtype, |
86 | | - "BLOCK_DIM_X": threads_per_block, |
87 | | - "ITEMS_PER_THREAD": items_per_thread, |
88 | | - } |
89 | | - ) |
90 | | - return Invocable( |
91 | | - temp_files=[ |
92 | | - make_binary_tempfile(ltoir, ".ltoir") |
93 | | - for ltoir in specialization.get_lto_ir() |
94 | | - ], |
95 | | - temp_storage_bytes=specialization.get_temp_storage_bytes(), |
96 | | - algorithm=specialization, |
97 | | - ) |
| 172 | + return _radix_sort(dtype, threads_per_block, items_per_thread, descending=False) |
98 | 173 |
|
99 | 174 |
|
100 | 175 | def radix_sort_keys_descending(dtype, threads_per_block, items_per_thread): |
@@ -125,49 +200,14 @@ def radix_sort_keys_descending(dtype, threads_per_block, items_per_thread): |
125 | 200 | ``{ [511, 510, 509, 508], [507, 506, 505, 504], ..., [3, 2, 1, 0] }``. |
126 | 201 |
|
127 | 202 | Args: |
128 | | - dtype: Numba data type of the keys to be sorted |
129 | | - threads_per_block: The number of threads in a block |
| 203 | + dtype: Data type of the keys to be sorted |
| 204 | +
|
| 205 | + threads_per_block: The number of threads in a block, either an integer |
| 206 | + or a tuple of 2 or 3 integers |
| 207 | +
|
130 | 208 | items_per_thread: The number of items each thread owns |
131 | 209 |
|
132 | 210 | Returns: |
133 | 211 | A callable object that can be linked to and invoked from a CUDA kernel |
134 | 212 | """ |
135 | | - template = Algorithm( |
136 | | - "BlockRadixSort", |
137 | | - "SortDescending", |
138 | | - "block_radix_sort", |
139 | | - ["cub/block/block_radix_sort.cuh"], |
140 | | - [ |
141 | | - TemplateParameter("KeyT"), |
142 | | - TemplateParameter("BLOCK_DIM_X"), |
143 | | - TemplateParameter("ITEMS_PER_THREAD"), |
144 | | - ], |
145 | | - [ |
146 | | - [ |
147 | | - Pointer(numba.uint8), |
148 | | - DependentArray(Dependency("KeyT"), Dependency("ITEMS_PER_THREAD")), |
149 | | - ], |
150 | | - [ |
151 | | - Pointer(numba.uint8), |
152 | | - DependentArray(Dependency("KeyT"), Dependency("ITEMS_PER_THREAD")), |
153 | | - Value(numba.int32), |
154 | | - Value(numba.int32), |
155 | | - ], |
156 | | - ], |
157 | | - ) |
158 | | - specialization = template.specialize( |
159 | | - { |
160 | | - "KeyT": dtype, |
161 | | - "BLOCK_DIM_X": threads_per_block, |
162 | | - "ITEMS_PER_THREAD": items_per_thread, |
163 | | - } |
164 | | - ) |
165 | | - |
166 | | - return Invocable( |
167 | | - temp_files=[ |
168 | | - make_binary_tempfile(ltoir, ".ltoir") |
169 | | - for ltoir in specialization.get_lto_ir() |
170 | | - ], |
171 | | - temp_storage_bytes=specialization.get_temp_storage_bytes(), |
172 | | - algorithm=specialization, |
173 | | - ) |
| 213 | + return _radix_sort(dtype, threads_per_block, items_per_thread, descending=True) |
0 commit comments