简体   繁体   English

如何在OpenCL中从内核打印结果?

[英]How to print results from kernel in OpenCL?

I am new to OpenCL. 我是OpenCL的新手。 I am trying to use OpenCL c++ kernel language extension http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2012/10/CPP_kernel_language.pdf . 我正在尝试使用OpenCL c ++内核语言扩展http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2012/10/CPP_kernel_language.pdf I am trying to print results using page 10 code of this document. 我正在尝试使用此文档的第10页代码打印结果。 Please find the code below from this documentation and correct me if am wrong anywhere. 请从此文档中找到以下代码,如果在任何地方有错误,请纠正我。

class Test{
public:
    void setX(int value){ x = value;}
    int getX(){ return x;}
private:
    int x;
};

int main() {
    cl_mem classObj = clCreateBuffer(context, CL_MEM_USE_HOST_PTR, sizeof(Test), &tempClass, &ret);
    void* dm_idata = clEnqueueMapBuffer(command_queue, classObj, CL_TRUE, CL_MAP_WRITE, 0 , sizeof(Test), 0, NULL, NULL, &ret);
    tempClass.setX(10); //prints this value
    clEnqueueUnmapMemObject(command_queue, classObj, dm_idata, 0, NULL, NULL);//class is passed to the device
    ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global_item_size, &local_item_size, 0, NULL, NULL);
    clEnqueueMapBuffer(command_queue, classObj, CL_TRUE, CL_MAP_WRITE, 0, sizeof(Test), 0, NULL, NULL, &ret);//class is passed back to the host
    printf("\n temp value: %d\n", tempClass.getX());
}

Here is the kernel code. 这是内核代码。

class Test {
    setX (int value);
private:
     int x;
};

__kernel void foo(__global Test* Inclass){   

if(get_global_id(0) == 0)
    Inclass->setX(6);
}

It prints the value from host code. 它从主机代码中打印值。 I need to get the result from kernel. 我需要从内核获取结果。 Any help is highly appreciated. 非常感谢您的帮助。

The result I got is 我得到的结果是

temp value = 10 温度值= 10

clEnqueueMapBuffer第二次调用应传递CL_MAP_READ ,而不是CL_MAP_WRITE ,因为您要读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM