little optimization to gpts and mine space partition bucket sort

This commit is contained in:
Richard Thier 2022-12-10 11:04:53 +01:00
parent 7e21807668
commit 50b1997d5c
3 changed files with 485 additions and 442 deletions

View File

@ -2,6 +2,7 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
// ChatGPT and me did this space partitioning bucket sort
void gpt_bucket_sort(uint32_t* array, int n) { void gpt_bucket_sort(uint32_t* array, int n) {
// Calculate the number of buckets to use // Calculate the number of buckets to use
int num_buckets = std::sqrt(n); int num_buckets = std::sqrt(n);
@ -36,3 +37,40 @@ void gpt_bucket_sort(uint32_t* array, int n) {
} }
} }
} }
// Further optimizations (no chatGPT)
void my_bucket_sort(uint32_t* array, int n) {
// Calculate the number of buckets to use
int num_buckets = std::sqrt(n);
// Create a vector of buckets
std::vector<std::vector<uint32_t>> buckets(num_buckets);
// Calculate the range of values that each bucket can hold
auto mm = std::minmax_element(array, array + n);
uint32_t min_value = *mm.first;
uint32_t max_value = *mm.second;
uint32_t range = max_value - min_value + 1;
uint32_t bucket_size = range / num_buckets + 1;
// Distribute the elements of the array into the buckets
for (int i = 0; i < n; i++) {
// Calculate the bucket index for this element
// using the range of values and the bucket size as the divisor
int bucket_index = (array[i] - min_value) / bucket_size;
buckets[bucket_index].push_back(array[i]);
}
// Sort the elements in each bucket using std::sort
for (int i = 0; i < num_buckets; i++) {
std::sort(buckets[i].begin(), buckets[i].end());
}
// Concatenate the buckets to get the sorted array
int k = 0;
for (int i = 0; i < num_buckets; i++) {
for (int j = 0; j < buckets[i].size(); j++) {
array[k++] = buckets[i][j];
}
}
}

View File

@ -31,5 +31,8 @@ clang_release: test.cpp magyarsort.h
clang_release3: test.cpp magyarsort.h clang_release3: test.cpp magyarsort.h
clang++ test.cpp -DNDEBUG -std=c++17 -O3 -o test.out clang++ test.cpp -DNDEBUG -std=c++17 -O3 -o test.out
clang_release_ypsu: ypsu.cpp magyarsort.h
clang++ ypsu.cpp -DNDEBUG -std=c++17 -O2 -o ypsu.out
clean: test.out clean: test.out
rm test.out rm test.out

116
ypsu.cpp
View File

@ -1,28 +1,28 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <chrono> #include <chrono>
#include <climits> #include <climits>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <functional> #include <functional>
#include <map> #include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <vector> #include <vector>
#include <numeric> #include <numeric>
#include <sys/mman.h> // mlock & munlock #include <sys/mman.h> // mlock & munlock
#include "ska_sort.hpp" #include "ska_sort.hpp"
#include "gptsort.h" #include "gptsort.h"
#define MAGYAR_SORT_DEFAULT_REUSE #define MAGYAR_SORT_DEFAULT_REUSE
#include "magyarsort.h" #include "magyarsort.h"
#include "space_partitioning_sort/spsort.h" #include "space_partitioning_sort/spsort.h"
std::map<std::string, double> results; std::map<std::string, double> results;
std::map<std::string, double> worst; std::map<std::string, double> worst;
void measure(const std::string &inputtype, const std::string &name, void measure(const std::string &inputtype, const std::string &name,
std::function<void()> f) { std::function<void()> f) {
auto begin = std::chrono::high_resolution_clock::now(); auto begin = std::chrono::high_resolution_clock::now();
f(); f();
@ -30,12 +30,12 @@
double seconds = dur / std::chrono::milliseconds(1) / 1000.0; double seconds = dur / std::chrono::milliseconds(1) / 1000.0;
results[name] = seconds; results[name] = seconds;
worst[name] = std::max(worst[name], seconds); worst[name] = std::max(worst[name], seconds);
} }
std::vector<std::string> inputtypes = { std::vector<std::string> inputtypes = {
"constant", "asc", "desc", "ascasc", "ascdesc", "constant", "asc", "desc", "ascasc", "ascdesc",
"descasc", "descdesc", "smallrange", "rand", "descasc", "descdesc", "smallrange", "rand",
}; };
std::vector<uint32_t> geninput(const std::string &type, int n) { std::vector<uint32_t> geninput(const std::string &type, int n) {
std::vector<uint32_t> v(n); std::vector<uint32_t> v(n);
if (type == "constant") { if (type == "constant") {
int c = rand(); int c = rand();
@ -81,9 +81,9 @@
} }
} }
return v; return v;
} }
void twopass(uint32_t *a, int n) { void twopass(uint32_t *a, int n) {
assert(n * int64_t(sizeof(a[0])) <= INT_MAX); assert(n * int64_t(sizeof(a[0])) <= INT_MAX);
// alloc helper buffers. // alloc helper buffers.
int sz = n * sizeof(a[0]); int sz = n * sizeof(a[0]);
@ -110,8 +110,8 @@
} }
for (int i = 0; i < n; i++) a[bucketdata[buf[i] >> 16]++] = buf[i]; for (int i = 0; i < n; i++) a[bucketdata[buf[i] >> 16]++] = buf[i];
free(buf); free(buf);
} }
void fourpass(uint32_t *a, int n) { void fourpass(uint32_t *a, int n) {
assert(n * int64_t(sizeof(a[0])) <= INT_MAX); assert(n * int64_t(sizeof(a[0])) <= INT_MAX);
// alloc helper buffers. // alloc helper buffers.
int sz = n * sizeof(a[0]); int sz = n * sizeof(a[0]);
@ -136,15 +136,15 @@
dst = (uint32_t *)((uintptr_t)dst ^ swapmask); dst = (uint32_t *)((uintptr_t)dst ^ swapmask);
} }
free(buf); free(buf);
} }
/** Only werks für das fourpassu! */ /** Only werks für das fourpassu! */
void my_memset(int *v) { void my_memset(int *v) {
memset(v, 0, (1 << 8) * sizeof(int)); memset(v, 0, (1 << 8) * sizeof(int));
} }
// hand-unrolled fourpass. // hand-unrolled fourpass.
void fourpassu(uint32_t *a, int n) { void fourpassu(uint32_t *a, int n) {
assert(n * int64_t(sizeof(a[0])) <= INT_MAX); assert(n * int64_t(sizeof(a[0])) <= INT_MAX);
// alloc helper buffers. // alloc helper buffers.
int sz = n * sizeof(a[0]); int sz = n * sizeof(a[0]);
@ -205,10 +205,10 @@
#pragma GCC unroll 32 #pragma GCC unroll 32
for (int i = 0; i < n; i++) a[bucketdata[buf[i] >> 24 & 0xff]++] = buf[i]; for (int i = 0; i < n; i++) a[bucketdata[buf[i] >> 24 & 0xff]++] = buf[i];
free(buf); free(buf);
} }
static inline uint32_t byterotate(uint32_t x) { return (x >> 8) | (x << 24); } static inline uint32_t byterotate(uint32_t x) { return (x >> 8) | (x << 24); }
void fourrots(uint32_t *arr, int n) { void fourrots(uint32_t *arr, int n) {
assert(n * int64_t(sizeof(arr[0])) <= INT_MAX); assert(n * int64_t(sizeof(arr[0])) <= INT_MAX);
assert(n % 4 == 0); assert(n % 4 == 0);
// alloc helper buffers. // alloc helper buffers.
@ -261,10 +261,10 @@
dst = (uint32_t *)((uintptr_t)dst ^ swapmask); dst = (uint32_t *)((uintptr_t)dst ^ swapmask);
} }
free(buf); free(buf);
} }
// frewr - four rewrites. // frewr - four rewrites.
void frewr(uint32_t *arr, int n) { void frewr(uint32_t *arr, int n) {
uint32_t *tmpbuf = (uint32_t *)malloc(n * 4); uint32_t *tmpbuf = (uint32_t *)malloc(n * 4);
mlock(tmpbuf, n * 4); mlock(tmpbuf, n * 4);
int btoffsets[4][256] = {}; int btoffsets[4][256] = {};
@ -302,9 +302,9 @@
} }
munlock(tmpbuf, n * 4); munlock(tmpbuf, n * 4);
free(tmpbuf); free(tmpbuf);
} }
void vsort(uint32_t *a, int n) { void vsort(uint32_t *a, int n) {
thread_local std::vector<uint32_t> bts[256]; thread_local std::vector<uint32_t> bts[256];
#pragma GCC unroll 4 #pragma GCC unroll 4
for (int shift = 0; shift < 32; shift += 8) { for (int shift = 0; shift < 32; shift += 8) {
@ -317,8 +317,8 @@
bts[bt].clear(); bts[bt].clear();
} }
} }
} }
void pagedsort(uint32_t *a, int n) { void pagedsort(uint32_t *a, int n) {
enum { pagesize = 1024 }; enum { pagesize = 1024 };
int pagecount = (n + pagesize - 1) / pagesize + 512; int pagecount = (n + pagesize - 1) / pagesize + 512;
uint32_t *pd = (uint32_t *)malloc(pagecount * pagesize * sizeof(a[0])); uint32_t *pd = (uint32_t *)malloc(pagecount * pagesize * sizeof(a[0]));
@ -390,16 +390,17 @@
} }
} }
free(pd); free(pd);
} }
// to measure / profile a single variant // to measure / profile a single variant
void measure_single(int n) { void measure_single(int n) {
for (auto inputtype : inputtypes) { for (auto inputtype : inputtypes) {
printf("%10s", inputtype.c_str()); printf("%10s", inputtype.c_str());
fflush(stdout); fflush(stdout);
std::vector<uint32_t> v(n); std::vector<uint32_t> v(n);
v = geninput(inputtype, n); v = geninput(inputtype, n);
measure(inputtype, "sp", [&] { spsort(&v[0], v.size()); }); //measure(inputtype, "sp", [&] { spsort(&v[0], v.size()); });
measure(inputtype, "magyar", [&] { MagyarSort::sort<uint32_t>(&v[0], v.size()); });
for (auto r : results) printf("%9.3fs", r.second); for (auto r : results) printf("%9.3fs", r.second);
puts(""); puts("");
@ -411,11 +412,11 @@
printf("%10s", ""); printf("%10s", "");
for (auto w : worst) printf("%10s", w.first.c_str()); for (auto w : worst) printf("%10s", w.first.c_str());
puts(""); puts("");
} }
int main(void) { int main(void) {
int n = 100000000; //int n = 100000000;
//int n = 10000000; int n = 10000000;
//int n = 100; //int n = 100;
// Uncomment this for profiling and alg! // Uncomment this for profiling and alg!
@ -457,16 +458,17 @@
w = v; w = v;
measure(inputtype, "4pasu", [&] { fourpassu(&w[0], w.size()); }); measure(inputtype, "4pasu", [&] { fourpassu(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
*/
w = v; w = v;
measure(inputtype, "4rot", [&] { fourrots(&w[0], w.size()); }); measure(inputtype, "4rot", [&] { fourrots(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
w = v; w = v;
/*measure(inputtype, "sp", [&] { spsort(&w[0], w.size()); }); measure(inputtype, "sp", [&] { spsort(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
w = v;*/ w = v;*/
measure(inputtype, "gptbuck", [&] { gpt_bucket_sort(&w[0], w.size()); }); measure(inputtype, "gptbuck", [&] { gpt_bucket_sort(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
measure(inputtype, "mybuck", [&] { my_bucket_sort(&w[0], w.size()); });
assert(w == expected);
/* /*
w = v; w = v;
measure(inputtype, "frewr", [&] { frewr(&w[0], w.size()); }); measure(inputtype, "frewr", [&] { frewr(&w[0], w.size()); });
@ -487,4 +489,4 @@
for (auto w : worst) printf("%10s", w.first.c_str()); for (auto w : worst) printf("%10s", w.first.c_str());
puts(""); puts("");
return 0; return 0;
} }