vector-qsort 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vector_qsort-0.1.0/.pytest_cache/README.md +8 -0
- vector_qsort-0.1.0/CMakeLists.txt +45 -0
- vector_qsort-0.1.0/LICENSE +21 -0
- vector_qsort-0.1.0/PKG-INFO +61 -0
- vector_qsort-0.1.0/README.md +51 -0
- vector_qsort-0.1.0/bench/bench_sort.py +94 -0
- vector_qsort-0.1.0/binding.cc +50 -0
- vector_qsort-0.1.0/include/vqsort.h +45 -0
- vector_qsort-0.1.0/pyproject.toml +32 -0
- vector_qsort-0.1.0/test/test_sort.py +78 -0
- vector_qsort-0.1.0/vector_qsort/__init__.py +37 -0
- vector_qsort-0.1.0/vector_qsort/__pycache__/__init__.cpython-314.pyc +0 -0
- vector_qsort-0.1.0/vector_qsort/py.typed +1 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# pytest cache directory #
|
|
2
|
+
|
|
3
|
+
This directory contains data from the pytest's cache plugin,
|
|
4
|
+
which provides the `--lf` and `--ff` options, as well as the `cache` fixture.
|
|
5
|
+
|
|
6
|
+
**Do not** commit this to version control.
|
|
7
|
+
|
|
8
|
+
See [the docs](https://docs.pytest.org/en/stable/how-to/cache.html) for more information.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.15)
|
|
2
|
+
project(vector_qsort LANGUAGES CXX)
|
|
3
|
+
|
|
4
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
5
|
+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
6
|
+
|
|
7
|
+
set(HWY_ENABLE_TESTS OFF CACHE BOOL "" FORCE)
|
|
8
|
+
set(HWY_ENABLE_EXAMPLES OFF CACHE BOOL "" FORCE)
|
|
9
|
+
set(HWY_ENABLE_CONTRIB ON CACHE BOOL "" FORCE)
|
|
10
|
+
|
|
11
|
+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../vendor/highway/CMakeLists.txt")
|
|
12
|
+
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../vendor/highway" "${CMAKE_CURRENT_BINARY_DIR}/highway_build" EXCLUDE_FROM_ALL)
|
|
13
|
+
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/vendor/highway/CMakeLists.txt")
|
|
14
|
+
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/vendor/highway" "${CMAKE_CURRENT_BINARY_DIR}/highway_build" EXCLUDE_FROM_ALL)
|
|
15
|
+
else()
|
|
16
|
+
include(FetchContent)
|
|
17
|
+
FetchContent_Declare(
|
|
18
|
+
highway
|
|
19
|
+
GIT_REPOSITORY https://github.com/google/highway.git
|
|
20
|
+
GIT_TAG master
|
|
21
|
+
GIT_SHALLOW TRUE
|
|
22
|
+
)
|
|
23
|
+
FetchContent_MakeAvailable(highway)
|
|
24
|
+
endif()
|
|
25
|
+
|
|
26
|
+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../core/vqsort.h")
|
|
27
|
+
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../core")
|
|
28
|
+
endif()
|
|
29
|
+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
|
30
|
+
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
|
|
31
|
+
endif()
|
|
32
|
+
|
|
33
|
+
find_package(Python 3.9 COMPONENTS Interpreter Development.Module REQUIRED)
|
|
34
|
+
find_package(nanobind CONFIG REQUIRED)
|
|
35
|
+
|
|
36
|
+
nanobind_add_module(_vector_qsort
|
|
37
|
+
binding.cc
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
target_link_libraries(_vector_qsort PRIVATE
|
|
41
|
+
hwy
|
|
42
|
+
hwy_contrib
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
install(TARGETS _vector_qsort LIBRARY DESTINATION vector_qsort)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hemanth HM (https://h3manth.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: vector-qsort
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Vectorized quicksort for NumPy arrays using Google Highway SIMD.
|
|
5
|
+
Author-Email: Hemanth HM <hemanth@h3manth.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Requires-Dist: numpy
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# vector-qsort
|
|
12
|
+
|
|
13
|
+
Vectorized quicksort for NumPy arrays using Google Highway SIMD.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install vector-qsort
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
import numpy as np
|
|
23
|
+
import vector_qsort
|
|
24
|
+
|
|
25
|
+
data = np.array([3.14, -1.5, 42.0, 0.0, -100.5, 2.71], dtype=np.float32)
|
|
26
|
+
|
|
27
|
+
# In-place SIMD sort
|
|
28
|
+
vector_qsort.sort(data)
|
|
29
|
+
# array([-100.5, -1.5, 0.0, 2.71, 3.14, 42.0], dtype=float32)
|
|
30
|
+
|
|
31
|
+
# Descending
|
|
32
|
+
vector_qsort.sort(data, desc=True)
|
|
33
|
+
|
|
34
|
+
# Non-mutating copy
|
|
35
|
+
sorted_data = vector_qsort.sorted(data)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`sort()` sorts 1D contiguous arrays in place with zero copies. `sorted()` returns a sorted copy. Supports `float32`, `float64`, `int32`, `uint32`, `int64`, `uint64`, `int16`, and `uint16`.
|
|
39
|
+
|
|
40
|
+
## Benchmarks
|
|
41
|
+
|
|
42
|
+
Measured on Apple Silicon (ARM NEON) vs NumPy's native in-place `np.sort()`:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
python bench/bench_sort.py
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
| Dtype | Size | Distribution | NumPy (median) | vector-qsort | Speedup |
|
|
49
|
+
|---|---|---|---|---|---|
|
|
50
|
+
| `float32` | 100,000 | Random | 1.14 ms | **0.73 ms** | **1.57x** |
|
|
51
|
+
| `float32` | 5,000,000 | Random | 81.97 ms | **50.39 ms** | **1.63x** |
|
|
52
|
+
| `float64` | 100,000 | Random | 2.25 ms | **1.33 ms** | **1.70x** |
|
|
53
|
+
| `float64` | 5,000,000 | Plateau | 39.40 ms | **18.04 ms** | **2.18x** |
|
|
54
|
+
|
|
55
|
+
## Note on scalar sorts
|
|
56
|
+
|
|
57
|
+
Modern scalar sorts like `driftsort` and `ipnsort` excel at generic types and presorted run-detection. `vector-qsort` is designed specifically for raw numeric throughput on contiguous buffers by saturating SIMD vector lanes.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT © [Hemanth.HM](https://h3manth.com)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# vector-qsort
|
|
2
|
+
|
|
3
|
+
Vectorized quicksort for NumPy arrays using Google Highway SIMD.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install vector-qsort
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Quick start
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
import numpy as np
|
|
13
|
+
import vector_qsort
|
|
14
|
+
|
|
15
|
+
data = np.array([3.14, -1.5, 42.0, 0.0, -100.5, 2.71], dtype=np.float32)
|
|
16
|
+
|
|
17
|
+
# In-place SIMD sort
|
|
18
|
+
vector_qsort.sort(data)
|
|
19
|
+
# array([-100.5, -1.5, 0.0, 2.71, 3.14, 42.0], dtype=float32)
|
|
20
|
+
|
|
21
|
+
# Descending
|
|
22
|
+
vector_qsort.sort(data, desc=True)
|
|
23
|
+
|
|
24
|
+
# Non-mutating copy
|
|
25
|
+
sorted_data = vector_qsort.sorted(data)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`sort()` sorts 1D contiguous arrays in place with zero copies. `sorted()` returns a sorted copy. Supports `float32`, `float64`, `int32`, `uint32`, `int64`, `uint64`, `int16`, and `uint16`.
|
|
29
|
+
|
|
30
|
+
## Benchmarks
|
|
31
|
+
|
|
32
|
+
Measured on Apple Silicon (ARM NEON) vs NumPy's native in-place `np.sort()`:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
python bench/bench_sort.py
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
| Dtype | Size | Distribution | NumPy (median) | vector-qsort | Speedup |
|
|
39
|
+
|---|---|---|---|---|---|
|
|
40
|
+
| `float32` | 100,000 | Random | 1.14 ms | **0.73 ms** | **1.57x** |
|
|
41
|
+
| `float32` | 5,000,000 | Random | 81.97 ms | **50.39 ms** | **1.63x** |
|
|
42
|
+
| `float64` | 100,000 | Random | 2.25 ms | **1.33 ms** | **1.70x** |
|
|
43
|
+
| `float64` | 5,000,000 | Plateau | 39.40 ms | **18.04 ms** | **2.18x** |
|
|
44
|
+
|
|
45
|
+
## Note on scalar sorts
|
|
46
|
+
|
|
47
|
+
Modern scalar sorts like `driftsort` and `ipnsort` excel at generic types and presorted run-detection. `vector-qsort` is designed specifically for raw numeric throughput on contiguous buffers by saturating SIMD vector lanes.
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
MIT © [Hemanth.HM](https://h3manth.com)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import numpy as np
|
|
3
|
+
import vector_qsort
|
|
4
|
+
|
|
5
|
+
def generate_data(dtype, size, distribution):
|
|
6
|
+
rng = np.random.default_rng(42)
|
|
7
|
+
if distribution == "random":
|
|
8
|
+
if np.issubdtype(dtype, np.floating):
|
|
9
|
+
return rng.standard_normal(size).astype(dtype)
|
|
10
|
+
else:
|
|
11
|
+
return rng.integers(-1_000_000, 1_000_000, size=size, dtype=dtype)
|
|
12
|
+
elif distribution == "sorted":
|
|
13
|
+
return np.arange(size, dtype=dtype)
|
|
14
|
+
elif distribution == "reverse":
|
|
15
|
+
return np.arange(size, 0, -1, dtype=dtype)
|
|
16
|
+
elif distribution == "plateau":
|
|
17
|
+
return (np.arange(size) % 10).astype(dtype)
|
|
18
|
+
raise ValueError(f"Unknown distribution: {distribution}")
|
|
19
|
+
|
|
20
|
+
def run_benchmark(sort_fn, master_array, iterations, warmup=5):
|
|
21
|
+
total = warmup + iterations
|
|
22
|
+
copies = [master_array.copy() for _ in range(total)]
|
|
23
|
+
|
|
24
|
+
# Warmup
|
|
25
|
+
for i in range(warmup):
|
|
26
|
+
sort_fn(copies[i])
|
|
27
|
+
|
|
28
|
+
# Measure
|
|
29
|
+
times = []
|
|
30
|
+
for i in range(warmup, total):
|
|
31
|
+
target = copies[i]
|
|
32
|
+
t0 = time.perf_counter_ns()
|
|
33
|
+
sort_fn(target)
|
|
34
|
+
t1 = time.perf_counter_ns()
|
|
35
|
+
times.append((t1 - t0) / 1e6) # ms
|
|
36
|
+
|
|
37
|
+
times.sort()
|
|
38
|
+
median = times[len(times) // 2]
|
|
39
|
+
min_time = times[0]
|
|
40
|
+
p95 = times[int(len(times) * 0.95)]
|
|
41
|
+
return median, min_time, p95
|
|
42
|
+
|
|
43
|
+
def main():
|
|
44
|
+
print("=" * 80)
|
|
45
|
+
print(" vector-qsort vs NumPy np.sort() In-Place Benchmark")
|
|
46
|
+
print(f" NumPy: {np.__version__} | Python: 3.14 | Platform: Apple Silicon (NEON)")
|
|
47
|
+
print("=" * 80)
|
|
48
|
+
print("")
|
|
49
|
+
|
|
50
|
+
sizes = [1_000, 10_000, 100_000, 1_000_000, 5_000_000]
|
|
51
|
+
dtypes = [np.float32, np.int32, np.float64]
|
|
52
|
+
distributions = ["random", "plateau"]
|
|
53
|
+
|
|
54
|
+
header = (
|
|
55
|
+
f"| {'Dtype':<12}"
|
|
56
|
+
f"| {'Size':<12}"
|
|
57
|
+
f"| {'Distribution':<14}"
|
|
58
|
+
f"| {'NumPy (median)':<16}"
|
|
59
|
+
f"| {'vqsort (median)':<17}"
|
|
60
|
+
f"| {'Speedup':<10}"
|
|
61
|
+
f"| {'M elem/s':<10} |"
|
|
62
|
+
)
|
|
63
|
+
print(header)
|
|
64
|
+
print("|" + "-" * 13 + "|" + "-" * 12 + "|" + "-" * 15 + "|" + "-" * 17 + "|" + "-" * 18 + "|" + "-" * 11 + "|" + "-" * 11 + "|")
|
|
65
|
+
|
|
66
|
+
for dt in dtypes:
|
|
67
|
+
dt_name = dt.__name__
|
|
68
|
+
for size in sizes:
|
|
69
|
+
for dist in distributions:
|
|
70
|
+
master = generate_data(dt, size, dist)
|
|
71
|
+
iters = 10 if size >= 1_000_000 else (25 if size >= 100_000 else 50)
|
|
72
|
+
|
|
73
|
+
# arr.sort() is NumPy's in-place sort
|
|
74
|
+
np_med, _, _ = run_benchmark(lambda a: a.sort(), master, iters)
|
|
75
|
+
vq_med, _, _ = run_benchmark(lambda a: vector_qsort.sort(a), master, iters)
|
|
76
|
+
|
|
77
|
+
speedup = np_med / vq_med
|
|
78
|
+
throughput = (size / 1e6) / (vq_med / 1000)
|
|
79
|
+
|
|
80
|
+
print(
|
|
81
|
+
f"| {dt_name:<12}"
|
|
82
|
+
f"| {size:<12,}"
|
|
83
|
+
f"| {dist:<14}"
|
|
84
|
+
f"| {np_med:>8.3f} ms "
|
|
85
|
+
f"| {vq_med:>8.3f} ms "
|
|
86
|
+
f"| {speedup:>6.2f}x "
|
|
87
|
+
f"| {throughput:>7.1f} M/s |"
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
print("")
|
|
91
|
+
print("Done.")
|
|
92
|
+
|
|
93
|
+
if __name__ == "__main__":
|
|
94
|
+
main()
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#include <nanobind/nanobind.h>
|
|
2
|
+
#include <nanobind/ndarray.h>
|
|
3
|
+
#include "vqsort.h"
|
|
4
|
+
|
|
5
|
+
namespace nb = nanobind;
|
|
6
|
+
|
|
7
|
+
template <typename T>
|
|
8
|
+
void sort_data(nb::ndarray<nb::c_contig> array, bool desc) {
|
|
9
|
+
T* ptr = static_cast<T*>(array.data());
|
|
10
|
+
size_t n = array.shape(0);
|
|
11
|
+
vector_qsort::Sort(ptr, n, desc);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
void sort_in_place(nb::ndarray<nb::c_contig> array, bool desc = false) {
|
|
15
|
+
if (array.ndim() != 1) {
|
|
16
|
+
throw nb::value_error("vector_qsort only supports 1-dimensional arrays");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
using dl_code = nanobind::dlpack::dtype_code;
|
|
20
|
+
auto dt = array.dtype();
|
|
21
|
+
if (dt.lanes != 1) {
|
|
22
|
+
throw nb::type_error("Only scalar element types are supported");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (dt.code == static_cast<uint8_t>(dl_code::Float) && dt.bits == 32) {
|
|
26
|
+
sort_data<float>(array, desc);
|
|
27
|
+
} else if (dt.code == static_cast<uint8_t>(dl_code::Float) && dt.bits == 64) {
|
|
28
|
+
sort_data<double>(array, desc);
|
|
29
|
+
} else if (dt.code == static_cast<uint8_t>(dl_code::Int) && dt.bits == 32) {
|
|
30
|
+
sort_data<int32_t>(array, desc);
|
|
31
|
+
} else if (dt.code == static_cast<uint8_t>(dl_code::UInt) && dt.bits == 32) {
|
|
32
|
+
sort_data<uint32_t>(array, desc);
|
|
33
|
+
} else if (dt.code == static_cast<uint8_t>(dl_code::Int) && dt.bits == 64) {
|
|
34
|
+
sort_data<int64_t>(array, desc);
|
|
35
|
+
} else if (dt.code == static_cast<uint8_t>(dl_code::UInt) && dt.bits == 64) {
|
|
36
|
+
sort_data<uint64_t>(array, desc);
|
|
37
|
+
} else if (dt.code == static_cast<uint8_t>(dl_code::Int) && dt.bits == 16) {
|
|
38
|
+
sort_data<int16_t>(array, desc);
|
|
39
|
+
} else if (dt.code == static_cast<uint8_t>(dl_code::UInt) && dt.bits == 16) {
|
|
40
|
+
sort_data<uint16_t>(array, desc);
|
|
41
|
+
} else {
|
|
42
|
+
throw nb::type_error("Unsupported dtype for vector_qsort (supported: float32, float64, int32, uint32, int64, uint64, int16, uint16)");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
NB_MODULE(_vector_qsort, m) {
|
|
47
|
+
m.doc() = "Google Highway SIMD Vectorized QuickSort";
|
|
48
|
+
m.def("sort_in_place", &sort_in_place, nb::arg("array"), nb::arg("desc") = false,
|
|
49
|
+
"Sort a 1D contiguous array in-place using Google Highway SIMD.");
|
|
50
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#ifndef VECTOR_QSORT_CORE_H_
|
|
2
|
+
#define VECTOR_QSORT_CORE_H_
|
|
3
|
+
|
|
4
|
+
#include <cstddef>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <algorithm>
|
|
7
|
+
#include <functional>
|
|
8
|
+
|
|
9
|
+
#include "hwy/base.h"
|
|
10
|
+
#include "hwy/contrib/sort/vqsort.h"
|
|
11
|
+
|
|
12
|
+
namespace vector_qsort {
|
|
13
|
+
|
|
14
|
+
template <typename T>
|
|
15
|
+
inline void Sort(T* data, size_t n, bool desc = false) {
|
|
16
|
+
if (n <= 1) return;
|
|
17
|
+
if (desc) {
|
|
18
|
+
hwy::VQSort(data, n, hwy::SortDescending());
|
|
19
|
+
} else {
|
|
20
|
+
hwy::VQSort(data, n, hwy::SortAscending());
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Specialization for double in case VQSortHaveFloat64 is not supported on rare targets
|
|
25
|
+
template <>
|
|
26
|
+
inline void Sort<double>(double* data, size_t n, bool desc) {
|
|
27
|
+
if (n <= 1) return;
|
|
28
|
+
if (hwy::VQSortHaveFloat64()) {
|
|
29
|
+
if (desc) {
|
|
30
|
+
hwy::VQSort(data, n, hwy::SortDescending());
|
|
31
|
+
} else {
|
|
32
|
+
hwy::VQSort(data, n, hwy::SortAscending());
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
if (desc) {
|
|
36
|
+
std::sort(data, data + n, std::greater<double>());
|
|
37
|
+
} else {
|
|
38
|
+
std::sort(data, data + n);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
} // namespace vector_qsort
|
|
44
|
+
|
|
45
|
+
#endif // VECTOR_QSORT_CORE_H_
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["scikit-build-core>=0.10", "nanobind>=2.0.0"]
|
|
3
|
+
build-backend = "scikit_build_core.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vector-qsort"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Vectorized quicksort for NumPy arrays using Google Highway SIMD."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Hemanth HM", email = "hemanth@h3manth.com" }]
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"numpy",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[tool.scikit-build]
|
|
18
|
+
cmake.build-type = "Release"
|
|
19
|
+
wheel.packages = ["vector_qsort"]
|
|
20
|
+
cmake.args = [
|
|
21
|
+
"-DHWY_ENABLE_TESTS=OFF",
|
|
22
|
+
"-DHWY_ENABLE_EXAMPLES=OFF",
|
|
23
|
+
"-DHWY_ENABLE_CONTRIB=ON"
|
|
24
|
+
]
|
|
25
|
+
sdist.include = [
|
|
26
|
+
"binding.cc",
|
|
27
|
+
"vector_qsort",
|
|
28
|
+
"CMakeLists.txt",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"include",
|
|
32
|
+
]
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import numpy as np
|
|
3
|
+
import vector_qsort
|
|
4
|
+
|
|
5
|
+
def test_all_dtypes_and_orders():
|
|
6
|
+
dtypes = [
|
|
7
|
+
np.float32, np.float64,
|
|
8
|
+
np.int32, np.uint32,
|
|
9
|
+
np.int64, np.uint64,
|
|
10
|
+
np.int16, np.uint16,
|
|
11
|
+
]
|
|
12
|
+
for dt in dtypes:
|
|
13
|
+
if np.issubdtype(dt, np.unsignedinteger):
|
|
14
|
+
raw = np.array([42, 5, 100, 0, 999, 500], dtype=dt)
|
|
15
|
+
else:
|
|
16
|
+
raw = np.array([42, -5, 100, 0, 999, -500], dtype=dt)
|
|
17
|
+
asc = raw.copy()
|
|
18
|
+
vector_qsort.sort(asc)
|
|
19
|
+
assert np.all(asc[:-1] <= asc[1:]), f"Ascending sort failed for {dt}"
|
|
20
|
+
|
|
21
|
+
desc = raw.copy()
|
|
22
|
+
vector_qsort.sort(desc, desc=True)
|
|
23
|
+
assert np.all(desc[:-1] >= desc[1:]), f"Descending sort failed for {dt}"
|
|
24
|
+
|
|
25
|
+
def test_edge_cases():
|
|
26
|
+
# Empty
|
|
27
|
+
empty = np.array([], dtype=np.float32)
|
|
28
|
+
vector_qsort.sort(empty)
|
|
29
|
+
assert len(empty) == 0
|
|
30
|
+
|
|
31
|
+
# Single element
|
|
32
|
+
single = np.array([42], dtype=np.int32)
|
|
33
|
+
vector_qsort.sort(single)
|
|
34
|
+
assert single[0] == 42
|
|
35
|
+
|
|
36
|
+
# All duplicates
|
|
37
|
+
dupes = np.array([7, 7, 7, 7, 7], dtype=np.float64)
|
|
38
|
+
vector_qsort.sort(dupes)
|
|
39
|
+
assert np.array_equal(dupes, [7, 7, 7, 7, 7])
|
|
40
|
+
|
|
41
|
+
# Already sorted
|
|
42
|
+
sorted_arr = np.arange(10, dtype=np.int32)
|
|
43
|
+
vector_qsort.sort(sorted_arr)
|
|
44
|
+
assert np.array_equal(sorted_arr, np.arange(10))
|
|
45
|
+
|
|
46
|
+
# Reverse sorted
|
|
47
|
+
rev = np.arange(10, 0, -1, dtype=np.int32)
|
|
48
|
+
vector_qsort.sort(rev)
|
|
49
|
+
assert np.array_equal(rev, np.arange(1, 11))
|
|
50
|
+
|
|
51
|
+
def test_sorted_copy():
|
|
52
|
+
orig = np.array([5, 3, 1, 4, 2], dtype=np.float32)
|
|
53
|
+
res = vector_qsort.sorted(orig)
|
|
54
|
+
assert res is not orig
|
|
55
|
+
assert np.array_equal(orig, [5, 3, 1, 4, 2])
|
|
56
|
+
assert np.array_equal(res, [1, 2, 3, 4, 5])
|
|
57
|
+
|
|
58
|
+
desc = vector_qsort.sorted(orig, desc=True)
|
|
59
|
+
assert np.array_equal(desc, [5, 4, 3, 2, 1])
|
|
60
|
+
|
|
61
|
+
def test_large_array():
|
|
62
|
+
rng = np.random.default_rng(42)
|
|
63
|
+
data = rng.standard_normal(100_000).astype(np.float32)
|
|
64
|
+
expected = np.sort(data)
|
|
65
|
+
|
|
66
|
+
vector_qsort.sort(data)
|
|
67
|
+
assert np.allclose(data, expected)
|
|
68
|
+
|
|
69
|
+
def test_error_handling():
|
|
70
|
+
# 2D array
|
|
71
|
+
mat = np.zeros((3, 3), dtype=np.float32)
|
|
72
|
+
with pytest.raises(ValueError, match="1-dimensional"):
|
|
73
|
+
vector_qsort.sort(mat)
|
|
74
|
+
|
|
75
|
+
# Unsupported dtype
|
|
76
|
+
unsupported = np.array(["a", "b", "c"])
|
|
77
|
+
with pytest.raises(TypeError):
|
|
78
|
+
vector_qsort.sort(unsupported)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
vector-qsort: Vectorized quicksort for NumPy arrays using Google Highway SIMD.
|
|
3
|
+
"""
|
|
4
|
+
from typing import Any
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from ._vector_qsort import sort_in_place
|
|
9
|
+
except ImportError:
|
|
10
|
+
try:
|
|
11
|
+
from _vector_qsort import sort_in_place
|
|
12
|
+
except ImportError as exc:
|
|
13
|
+
raise ImportError(
|
|
14
|
+
"Could not load native _vector_qsort extension. Run `pip install -e .`"
|
|
15
|
+
) from exc
|
|
16
|
+
|
|
17
|
+
def sort(arr: Any, desc: bool = False) -> Any:
|
|
18
|
+
"""
|
|
19
|
+
Sort a 1D contiguous array in-place using Google Highway SIMD.
|
|
20
|
+
"""
|
|
21
|
+
if isinstance(arr, np.ndarray):
|
|
22
|
+
if not arr.flags.c_contiguous:
|
|
23
|
+
raise ValueError("Array must be C-contiguous. Use np.ascontiguousarray() if needed.")
|
|
24
|
+
sort_in_place(arr, desc=desc)
|
|
25
|
+
return arr
|
|
26
|
+
|
|
27
|
+
def sorted(arr: Any, desc: bool = False) -> Any:
|
|
28
|
+
"""
|
|
29
|
+
Return a sorted copy of the array without mutating the original.
|
|
30
|
+
"""
|
|
31
|
+
if isinstance(arr, np.ndarray):
|
|
32
|
+
copy = arr.copy()
|
|
33
|
+
sort_in_place(copy, desc=desc)
|
|
34
|
+
return copy
|
|
35
|
+
raise TypeError(f"sorted() expects numpy.ndarray, got {type(arr).__name__}")
|
|
36
|
+
|
|
37
|
+
__all__ = ["sort", "sorted"]
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Marker file for PEP 561
|