所以我想模擬一個名爲Tasep的一維物理模型。蒙特卡羅與費率,系統模擬與CUDA C++
我寫了一個代碼來模擬C++中的這個系統,但我絕對需要提升性能。
該模型非常簡單(下面的C++代碼) - 1
和0
的數組。 1
代表顆粒並且0
是無顆粒,意思是空的。一個粒子向右移動一個元素,速度爲1
,如果該元素爲空。最後一個位置的粒子將以beta
(比如0.3)的速度消失。最後,如果第一個位置是空的,則粒子將出現在那裏,速率爲alpha
。
一個線程很容易,我只是隨機選取一個元素,並按照上面所述的概率1/alpha/beta
進行操作。但是這可能需要很長時間。
於是,我就做類似的事情有許多線程,使用GPU,並提出了很多問題:
使用GPU和CUDA在所有好主意,這樣的事情?
我應該有多少個線程?我可以爲每個網站有一個線程(
10E+6
),我應該嗎?如何同步不同線程之間的內存訪問?到目前爲止我使用了原子操作。
生成隨機數據的正確方法是什麼?如果我使用一百萬個線程,可以爲每個線程分配一個隨機生成器嗎?
我該如何照顧利率?
我對CUDA很新。我設法從CUDA示例和一些教程運行代碼。雖然我有一些上面的代碼(雖然仍然給出了奇怪的結果),但我不把它放在這裏,因爲我認爲這些問題更一般。
因此,這裏是C++它的一個線程版本:
int Tasep()
{
const int L = 750000;
// rates
int alpha = 330;
int beta = 300;
int ProbabilityNormalizer = 1000;
bool system[L];
int pos = 0;
InitArray(system); // init to 0's and 1's
/* Loop */
for (int j = 0; j < 10*L*L; j++)
{
unsigned long randomNumber = xorshf96();
pos = (randomNumber % (L)); // Pick Random location in the the array
if (pos == 0 && system[0] == 0) // First site and empty
system[0] = (alpha > (xorshf96() % ProbabilityNormalizer)); // Insert a particle with chance alpha
else if (pos == L - 1) // last site
system[L - 1] = system[L - 1] && (beta < (xorshf96() % ProbabilityNormalizer)); // Remove a particle if exists with chance beta
else if (system[pos] && !system[pos + 1]) // If current location have a particle and the next one is empty - Jump right
{
system[pos] = false;
system[pos + 1] = true;
}
if ((j % 1000) == 0) // Just do some Loggingg
Log(system, j);
}
getchar();
return 0;
}
我會是誰願意幫助非常感激,給他/她的意見。