Как корректно распараллелить метод Монте-Карло?

Пытаюсь реализовать метод Монте-Карло. Есть единичный квадрат и вписанная в него единичная окружность. Накидываю точки случайным образом в нескольких потоках, затем смотрю отношение попавших в окружность к общему количеству точек.

void monte_carlo_par(unsigned _seed, unsigned _pointsNumber, unsigned _threadNumber, unsigned *_results) {
    vector<thread> threadPool;    
    vector<LCG> lcg(_threadNumber);   // для каждого потока свой датчик случайных чисел

    for (unsigned idx = 0; idx < _threadNumber; idx++) {
        lcg[idx].srand(_seed + idx);
        _results[idx] = 0;
        thread curThread(throwPoints, idx, _pointsNumber / _threadNumber, ref(lcg[idx]), ref(_results[idx]));
        threadPool.push_back(move(curThread));

        this_thread::sleep_for(chrono::milliseconds(5));
    }

    for (unsigned idx = 0; idx < _threadNumber; idx++) {
        threadPool[idx].join();
    }
}

Проблема в том, параллельный вариант считается дольше обычного.

Thread #0 is started with 100000000 tosses...
Thread #0 is over!
Procedure takes 2.61275 seconds
Total points from a circle (R = 1) to total number of points = 0.785325
pi approximately equals to 3.1413

serge@serge-note:~/Temp/MonteCarlo$ ./main 2 100000000
Thread #0 is started with 50000000 tosses...
Thread #1 is started with 50000000 tosses...
Thread #1 is over!
Thread #0 is over!
Procedure takes 3.82649 seconds
Total points from a circle (R = 1) to total number of points = 0.785384
pi approximately equals to 3.14154

serge@serge-note:~/Temp/MonteCarlo$ ./main 3 100000000
Thread #0 is started with 33333333 tosses...
Thread #1 is started with 33333333 tosses...
Thread #2 is started with 33333333 tosses...
Thread #0 is over!
Thread #1 is over!
Thread #2 is over!
Procedure takes 3.15468 seconds
Total points from a circle (R = 1) to total number of points = 0.785326
pi approximately equals to 3.1413

Не понимаю, в чём заключается проблема. Ощущение, что потоки выполняются друг за другом


Ответы (0 шт):