multithreading - Random function in multi-threaded c program -
please see whole question
i know srand()
should called once, 2nd code segment shows that not solve issue!!!!
the program have written giving me outputs can't quite make out why so. different alterations of code segments give different outputs.
objective of code:
code uses omp
run piece of code 3 threads. each thread has print 3 random values using rand()
function. so, total of 9 outputs come. thread 0
main thread/ main program's run flow. thread 1
, thread 2
fellow new threads created @ start of code threads.
code:
#include<omp.h> #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { #pragma omp parallel num_threads(3) { srand(time(null)); int i=0; for(i=0;i<3;i++) { printf("\nrandom number: %d thread %d", rand(), omp_get_thread_num()); } } return 0; }
the output:
random number: 17105 thread 0 random number: 30076 thread 0 random number: 21481 thread 0 random number: 17105 thread 1 random number: 30076 thread 1 random number: 21481 thread 1 random number: 17105 thread 2 random number: 30076 thread 2 random number: 21481 thread 2
but if make keep srand(time(null))
before code thread like,
srand(time(null)); #pragma omp parallel num_threads(3) { int i=0; ...... ......(rest same)
the output is, the output:
random number: 16582 thread 0 random number: 14267 thread 0 random number: 14030 thread 0 random number: 41 thread 1 random number: 18467 thread 1 random number: 6334 thread 1 random number: 41 thread 2 random number: 18467 thread 2 random number: 6334 thread 2
problem, , doubts:
- placing `srand` outside, threads' 1st call `rand()` gave same random number, of 2nd call gave same random number, , 3rd call also.
- placing `srand` inside, main thread's calls resulted in different random numbers others. but, 2 new other threads among them gave same random number respective calls `rand()`.
so,
- happening here? how come placement of `srand()` function make difference main thread (thread `0`)?
- why either ways the other 2 new threads output same random number respective call `rand()`?
- how `srand()` , `rand()` linked, cause abnormality?
- , tried giving wait intervals each thread remove possibility of `rand()` function being called different threads @ same time, might result in same random number maybe. problem before. no change in output (just time @ output occurred different).
please me understand whole thing..
updated: inserted direct answers op's enumerated questions.
what happening here?
although versions of rand()
function may "thread safe" in sense, there no reason believe or expect without external memory synchronization, set of values returned multiple rand()
calls executed different threads same set of values returned same number of calls executed 1 thread. in particular, rand()
maintains internal state modified on each call, , without memory synchronization, entirely plausible 1 thread not see updates internal state performed other threads. in case, 2 or more threads may generate partially or wholly same sequence of numbers.
how come placement of
srand()
function make difference main thread (thread0
)?
the thing can said if srand()
outside parallel block executed main thread, whereas if inside executed separately each thread. inasmuch code not synchronized, effects of each case not predictable source code, next comments speculative.
supposing time()
, (only) one-second precision, returns same value in each thread, placing srand()
inside parallel region ensures every thread sees same initial random number seed. if not see each other's updates generate same sequences of pseudo-random numbers. note, however, can neither safely rely on threads seeing each other's updates nor safely rely on them not seeing each others updates.
if put srand()
outside parallel region, however, executed main thread, there additional possibilities. if omp maintains thread pool members started before enter parallel section, may threads 1 , 2 fail see effect of thread 0's srand()
call @ all, , therefore both proceed default seed. there other possibilities.
why either ways the other 2 new threads output same random number respective call
rand()
?
it's impossible certainty. i'm inclined guess, however, none of threads involved see each other's updates rand()
's internal state.
how
srand()
,rand()
linked, cause abnormality?
the 2 functions intimately linked. purpose of srand()
modify rand()
's internal state (to "seed" it, hence "s" in "srand"), start psuedo-random number sequence generates @ different (but still deterministic) point.
this problem can solved in same way problem involving multi-threaded access shared variables can solved: applying synchronization. in case, straightforward form of synchronization protect rand()
calls mutex. since omp code, best bet might use omp locks implement mutex, seems dicey mix explicit pthreads objects omp declarations.
Comments
Post a Comment