High performance algorithms in C#: SIMD/SSE, multi-core and faster
High performance algorithms in C#: SIMD/SSE, multi-core and faster
If you like HPCsharp, give it a star and donate to help us keep more good stuff coming.
Give us feedback and let us know where else could use additional performance.
NuGet package of high performance Parallel C# generic algorithms (multi-core and SSE). Runs on Windows and Linux (.NET 5, 6 and 8, .NET Standard 2.0 and 2.1). Raises C# performance many times over standard versions. Familiar interfaces, similar to standard C# algorithms and Linq. Free, open source, on nuget.org
| Algorithm | * | ** | SSE | Multi-Core | Array | List | Details |
|---|---|---|---|---|---|---|---|
| Add | 2 | 14 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Adds two arrays element-wise | |
| Binary Search | 1 | 2 | :heavy_check_mark: | :heavy_check_mark: | Generic IComparer\ | ||
| Block Swap | 4 | 5 | :heavy_check_mark: | Generic | |||
| Parallel .ToArray() | 1 | 11 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | List.ToArray() and Array.Copy() parallel generic |
| Counting Sort | 3 | 14 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | byte, ushort, sbyte, short arrays. Ludicrous speed! | |
| Divide-and-Conquer | 2 | 4 | :heavy_check_mark: | :heavy_check_mark: | Generic serial and parallel abstraction | ||
| Fill | 4 | 10 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Numeric arrays | |
| Heap Sort | 1 | 2 | :heavy_check_mark: | ||||
| Histogram | 14 | 35 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Byte, N-bit components of numeric arrays | |
| Insertion Sort | 1 | 2 | :heavy_check_mark: | :heavy_check_mark: | Generic IComparer\ | ||
| Introspective Sort | 1 | 3 | :heavy_check_mark: | ||||
| Max, Min | 2 | 12 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Generic IComparer\ |
| Mean Absolute Deviation | 3 | 6 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | float[] and double[] |
| Median-of-Medians | 1 | 1 | |||||
| Merge | 6 | 18 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Generic IComparer\ | |
| Merge In-Place | 1 | 3 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Generic IComparer\ | |
| Multi-way Merge | 1 | 1 | :heavy_check_mark: | ||||
| Merge Sort | 2 | 25 | :heavy_check_mark: | :heavy_check_mark: | Generic, Stable or not, whole or partial | ||
| Merge Sort In-Place | 2 | 8 | :heavy_check_mark: | :heavy_check_mark: | Generic, Adaptive, whole or partial | ||
| Priority Queue | 2 | 15 | :heavy_check_mark: | ||||
| Partition (Radix) | 1 | 5 | |||||
| Quicksort | 5 | 9 | :heavy_check_mark: | :heavy_check_mark: | |||
| Radix Sort (LSD) | 6 | 40 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Numeric arrays, user defined types, Stable | |
| Radix Sort (MSD) | 4 | 24 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Numeric arrays, user defined types, In-place | |
| Selection | 4 | 6 | :heavy_check_mark: | ||||
| Selection (Radix) | 3 | 10 | :heavy_check_mark: | :heavy_check_mark: | k-th largest, top-k largest, k[]-th largest | ||
| Sequence Equal | 2 | 19 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | ||
| Suffix Sum | 1 | 1 | :heavy_check_mark: | ||||
| Standard Deviation | 7 | 12 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Avoids arithmetic overflow exception | |
| Sum | 7 | 214 | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Numeric arrays. Better in many ways | |
| Swap | 4 | 4 | :heavy_check_mark: | Generic swap variations | |||
| Zero Array Detect | 3 | 13 | :heavy_check_mark: | :heavy_check_mark: | Detect if byte array is all zeroes |
* Number of different algorithms
** Number of functions for this algorithm\
Usage examples are provided in the HPCsharpExamples directory, which has a VisualStudio 2022 solution. Build and run it to see performance gains on your computer or a cloud node. To get the maximum performance make sure to target x64 processor architecture for the Release build in VisualStudio, increasing performance by as much as 50%.
HPCsharp has a book dedicated to Parallel Algorithm implementations within.
The first time you call a function that is implemented using SIMD/SSE instructions, C# just-in-time (JIT) compiler takes the time to compile and optimize that function, which results in much slower performance. On the second use of the function and on subsequent uses, the SIMD/SSE function will run at its full performance. Keep this behavior of the C# JIT compiler in mind as you use or benchmark HPCsharp functions.
C# implements two ways to sort arrays: .Sort and Linq.OrderBy. Sort does not support multi-core, whereas Linq.OrderBy does. The following Table shows performance of these two algorithms, for three input data distributions, in Millions of Int32's per second.
| Algorithm | Random | Presorted | Constant | Computer |
|---|---|---|---|---|
| .Sort | 11 | 70 | 32 | 1-core of 6-core i7-9750H |
| .Sort | 13 | 136 | 78 | 1-core of Intel i7-12700H |
| .Sort | 9 | 58 | 46 | 1-core of 32-core AMD EPYC |
| Linq.OrderBy | 2.3 | 6.3 | 6.3 | 1-core of Intel i7-12700H |
| Linq.AsParallel.OrderBy | 2.1 | 6.3 | 6.3 | 14-core Intel i7-12700H |
| Linq.OrderBy | 2.3 | 7.7 | 8.0 | single-core on 14 core Intel Xeon |
| Linq.AsParallel.OrderBy | 1.1 | 5.5 | 5.4 | single-core on 32 core AMD EPYC |
HPCsharp implements LSD Radix Sort, which is a linear time O(N), stable sorting algorithm. The following table shows performance for three input data distributions, in Millions of UInt32's per second.
| Algorithm | Random | Presorted | Constant | Computer |
|---|---|---|---|---|
| LSD Radix Sort (Parallel) | 655 | 652 | 692 | 48-core AWS C7i.24xlarge (Intel) |
| LSD Radix Sort (Parallel) | 716 | 744 | 766 | 48-core AWS C7a.24xlarge (AMD) |
| LSD Radix Sort (Parallel) | 524 | 538 | 676 | 14-core Intel i7-12700H |
| LSD Radix Sort (Sequential) | 127 | 62 | 181 | 1-core of Intel i7-12700H |
Several implementations available: serial, partially parallel, and fully parallel. Serial algorithm runs on a single core. Partially parallel algorithm runs the counting/histogram phase of the algorithm in parallel, and the permutation phase serially. Fully parallel algorith runs both phases of the algorithm on multiple cores in parallel.
Radix Sort has been extended to sort user defined classes based on a UInt32 or UInt64 key within the class. Radix Sort is currently using only a single core.
Single-core (sequential) in-place MSD Radix Sort provides competitive performance with a truly in-place implementation, which is linear-time. It is not a stable sort, just like Array.Sort. This algorithm supports keys of various data types: unsigned and signed integers (32-bit and 64-bit), floating-point and double.
| Algorithm | Random | Presorted | Constant | Data Type | Computer |
|---|---|---|---|---|---|
| MSD Radix Sort (in-place) | 28 | 42 | 333 | int | 1-core of Intel i7-12700H |
| MSD Radix Sort (in-place) | 19 | 35 | 146 | long | 1-core of Intel i7-12700H |
| MSD Radix Sort (in-place) | 21 | 33 | 241 | float | 1-core of Intel i7-12700H |
| MSD Radix Sort (in-place) | 16 | 28 | 120 | double | 1-core of Intel i7-12700H |
Merge Sort provides a performance boost comparing with Linq.OrderBy when running on a single core, but is not competitive with Array.Sort(). On a single core on variety of machines, sorting an array of Int32's, performance in Millions of Int32's per second is:
| Algorithm | Random | Presorted | Constant | Description |
|---|---|---|---|---|
| HPC# .SortMerge | 8 | 30 | 27 | 1-core of Intel i7-12700H |
| HPC# .SortMerge | 5 | 16 | 15 | 1-core of 32-core AMD EPYC |
Parallel Merge Sort uses multiple CPU cores to accelerate performance, which scales well with the number of cores and the number of memory channels. C# Array.Sort does not support parallel sorting. On variety of machines, sorting an array of Int32's, performance in Millions of Int32's per second is:
| Algorithm | Random | Presorted | Constant | Description |
|---|---|---|---|---|
| HPC# .SortMergePar | 136 | 659 | 451 | 14-core Intel i7-12700H |
| HPC# .SortMergePar | 272 | 875 | 736 | 48-core AWS C7a.24xlarge (AMD) |
| HPC# .SortMergePar | 293 | 893 | 760 | 32-core AMD EPYC |
| HPC# .SortMergePar | 397 | 915 | 754 | 48-core Intel Xeon 8275CL |
HPCsharp's Parallel Merge Sort is not stable, just like Array.Sort. The version benchmarked above is the not-in-place one. Faster than Array.Sort and List.Sort across all distributions, and substantially faster than Linq.OrderBy and Linq.OrderBy.AsParallel, which doesn't scale well as the number of cores increases. HPCsharp's Parallel Merge Sort scales very well with the number of cores, for all distributions providing higher performance than Array.Sort() and Linq.OrderBy and Linq.OrderBy.AsParallel.
HPCsharp improves .Sum() of numeric arrays in the following ways:
The table below compares performance (in GigaAdds/second) of Linq.AsParallel().Sum() and HPCsharp.SumSsePar() - both use multi-core (6 or 14 of them), with HPCsharp also using SIMD/SSE data parallel instructions on each core to gain additional performance:
| Library | sbyte | byte | short | ushort | int | uint | long | ulong | Details |
|---|---|---|---|---|---|---|---|---|---|
| array.Sum() | n/a | n/a | n/a | n/a | 1.5* | n/a | 1.7* | n/a | using 6 cores |
| array.Sum(v => (long)v) | 0.72 | 0.76 | 0.75 | 0.76 | 0.7 | 0.7 | using 6 cores | ||
| array.Sum(v => (decimal)v) | 0.35 | 0.31 | 0.29 | using 6 cores | |||||
| Parallel.ForEach((long)v) | 5.9 | 10.9 | 10.7 | using 6 cores, HPC# includes | |||||
| Parallel.ForEach((long)v) | 1.0 | 0.7 | Raspberry Pi 4, 4-core ARM | ||||||
| HPC# (6-core) | 33 | 33 | 17 | 17 | 8.4 | 8.4 | 3.7 | 4 | using 6 cores, 2 memory channels |
| HPC# (6-core) | 26 | 26 | 13 | 3.6 | using 2 cores | ||||
| HPC# (14-core) | 63 | 63 | 16 | 22 | 14 | 14 | 3.2 | 7.1 | 4 memory channels |
| HPC# (32-core) | 100 | AMD EPYC 7502P w/ 8-channel DDR4 3200 |
* arithmetic overflow exception is possible
n/a not available
| Library | float | floatToDouble | double | decimal | BigInteger |
|---|---|---|---|---|---|
| array.Sum() | 1.8 | 2.1 | 0.38 | 0.016** | |
| array.Sum(v => (double)v) | 0. |
No open issues yet, or sync has not completed.