Repository navigation
Expand file tree
/
Copy pathsort.cpp
More file actions
230 lines (191 loc) · 8.06 KB
/
Copy pathsort.cpp
File metadata and controls
230 lines (191 loc) · 8.06 KB
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
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2024 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===--------------------------------------------------------------------===//
///
/// \file
/// This file defines functions of dpctl.tensor._tensor_sorting_impl
/// extension.
//===--------------------------------------------------------------------===//
#include "dpctl4pybind11.hpp"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <sycl/sycl.hpp>
#include "utils/math_utils.hpp"
#include "utils/memory_overlap.hpp"
#include "utils/output_validation.hpp"
#include "utils/type_dispatch.hpp"
#include "kernels/sorting/sort.hpp"
#include "rich_comparisons.hpp"
#include "sort.hpp"
namespace td_ns = dpctl::tensor::type_dispatch;
namespace dpctl
{
namespace tensor
{
namespace py_internal
{
template <typename sorting_contig_impl_fnT>
std::pair<sycl::event, sycl::event>
py_sort(const dpctl::tensor::usm_ndarray &src,
const int trailing_dims_to_sort,
const dpctl::tensor::usm_ndarray &dst,
sycl::queue &exec_q,
const std::vector<sycl::event> &depends,
const sorting_contig_impl_fnT &stable_sort_contig_fns)
{
int src_nd = src.get_ndim();
int dst_nd = dst.get_ndim();
if (src_nd != dst_nd) {
throw py::value_error("The input and output arrays must have "
"the same array ranks");
}
int iteration_nd = src_nd - trailing_dims_to_sort;
if (trailing_dims_to_sort <= 0 || iteration_nd < 0) {
throw py::value_error("Trailing_dim_to_sort must be positive, but no "
"greater than rank of the array being sorted");
}
const py::ssize_t *src_shape_ptr = src.get_shape_raw();
const py::ssize_t *dst_shape_ptr = dst.get_shape_raw();
bool same_shapes = true;
size_t iter_nelems(1);
for (int i = 0; same_shapes && (i < iteration_nd); ++i) {
auto src_shape_i = src_shape_ptr[i];
same_shapes = same_shapes && (src_shape_i == dst_shape_ptr[i]);
iter_nelems *= static_cast<size_t>(src_shape_i);
}
size_t sort_nelems(1);
for (int i = iteration_nd; same_shapes && (i < src_nd); ++i) {
auto src_shape_i = src_shape_ptr[i];
same_shapes = same_shapes && (src_shape_i == dst_shape_ptr[i]);
sort_nelems *= static_cast<size_t>(src_shape_i);
}
if (!same_shapes) {
throw py::value_error(
"Destination shape does not match the input shape");
}
if (!dpctl::utils::queues_are_compatible(exec_q, {src, dst})) {
throw py::value_error(
"Execution queue is not compatible with allocation queues");
}
dpctl::tensor::validation::CheckWritable::throw_if_not_writable(dst);
if ((iter_nelems == 0) || (sort_nelems == 0)) {
// Nothing to do
return std::make_pair(sycl::event(), sycl::event());
}
// check that dst and src do not overlap
auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
if (overlap(src, dst)) {
throw py::value_error("Arrays index overlapping segments of memory");
}
dpctl::tensor::validation::AmpleMemory::throw_if_not_ample(
dst, sort_nelems * iter_nelems);
int src_typenum = src.get_typenum();
int dst_typenum = dst.get_typenum();
const auto &array_types = td_ns::usm_ndarray_types();
int src_typeid = array_types.typenum_to_lookup_id(src_typenum);
int dst_typeid = array_types.typenum_to_lookup_id(dst_typenum);
if (src_typeid != dst_typeid) {
throw py::value_error("Both input arrays must have "
"the same value data type");
}
// handle special case when both reduction and iteration are 1D contiguous
bool is_src_c_contig = src.is_c_contiguous();
bool is_dst_c_contig = dst.is_c_contiguous();
if (is_src_c_contig && is_dst_c_contig) {
using dpctl::tensor::kernels::stable_sort_axis1_contig_impl;
static constexpr py::ssize_t zero_offset = py::ssize_t(0);
auto fn = stable_sort_contig_fns[src_typeid];
sycl::event comp_ev =
fn(exec_q, iter_nelems, sort_nelems, src.get_data(), dst.get_data(),
zero_offset, zero_offset, zero_offset, zero_offset, depends);
sycl::event keep_args_alive_ev =
dpctl::utils::keep_args_alive(exec_q, {src, dst}, {comp_ev});
return std::make_pair(keep_args_alive_ev, comp_ev);
}
throw py::value_error(
"Both source and destination arrays must be C-contiguous");
}
using dpctl::tensor::kernels::sort_contig_fn_ptr_t;
static sort_contig_fn_ptr_t
ascending_sort_contig_dispatch_vector[td_ns::num_types];
static sort_contig_fn_ptr_t
descending_sort_contig_dispatch_vector[td_ns::num_types];
template <typename fnT, typename argTy> struct AscendingSortContigFactory
{
fnT get()
{
using Comp = typename AscendingSorter<argTy>::type;
using dpctl::tensor::kernels::stable_sort_axis1_contig_impl;
return stable_sort_axis1_contig_impl<argTy, Comp>;
}
};
template <typename fnT, typename argTy> struct DescendingSortContigFactory
{
fnT get()
{
using Comp = typename DescendingSorter<argTy>::type;
using dpctl::tensor::kernels::stable_sort_axis1_contig_impl;
return stable_sort_axis1_contig_impl<argTy, Comp>;
}
};
void init_sort_dispatch_vectors(void)
{
using dpctl::tensor::kernels::sort_contig_fn_ptr_t;
td_ns::DispatchVectorBuilder<sort_contig_fn_ptr_t,
AscendingSortContigFactory, td_ns::num_types>
dtv1;
dtv1.populate_dispatch_vector(ascending_sort_contig_dispatch_vector);
td_ns::DispatchVectorBuilder<sort_contig_fn_ptr_t,
DescendingSortContigFactory, td_ns::num_types>
dtv2;
dtv2.populate_dispatch_vector(descending_sort_contig_dispatch_vector);
}
void init_sort_functions(py::module_ m)
{
dpctl::tensor::py_internal::init_sort_dispatch_vectors();
auto py_sort_ascending = [](const dpctl::tensor::usm_ndarray &src,
const int trailing_dims_to_sort,
const dpctl::tensor::usm_ndarray &dst,
sycl::queue &exec_q,
const std::vector<sycl::event> &depends)
-> std::pair<sycl::event, sycl::event> {
return dpctl::tensor::py_internal::py_sort(
src, trailing_dims_to_sort, dst, exec_q, depends,
dpctl::tensor::py_internal::ascending_sort_contig_dispatch_vector);
};
m.def("_sort_ascending", py_sort_ascending, py::arg("src"),
py::arg("trailing_dims_to_sort"), py::arg("dst"),
py::arg("sycl_queue"), py::arg("depends") = py::list());
auto py_sort_descending = [](const dpctl::tensor::usm_ndarray &src,
const int trailing_dims_to_sort,
const dpctl::tensor::usm_ndarray &dst,
sycl::queue &exec_q,
const std::vector<sycl::event> &depends)
-> std::pair<sycl::event, sycl::event> {
return dpctl::tensor::py_internal::py_sort(
src, trailing_dims_to_sort, dst, exec_q, depends,
dpctl::tensor::py_internal::descending_sort_contig_dispatch_vector);
};
m.def("_sort_descending", py_sort_descending, py::arg("src"),
py::arg("trailing_dims_to_sort"), py::arg("dst"),
py::arg("sycl_queue"), py::arg("depends") = py::list());
return;
}
} // end of namespace py_internal
} // end of namespace tensor
} // end of namespace dpctl