c# - Determininistic random numbers in parallel code -
i have question regarding thread ordering tpl. indeed, important me parallel.for loop executed in order of loop. mean given 4 threads, first thread execute every 4k loop, 2nd thread every 4k+1 etc (k between 0 and, nbsim/4).
1st thread -> 1st loop, 2nd thread -> 2nd loop , 3rd thread -> 3rd loop 4th thread -> 4th loop , 1th thread -> 5th loop etc ...
i have seen orderedpartition directive not quite sure of way should apply loop , not parallel.foreach loop.
many help.
follwing previous remkarks, completing description :
actually, after consideration, believe problem not ordering. indeed, working on monte-carlo engine, in each iteration generating set of random numbers (always same (seed =0)) , apply business logic them. should deterministic , when running algorithm twice should exact same results. unfortunately not case, , strugeling understand why. idea, on how solve kind of problems (without printing out every variable have)?
edit number 2:
thank suggestions first, here way code ordered :
paralleloptions options = new paralleloptions(); options.maxdegreeofparallelism = 4; //or 1 parallelloopresult res = parallel.for<localdatastruct>(1,nbsim, options, () => new localdatastruct(//params of constructor of localdata), (isim, loopstate, localdatastruct) => { //logic return localdatastruct; }, localdatastruct => { lock(syncobject) { //critical section outputting parameters }); when setting degreeofparallelism 1,everything works fine, when setting degree of parallelism 4 getting results false , non deterministic (when running code twice different results). due mutable objects checking now, source code quite extensive, takes time. think there strategy check code other review (priniting out variables impossible in case (> 1000)? when setting nb of simulation 4 4 threads working fine well, due luck believe ( s why metionned first idea regarding ordering).
you can enforce ordering in plinq comes @ cost. gives ordered results not enforce ordering of execution.
you cannot tpl without serializing algorithm. tpl works on task model. allows schedule tasks executed scheduler no guarantee order in tasks executed. typically parallel implementations take plinq approach , guarantee ordering of results not ordering of execution.
why ordered execution important?
so. monte-carlo engine need make sure each index in array received same random numbers. not mean need order threads, make random numbers ordered across work done each thread. if each loop of parallelforeach passed not array of elements work on it's own instance of random number generator (with different fixed seed per thread) still deterministic results.
i'm assuming familiar challenges related parallelizing monte-carlo , generating random number sequences. if not here's started;pseudo-random number generation parallel monte carlo—a splitting approach, fast, high-quality, parallel random-number generators: comparing implementations.
some suggestions
i start off ensuring can deterministic results in sequential case replacing parallelforeach foreach , see if runs correctly. try comparing output of sequential , parallel run, add diagnostic output , pipe text file. use diff tool compare results.
if ok parallel implementation, pointed out below related mutable state. things consider:
is random number generator threadsafe?
randompoor random number generator @ best , far know not designed parallel execution. not suitable m-c calculations, parallel or otherwise.does code have other state shared between threads, if it? state mutated in non-deterministic manner , effect results.
are merging results different threads in parallel. non-associativity of parallel floating point operations cause issues here, see how can floating point calculations made deterministic?. if thread results deterministic if combining them in non deterministic way still have issues.
Comments
Post a Comment