如何有效地交换OpenCL内存缓冲区? [英] How to effectively swap OpenCL memory buffers?

查看:125
本文介绍了如何有效地交换OpenCL内存缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我正在寻找如何有效交换两个OpenCL缓冲区的方法.我的内核使用两个gloabl缓冲区,一个作为输入,一个作为输出.但是,我用相同的NDRange在for循环中调用我的内核,每次设置内核参数,排队内核并交换缓冲区,因为先前的输出缓冲区将是下一次迭代的输入缓冲区种子.

Exactly as the title suggests I am looking for how to effectively swap two OpenCL buffers. My kernel uses two gloabl buffers, one as input and one as output. However, I invoke my kernel in a for loop with the same NDRange, each time setting the kernel arguments, enqueueing the kernel, and swapping the buffers because the previous output buffer will be the input buffer seed for the next iteration.

这里交换这两个缓冲区的合适方法是什么?我想将缓冲区复制回主机到已经分配了内存的数组之一,并使用clEnqueueWriteBuffer()clEnqueueReadBuffer()将其复制到下一个输入缓冲区是一种低效的方法.否则,我只是使用一个临时cl_mem变量进行交换.

What is the appropriate way here, to swap these two buffers? I imagine that copying the buffer back to the host to one of the already malloc'd arrays and copying it into the next input buffer using clEnqueueWriteBuffer() and clEnqueueReadBuffer() is an inefficient way to go. Otherwise I am just using a temporary cl_mem variable to do my swapping.

推荐答案

您不需要,只需使用clSetKernelArg设置正确的内核args,然后再次将您的内核排队(使用clEnqueueNDRangeKernel).缓冲区将保留在设备上,不会将任何内容复制回主机.

You don't need to, just set the right kernel args using clSetKernelArg before enqueuing your kernel a second time (using clEnqueueNDRangeKernel). The buffers will stay on the device, nothing will be copied back to the host.

在这种情况下,必须使用CL_MEM_READ_WRITE创建缓冲区.

Your buffer has to be created with CL_MEM_READ_WRITE in this case of course.

这篇关于如何有效地交换OpenCL内存缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆