Random Number to each Process in MPI

前端 未结 2 569
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 05:54

I\'m using MPICH2 to implement an \"Odd-Even\" Sort. I did the implementation but when I randomize to each process his value, the same number is randomized to all processe

2条回答
  •  梦毁少年i
    2020-12-19 06:11

    This task is not trivial.

    You are getting same numbers because you initialize srand() with time(0). What time(0) does is return current second (since epoch). So if all the processes have syncronized clocks all will initialize with the same seed as long as they call srand() on the same second, which is pretty probable. I have observed this even on large machines.

    Solution 1. Use local values to initialize random seed.

    What I did was to include into computing random seed some memory usage from cat /proc/meminfo combined with /dev/random, which are more local to physical machine than clocks. Note that this might still fail for N tasks on 1 machine. But if I recall correctly I also used task_id. Anything that is local to task will suffice. Combining stuff is also good idea. After all this computations should be very short compared to real computations. And its better to stay on the safe side.

    Solution 2. Compute seeds as pre-processing step.

    You could also generate random seeds from task 0 using your method and propagate it with send-to-all. Though, it might have scaling troubles when going huge scale (like 10^5 processes). You could also use any other method to load parameters and just prepare seeds as a pre-processing step. However it also involves some non-trivial work.

提交回复
热议问题