Changeset 1959528


Ignore:
Timestamp:
Jan 12, 2022, 6:34:58 PM (2 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
00f5fde
Parents:
a10f6b4
Message:

third attempt at specialized PRNG

Location:
libcfa/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/kernel/startup.cfa

    ra10f6b4 r1959528  
    101101extern void __wake_proc(processor *);
    102102extern int cfa_main_returned;                                                   // from interpose.cfa
     103extern uint32_t __global_random_seed;
    103104
    104105//-----------------------------------------------------------------------------
     
    489490        preferred = ready_queue_new_preferred();
    490491        last_proc = 0p;
     492        random_state = __global_random_seed;
    491493        #if defined( __CFA_WITH_VERIFY__ )
    492494                canary = 0x0D15EA5E0D15EA5Ep;
  • libcfa/src/concurrency/thread.cfa

    ra10f6b4 r1959528  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 10 17:05:35 2022
    13 // Update Count     : 28
     12// Last Modified On : Wed Jan 12 18:28:18 2022
     13// Update Count     : 35
    1414//
    1515
     
    2727uint64_t thread_rand();
    2828
    29 extern uint32_t __thread_seed;                                                  // global thread seed
     29extern uint32_t __global_random_seed;
    3030
    3131//-----------------------------------------------------------------------------
     
    4141        self_mon.owner = &this;
    4242        self_mon.recursion = 1;
    43         random_state = __thread_seed;
    4443        self_mon_p = &self_mon;
    4544        curr_cluster = &cl;
     
    4847        preferred = ready_queue_new_preferred();
    4948        last_proc = 0p;
     49        random_state = __global_random_seed;
    5050        #if defined( __CFA_WITH_VERIFY__ )
    5151                canary = 0x0D15EA5E0D15EA5Ep;
     
    180180        return ret;
    181181}
    182 
     182 
    183183#define GENERATOR LCG
    184184
    185 inline uint32_t MarsagliaXor( uint32_t & state ) {
     185static inline uint32_t MarsagliaXor( uint32_t & state ) {
     186        uint32_t ret = state;
    186187        state ^= state << 6;
    187188        state ^= state >> 21;
    188189        state ^= state << 7;
    189         return state;
     190        return ret;
    190191} // MarsagliaXor
    191192
    192 inline uint32_t LCG( uint32_t & state ) {                               // linear congruential generator
    193         return state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime!
     193static inline uint32_t LCG( uint32_t & state ) {                // linear congruential generator
     194        uint32_t ret = state;
     195        state = 36969 * (state & 65535) + (state >> 16);        // 36969 is NOT prime! No not change it!
     196        return ret;
    194197} // LCG
    195198
    196 void set_seed( uint32_t seed ) { active_thread()->random_state = seed; __thread_seed = seed; }
     199void set_seed( uint32_t seed ) { active_thread()->random_state = seed; __global_random_seed = seed; }
    197200uint32_t prng( void ) { return GENERATOR( active_thread()->random_state ); } // [0,UINT_MAX]
    198201
  • libcfa/src/startup.cfa

    ra10f6b4 r1959528  
    1010// Created On       : Tue Jul 24 16:21:57 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jan  9 23:18:23 2021
    13 // Update Count     : 34
     12// Last Modified On : Wed Jan 12 16:25:37 2022
     13// Update Count     : 49
    1414//
    1515
     
    1818#include <stdlib.h>                                                                             // getenv
    1919#include "startup.hfa"
     20#include "bits/defs.hfa"
     21
     22extern uint32_t __global_random_seed;
    2023
    2124extern "C" {
     
    4851        void __cfaabi_core_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_CORE ) ));
    4952        void __cfaabi_core_startup( void ) {
     53                __global_random_seed = rdtscl();
    5054                __cfaabi_interpose_startup();
    5155                __cfaabi_device_startup();
  • libcfa/src/stdlib.cfa

    ra10f6b4 r1959528  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 10 17:07:21 2022
    13 // Update Count     : 572
     12// Last Modified On : Wed Jan 12 16:25:36 2022
     13// Update Count     : 578
    1414//
    1515
     
    223223//---------------------------------------
    224224
     225// Pipelined to allow OoO overlap with reduced dependencies. Critically, return the current value, and compute and store
     226// the next value.
     227
    225228#define GENERATOR LCG
    226229
    227 inline uint32_t MarsagliaXor( uint32_t & state ) {
     230static inline uint32_t MarsagliaXor( uint32_t & state ) {
     231        uint32_t ret = state;
    228232        state ^= state << 6;
    229233        state ^= state >> 21;
    230234        state ^= state << 7;
    231         return state;
     235        return ret;
    232236} // MarsagliaXor
    233237
    234 inline uint32_t LCG( uint32_t & state ) {                               // linear congruential generator
    235         return state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime!
     238static inline uint32_t LCG( uint32_t & state ) {                // linear congruential generator
     239        uint32_t ret = state;
     240        state = 36969 * (state & 65535) + (state >> 16);        // 36969 is NOT prime! No not change it!
     241        return ret;
    236242} // LCG
    237243
    238 uint32_t __thread_seed = rdtscl();                                              // global thread seed
    239 
    240 void set_seed( uint32_t seed ) { __thread_seed = seed; }
    241 uint32_t get_seed() { return  __thread_seed; }
     244uint32_t __global_random_seed;
     245
     246void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; GENERATOR( state ); } // set seed
    242247uint32_t prng( PRNG & prng ) with( prng ) { callcnt += 1; return GENERATOR( state ); }
    243248
    244 uint32_t prng( void ) { return GENERATOR( __thread_seed ); } // [0,UINT_MAX]
     249void set_seed( uint32_t seed ) {
     250        active_thread()->random_state = __global_random_seed = seed;
     251        GENERATOR( active_thread()->random_state );
     252} // set_seed
     253uint32_t get_seed() { return __global_random_seed; }
     254uint32_t prng( void ) { return GENERATOR( active_thread()->random_state ); } // [0,UINT_MAX]
    245255
    246256//---------------------------------------
  • libcfa/src/stdlib.hfa

    ra10f6b4 r1959528  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 10 17:03:18 2022
    13 // Update Count     : 619
     12// Last Modified On : Wed Jan 12 09:09:48 2022
     13// Update Count     : 620
    1414//
    1515
     
    386386//---------------------------------------
    387387
     388// Sequential Pseudo Random-Number Generator : generate repeatable sequence of values that appear random.
     389//
     390// Declaration :
     391//   PRNG sprng = { 1009 } - set starting seed versus random seed
     392//   
     393// Interface :
     394//   set_seed( sprng, 1009 ) - set starting seed for ALL kernel threads versus random seed
     395//   get_seed( sprng ) - read seed
     396//   prng( sprng ) - generate random value in range [0,UINT_MAX]
     397//   prng( sprng, u ) - generate random value in range [0,u)
     398//   prng( sprng, l, u ) - generate random value in range [l,u]
     399//   calls( sprng ) - number of generated random value so far
     400//
     401// Examples : generate random number between 5-21
     402//   prng( sprng ) % 17 + 5;    values 0-16 + 5 = 5-21
     403//   prng( sprng, 16 + 1 ) + 5;
     404//   prng( sprng, 5, 21 );
     405//   calls( sprng );
     406
    388407struct PRNG {
    389408        uint32_t callcnt;                                                                       // call count
     
    392411}; // PRNG
    393412
     413void set_seed( PRNG & prng, uint32_t seed_ );
    394414uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
    395415static inline {
    396         void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed
    397416        void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed
    398417        void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed
     
    403422} // distribution
    404423
     424// Concurrent Pseudo Random-Number Generator : generate repeatable sequence of values that appear random.
     425//
     426// Interface :
     427//   set_seed( 1009 ) - fixed seed for all kernel threads versus random seed
     428//   get_seed() - read seed
     429//   prng() - generate random value in range [0,UINT_MAX]
     430//   prng( u ) - generate random value in range [0,u)
     431//   prng( l, u ) - generate random value in range [l,u]
     432//
     433// Examples : generate random number between 5-21
     434//   prng() % 17 + 5;   values 0-16 + 5 = 5-21
     435//   prng( 16 + 1 ) + 5;
     436//   prng( 5, 21 );
     437
    405438void set_seed( uint32_t seed_ ) OPTIONAL_THREAD;
    406 uint32_t get_seed() __attribute__(( warn_unused_result ));
     439uint32_t get_seed() __attribute__(( warn_unused_result )) OPTIONAL_THREAD;
    407440uint32_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
    408441static inline {
Note: See TracChangeset for help on using the changeset viewer.