gpt4 book ai didi

c++ - OpenCL 程序在 CPU 上运行,但在 GPU 上运行时输出空图像

转载 作者:太空狗 更新时间:2023-10-29 20:04:35 28 4
gpt4 key购买 nike

我有以下 OpenCL 内核,一个高斯模糊

__constant sampler_t sampler =
CLK_NORMALIZED_COORDS_FALSE |
CLK_ADDRESS_CLAMP_TO_EDGE |
CLK_FILTER_NEAREST;

__constant float gaussian_kernel[3][3] = {
{0.0625f, 0.125f, 0.0625f},
{0.125f, 0.25f, 0.125f},
{0.0625f, 0.125f, 0.0625f} };

void kernel gaussian_blur(
read_only image2d_t input_image,
write_only image2d_t output_image) {

int x = get_global_id(0);
int y = get_global_id(1);

int2 coords[9] = {
{ x - 1, y - 1 }, { x, y - 1 }, { x + 1, y - 1 },
{ x - 1, y }, { x, y }, { x + 1, y },
{ x - 1, y + 1 }, { x, y + 1 }, { x + 1, y + 1 }
};

float4 pixel_value = { 0.f, 0.f, 0.f, 0.f };

for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
int index = i * 3 + j;
float4 blurred =
as_float4(read_imageui(input_image, sampler, coords[index]));

pixel_value.x += (blurred.x * gaussian_kernel[i][j]);
pixel_value.y += (blurred.y * gaussian_kernel[i][j]);
pixel_value.z += (blurred.z * gaussian_kernel[i][j]);
pixel_value.w += (blurred.w * gaussian_kernel[i][j]);
}
}

uint4 final_value = as_uint4(pixel_value);

write_imageui(output_image, coords[4], final_value);
}

当我指定设备用作 CPU 时,模糊效果正常。这里是设备选择代码

std::vector<cl::Platform> all_platforms;
cl::Platform::get(&all_platforms);

if(all_platforms.size() == 0) {
std::cerr << "No platforms available" <<std::endl;
exit(-1);
}

cl::Platform default_platform = all_platforms[0];

std::vector<cl::Device> all_devices;
default_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);

if(all_devices.size() == 0) {
std::cerr << "No device found" << std::endl;
exit(-1);
}

cl::Device default_device = all_devices[1]; //Changing this index to 0 uses my graphics card

现在,如果 default_device 设置为 GPU,则程序只会输出一张空图像。相关的图像设置代码是(注意 input 是一个 Magick::Imagein_pixels 一个 unsigned short< 的堆分配数组):

cl::ImageFormat format(CL_RGBA, CL_UNSIGNED_INT16);

cl::Image2D input_image_buffer;
input.write(0, 0,
input.baseColumns(), input.baseRows(), "RGBA", Magick::ShortPixel, in_pixels);

input_image_buffer = cl::Image2D(
context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
format,
input.baseColumns(),
input.baseRows(),
0,
in_pixels,
&cl_error);

cl::Image2D output_image_buffer;
output_image_buffer = cl::Image2D(
context,
CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
format,
input.baseColumns(),
input.baseRows(),
0,
out_pixels,
&cl_error);

并且内核设置/图像输出代码(gaussian_program当然没有错误地构建)

cl::Kernel gaussian_kernel(gaussian_program, "gaussian_blur");

cl::CommandQueue queue(context, default_device, 0, &cl_error);

cl::size_t<3> origin;
cl::size_t<3> size;
origin[0] = 0;
origin[1] = 0;
origin[2] = 0;

size[0] = input.baseColumns();
size[1] = input.baseRows();
size[2] = 1;

cl_error = gaussian_kernel.setArg(0, input_image_buffer);

cl_error = gaussian_kernel.setArg(1, output_image_buffer);

cl::NDRange range(input.baseColumns(), input.baseRows());

cl_error = queue.enqueueNDRangeKernel(
gaussian_kernel,
cl::NullRange,
range,
cl::NullRange);

queue.finish();

try{
output.read(
input.baseColumns(),
input.baseRows(),
"RGBA", Magick::ShortPixel, out_pixels);
}
catch(Magick::Exception& ex) {
std::cerr << "A Magick error occured while writing the pixel cache: " <<
std::endl << ex.what() << std::endl;
return false;
}

现在,为了这个示例的目的,我删除了很多错误检查,但原始代码在每次 OpenCL 调用后检查 cl_error 并且从不发出错误信号。代码在 CPU 上按预期执行,但在 GPU 上执行代码时图像为空。

一开始我怀疑是同步问题(queue.finish() 调用是为了那个精确的目的,即使是在 CPU 上也是如此)但是用 cl::finish( )queue.finish() 尝试序列化执行的调用根本没有帮助。

我明显做错了什么吗?此 OpenCL 内核是否存在在 GPU 上失败但在 CPU 上失败的潜在原因?

郑重声明,我在 Ubuntu 13.04 上使用 AMD APP SDK OpenCL 实现和 Radeon HD 7970。

最佳答案

好吧,你在哪里从你的 GPU 读回图像?

您使用标志 CL_MEM_COPY_HOST_PTR 分配您的输入图像

This flag is valid only if host_ptr is not NULL. If specified, it indicates that the application wants the OpenCL implementation to allocate memory for the memory object and copy the data from memory referenced by host_ptr.

和你的输出图像 CL_MEM_USE_HOST_PTR

This flag is valid only if host_ptr is not NULL. If specified, it indicates that the application wants the OpenCL implementation to use memory referenced by host_ptr as the storage bits for the memory object.

OpenCL implementations are allowed to cache the buffer contents pointed to by host_ptr in device memory. This cached copy can be used when kernels are executed on a device.

The result of OpenCL commands that operate on multiple buffer objects created with the same host_ptr or overlapping host regions is considered to be undefined.

您的分配没有任何问题,但是您永远不会告诉 OpenCL 实现将内存写入使用的设备并将该内存读回主机主内存。这可能对 CPU 有效,因为内存已经在它们的设备内存(主内存)中,但对 GPU 无效。

OpenCL c++ 绑定(bind)提供 cl::enqueueWriteImage(/*params*/);cl::enqueueReadImage(/*params*/); 来写入和从设备读取图像缓冲区或从设备读取图像缓冲区。

关于c++ - OpenCL 程序在 CPU 上运行,但在 GPU 上运行时输出空图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18776752/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com