2013-03-14 91 views
0

我有一個奇怪的行爲在CUDA中的問題。 我目前正在開發一個關於粒子軌跡的蒙特卡洛模擬,我正在做以下事情。CUDA和蒙地卡羅與定義的本地行爲

我的粒子在給定日期t(n)的位置p(n)取決於我的粒子在前一日期t(n-1)的位置t(n-1)。事實上,假設值v(n)是從值p(n-1)計算出來的。這裏是我的代碼一個簡單的例子:

__device__ inline double calculateStep(double drift, double vol, double dt, double randomWalk, double S_t){ 
    return exp((drift - vol*vol*0.5)*dt + randomWalk*vol*sqrt(dt))*S_t; 
}  

__device__ double doSomethingWhith(double v_n, ….) { 
    ... 
    Return v_n*exp(t)*S 
} 



__global__ myMCsimulation(double* matrice, double * randomWalk, int nbreSimulation, int nPaths, double drift, ……) { 


    double dt = T/nPaths; 
    unsigned int tid = threadIdx.x + blockDim.x * blockIdx.x; 
    unsigned int stride = blockDim.x*gridDim.x; 
    unsigned int index = tid; 
    double mydt = (index - nbreSimulation)/nbreSimulation*dt + dt; 

    for (index = tid; index < nbreSimulation*nPaths; index += stride) { 
    if (index >= nbreSimulation) 
    { 
    double v_n = DoSomethingWith(drift,dt, matrice[index – nbreSimulation]); 
    matrice[index] = matrice[index - nbreSimulation ] * calculateStep(drift,v_n,dt,randomWalk[index]); // 
    } 
...} 

最後的代碼行:

matrice[index] = matrice[index - nbreSimulation ] * calculateStep(drift,v_n,dt,randomWalk[index]); 

讓我填只有矩陣矩陣的計算的第二排。我不知道爲什麼。

當我通過改變代碼行:

matrice[index] = DoSomethingWith(drift,dt, matrice[index – nbreSimulation]); 

我的矩陣,以及填補了和我有我所有的值改變,那麼我能夠找回matrice[index – nbreSimulation]。 我認爲這是一個併發訪問,但我不知道,我試過__syncthreads(),但它沒有奏效。

請問有人可以幫忙嗎?

非常感謝

+0

無論這個問題,我建議你開始接受對你有幫助的答案。請參閱[about](http://stackoverflow.com/about)頁面。 – 2013-03-14 16:51:24

+0

看起來像'matrice [index] = matrice [index - nbreSimulation] * ...'可能存在併發訪問問題,如果有一個線程對應於'index - nbreSimulation' - 該線程可能會在之前運行或在對應於'index'的線程之後。但我不認爲這隻能解釋矩陣中的一些值被填充。它可能只是矩陣上的索引問題。你有沒有試過用'cuda-memcheck'運行代碼?它可能會顯示一些可能提供線索的超出範圍的索引。我不知道'__synchronize()'調用,也許你的意思是'__syncthreads()'? – 2013-03-14 16:56:09

+0

謝謝你的幫助。是的,我犯了一個錯誤,它是__syncthreads()。在我的代碼中,一個粒子路徑是由一個線程模擬的,那麼關於併發訪問通常應該是可以的。我無法解釋這種奇怪的行爲。乘法觸發一個奇怪的結果.. – ALFRAM 2013-03-14 17:03:39

回答

1

我已經改變了我的代碼的以下事情,現在它完美的作品。

if (index < nbreSimulation) { 
      matrice[index] = S0;  
      for (workingCol=1; workingCol< nPaths; workingCol++) { 
       previousMove = index; 
       index = index + nbreSimulation; 
        ................ 
       matrice[index] = calculateStep(drift,vol_int[index],dt,randomWalk[index], matrice[previousMove]);    } 
     } 
    } 
0

我曾嘗試以下事情:

我已經聲明瞭一個共享變量(double數組),其包含在每次迭代中計算出的值:無

__shared__ double mat[]; 

...... 
for (index = tid; index < nbreSimulation*nPaths; index += stride) { 
    ..... 
    mat[index] = computedValue; 
    ...... 
} 

成功。有沒有人看到這個問題?