2016-01-11 45 views
0

我的問題是,我無法修改我的內核中的值。OpenCL - 無法修改值

這是不起作用的代碼:

1:內核:

__kernel void GetCellIndex(__global Particle* particles) { 
    int globalID = get_global_id(0); 
    particles[globalID].position.x = globalID; 
}; 

第二:

typedef struct _Particle 
{ 
    float3 position; 
}Particle; 

3:在內核中使用的結構自推出CPU到GPU:

(Particle*) particles = (Particle*)malloc(sizeof(Particle)*200); 
for (int i = 0; i < 200; i++) 
{ 
    particles[i].position.x = 5f; 
} 

cl_mem cl_Particles = clCreateBuffer(context, CL_MEM_READ_WRITE | 
    CL_MEM_COPY_HOST_PTR, sizeof(Particle)*maxParticle, &particles[0], NULL); 



//init of kernel etc. 



err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&cl_Particles); 
if (err != 0) { 
    std::cout << "Error: setKernelArg 0 does not work!" << std::endl; 
    system("Pause"); 
} 

4:運行內核:

size_t localItem = 1; 
err = clEnqueueNDRangeKernel(queue, kernel, 1, 0, &(size_t)200+1, &localItem, 0, NULL, NULL); 
if (err != 0) { 
    std::cout << "Error: EnqueueNDRange does not work!" << std::endl; 
} 

err = clFlush(queue); 
if (err != 0) { 
    std::cout << "Error: Flush does not work: " << err << std::endl; 
} 

err = clFinish(queue); 
if (err != 0) { 
    std::cout << "Error: Finish does not work: " << err << std::endl; 
} 

5日:在GPU上採用結構:

typedef struct _Particle 
{ 
    cl_float3 position; 
}Particle; 

6:最後的緩衝區的讀取:

clEnqueueReadBuffer(queue, cl_Particles, CL_TRUE, 0, 200 * sizeof(Particle), particles, 0, NULL, NULL); 

後這個步驟我內核不影響在clEnqueueReadBuffer中返回的值...

有誰知道爲什麼?什麼是解決這裏的問題

+0

你在閱讀中遇到錯誤嗎?如果內核超出了內存界限,那麼錯誤將會彈出。還要檢查你的Host端是否使用'cl_float3'來聲明你的'Particle'對象。否則,你將分配一個比需要更小的內存。 – DarkZeros

+0

感謝您的回覆! 閱讀不會引發錯誤。 和「使用cl_float3」是什麼意思?我在我的聲明中使用它? – 3DExtended

回答

0

問題:

更改的malloc行類似

particles = new Particles[200]; 

也是在一個單獨的步驟緩衝區寫入數據(與clEnqueueWriteBuffer(...)

其餘的應該像上面的代碼一樣工作