2021-03-11 21:22:37 +01:00
|
|
|
#ifndef MAGYAR_SORT_H
|
|
|
|
|
#define MAGYAR_SORT_H
|
|
|
|
|
/**
|
|
|
|
|
* single header lib: In-place, fast heavily modified and optimized radix sort.
|
|
|
|
|
*
|
|
|
|
|
* Only unsigned ints for now, but should be able to modify for int and float...
|
|
|
|
|
* This is the counting variant with smart changes (not per-bit).
|
|
|
|
|
*
|
|
|
|
|
* LICENCE: CC3 - look it up, you need to mention me but that is all
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-17 22:09:35 +01:00
|
|
|
/*
|
|
|
|
|
* Does not help much:
|
|
|
|
|
// #pragma GCC target ("avx2")
|
|
|
|
|
// #pragma GCC optimization ("unroll-loops")
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-11 21:38:06 +01:00
|
|
|
#include <cstdio>
|
2021-03-11 21:22:37 +01:00
|
|
|
#include <cstdint>
|
2021-03-11 23:04:50 +01:00
|
|
|
#include <cstring> // memset
|
2021-03-11 21:22:37 +01:00
|
|
|
|
2021-03-13 15:51:24 +01:00
|
|
|
// TODO: Only for the regular radix I guess
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <algorithm> // std::swap
|
|
|
|
|
|
2021-03-11 21:22:37 +01:00
|
|
|
namespace MagyarSort {
|
2021-03-13 15:51:24 +01:00
|
|
|
/* CONFIG */
|
2021-03-11 21:22:37 +01:00
|
|
|
|
|
|
|
|
// Only change these if you know what you are doing
|
|
|
|
|
// I use these because I want to see if nibbles are
|
|
|
|
|
// better or something...
|
|
|
|
|
//
|
|
|
|
|
// Bytes of nibbles only:
|
|
|
|
|
// - DIGIT_RANGE and BITS_PER_DIGIT should correspond
|
|
|
|
|
// - DIGITS should also correspond with the uint32_t
|
|
|
|
|
// - and DIGIT_RANGE should be 2^n value (16 or 256)
|
2021-03-13 15:51:24 +01:00
|
|
|
#ifdef MAGYAR_SORT_NIBBLE
|
|
|
|
|
// Per-nibble digits sorting
|
2021-03-11 23:20:03 +01:00
|
|
|
static constexpr int DIGITS = 8; // "helyiérték"
|
|
|
|
|
static constexpr int BITS_PER_DIGIT = 4; // "bit / helyiérték"
|
|
|
|
|
static constexpr int DIGIT_RANGE = 16; // "helyiérték állapottér"
|
2021-03-13 15:51:24 +01:00
|
|
|
#else
|
|
|
|
|
// Per-byte digits sorting
|
|
|
|
|
static constexpr int DIGITS = 4; // "helyiérték"
|
|
|
|
|
static constexpr int BITS_PER_DIGIT = 8; // "bit / helyiérték"
|
|
|
|
|
static constexpr int DIGIT_RANGE = 256; // "helyiérték állapottér"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* DEBUG */
|
|
|
|
|
|
|
|
|
|
void debugArr(uint32_t *arr, size_t size) {
|
2021-12-17 21:17:53 +01:00
|
|
|
for(size_t i = 0; i < size; ++i) {
|
2021-03-13 15:51:24 +01:00
|
|
|
printf("%x, ", arr[i]);
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP>
|
|
|
|
|
void debugRadics(COUNTER_TYP *radics) {
|
2021-03-13 15:51:24 +01:00
|
|
|
for(size_t j = 0; j < DIGITS; ++j) {
|
2021-12-14 17:29:33 +01:00
|
|
|
printf("d%zu: ", j);
|
2021-12-17 21:17:53 +01:00
|
|
|
for(int i = 0; i < DIGIT_RANGE; ++i) {
|
2021-12-14 17:29:33 +01:00
|
|
|
printf("%zu,", radics[i + DIGIT_RANGE*j]);
|
2021-03-13 15:51:24 +01:00
|
|
|
}
|
|
|
|
|
printf("\n\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* HELPERS */
|
2021-03-11 21:22:37 +01:00
|
|
|
|
2021-03-11 22:05:55 +01:00
|
|
|
template<int DIGIT_CHOICE>
|
2021-12-13 02:18:08 +01:00
|
|
|
static inline __attribute__((always_inline)) uint32_t getDigit(uint32_t num) noexcept {
|
2021-03-11 22:05:55 +01:00
|
|
|
static constexpr int SHIFT = DIGIT_CHOICE * BITS_PER_DIGIT;
|
2021-03-11 21:22:37 +01:00
|
|
|
|
|
|
|
|
uint32_t shifted = num >> SHIFT;
|
2021-03-11 21:38:06 +01:00
|
|
|
return shifted & (DIGIT_RANGE - 1);
|
2021-03-11 21:22:37 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-11 23:04:50 +01:00
|
|
|
/** Recursive Functor: no class should be generated I think (compiler should be smart) */
|
2021-12-17 21:17:53 +01:00
|
|
|
template<int DIGIT, typename COUNTER_TYP>
|
|
|
|
|
struct OccurenceMagic : public OccurenceMagic<DIGIT - 1, COUNTER_TYP> {
|
|
|
|
|
inline __attribute__((always_inline)) OccurenceMagic(uint32_t arr[], COUNTER_TYP i, COUNTER_TYP *radicsOut) noexcept
|
|
|
|
|
: OccurenceMagic<DIGIT - 1 ,COUNTER_TYP>(arr, i, radicsOut) {
|
2021-03-11 23:04:50 +01:00
|
|
|
// Parents run first so template recursion runs DIGIT=0 first...
|
2021-03-11 22:34:44 +01:00
|
|
|
++radicsOut[getDigit<DIGIT>(arr[i]) + DIGIT_RANGE * DIGIT];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
/** Ends template recursion */
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP>
|
|
|
|
|
struct OccurenceMagic<-1, COUNTER_TYP> {
|
2021-12-18 01:23:06 +01:00
|
|
|
inline __attribute__((always_inline)) OccurenceMagic(uint32_t arr[], COUNTER_TYP i, COUNTER_TYP *radicsOut) noexcept {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** ARR_END must be an (STEP * k) */
|
|
|
|
|
template<int ARR_END, int STEP, typename ARR_T, int R_OR_W = 0 /* 0:R, 1:W */, int LOCALITY = 3 /* 3 is best, 0 worst*/>
|
|
|
|
|
struct PrefetchMagic : public PrefetchMagic<(ARR_END - STEP), STEP, ARR_T, R_OR_W, LOCALITY> {
|
|
|
|
|
inline __attribute__((always_inline)) PrefetchMagic(ARR_T *arr) noexcept
|
|
|
|
|
: PrefetchMagic<(ARR_END - STEP), STEP, ARR_T, R_OR_W, LOCALITY>(arr) {
|
|
|
|
|
__builtin_prefetch(&arr[ARR_END - STEP], R_OR_W, LOCALITY);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<int STEP, typename ARR_T, int R_OR_W, int LOCALITY>
|
|
|
|
|
struct PrefetchMagic<0, STEP, ARR_T, R_OR_W, LOCALITY> {
|
|
|
|
|
inline __attribute__((always_inline)) PrefetchMagic(ARR_T *arr) noexcept {}
|
2021-03-11 22:34:44 +01:00
|
|
|
};
|
|
|
|
|
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP>
|
|
|
|
|
static inline void countOccurences(uint32_t arr[], COUNTER_TYP size, COUNTER_TYP *radicsOut) noexcept {
|
2021-12-17 22:48:38 +01:00
|
|
|
COUNTER_TYP i = 0;
|
2021-12-18 01:48:42 +01:00
|
|
|
//#pragma GCC unroll 4
|
2021-12-17 22:48:38 +01:00
|
|
|
for(; i < size - 64; i += 64) {
|
2021-12-18 01:23:06 +01:00
|
|
|
// Prefetch for read level-1 cache
|
2021-12-18 01:48:42 +01:00
|
|
|
//__builtin_prefetch(&arr[i + (1 * 16)], 0/*r*/, 2/*L2 or L3 cache likely*/);
|
2021-12-18 01:23:06 +01:00
|
|
|
__builtin_prefetch(&arr[i + (1 * 16)]);
|
2021-03-11 22:34:44 +01:00
|
|
|
// Creates no object, struct is empty
|
2021-12-17 21:17:53 +01:00
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i, radicsOut);
|
2021-12-17 22:48:38 +01:00
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 1, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 2, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 3, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 4, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 5, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 6, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 7, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 8, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 9, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 10, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 11, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 12, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 13, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 14, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 15, radicsOut);
|
2021-12-18 01:23:06 +01:00
|
|
|
// Prefetch for read level-1 cache
|
|
|
|
|
__builtin_prefetch(&arr[i + (2 * 16)]);
|
2021-12-17 22:48:38 +01:00
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 16, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 17, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 18, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 19, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 20, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 21, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 22, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 23, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 24, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 25, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 26, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 27, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 28, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 29, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 30, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 31, radicsOut);
|
2021-12-18 01:23:06 +01:00
|
|
|
__builtin_prefetch(&arr[i + (3 * 16)]);
|
2021-12-17 22:48:38 +01:00
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 32, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 33, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 34, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 35, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 36, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 37, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 38, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 39, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 40, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 41, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 42, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 43, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 44, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 45, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 46, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 47, radicsOut);
|
2021-12-18 01:23:06 +01:00
|
|
|
// __builtin_prefetch(&arr[i + (4 * 16)]); // Only needed for longer than 64 unrolls
|
2021-12-17 22:48:38 +01:00
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 48, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 49, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 50, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 51, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 52, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 53, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 54, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 55, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 56, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 57, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 58, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 59, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 60, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 61, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 62, radicsOut);
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i + 63, radicsOut);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 01:48:42 +01:00
|
|
|
#pragma GCC unroll 4
|
2021-12-17 22:48:38 +01:00
|
|
|
for(; i < size; ++i) {
|
|
|
|
|
OccurenceMagic<DIGITS - 1, COUNTER_TYP>(arr, i, radicsOut);
|
2021-03-11 22:05:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-11 21:38:06 +01:00
|
|
|
|
2021-03-11 23:04:50 +01:00
|
|
|
/** Recursive Functor: no class should be generated I think (compiler should be smart) */
|
2021-12-17 21:17:53 +01:00
|
|
|
template<int DIGIT, typename COUNTER_TYP>
|
|
|
|
|
struct PrefixMagic : public PrefixMagic<DIGIT - 1, COUNTER_TYP> {
|
|
|
|
|
inline __attribute__((always_inline)) PrefixMagic(COUNTER_TYP *radics, COUNTER_TYP *prev, int i) noexcept
|
|
|
|
|
: PrefixMagic<DIGIT - 1, COUNTER_TYP>(radics, prev, i) {
|
2021-03-11 23:04:50 +01:00
|
|
|
static constexpr int DSTART = (DIGIT * DIGIT_RANGE);
|
|
|
|
|
radics[DSTART + i] += prev[DIGIT];
|
|
|
|
|
prev[DIGIT] = radics[DSTART + i];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
/** Ends template recursion */
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP>
|
|
|
|
|
struct PrefixMagic<-1, COUNTER_TYP> {
|
|
|
|
|
inline PrefixMagic(COUNTER_TYP *radics, COUNTER_TYP *prev, int i) noexcept {}
|
2021-03-11 23:04:50 +01:00
|
|
|
};
|
|
|
|
|
|
2021-03-13 15:51:24 +01:00
|
|
|
/** Gets REFERENCE to the given digit from the radix-array that has more than one digits */
|
2021-12-17 21:17:53 +01:00
|
|
|
template<int DIGIT, typename COUNTER_TYP>
|
|
|
|
|
static inline __attribute__((always_inline)) COUNTER_TYP &rGet(COUNTER_TYP *radics, int i) noexcept {
|
2021-03-13 15:51:24 +01:00
|
|
|
static constexpr int DSTART = (DIGIT * DIGIT_RANGE);
|
|
|
|
|
return radics[DSTART + i];
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 23:37:48 +01:00
|
|
|
/** Helper for calcPrefixSums */
|
|
|
|
|
template<int DIGIT, typename COUNTER_TYP>
|
|
|
|
|
struct PMagic2 : public PMagic2<DIGIT - 1, COUNTER_TYP> {
|
|
|
|
|
inline __attribute__((always_inline)) PMagic2(COUNTER_TYP *radics, COUNTER_TYP *prev)
|
|
|
|
|
: PMagic2<DIGIT - 1, COUNTER_TYP>(radics, prev) {
|
|
|
|
|
// Again first the 0th digit because of parent constructors!
|
|
|
|
|
// This is a template-unrolled loop too
|
|
|
|
|
PrefixMagic<DIGITS - 1, COUNTER_TYP>(radics, prev, DIGIT);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Template recursion endpoint */
|
|
|
|
|
template<typename COUNTER_TYP>
|
|
|
|
|
struct PMagic2<-1, COUNTER_TYP> {
|
|
|
|
|
inline __attribute__((always_inline)) PMagic2(COUNTER_TYP *radics, COUNTER_TYP *prev) {}
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP>
|
|
|
|
|
static inline void calcPrefixSums(COUNTER_TYP *radics) noexcept {
|
|
|
|
|
static thread_local COUNTER_TYP prev[DIGITS];
|
2021-03-11 23:04:50 +01:00
|
|
|
memset(prev, 0, sizeof(prev));
|
|
|
|
|
|
2021-12-17 23:37:48 +01:00
|
|
|
// This is a template-unrolled loop too
|
|
|
|
|
PMagic2<DIGIT_RANGE - 1, COUNTER_TYP>(radics, prev);
|
2021-03-11 22:05:55 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-13 15:51:24 +01:00
|
|
|
/** Recursive Functor: no class should be generated I think (compiler should be smart) */
|
2021-12-17 21:17:53 +01:00
|
|
|
template<int DIGIT, typename COUNTER_TYP>
|
|
|
|
|
struct RadixMagic : public RadixMagic<DIGIT - 1, COUNTER_TYP> {
|
2021-12-18 02:20:42 +01:00
|
|
|
inline __attribute__((always_inline)) RadixMagic(COUNTER_TYP *radics, uint32_t *from, uint32_t *to, COUNTER_TYP size) noexcept
|
2021-12-17 21:17:53 +01:00
|
|
|
: RadixMagic<DIGIT - 1, COUNTER_TYP>(radics, from, to, size) {
|
2021-12-18 02:34:22 +01:00
|
|
|
// Tricky: see (**)
|
|
|
|
|
if(this->swapped) { // never true for DIGIT 0
|
|
|
|
|
std::swap(from, to);
|
2021-12-18 02:20:42 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-13 15:51:24 +01:00
|
|
|
// DEBUG
|
|
|
|
|
//printf("%d before: ", DIGIT);
|
|
|
|
|
//debugArr(from, size);
|
|
|
|
|
|
2021-12-13 03:48:17 +01:00
|
|
|
#pragma GCC unroll 64
|
2021-12-17 21:17:53 +01:00
|
|
|
for(COUNTER_TYP i = size; i > 0; --i) { // right-to-left to ensure already sorted digits order we keep for iterations
|
2021-12-17 21:42:35 +01:00
|
|
|
// Prefetch caches
|
2021-12-17 22:09:35 +01:00
|
|
|
/*
|
2021-12-17 21:42:35 +01:00
|
|
|
__builtin_prefetch(&from[i]); // TODO: is good?
|
|
|
|
|
if(i >= 64) { __builtin_prefetch(&from[i - 64]); } // TODO: manually unroll?
|
2021-12-17 22:09:35 +01:00
|
|
|
*/
|
2021-03-13 15:51:24 +01:00
|
|
|
// Get num and its new offset / location
|
2021-12-18 02:34:22 +01:00
|
|
|
auto num = from[i - 1];
|
2021-03-13 15:51:24 +01:00
|
|
|
auto digVal = getDigit<DIGIT>(num);
|
|
|
|
|
auto offset = (--rGet<DIGIT>(radics, digVal));
|
|
|
|
|
|
|
|
|
|
// Add to the proper target location
|
2021-12-18 02:34:22 +01:00
|
|
|
to[offset] = num;
|
2021-03-11 23:20:03 +01:00
|
|
|
}
|
2021-03-13 15:51:24 +01:00
|
|
|
|
|
|
|
|
// DEBUG
|
|
|
|
|
//printf("%d after: ", DIGIT);
|
|
|
|
|
//debugArr(to, size);
|
|
|
|
|
|
2021-12-18 02:34:22 +01:00
|
|
|
// (**) Only swaps pointers above in the child class constructor IF NEEDED :-)
|
|
|
|
|
this->swapped = !this->swapped;
|
2021-03-11 23:20:03 +01:00
|
|
|
}
|
2021-03-13 15:51:24 +01:00
|
|
|
};
|
|
|
|
|
/** Ends template recursion */
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP>
|
|
|
|
|
struct RadixMagic<-1, COUNTER_TYP> {
|
2021-12-18 02:34:22 +01:00
|
|
|
bool swapped = false;
|
2021-12-17 21:17:53 +01:00
|
|
|
inline RadixMagic(COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept { }
|
2021-03-13 15:51:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* SORT */
|
2021-03-11 23:20:03 +01:00
|
|
|
|
2021-12-15 03:14:35 +01:00
|
|
|
/*
|
2021-12-15 00:43:25 +01:00
|
|
|
* Sort the given array (in-place sorting) with the given size.
|
|
|
|
|
*
|
|
|
|
|
* Rem.: If you use the VectorGiverWithReuse please remind yourself to Gc() it time-to-time!
|
|
|
|
|
*
|
2021-12-15 03:14:35 +01:00
|
|
|
* Beware: GC needs to happen on all threads that use us!
|
|
|
|
|
*
|
2021-12-15 00:43:25 +01:00
|
|
|
* @param arr The array to sort. Result will be in the same array - as sorted.
|
2021-12-17 21:17:53 +01:00
|
|
|
* @param size The lenght of the array - should fit in the COUNTER_TYP.
|
|
|
|
|
* @param COUNTER_TYP OPTIONAL: When set this type will be the counter type.
|
2021-12-15 03:14:35 +01:00
|
|
|
* @param REUSE OPTIONAL: When true, we reuse the array instead of always gettin' and releasin' from da heap.
|
|
|
|
|
* @param GC OPTIONAL: When true, we garbage collect memory from previous sorts if REUSE is true.
|
|
|
|
|
* @param GC_WITHOUT_SORT OPTIONAL: When true, we "just GC" but do not sort in case of GC is true.
|
2021-12-15 00:43:25 +01:00
|
|
|
*/
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP = size_t, bool REUSE = false, bool GC = false, bool GC_WITHOUT_SORT = false>
|
|
|
|
|
inline void __attribute__((always_inline)) sort_impl(uint32_t arr[], COUNTER_TYP size) noexcept {
|
2021-12-15 03:14:35 +01:00
|
|
|
// Most funny optimization is this multiply here :-)
|
|
|
|
|
//
|
|
|
|
|
// Literally.. come on.. this makes it nearly a compile-time, macro-like
|
|
|
|
|
// ifdef-like thing as we avoid memory allocations of size BUT also we
|
|
|
|
|
// optimize the first call for sort when we REUSE the array so size is fine!
|
|
|
|
|
static thread_local std::vector<uint32_t> arc(size * REUSE);
|
|
|
|
|
|
|
|
|
|
// "Garbage-collection"
|
|
|
|
|
if(GC) {
|
|
|
|
|
arc = std::vector<uint32_t>();
|
|
|
|
|
// This must be implemented, because we can only access
|
|
|
|
|
// the static in our function body so this is the "way".
|
|
|
|
|
if(GC_WITHOUT_SORT) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 22:05:55 +01:00
|
|
|
// Holds "digit" occurences, prefix sums, whatevers
|
|
|
|
|
// First "DIGIT_RANGE" elem is for MSB "DIGITS", last is for LSB
|
2021-12-17 21:17:53 +01:00
|
|
|
static thread_local COUNTER_TYP radics[DIGITS * DIGIT_RANGE];
|
2021-12-18 01:23:06 +01:00
|
|
|
// Write prefetchin'
|
|
|
|
|
//__builtin_prefetch(&radicsOut[..], 1);
|
|
|
|
|
PrefetchMagic<DIGITS * DIGIT_RANGE, (64/sizeof(COUNTER_TYP)), COUNTER_TYP, 1/*w*/> pm(radics);
|
2021-03-11 23:04:50 +01:00
|
|
|
memset(radics, 0, sizeof(radics));
|
2021-03-11 22:05:55 +01:00
|
|
|
|
|
|
|
|
// Calculate occurences of digits
|
2021-03-11 22:38:23 +01:00
|
|
|
countOccurences(arr, size, radics);
|
2021-03-11 22:05:55 +01:00
|
|
|
|
2021-12-17 21:17:53 +01:00
|
|
|
//debugRadics<COUNTER_TYP>(radics);
|
2021-03-11 23:20:03 +01:00
|
|
|
|
2021-03-11 22:05:55 +01:00
|
|
|
// Calculate prefix sums
|
2021-03-11 23:04:50 +01:00
|
|
|
calcPrefixSums(radics);
|
2021-03-11 21:22:37 +01:00
|
|
|
|
2021-12-17 21:17:53 +01:00
|
|
|
//debugRadics<COUNTER_TYP>(radics);
|
2021-03-13 15:51:24 +01:00
|
|
|
|
|
|
|
|
/* Regular (old) radix sort with small twist */
|
|
|
|
|
|
|
|
|
|
// Regular radix sort - I just changed occurence couting and prefix summing to have more ILP
|
|
|
|
|
// But because my approach does not use that, I want to keep this version in a branch for a
|
|
|
|
|
// regular radix sort using better ILP just to see how it is doing if I wrote those "Magic"
|
|
|
|
|
// above already anyways...
|
|
|
|
|
|
|
|
|
|
// Regular radix sort needs a copy, see: https://www.youtube.com/watch?v=ujb2CIWE8zY
|
2021-12-15 03:14:35 +01:00
|
|
|
// But instead of the below, we do a trickery...
|
|
|
|
|
//
|
2021-12-15 00:43:25 +01:00
|
|
|
//std::vector<uint32_t> arc(size);
|
|
|
|
|
//auto arc = VectorGiver::Give(size); // "auto" is needed for this to perform well with some givers!
|
2021-12-15 03:14:35 +01:00
|
|
|
//
|
|
|
|
|
// Rem.: The branch is optimized out in compile time!
|
|
|
|
|
if(REUSE) {
|
|
|
|
|
arc.resize(size);
|
|
|
|
|
} else {
|
|
|
|
|
// Must not be .clean() !!!
|
|
|
|
|
// We must regain memory of previous!
|
|
|
|
|
arc = std::move(std::vector<uint32_t>(size));
|
|
|
|
|
}
|
2021-03-13 15:51:24 +01:00
|
|
|
|
|
|
|
|
uint32_t *from = arr;
|
|
|
|
|
uint32_t *to = &arc[0];
|
|
|
|
|
|
2021-12-18 02:20:42 +01:00
|
|
|
RadixMagic<DIGITS - 1, COUNTER_TYP> r(radics, from, to, size);
|
2021-03-13 15:51:24 +01:00
|
|
|
|
|
|
|
|
// With an other API we could spare this copy if we can delete original arr and return ptr or something...
|
|
|
|
|
// I am fine with this... this is not my main idea anyways, just little ILP tweak to regular radix sort
|
2021-12-15 03:14:35 +01:00
|
|
|
//if(to != arr) // <- logically, but bad they are already swapped here!!! BEWARE
|
2021-12-18 02:34:22 +01:00
|
|
|
if(r.swapped) { // <- in reality this is what we want because of last swap happened anyways!
|
|
|
|
|
memcpy(arr, to, size);
|
2021-03-13 15:51:24 +01:00
|
|
|
}
|
2021-03-11 21:22:37 +01:00
|
|
|
}
|
2021-12-15 03:14:35 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Garbage collect reused data structures from last call.
|
|
|
|
|
*
|
|
|
|
|
* This is optimized and is a NO-OP if MAGYAR_SORT_DEFAULT_REUSE is not defined!
|
|
|
|
|
* - unless you use the FORCE! May it be with you if you need it.
|
|
|
|
|
*
|
|
|
|
|
* @param FORCE OPTIONAL: When true, the gc happens even if MAGYAR_SORT_DEFAULT_REUSE is not defined!
|
|
|
|
|
*/
|
2021-12-17 21:17:53 +01:00
|
|
|
template<bool FORCE = false, typename COUNTER_TYP = size_t>
|
2021-12-15 03:14:35 +01:00
|
|
|
inline void gc() noexcept {
|
|
|
|
|
if(FORCE) {
|
|
|
|
|
// Only GC-ing
|
2021-12-17 21:17:53 +01:00
|
|
|
MagyarSort::sort_impl<COUNTER_TYP, true, true, true>(nullptr, 0);
|
2021-12-15 03:14:35 +01:00
|
|
|
} else {
|
|
|
|
|
#ifdef MAGYAR_SORT_DEFAULT_REUSE
|
|
|
|
|
// Only GC-ing
|
2021-12-17 21:17:53 +01:00
|
|
|
MagyarSort::sort_impl<COUNTER_TYP, true, true, true>(nullptr, 0);
|
2021-12-15 03:14:35 +01:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sort the given array (in-place sorting) with the given size.
|
|
|
|
|
*
|
|
|
|
|
* Rem.: Please remind yourself to cc() from time-to-time!
|
|
|
|
|
* Rem.: Thread-safe to use!
|
|
|
|
|
*
|
|
|
|
|
* Beware: MagyarSort::gc<true>(); needs to happen on all threads that use this variant otherwise memory leaks away!
|
|
|
|
|
* Please mind the "true" template parameter that forces the GC even when sort by default not reuses...
|
|
|
|
|
*
|
|
|
|
|
* @param arr The array to sort. Result will be in the same array - as sorted.
|
|
|
|
|
* @param size The lenght of the array.
|
2021-12-17 21:17:53 +01:00
|
|
|
* @param COUNTER_TYP OPTIONAL: It is best kepts as size_t, but for smaller arrays, you can use uint32_t.
|
2021-12-15 03:14:35 +01:00
|
|
|
* @param GC OPTIONAL: When true, we garbage collect before this sort - so cached memory size will be "size" elems.
|
|
|
|
|
*/
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP = size_t, bool GC = false>
|
|
|
|
|
inline void __attribute__((always_inline)) sort_reuse(uint32_t arr[], COUNTER_TYP size) noexcept {
|
2021-12-15 03:14:35 +01:00
|
|
|
// Reuse the temporary vectors across runs
|
|
|
|
|
// This results in much less heap allocations and much faster on gcc
|
|
|
|
|
// and also a bit faster on clang too.
|
2021-12-17 21:17:53 +01:00
|
|
|
MagyarSort::sort_impl<COUNTER_TYP, true>(arr, size);
|
2021-12-15 03:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sort the given array (in-place sorting) with the given size.
|
|
|
|
|
*
|
|
|
|
|
* Rem.: Thread-safe to use!
|
|
|
|
|
*
|
|
|
|
|
* Beware: MagyarSort::gc(); needs to happen on all threads that use this variant otherwise memory leaks away!
|
|
|
|
|
*
|
|
|
|
|
* @param arr The array to sort. Result will be in the same array - as sorted.
|
|
|
|
|
* @param size The lenght of the array.
|
2021-12-17 21:17:53 +01:00
|
|
|
* @param COUNTER_TYP OPTIONAL: It is best kepts as size_t, but for smaller arrays, you can use uint32_t.
|
2021-12-15 03:14:35 +01:00
|
|
|
*/
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP = size_t>
|
2021-12-15 03:14:35 +01:00
|
|
|
inline void __attribute__((always_inline)) sort_no_reuse(uint32_t arr[], size_t size) noexcept {
|
|
|
|
|
// We use the heap once per every call...
|
|
|
|
|
// This is safer and we do not need garbage collecting
|
2021-12-17 21:17:53 +01:00
|
|
|
MagyarSort::sort_impl<COUNTER_TYP>(arr, size);
|
2021-12-15 03:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Sort the given array (in-place sorting) with the given size.
|
|
|
|
|
*
|
|
|
|
|
* Rem.: If you use the VectorGiverWithReuse please remind yourself to Gc() it time-to-time!
|
|
|
|
|
*
|
|
|
|
|
* Beware: MagyarSort::gc(); should be called after "sort bursts" (consecutive fast sorts of when you need memory
|
|
|
|
|
* on all threads that use this variant otherwise memory leaks away as biggest sorted array keeps being in ram!
|
|
|
|
|
* This depends on the config #define MAGYAR_SORT_DEFAULT_REUSE is defined or not. Define and you get reuse
|
|
|
|
|
* and if you get reuse you can call multiple sorts with reused temporary buffers that you gc() afterwards!
|
|
|
|
|
*
|
|
|
|
|
* @param arr The array to sort. Result will be in the same array - as sorted.
|
|
|
|
|
* @param size The lenght of the array.
|
2021-12-17 21:17:53 +01:00
|
|
|
* @param COUNTER_TYP OPTIONAL: It is best kepts as size_t, but for smaller arrays, you can use uint32_t.
|
2021-12-15 03:14:35 +01:00
|
|
|
*/
|
2021-12-17 21:17:53 +01:00
|
|
|
template<typename COUNTER_TYP = size_t>
|
2021-12-15 03:14:35 +01:00
|
|
|
inline void sort(uint32_t arr[], size_t size) noexcept {
|
|
|
|
|
#ifdef MAGYAR_SORT_DEFAULT_REUSE
|
2021-12-17 21:17:53 +01:00
|
|
|
MagyarSort::sort_reuse<COUNTER_TYP>(arr, size);
|
2021-12-15 03:14:35 +01:00
|
|
|
#else
|
2021-12-17 21:17:53 +01:00
|
|
|
MagyarSort::sort_no_reuse<COUNTER_TYP>(arr, size);
|
2021-12-15 03:14:35 +01:00
|
|
|
#endif
|
|
|
|
|
}
|
2021-03-11 21:22:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|