multithreading - reproducible random sequences for multithreaded C pthreads simulator -
i'm working on wa-tor simulator in c strict requirements , i'm little confused on how reproducible behaviour between calls same arguments.
wa-tor 2d array of cells can contain water only, fish or shark.
what have (i'll try list what's interesting understand problem):
- a configuration file, user can specify integer seed, among other options
- a master process, parse configuration file , spawns (fork+execve) many worker subprocess requested user. further collect status workers displaying single big grid. workers communicate master through unix socket file.
- many multithreaded (pthreads) worker process.
workers connect master socket , receive simulation parameters, included seed specified user (at moment seed differentiated between workers adding worker id it. worker id progressive number master reproducibly assigns worker , included on command line on execve, worker can identify upon connection master's socket , each time assigned same grid section).
worker randomly initialize subgrid sending master, receive/send border values from/to adjacent workers.
worker's state on evolves in discrete steps called chronon. in each step worker calculate random action each creature of subgrid, exchanges updates adjacent worker , master.
one thread listen signals, while routine handles "simulation" part. simulation routine -at end of each step- spawn 4 other threads send updates adjacent threads, 4 receive updates, 1 send status master. threads hereafter joined before starting step.
what i'm doing @ moment handle generation of random numbers have global "unsigned int seed", provided value received master @ beginning of worker process (before thread spawned), , included in every file need using "extern" keyword. use address on calls "rand_r(&seed)".
the first problem value of seed not updated after calls rand_r, instead can observe running single threaded application. forgetting?
then i'm asking if global variable correct way of doing reproducible sequences admitting rand_r called different threads, , sequences of actions change communication between workers doesn't follow strict order, -and if not such way, starting single integer value seed? requirements that, starting same exact parameters , seed, 2 executions produces same results step after step (like respectable simulator, guess :)
p.s. i'm thinking of switching drand48_r() soup same, guess.
regards dr
by having single global var seed , behavior of rand_r not thread safe - thread safe if there seed var per thread. suggest generate distinct seed per thread rand_r initial seed @ init time. each thread runs independently own same random sequence.
Comments
Post a Comment