2014-04-03 49 views
0

我在my code中创建了一个类Histogram,该类用作Boost 1.54的boost::accumulators::accumulator_set的包装。似乎重要的是我的问题的事物是从Histogram.hpp文件的那些行:两个boost :: accumulators :: accumulator_set相互干扰

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; 
}; 

然后在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))) { 
} 

使用这个直方图(do_iterations()main-metropolis.cpp)的代码开始与以下内容:

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

它的工作原理就像我期望当我用第二行d eactivated。我的模拟产生一些数据点,将其放入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 

然后我激活线,position_histogram作品在一个非常奇怪的方式。该箱都为零,但数据被分配到溢出箱在第一和最后一个窗口:

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 

如果我换行,这是action_histogram打破。所以第二个总是打破第一个。为什么第二个Histogram的初始化以及第二个accumulator_set会导致第一个中断?


请使用修订d3081a1ef7当您浏览the code因为我建立我自己的直方图实现由现在继续工作。

回答

2

你将不得不调试或提供更多信息。

我在研究概念证明中同时使用了累加器并总是与多个实例同时使用,而且我还没有遇到过这种情况。然后我意识到我从来没有并行做直方图,所以我测试了它。

它在我的测试基础上的声明平底锅出来,看到它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) 
    } 
} 

输出,如预期:

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 

此外,喂奶时两个累加器不同的样品,我无法显示任何明显的故障。

希望这可以帮助你体会到什么是你的情况不同(例如你真的喂两个累加器正确样品?)

我已经测试与升压1.53-1.55

+0

我添加链接到源代码在问题中。累加器应该得到正确的样本,因为其中一个工作并显示合理的直方图。 “template '做了什么? –

+0

对不起,关于该标签。这是我不得不诊断潜在问题的一个想法的遗留问题。结果是没有必要的。忽略标签:) – sehe

+0

此功能是否有名称?我想了解它,因为我现在正在学习C++。 - 所以我的代码中的问题似乎在另一个复杂的地方产生了? –