【问题标题】:OpenCL - Can't modify valuesOpenCL - 无法修改值
【发布时间】:2016-04-16 00:19:22
【问题描述】:

我的问题是,我无法修改内核中的值。

这是不起作用的代码:

第一个:内核:

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

第二个:内核中使用的结构体:

typedef struct _Particle
{
    float3 position;
}Particle;

第三个:从 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:运行内核:

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:GPU 上使用的结构体:

typedef struct _Particle
{
    cl_float3 position;
}Particle;

6th:最后读取缓冲区:

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

执行此步骤后,我的内核不会影响 clEnqueueReadBuffer 中返回的值...

有人知道为什么吗?这里有什么问题

【问题讨论】:

  • 您是否在读取时遇到错误?如果内核超出内存界限,那就是弹出错误的地方。还要检查您的主机端是否使用cl_float3 来声明您的Particleobject。否则,您将分配的内存比需要的少。
  • 感谢您的回复!读数不会引发错误。 “使用 cl_float3”是什么意思?我在声明中使用它?

标签: c++ struct kernel buffer opencl


【解决方案1】:

解决了问题:

将 malloc 行更改为类似

particles = new Particles[200];

也将数据分步写入缓冲区(使用clEnqueueWriteBuffer(...)

其余的应该像上面的代码一样工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2012-01-26
    相关资源
    最近更新 更多