- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下 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::Image
和 in_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/
Intel、AMD 和 Khronos OpenCL 之间有什么区别。我对 OpenCL 完全陌生,想从它开始。我不知道在我的操作系统上安装哪个更好。 最佳答案 OpenCL 是 C 和 C++ 语言
我在这里的一篇文章中看到,我们可以从 OpenCL 内核调用函数。但是在我的情况下,我还需要并行化该复杂函数(由所有可用线程运行),所以我是否必须将该函数也设为内核并像从主内核中调用函数一样直接调
最近我看到一些开发板支持 OpenCL EP,例如 odroid XU。我知道的一件事是 OpenCL EP 适用于 ARM 处理器,但它与基于主要桌面的 OpenCL 在哪些特性上有所不同。 最佳答
我想知道在 OpenCL 中设置为内核函数的参数数量是否有任何限制。设置参数时出现 INVALID_ARG_INDEX 错误。我在内核函数中设置了 9 个参数。请在这方面帮助我。 最佳答案 您可以尝试
我对零拷贝的工作原理有点困惑。 1-要确认以下内容对应于opencl中的零拷贝。 ....................... . . . .
我是 OpenCL 的初学者,我很难理解某些东西。 我想改进主机和设备之间的图像传输。 我制定了一个计划以更好地了解我。 顶部:我现在拥有的 |底部:我想要的 HtD(主机到设备)和 DtH(设备到主
今天我又加了四个 __local变量到我的内核以转储中间结果。但是只需将另外四个变量添加到内核的签名并添加相应的内核参数就会将内核的所有输出呈现为“0”。没有一个 cl 函数返回错误代码。 我进一步尝
我知道工作项被分组到工作组中,并且您不能在工作组之外进行同步。 这是否意味着工作项是并行执行的? 如果是这样,使用 128 个工作项创建 1 个工作组是否可能/有效? 最佳答案 组内的工作项将一起安排
我相当确定经纱仅在 CUDA 中定义。但也许我错了。就 OpenCL 而言,什么是扭曲? 它与工作组不一样,是吗? 任何相关的反馈都受到高度赞赏。谢谢! 最佳答案 它没有在 OpenCL 标准中定义。
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
在OpenCL中,我的理解是可以使用barrier()函数来同步工作组中的线程。我(通常)确实了解它们的用途以及何时使用它们。我还知道工作组中的所有线程都必须遇到障碍,否则会出现问题。然而,到目前为止
我的主板上有 Nvidia 显卡 (GeForce GT 640)。我已经在我的盒子上安装了 OpenCL。当我使用“clGetPlatformInfo(参数)”查询平台时,我看到以下输出:-#可用平
我目前正在构建一个 ray marcher 来查看像 mandelbox 等东西。它工作得很好。但是,在我当前的程序中,它使用每个 worker 作为从眼睛转换的光线。这意味着每个 worker 有大
我编写了两个不同的 openCl 内核,使用 nvidia profiler 获取了有关它们的一些信息,发现两者每个工作项都使用 63 个寄存器。 我尝试了一切我能想到的方法来降低这个数字(用 ush
我的主板上有 Nvidia 显卡 (GeForce GT 640)。我已经在我的盒子上安装了 OpenCL。当我使用“clGetPlatformInfo(参数)”查询平台时,我看到以下输出:-#可用平
我目前正在构建一个 ray marcher 来查看像 mandelbox 等东西。它工作得很好。但是,在我当前的程序中,它使用每个 worker 作为从眼睛转换的光线。这意味着每个 worker 有大
我正在尝试使用 OpenCL 加速一些计算,算法的一部分包括矩阵求逆。是否有任何开源库或免费可用的代码来计算用 OpenCL 或 CUDA 编写的矩阵的 lu 分解(lapack dgetrf 和 d
我正在尝试在 OpenCL 内核中使用递归。编译成功,但运行时出现编译错误,所以我想知道,由于 CUDA 现在支持动态并行,OpenCL 是否支持动态并行? 最佳答案 OpenCL 不支持递归。请参阅
考虑以下代码,它从大小为 size 的 double 组创建缓冲区内存对象: coef_mem = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM
OpenCL 中目标平台的示例是什么?例如,它是 Windows、Android、Mac 等操作系统,还是设备中的实际芯片? 最佳答案 OpenCL 平台本质上是一个 OpenCL 实现。它与操作系统
我是一名优秀的程序员,十分优秀!