Random Number to each Process in MPI

纵然是瞬间 提交于 2019-11-28 04:07:39

问题


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 processes.

Here is the code for each process, each process randomized his value..

int main(int argc,char *argv[])
{
    int  nameLen, numProcs, myID;
    char processorName[MPI_MAX_PROCESSOR_NAME];
    int myValue;

    MPI_Init(&argc,&argv);
    MPI_Comm_rank(MPI_COMM_WORLD,&myID);
    MPI_Comm_size(MPI_COMM_WORLD,&numProcs);    
    MPI_Get_processor_name(processorName,&nameLen);
    MPI_Status status;

    srand((unsigned)time(NULL));
    myValue = rand()%30+1; 

    cout << "myID: " << myID << " value: " << myValue<<endl;
    MPI_Finalize();

    return 0;
 }

why each process get the same value?

Edit : thanks for the answers :)

I changed the line

 srand((unsigned)time(NULL));

to

 srand((unsigned)time(NULL)+myID*numProcs + nameLen);

and it gives a different values for each process :)


回答1:


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.




回答2:


It's because your seed does not change enough and the randomness depends on your seed.

From the srand docs:

For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.

EDIT: Try generating the seed beforehand or change the seed by hand for every srand call.



来源:https://stackoverflow.com/questions/20141239/random-number-to-each-process-in-mpi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!