[英]OpenCL - Can't modify values
My Problem is, that I can't modify values in my kernel. 我的问题是,我无法修改内核中的值。
This is the code that does not work: 这是无效的代码:
1st: the kernel: 1:内核:
__kernel void GetCellIndex(__global Particle* particles) {
int globalID = get_global_id(0);
particles[globalID].position.x = globalID;
};
2nd: the struct used in the kernel: 第二:内核中使用的结构:
typedef struct _Particle
{
float3 position;
}Particle;
3rd: the pushing from CPU to GPU: 第三:从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");
}
4th: run the kernel: 第四:运行内核:
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;
}
5th: the used struct on the GPU: 第五:GPU上使用的结构:
typedef struct _Particle
{
cl_float3 position;
}Particle;
6th: finally the reading of the buffer: 第六:最后读取缓冲区:
clEnqueueReadBuffer(queue, cl_Particles, CL_TRUE, 0, 200 * sizeof(Particle), particles, 0, NULL, NULL);
after this steps my kernel does not effect the values returned in clEnqueueReadBuffer... 在此步骤之后,我的内核不会影响clEnqueueReadBuffer中返回的值。
Does anyone know why? 有人知道为什么吗? what is the problem here
这里有什么问题
Solved the problem: 解决了问题:
Change the malloc line to something like 将malloc行更改为类似
particles = new Particles[200];
also write the data in a seperate step to the buffer (with clEnqueueWriteBuffer(...)
) 还要将数据分步写入缓冲区(使用
clEnqueueWriteBuffer(...)
)
and the rest should work just like this code above 其余的应该像上面的代码一样工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.