3

I am running this

#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <time.h>
namespace mpi = boost::mpi;

int main()
{
    mpi::environment env;
    mpi::communicator world;



    srand (time(NULL));
    std::srand(time(0) + world.rank());
    int my_number = std::rand();
    if (world.rank() == 0) {
        std::vector<int> all_numbers;
        gather(world, my_number, all_numbers, 0);
        for (int proc = 0; proc < world.size(); ++proc)
            std::cout << "Process #" << proc << " thought of "
            << all_numbers[proc] << std::endl;
    } else {
        gather(world, my_number, 0);
    }

    return 0;
}

to distributively generate random number, however, it gives me the number around the same magnitude everytime....

dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238772362
Process #1 thought of 238789169
Process #2 thought of 238805976
Process #3 thought of 238822783
dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238805976
Process #1 thought of 238822783
Process #2 thought of 238839590
Process #3 thought of 238856397
dhcp-18-189-66-216:ising2 myname$ make
mpic++ -I/usr/local/include/boost -L/usr/local/lib -lboost_mpi -lboost_serialization main.cpp -o main
mpirun -n 4 main
Process #0 thought of 238856397
Process #1 thought of 238873204
Process #2 thought of 238890011
Process #3 thought of 238906818
dhcp-18-189-66-216:ising2 myname$ 

In the website, http://www.boost.org/doc/libs/1_55_0/doc/html/mpi/tutorial.html , others said they get:

Process #0 thought of 332199874
Process #1 thought of 20145617
Process #2 thought of 1862420122
Process #3 thought of 480422940
Process #4 thought of 1253380219
Process #5 thought of 949458815
Process #6 thought of 650073868

I am very confused.... Any help? Thank you.

wasabi123
  • 173
  • 1
  • 13
  • 3
    Why do you seed twice? – stefan Jun 24 '15 at 16:21
  • 4
    Using `std::rand` is your problem. **Don't**. [It's basically junk](http://dilbert.com/strip/2001-10-25). Use a [proper random number generator](http://en.cppreference.com/w/cpp/numeric/random). – tadman Jun 24 '15 at 16:21
  • @ScottHunter I amended that comment to a link with a dozen different methods. `rand()` has long been known to be virtually useless. Unless you really do not care about how random your numbers are, use a proper library. – tadman Jun 24 '15 at 16:24
  • [I don't see the problem](http://assets.amuniversal.com/321a39e06d6401301d80001dd8b71c47). – Quentin Jun 24 '15 at 16:41
  • Meditate on this: Any algorithm that is always guaranteed to produce the same output from the same input is deterministic. Deterministic algorithms cannot output any more randomness than is fed into them. – David Schwartz Jun 24 '15 at 16:46

3 Answers3

3

Your problem is the rand() function of the cstdlib. It is not a good random number generator. If you want to use proper random numbers in C++, use some from the header from C++11 or external random number generators (e.g. mersenne twister). But nonetheless, using random numbers in for parallel programs is no easy task. You should use random number generators, which are specialised on that (e.g. r250_omp.h).

user1280483
  • 470
  • 4
  • 11
Melenor
  • 40
  • 1
1

The problem is likely to be caused by your rand. See the discussion and answers for this question: First random number is always smaller than rest

It seems that the generated numbers for neighboring seeds (your case) can be quite heavily correlated. rand's implementations might vary, and for some implementations it seems to be a much more pronounced phenomena.

Community
  • 1
  • 1
Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
0

I think your random gen should be like this:

int max=100, min=0;
srand(time(NULL));
int random = (rand() % (max-min)) + min;
Mattia Merlini
  • 643
  • 1
  • 8
  • 24