简体   繁体   English

两个boost :: accumulators :: accumulator_set相互干扰

[英]Two boost::accumulators::accumulator_set interfere with each other

I have created a class Histogram in my code which is intended as a wrapper for boost::accumulators::accumulator_set from Boost 1.54. 我在我的代码中创建了一个类Histogram ,它用作Boost 1.54中boost::accumulators::accumulator_set的包装器。 The things that seem important to my problem are those lines from the Histogram.hpp file: 对我的问题似乎重要的事情是Histogram.hpp文件中的那些行:

using namespace boost::accumulators;

class Histogram {
    public:
        Histogram(int bins, size_t cache);
        accumulator_set<double,
                        features<tag::min, tag::max, tag::mean, tag::density>> acc;
};

Then in Histogram.cpp I have the constructor: 然后在Histogram.cpp我有构造函数:

Histogram::Histogram(int bins, size_t cache)
    : acc(accumulator_set<double,
          features<tag::min, tag::max, tag::mean, tag::density>>(
              tag::density::num_bins = bins,
              tag::density::cache_size = std::min(cache, MAX_CACHE_ENTRIES))) {
}

The code that uses this histogram ( do_iterations() in main-metropolis.cpp ) starts with the following: 使用此直方图的代码do_iterations() main-metropolis.cpp do_iterations() )从以下开始:

Histogram position_histogram{settings.position_hist_bins, settings.time_sites * settings.iterations};
//Histogram action_histogram{settings.action_hist_bins, settings.iterations};

It works just like I expect when I run it with the second line deactivated. 当我在第二行停用的情况下运行它时,它就像我期望的那样工作。 My simulation generates some data points, puts it into the Histogram::acc and lets me extract it afterwards: 我的模拟生成了一些数据点,将它放入Histogram::acc然后让我提取它:

-2.86958    0
-2.37393    0.0002
-1.87829    0.0071
-1.38265    0.06621
-0.887001   0.23902
-0.391356   0.33247
0.104288    0.2342
0.599932    0.08449
1.09558 0.02843
1.59122 0.00775
2.08687 0.00012
2.58251 1e-05
# Min -2.37393
# Max 2.58251
# Mean -0.0809983

Then I activate the line, and the position_histogram works in a really strange way. 然后我激活该行,并且position_histogram以一种非常奇怪的方式工作。 The bins are all zero, but the data is distributed into the overflow bins in the first and last bin: 这些箱都是零,但数据被分配到第一个和最后一个箱中的溢流箱:

0   0.57785
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0.42215
# Min -2.37393
# Max 2.58251
# Mean -0.0809983

If I swap the lines, it is action_histogram that breaks. 如果我交换线条,那就是action_histogram会中断。 So the second one always breaks the first one. 所以第二个总是打破第一个。 Why does an initialization of a second Histogram and therefore a second accumulator_set causes the first one to break? 为什么第二个Histogram的初始化以及第二个accumulator_set会导致第一个Histogram断开?


Please use revision d3081a1ef7 when you browse through the code since I build my own histogram implementation by now to continue work. 浏览代码时请使用修订版d3081a1ef7 ,因为我现在构建自己的直方图实现以继续工作。

You're going to have to debug this or supply more information. 您将不得不调试此信息或提供更多信息。

I have used accumulators and always with more than one instance simultaneously in my research proof-of-concepts, and I have not encountered this. 我在研究概念验证中使用了累加器并且总是同时使用多个实例,而我没有遇到过这种情况。 Then I realized I never did a density histogram in parallel, so I tested it out. 然后我意识到我从未做过并行的density直方图,所以我测试了它。

It pans out in my test based on your declarations, see it Live On Coliru : 它根据你的声明在我的测试中进行调查,看看Live On Coliru

#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/random.hpp>
#include <boost/bind.hpp>

using namespace boost::accumulators;

static const size_t MAX_CACHE_ENTRIES = 32;

class Histogram {
    public:
        Histogram(int bins, size_t cache)
            : acc(accumulator_set<double,
                    features<tag::min, tag::max, tag::mean, tag::density>>(
                        tag::density::num_bins = bins,
                        tag::density::cache_size = std::min(cache, MAX_CACHE_ENTRIES))) {
            }        

        accumulator_set<double,
                        features<tag::min, tag::max, tag::mean, tag::density>> acc;
};

int main()
{
    Histogram position_histogram { 10, 32 };
    Histogram action_histogram   { 10, 32 };

    auto random = boost::bind(boost::uniform_real<double>(-100,100), boost::mt19937(42));

    size_t samples = 1<<20;
    while (samples--)
    {
        auto v = random();
        position_histogram.acc(v);
        action_histogram.acc(v);
    }

    for (auto& acc : { position_histogram.acc, action_histogram.acc })
    {
        auto hist = density(acc);

        double total = 0.0;

        for( int i = 0; i < hist.size(); i++ ) 
        {
            std::cout << "Bin lower bound: " << hist[i].first << ", Value: " << hist[i].second << std::endl; 
            total += hist[i].second;
        }

        std::cout << "Total: " << total << std::endl; //should be 1 (and it is)
    }
}

Output, as expected: 输出,如预期:

Bin lower bound: -119.673, Value: 0.000766754
Bin lower bound: -99.8442, Value: 0.099205
Bin lower bound: -80.0156, Value: 0.0987797
Bin lower bound: -60.1869, Value: 0.0990477
Bin lower bound: -40.3583, Value: 0.0991993
Bin lower bound: -20.5296, Value: 0.0989904
Bin lower bound: -0.700967, Value: 0.0993652
Bin lower bound: 19.1277, Value: 0.0993567
Bin lower bound: 38.9563, Value: 0.0993252
Bin lower bound: 58.785, Value: 0.0993109
Bin lower bound: 78.6137, Value: 0.0989342
Bin lower bound: 98.4423, Value: 0.00771904
Total: 1
Bin lower bound: -119.673, Value: 0.000766754
Bin lower bound: -99.8442, Value: 0.099205
Bin lower bound: -80.0156, Value: 0.0987797
Bin lower bound: -60.1869, Value: 0.0990477
Bin lower bound: -40.3583, Value: 0.0991993
Bin lower bound: -20.5296, Value: 0.0989904
Bin lower bound: -0.700967, Value: 0.0993652
Bin lower bound: 19.1277, Value: 0.0993567
Bin lower bound: 38.9563, Value: 0.0993252
Bin lower bound: 58.785, Value: 0.0993109
Bin lower bound: 78.6137, Value: 0.0989342
Bin lower bound: 98.4423, Value: 0.00771904
Total: 1

Also, when feeding both accumulators different samples, I couldn't make it display any obvious malfunctions. 另外,当给两个蓄电池提供不同的样品时,我无法显示任何明显的故障。

Hope this helps you realize what is different about your situation (eg do you actually feed both accumulators the right samples?) 希望这有助于您了解您的情况有何不同(例如,您是否真的为两个蓄电池提供正确的样品?)

I've test with boost 1.53-1.55 我用1.53-1.55进行了测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM