Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Number to each Process in MPI

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 :)

like image 716
Elior Avatar asked Nov 22 '13 09:11

Elior


1 Answers

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.

like image 52
luk32 Avatar answered Nov 14 '22 22:11

luk32