]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/liblfds700/src/lfds700_misc/lfds700_misc_init.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / liblfds700 / src / lfds700_misc / lfds700_misc_init.c
1 /***** includes *****/
2 #include "lfds700_misc_internal.h"
3
4
5
6
7
8 /****************************************************************************/
9 void lfds700_misc_library_init_valid_on_current_logical_core()
10 {
11   /* TRD : the PRNG arrangement is that each thread has its own state, for a maximum-speed PRNG, where output
12            quality is second consideration to performance
13
14            on 64-bit platforms this is xorshift64*, on 32-bit platforms, an unadorned xorshift32
15
16            the seed for each thread however comes from a single, global, maximum-quality PRNG, where quality of
17            output is the primary consideration
18
19            for this, I'm using a xorshift1024*
20
21            since the generation from this global PRNG state is not thread safe, but is still quick in
22            thread start-up terms, I run a little spin-lock around it
23
24            regarding the seed for this high quality PRNG; it is customary to use time(), but this has a number of
25            drawbacks;
26
27            1. liblfds would depend on time() (currently it does not depend on a hosted implementation of standard library)
28            2. the output from time may only be 32 bit, and even when it isn't, the top 32 bits are currently all zero...
29            3. many threads can begin in the same second; I'd need to add in their thread number,
30               which means I'd need to *get* their thread number...
31
32            as such, I've decided to use a *fixed* 64-bit seed for the high-quality PRNG; this seed is run
33            through the MurmerHash3 avalanche phase to generate successive 64-bit values, which populate
34            the 1024 state of xorshift1024*
35
36            if you have access to a high-frequency clock (often 64-bit), you can use this for the seed
37            (don't use it for the per-thread PRNG, unless you know the clock can be read without a context switch)
38
39            murmurhash3 code from here; http://xorshift.di.unimi.it/murmurhash3.c
40   */
41
42   lfds700_misc_prng_internal_big_slow_high_quality_init( LFDS700_MISC_PRNG_SEED );
43
44   lfds700_misc_globals.exponential_backoff_timeslot_length_in_loop_iterations_for_cas = EXPONENTIAL_BACKOFF_TIMESLOT_LENGTH_IN_INCS_FOR_CAS;
45   lfds700_misc_globals.exponential_backoff_timeslot_length_in_loop_iterations_for_dwcas = EXPONENTIAL_BACKOFF_TIMESLOT_LENGTH_IN_INCS_FOR_DWCAS;
46
47   LFDS700_MISC_BARRIER_STORE;
48
49   lfds700_misc_force_store();
50
51   return;
52 }
53