C ++ 11并行化:Armadillo的set_seed_random()的瓶颈 [英] C++11 parallelization: bottleneck in Armadillo's set_seed_random()

查看:187
本文介绍了C ++ 11并行化:Armadillo的set_seed_random()的瓶颈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,使用arma_rng::set_seed_random()会产生瓶颈.我展示了一种再现它的方法.

In C++11, the use of arma_rng::set_seed_random() generates a bottleneck. I show a way to reproduce it.

考虑以下简单代码:

#include <armadillo>    // Load Armadillo library.
using namespace arma;

int main()
{
bool jj = true;
while ( jj == true ){
    arma_rng::set_seed_random();            // Set the seed to generate random numbers.
    double rnd_number = randu<double>();    // Generate a random number.
}

}

我用

g++ -std=c++11 -Wall -g bayesian_estimation.cpp -o bayesian_estimation -O2 -larmadillo

当我在终端中运行可执行文件时,我看到其中一个内核正在以接近100%的CPU%处理该可执行文件.如果我运行它的更多实例,则会减少每个相应进程的CPU%,但不会使用新的(和空闲!)内核.我在

When I run the executable in a terminal, I see that one of the cores is handling it with a CPU% close to 100%. If I run more instances of it, the CPU% of each corresponding process is reduced, but no new (and idle!) cores are used. I illustrate this kind of behavior in detail in this question.

为什么会这样?

推荐答案

我假设Armadillo从操作系统维护的真实随机数池中获取set_seed_random()的种子(例如,大多数情况下为/dev/random * NIX OS).由于这需要一个物理的熵源(通常使用击键,网络事件,其他中断源的时间),因此该池是有限的,并且耗尽速度快于生成新的随机数.

I would assume that Armadillo takes the seed for set_seed_random() from the pool of true random numbers that is maintained by the OS (e.g. /dev/random on most *NIX OS). Since this needs a physical source of entropy (usually, the timing of keystrokes, network events, other interrupt sources is used), this pool is finite and can be exhausted faster than new random numbers can be generated.

在您的情况下,我将假设一个全速运行的可执行文件正在以与添加新熵大致相同的速率消耗池.一旦添加第二个,第三个...,它们就会在等待新的随机数进入池中时停滞.

And in your case, I would assume that one executable running at full speed is depleting the pool at roughly the same rate that new entropy is added. As soon as you add a second, third, ..., they stall while waiting for new random numbers to enter the pool.

这篇关于C ++ 11并行化:Armadillo的set_seed_random()的瓶颈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆