Changeset 2210cfc


Ignore:
Timestamp:
Jan 10, 2022, 5:43:42 PM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
c52f033
Parents:
4177592f
Message:

second attempt at specialized PRNG

Location:
libcfa/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/invoke.h

    r4177592f r2210cfc  
    1010// Created On       : Tue Jan 17 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jan  6 16:37:40 2022
    13 // Update Count     : 47
     12// Last Modified On : Sun Jan  9 19:06:45 2022
     13// Update Count     : 48
    1414//
    1515
     
    211211                struct processor * last_proc;
    212212
     213                uint32_t random_state;                                                  // fast random numbers
     214
    213215                #if defined( __CFA_WITH_VERIFY__ )
    214216                        void * canary;
  • libcfa/src/concurrency/thread.cfa

    r4177592f r2210cfc  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Dec  4 09:17:49 2019
    13 // Update Count     : 9
     12// Last Modified On : Mon Jan 10 17:05:35 2022
     13// Update Count     : 28
    1414//
    1515
     
    2727uint64_t thread_rand();
    2828
     29extern uint32_t __thread_seed;                                                  // global thread seed
     30
    2931//-----------------------------------------------------------------------------
    3032// Thread ctors and dtors
    31 void ?{}(thread$ & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
     33void ?{}( thread$ & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
    3234        context{ 0p, 0p };
    3335        self_cor{ name, storage, storageSize };
     
    3941        self_mon.owner = &this;
    4042        self_mon.recursion = 1;
     43        random_state = __thread_seed;
    4144        self_mon_p = &self_mon;
    4245        curr_cluster = &cl;
     
    178181}
    179182
     183#define GENERATOR LCG
     184
     185inline uint32_t MarsagliaXor( uint32_t & state ) {
     186        state ^= state << 6;
     187        state ^= state >> 21;
     188        state ^= state << 7;
     189        return state;
     190} // MarsagliaXor
     191
     192inline uint32_t LCG( uint32_t & state ) {                               // linear congruential generator
     193        return state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime!
     194} // LCG
     195
     196void set_seed( uint32_t seed ) { active_thread()->random_state = seed; __thread_seed = seed; }
     197uint32_t prng( void ) { return GENERATOR( active_thread()->random_state ); } // [0,UINT_MAX]
     198
    180199// Local Variables: //
    181200// mode: c //
  • libcfa/src/stdlib.cfa

    r4177592f r2210cfc  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan  3 09:36:27 2022
    13 // Update Count     : 519
     12// Last Modified On : Mon Jan 10 17:07:21 2022
     13// Update Count     : 572
    1414//
    1515
    1616#include "stdlib.hfa"
     17//#include "concurrency/kernel/fwd.hfa"
     18#include "concurrency/invoke.h"                                                 // random_state
    1719
    1820//---------------------------------------
     
    221223//---------------------------------------
    222224
    223 static uint32_t seed = 0;                                                               // current seed
    224 static thread_local uint32_t state;                                             // random state
    225 
    226 void set_seed( uint32_t seed_ ) { state = seed = seed_; }
    227 uint32_t get_seed() { return seed; }
    228 
    229225#define GENERATOR LCG
    230226
    231227inline uint32_t MarsagliaXor( uint32_t & state ) {
    232         if ( unlikely( seed == 0 ) ) set_seed( rdtscl() );
    233         else if ( unlikely( state == 0 ) ) state = seed;
    234228        state ^= state << 6;
    235229        state ^= state >> 21;
     
    239233
    240234inline uint32_t LCG( uint32_t & state ) {                               // linear congruential generator
    241         if ( unlikely( seed == 0 ) ) set_seed( rdtscl() );
    242         else if ( unlikely( state == 0 ) ) state = seed;
    243235        return state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime!
    244236} // LCG
    245237
     238uint32_t __thread_seed = rdtscl();                                              // global thread seed
     239
     240void set_seed( uint32_t seed ) { __thread_seed = seed; }
     241uint32_t get_seed() { return  __thread_seed; }
    246242uint32_t prng( PRNG & prng ) with( prng ) { callcnt += 1; return GENERATOR( state ); }
    247243
    248 uint32_t prng( void ) { return GENERATOR( state ); }
     244uint32_t prng( void ) { return GENERATOR( __thread_seed ); } // [0,UINT_MAX]
    249245
    250246//---------------------------------------
  • libcfa/src/stdlib.hfa

    r4177592f r2210cfc  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jan  2 22:53:57 2022
    13 // Update Count     : 594
     12// Last Modified On : Mon Jan 10 17:03:18 2022
     13// Update Count     : 619
    1414//
    1515
     
    2121#include <stdlib.h>                                                                             // *alloc, strto*, ato*
    2222#include <heap.hfa>
     23
    2324
    2425// Reduce includes by explicitly defining these routines.
     
    391392}; // PRNG
    392393
    393 extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
     394uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
    394395static inline {
    395396        void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed
     
    402403} // distribution
    403404
    404 extern void set_seed( uint32_t seed );                                  // set per thread seed
    405 extern uint32_t get_seed();                                                             // get seed
    406 extern uint32_t prng( void ) __attribute__(( warn_unused_result )); // [0,UINT_MAX]
     405void set_seed( uint32_t seed_ ) OPTIONAL_THREAD;
     406uint32_t get_seed() __attribute__(( warn_unused_result ));
     407uint32_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX]
    407408static inline {
    408         uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result ));
    409         uint32_t prng( uint32_t u ) { return prng() % u; }      // [0,u)
    410         uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result ));
    411         uint32_t prng( uint32_t l, uint32_t u ) { return prng( u - l + 1 ) + l; } // [l,u]
     409        uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result )) { return prng() % u; } // [0,u)
     410        uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( u - l + 1 ) + l; } // [l,u]
    412411} // distribution
    413412
Note: See TracChangeset for help on using the changeset viewer.