From 50c96c3ee5c25d9de38449a442679f895322f04e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 24 Feb 2021 20:55:25 +0100 Subject: More file renames --- benchmarks/crand_benchmark.cpp | 201 ---------------------------------------- benchmarks/crand_benchmark2.cpp | 138 --------------------------- benchmarks/shootout4_crand.cpp | 201 ++++++++++++++++++++++++++++++++++++++++ benchmarks/shootout5_crand.cpp | 137 +++++++++++++++++++++++++++ 4 files changed, 338 insertions(+), 339 deletions(-) delete mode 100644 benchmarks/crand_benchmark.cpp delete mode 100644 benchmarks/crand_benchmark2.cpp create mode 100644 benchmarks/shootout4_crand.cpp create mode 100644 benchmarks/shootout5_crand.cpp diff --git a/benchmarks/crand_benchmark.cpp b/benchmarks/crand_benchmark.cpp deleted file mode 100644 index df13befd..00000000 --- a/benchmarks/crand_benchmark.cpp +++ /dev/null @@ -1,201 +0,0 @@ -#include -#include -#include -#include - -static inline uint64_t rotl64(const uint64_t x, const int k) - { return (x << k) | (x >> (64 - k)); } - -static uint64_t splitmix64_x = 87213627321ull; /* The state can be seeded with any value. */ - -uint64_t splitmix64(void) { - uint64_t z = (splitmix64_x += 0x9e3779b97f4a7c15); - z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; - z = (z ^ (z >> 27)) * 0x94d049bb133111eb; - return z ^ (z >> 31); -} - -static void init_state(uint64_t *rng, uint64_t seed) { - splitmix64_x = seed; - for (int i=0; i<4; ++i) rng[i] = splitmix64(); -} - -/* jsf64 */ - -static inline uint64_t jsf64(uint64_t *s) { - uint64_t e = s[0] - rotl64(s[1], 7); - s[0] = s[1] ^ rotl64(s[2], 13); - s[1] = s[2] + rotl64(s[3], 37); - s[2] = s[3] + e; - s[3] = e + s[0]; - return s[3]; -} - -/* sfc64 */ - -static inline uint64_t sfc64(uint64_t *s) { - uint64_t result = s[0] + s[1] + s[3]++; - s[0] = s[1] ^ (s[1] >> 11); - s[1] = s[2] + (s[2] << 3); - s[2] = rotl64(s[2], 24) + result; - return result; -} - -/* sfc64 with Weyl increment */ -static uint64_t weyl = 1234566789123ull; -static inline uint64_t sfc64w(uint64_t *s) { - uint64_t result = s[0] + s[1] + (s[3] += weyl|1); - s[0] = s[1] ^ (s[1] >> 11); - s[1] = s[2] + (s[2] << 3); - s[2] = rotl64(s[2], 24) + result; - return result; -} - - -/* xoshiro256** */ - -static inline uint64_t xoshiro256starstar(uint64_t* s) { - const uint64_t result = rotl64(s[1] * 5, 7) * 9; - const uint64_t t = s[1] << 17; - s[2] ^= s[0]; - s[3] ^= s[1]; - s[1] ^= s[2]; - s[0] ^= s[3]; - s[2] ^= t; - s[3] = rotl64(s[3], 45); - return result; -} - -// wyrand - 2020-12-07 -static inline void _wymum(uint64_t *A, uint64_t *B){ -#if defined(__SIZEOF_INT128__) - __uint128_t r = *A; r *= *B; - *A = (uint64_t) r; *B = (uint64_t ) (r >> 64); -#elif defined(_MSC_VER) && defined(_M_X64) - *A = _umul128(*A, *B, B); -#else - uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B, hi, lo; - uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t>32)+(rm1>>32)+c; - *A=lo; *B=hi; -#endif -} -static inline uint64_t _wymix(uint64_t A, uint64_t B){ - _wymum(&A,&B); return A^B; -} -static inline uint64_t wyrand64(uint64_t *seed){ - static const uint64_t _wyp[] = {0xa0761d6478bd642full, 0xe7037ed1a0b428dbull}; - *seed+=_wyp[0]; return _wymix(*seed,*seed^_wyp[1]); -} - - -inline unsigned long long lehmer64(uint64_t* s) { - *(__uint128_t *)s *= 0xda942042e4dd58b5ull; - return *(__uint128_t *)s >> 64; -} - -using namespace std; - -int main(void) -{ - enum {N = 800000000}; - uint64_t* recipient = new uint64_t[N]; - static stc64_t rng; - init_state(rng.state, 12345123); - - cout << "WARMUP" << endl; - for (size_t i = 0; i < N; i++) - recipient[i] = wyrand64(rng.state); - - clock_t beg, end; - for (size_t ti = 0; ti < 4; ti++) { - cout << endl << "ROUND " << ti+1 << endl; - beg = clock(); - for (size_t i = 0; i < N; i++) - recipient[i] = wyrand64(rng.state); - end = clock(); - cout << "wyrand64:\t" - << (float(end - beg) / CLOCKS_PER_SEC) - << " s: " << recipient[312] << endl; - beg = clock(); - for (size_t i = 0; i < N; i++) - recipient[i] = sfc64w(rng.state); - end = clock(); - cout << "sfc64w:\t\t" - << (float(end - beg) / CLOCKS_PER_SEC) - << " s: " << recipient[312] << endl; - - beg = clock(); - for (size_t i = 0; i < N; i++) - recipient[i] = stc64_rand(&rng); - end = clock(); - cout << "stc64:\t\t" - << (float(end - beg) / CLOCKS_PER_SEC) - << " s: " << recipient[312] << endl; - - beg = clock(); - for (size_t i = 0; i < N; i++) - recipient[i] = xoshiro256starstar(rng.state); - end = clock(); - cout << "xoshiro256**:\t" - << (float(end - beg) / CLOCKS_PER_SEC) - << " s: " << recipient[312] << endl; - - beg = clock(); - for (size_t i = 0; i < N; i++) - recipient[i] = lehmer64(rng.state); - end = clock(); - cout << "lehmer64:\t" - << ((float) end - beg) / CLOCKS_PER_SEC - << " s: " << recipient[312] << endl; - - cout << "Next we do random number computations only, doing no work." - << endl; - uint64_t s = 0; - beg = clock(); - for (size_t i = 0; i < N; i++) - s += wyrand64(rng.state); - end = clock(); - cout << "wyrand64:\t" - << ((float) end - beg) / CLOCKS_PER_SEC - << " s: " << s << endl; - - s = 0; - beg = clock(); - for (size_t i = 0; i < N; i++) - s += sfc64w(rng.state); - end = clock(); - cout << "sfc64w:\t\t" - << ((float) end - beg) / CLOCKS_PER_SEC - << " s: " << s << endl; - - s = 0; - beg = clock(); - for (size_t i = 0; i < N; i++) - s += stc64_rand(&rng); - end = clock(); - cout << "stc64:\t\t" - << ((float) end - beg) / CLOCKS_PER_SEC - << " s: " << s << endl; - - s = 0; - beg = clock(); - for (size_t i = 0; i < N; i++) - s += xoshiro256starstar(rng.state); - end = clock(); - cout << "xoshiro256**:\t" - << ((float) end - beg) / CLOCKS_PER_SEC - << " s: " << s << endl; - - s = 0; - beg = clock(); - for (size_t i = 0; i < N; i++) - s += lehmer64(rng.state); - end = clock(); - cout << "lehmer64:\t" - << ((float) end - beg) / CLOCKS_PER_SEC - << " s: " << s << endl; - } - delete[] recipient; - return 0; -} diff --git a/benchmarks/crand_benchmark2.cpp b/benchmarks/crand_benchmark2.cpp deleted file mode 100644 index 6a517a0b..00000000 --- a/benchmarks/crand_benchmark2.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include -#include "stc/crandom.h" -#include "others/pcg_random.hpp" - -static struct stc32_state { stc64_t rng; uint64_t spare; unsigned n; } stc32_global = - {{0x7a5fed, 0x8e3f52, 0x9bc713, 0x6a09e667a7541669}, 0, 0}; - -STC_INLINE void stc32_srandom(uint64_t seed) { stc32_global.rng = stc64_init(seed); } -STC_INLINE uint32_t stc32_random(void) { - return (uint32_t) (++stc32_global.n & 1 ? (stc32_global.spare = stc64_rand(&stc32_global.rng)) - : (stc32_global.spare >> 32)); -} - -static unsigned long myrand_next = 1; - -/* RAND_MAX assumed to be 32767 */ -int myrand(void) { - myrand_next = myrand_next * 214013 + 2531011; - return (myrand_next >> 16) & 0x7fff; -} - -void mysrand(unsigned seed) { - myrand_next = seed; -} - - -enum {N = 1000000000}; - -void test1(void) -{ - clock_t diff, before; - uint64_t sum; - - std::random_device device; - std::mt19937 rng(device()); - std::uniform_int_distribution idist(1, 10); - std::uniform_real_distribution fdist(1, 10); - - before = clock(); - sum = 0; - c_forrange (N) { - sum += rng(); - } - diff = clock() - before; - printf("std::random:\t\t%.02f, %zu, sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng); - - before = clock(); - sum = 0; - c_forrange (N) { - sum += idist(rng); - } - diff = clock() - before; - printf("std::uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum); - - c_forrange (30) printf("%02d ", idist(rng)); - puts(""); - c_forrange (8) printf("%f ", fdist(rng)); - puts("\n"); -} - -void test2() -{ - clock_t diff, before; - uint64_t sum; - - // Seed with a real random value, if available - pcg_extras::seed_seq_from seed_source; - - // Make a random number engine - pcg64 rng(seed_source); - - // Choose a random mean between 1 and 10 - std::uniform_int_distribution idist(1, 10); - std::uniform_real_distribution fdist(1, 10); - - before = clock(); - sum = 0; - c_forrange (N) { - sum += rng(); - } - diff = clock() - before; - printf("pcg64::random:\t\t%.02f, %zu, sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng); - - before = clock(); - sum = 0; - c_forrange (N) { - sum += idist(rng); - } - diff = clock() - before; - printf("pcg64::uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum); - - c_forrange (30) printf("%02d ", idist(rng)); - puts(""); - c_forrange (8) printf("%f ", fdist(rng)); - puts("\n"); -} - - -void test3(void) -{ - clock_t diff, before; - uint64_t sum; - - stc64_t rng = stc64_init(time(NULL)); - stc64_uniform_t idist = stc64_uniform_init(1, 10); - stc64_uniformf_t fdist = stc64_uniformf_init(1, 10); - - before = clock(); - sum = 0; - c_forrange (N) { - //sum += stc64_rand(&rng); - sum += rand(); - } - diff = clock() - before; - printf("stc64_random:\t\t%.02f, %zu sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng); - - before = clock(); - sum = 0; - c_forrange (N) { - sum += stc64_uniform(&rng, &idist); - } - diff = clock() - before; - printf("stc64_uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum); - - c_forrange (30) printf("%02zd ", stc64_uniform(&rng, &idist)); - puts(""); - c_forrange (8) printf("%f ", stc64_uniformf(&rng, &fdist)); - puts("\n"); -} - -int main() -{ - test1(); - test2(); - test3(); -} \ No newline at end of file diff --git a/benchmarks/shootout4_crand.cpp b/benchmarks/shootout4_crand.cpp new file mode 100644 index 00000000..df13befd --- /dev/null +++ b/benchmarks/shootout4_crand.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +#include + +static inline uint64_t rotl64(const uint64_t x, const int k) + { return (x << k) | (x >> (64 - k)); } + +static uint64_t splitmix64_x = 87213627321ull; /* The state can be seeded with any value. */ + +uint64_t splitmix64(void) { + uint64_t z = (splitmix64_x += 0x9e3779b97f4a7c15); + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; + return z ^ (z >> 31); +} + +static void init_state(uint64_t *rng, uint64_t seed) { + splitmix64_x = seed; + for (int i=0; i<4; ++i) rng[i] = splitmix64(); +} + +/* jsf64 */ + +static inline uint64_t jsf64(uint64_t *s) { + uint64_t e = s[0] - rotl64(s[1], 7); + s[0] = s[1] ^ rotl64(s[2], 13); + s[1] = s[2] + rotl64(s[3], 37); + s[2] = s[3] + e; + s[3] = e + s[0]; + return s[3]; +} + +/* sfc64 */ + +static inline uint64_t sfc64(uint64_t *s) { + uint64_t result = s[0] + s[1] + s[3]++; + s[0] = s[1] ^ (s[1] >> 11); + s[1] = s[2] + (s[2] << 3); + s[2] = rotl64(s[2], 24) + result; + return result; +} + +/* sfc64 with Weyl increment */ +static uint64_t weyl = 1234566789123ull; +static inline uint64_t sfc64w(uint64_t *s) { + uint64_t result = s[0] + s[1] + (s[3] += weyl|1); + s[0] = s[1] ^ (s[1] >> 11); + s[1] = s[2] + (s[2] << 3); + s[2] = rotl64(s[2], 24) + result; + return result; +} + + +/* xoshiro256** */ + +static inline uint64_t xoshiro256starstar(uint64_t* s) { + const uint64_t result = rotl64(s[1] * 5, 7) * 9; + const uint64_t t = s[1] << 17; + s[2] ^= s[0]; + s[3] ^= s[1]; + s[1] ^= s[2]; + s[0] ^= s[3]; + s[2] ^= t; + s[3] = rotl64(s[3], 45); + return result; +} + +// wyrand - 2020-12-07 +static inline void _wymum(uint64_t *A, uint64_t *B){ +#if defined(__SIZEOF_INT128__) + __uint128_t r = *A; r *= *B; + *A = (uint64_t) r; *B = (uint64_t ) (r >> 64); +#elif defined(_MSC_VER) && defined(_M_X64) + *A = _umul128(*A, *B, B); +#else + uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B, hi, lo; + uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t>32)+(rm1>>32)+c; + *A=lo; *B=hi; +#endif +} +static inline uint64_t _wymix(uint64_t A, uint64_t B){ + _wymum(&A,&B); return A^B; +} +static inline uint64_t wyrand64(uint64_t *seed){ + static const uint64_t _wyp[] = {0xa0761d6478bd642full, 0xe7037ed1a0b428dbull}; + *seed+=_wyp[0]; return _wymix(*seed,*seed^_wyp[1]); +} + + +inline unsigned long long lehmer64(uint64_t* s) { + *(__uint128_t *)s *= 0xda942042e4dd58b5ull; + return *(__uint128_t *)s >> 64; +} + +using namespace std; + +int main(void) +{ + enum {N = 800000000}; + uint64_t* recipient = new uint64_t[N]; + static stc64_t rng; + init_state(rng.state, 12345123); + + cout << "WARMUP" << endl; + for (size_t i = 0; i < N; i++) + recipient[i] = wyrand64(rng.state); + + clock_t beg, end; + for (size_t ti = 0; ti < 4; ti++) { + cout << endl << "ROUND " << ti+1 << endl; + beg = clock(); + for (size_t i = 0; i < N; i++) + recipient[i] = wyrand64(rng.state); + end = clock(); + cout << "wyrand64:\t" + << (float(end - beg) / CLOCKS_PER_SEC) + << " s: " << recipient[312] << endl; + beg = clock(); + for (size_t i = 0; i < N; i++) + recipient[i] = sfc64w(rng.state); + end = clock(); + cout << "sfc64w:\t\t" + << (float(end - beg) / CLOCKS_PER_SEC) + << " s: " << recipient[312] << endl; + + beg = clock(); + for (size_t i = 0; i < N; i++) + recipient[i] = stc64_rand(&rng); + end = clock(); + cout << "stc64:\t\t" + << (float(end - beg) / CLOCKS_PER_SEC) + << " s: " << recipient[312] << endl; + + beg = clock(); + for (size_t i = 0; i < N; i++) + recipient[i] = xoshiro256starstar(rng.state); + end = clock(); + cout << "xoshiro256**:\t" + << (float(end - beg) / CLOCKS_PER_SEC) + << " s: " << recipient[312] << endl; + + beg = clock(); + for (size_t i = 0; i < N; i++) + recipient[i] = lehmer64(rng.state); + end = clock(); + cout << "lehmer64:\t" + << ((float) end - beg) / CLOCKS_PER_SEC + << " s: " << recipient[312] << endl; + + cout << "Next we do random number computations only, doing no work." + << endl; + uint64_t s = 0; + beg = clock(); + for (size_t i = 0; i < N; i++) + s += wyrand64(rng.state); + end = clock(); + cout << "wyrand64:\t" + << ((float) end - beg) / CLOCKS_PER_SEC + << " s: " << s << endl; + + s = 0; + beg = clock(); + for (size_t i = 0; i < N; i++) + s += sfc64w(rng.state); + end = clock(); + cout << "sfc64w:\t\t" + << ((float) end - beg) / CLOCKS_PER_SEC + << " s: " << s << endl; + + s = 0; + beg = clock(); + for (size_t i = 0; i < N; i++) + s += stc64_rand(&rng); + end = clock(); + cout << "stc64:\t\t" + << ((float) end - beg) / CLOCKS_PER_SEC + << " s: " << s << endl; + + s = 0; + beg = clock(); + for (size_t i = 0; i < N; i++) + s += xoshiro256starstar(rng.state); + end = clock(); + cout << "xoshiro256**:\t" + << ((float) end - beg) / CLOCKS_PER_SEC + << " s: " << s << endl; + + s = 0; + beg = clock(); + for (size_t i = 0; i < N; i++) + s += lehmer64(rng.state); + end = clock(); + cout << "lehmer64:\t" + << ((float) end - beg) / CLOCKS_PER_SEC + << " s: " << s << endl; + } + delete[] recipient; + return 0; +} diff --git a/benchmarks/shootout5_crand.cpp b/benchmarks/shootout5_crand.cpp new file mode 100644 index 00000000..eacca54a --- /dev/null +++ b/benchmarks/shootout5_crand.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include "stc/crandom.h" +#include "others/pcg_random.hpp" + +static struct stc32_state { stc64_t rng; uint64_t spare; unsigned n; } stc32_global = + {{0x7a5fed, 0x8e3f52, 0x9bc713, 0x6a09e667a7541669}, 0, 0}; + +STC_INLINE void stc32_srandom(uint64_t seed) { stc32_global.rng = stc64_init(seed); } +STC_INLINE uint32_t stc32_random(void) { + return (uint32_t) (++stc32_global.n & 1 ? (stc32_global.spare = stc64_rand(&stc32_global.rng)) + : (stc32_global.spare >> 32)); +} + +static unsigned long myrand_next = 1; + +/* RAND_MAX assumed to be 32767 */ +int myrand(void) { + myrand_next = myrand_next * 214013 + 2531011; + return (myrand_next >> 16) & 0x7fff; +} + +void mysrand(unsigned seed) { + myrand_next = seed; +} + + +enum {N = 1000000000}; + +void test1(void) +{ + clock_t diff, before; + uint64_t sum; + + std::random_device device; + std::mt19937 rng(device()); + std::uniform_int_distribution idist(1, 10); + std::uniform_real_distribution fdist(1, 10); + + before = clock(); + sum = 0; + c_forrange (N) { + sum += rng(); + } + diff = clock() - before; + printf("std::random:\t\t%.02f, %zu, sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng); + + before = clock(); + sum = 0; + c_forrange (N) { + sum += idist(rng); + } + diff = clock() - before; + printf("std::uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum); + + c_forrange (30) printf("%02d ", idist(rng)); + puts(""); + c_forrange (8) printf("%f ", fdist(rng)); + puts("\n"); +} + +void test2() +{ + clock_t diff, before; + uint64_t sum; + + // Seed with a real random value, if available + pcg_extras::seed_seq_from seed_source; + + // Make a random number engine + pcg64 rng(seed_source); + + // Choose a random mean between 1 and 10 + std::uniform_int_distribution idist(1, 10); + std::uniform_real_distribution fdist(1, 10); + + before = clock(); + sum = 0; + c_forrange (N) { + sum += rng(); + } + diff = clock() - before; + printf("pcg64::random:\t\t%.02f, %zu, sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng); + + before = clock(); + sum = 0; + c_forrange (N) { + sum += idist(rng); + } + diff = clock() - before; + printf("pcg64::uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum); + + c_forrange (30) printf("%02d ", idist(rng)); + puts(""); + c_forrange (8) printf("%f ", fdist(rng)); + puts("\n"); +} + + +void test3(void) +{ + clock_t diff, before; + uint64_t sum; + + stc64_t rng = stc64_init(time(NULL)); + stc64_uniform_t idist = stc64_uniform_init(1, 10); + stc64_uniformf_t fdist = stc64_uniformf_init(1, 10); + + before = clock(); + sum = 0; + c_forrange (N) { + sum += stc64_rand(&rng); + } + diff = clock() - before; + printf("stc64_random:\t\t%.02f, %zu sz:%zu\n", (float) diff / CLOCKS_PER_SEC, sum, sizeof rng); + + before = clock(); + sum = 0; + c_forrange (N) { + sum += stc64_uniform(&rng, &idist); + } + diff = clock() - before; + printf("stc64_uniform:\t\t%.02f, %zu\n\n", (float) diff / CLOCKS_PER_SEC, sum); + + c_forrange (30) printf("%02zd ", stc64_uniform(&rng, &idist)); + puts(""); + c_forrange (8) printf("%f ", stc64_uniformf(&rng, &fdist)); + puts("\n"); +} + +int main() +{ + test1(); + test2(); + test3(); +} \ No newline at end of file -- cgit v1.2.3