Skip to content

Commit d5433b4

Browse files
author
anicusan
committed
not depending on Base.Sort internal values, should work on Julia nightly too
1 parent 3cad9c0 commit d5433b4

File tree

2 files changed

+26
-816
lines changed

2 files changed

+26
-816
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SyncSort: Cross-Architecture Synchronised Sorting of Multiple Arrays
22

3-
While key-value pairs are standard in sorting, oftentimes these keys and values are kept in separate arrays (see Explanation below); we can sort multiple secondary vector following the values of a primary key vector using a new sorting interface:
3+
While key-value pairs are standard in sorting, oftentimes these keys and values are kept in separate arrays (see Explanation below); we can sort multiple secondary vectors following the values of a primary key vector using a new sorting interface:
44

55
```julia
66
syncsort!(v, rest...)
@@ -11,22 +11,23 @@ ids = rand(Int64, num_particles)
1111
xcoords = rand(num_particles)
1212
ycoords = rand(num_particles)
1313

14+
# Reordering applied to `ids` will be done in sync with `xcoords` and `ycoords`
1415
syncsort!(ids, xcoords, ycoords)
15-
1616
```
1717

1818

1919
## Explanation
20+
2021
Key-value pair sorting assumes an _Array of Structures_ (AoS) format - e.g., a `Vector{Particle}` where `struct Particle` contains some fields `coord::Float64` and `id::Int64`. However, in many applications, it is more performant to store these fields as a _Structure of Arrays_ (SoA), i.e. separate `coords::Vector{Float64` and `ids::Vector{Int64}`.
2122

2223
If we had an Array of Structures, using standard sorting interfaces - as is the case in virtually all programming languages - we'd do something like:
2324

2425
```julia
2526
# One primary key and two secondary values
2627
struct Particle
27-
id::Int64
28-
xcoord::Float64
29-
ycoord::Float64
28+
id::Int64
29+
xcoord::Float64
30+
ycoord::Float64
3031
end
3132

3233
# Array of structures
@@ -39,12 +40,12 @@ sort!(particles; by=p->p.id)
3940
In high-performance applications, we'd use a Structure of Arrays, where we have separate arrays for our keys and values; the typical solution would involve creating a mask of indices sorting the primary array, in Julia named `sortperm`:
4041

4142
```julia
42-
# Keys and values held in separate, contiguous arrays - could be bundled in a Structure of Arrays
43+
# Keys and values held in separate vectors
4344
ids = rand(Int64, 10_000)
4445
xcoords = rand(Float64, 10_000)
4546
ycoords = rand(Float64, 10_000)
4647

47-
# Get indices permutation that sort the primary array
48+
# Get indices permutation that sorts the primary array
4849
sorted_indices = sortperm(ids)
4950

5051
# Use indices mask to permute arrays
@@ -53,12 +54,12 @@ xcoords = xcoords[sorted_indices]
5354
ycoords = ycoords[sorted_indices]
5455
```
5556

56-
An easier and much more performant solution - in terms of time, space, and program (re-)design - would be `syncsort`:
57+
An easier and much more performant solution - in terms of time, space, and especially program (re-)design - would be `syncsort`:
5758

5859
```julia
5960
using SyncSort
6061

61-
# Keys and values held in separate, contiguous arrays - could be bundled in a Structure of Arrays
62+
# Keys and values held in separate vectors
6263
ids = rand(Int64, 10_000)
6364
xcoords = rand(Float64, 10_000)
6465
ycoords = rand(Float64, 10_000)
@@ -69,9 +70,9 @@ syncsort!(ids, xcoords, ycoords)
6970

7071
## CPU Sorting Credits
7172

72-
The CPU sorting routines in `src/cpu_sort.jl` are copied verbatim from the Julia standard library, with some changes to propagate the secondary arrays and follow the swaps done by the standard sorting routines. All credits go to the Julia contributors, with special thanks to @LilithHafner for writing one of the most performant sorting algorithms of [any programming language](https://github.com/LilithHafner/InterLanguageSortingComparisons) - thank you! The Julia license text is included within the file, while this package is shared under the same MIT license.
73+
The CPU sorting routines in `src/cpu_sort.jl` are copied verbatim from the Julia standard library, with some changes to propagate the secondary arrays and follow the swaps done by the standard sorting routines. All credits go to the Julia contributors, with special thanks to @LilithHafner for writing one of the most performant sorting algorithms of [any programming language](https://github.com/LilithHafner/InterLanguageSortingComparisons) - thank you! The Julia license text is included within the file.
7374

7475

7576
## License
7677

77-
This package is shared under the same license as the Julia standard library, MIT.
78+
This package is shared under the same license as the Julia standard library, MIT.

0 commit comments

Comments
 (0)