2014-04-03 75 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++。 - 所以我的代碼中的問題似乎在另一個複雜的地方產生了? –