2014-02-11 76 views
0

我在使用OpenCL的GPU上運行昂貴的計算任務。該任務需要在每個工作人員中生成許多隨機數。其中一些隨機數據應該在一定的時間間隔內均勻生成,但其他一些隨機數字必須是高斯分佈在(變化的)值周圍。OpenCL中的高斯分佈隨機數

  1. 有沒有這方面的圖書館?
  2. 如果沒有,那麼實現這樣一個事情的簡單方法是什麼?

到目前爲止,我一直在python中創建隨機數並將它們傳遞給OpenCL。然而,現在的瓶頸是這些隨機數的傳遞(至少比實際計算慢一個數量級)。

+0

參見:http://stackoverflow.com/questions/9912143/how-to-get-a-random-number-in -opencl – OlivierLi

回答

1

Box-Muller transform是一種將均勻隨機變量轉換爲正態分佈的易於並行化的方法。我已經將它與ddemidov提到的Random123庫一起使用。

+0

使用Box-Muller和線性同餘發生器時要小心,在算法之間有一個衆所周知的交互作用,這會導致結果沿螺旋下降。 – pjs

0

Boost.Compute庫提供normal_distribution類以及幾個隨機數發生器(mersenne_twister_enginelinear_congruential_engine)。這些可以一起使用來在設備上產生正常(又名高斯)分佈隨機值。

例如,以產生隨機float值居中在5.0

// create a vector of floats on the device 
boost::compute::vector<float> vec(1000, context); 

// initialize the default random engine 
boost::compute::default_random_engine engine(queue); 

// setup the normal distribution to produce floats centered at 5 
boost::compute::normal_distribution<float> distribution(5.0f, 1.0f); 

// generate the random values and store them to 'vec' 
distribution.generate(vec.begin(), vec.end(), engine, queue);