Random Number to each Process in MPI
Asked Answered
E

2

7

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

Empyrean answered 22/11, 2013 at 9:25 Comment(0)
C
6

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.

Colotomy answered 22/11, 2013 at 9:28 Comment(0)
E
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.

Everything answered 22/11, 2013 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.