gpt4 book ai didi

c++ - 使用 clCreateCommandQueueWithProperties 时出现段错误

转载 作者:行者123 更新时间:2023-11-30 03:25:56 28 4
gpt4 key购买 nike

我正在尝试用 C++ 编写 OpenCL 包装器。昨天我在我的 Windows 10 机器上工作(NVIDIA GTX970 Ti,我相信是最新的 NVIDIA GeForce 驱动程序)并且我的代码运行完美。

今天,我在我的笔记本电脑(Arch Linux、AMD Radeon R7 M265、Mesa 17.3.3)上试用它,我在尝试创建命令队列时遇到段错误。

这是 GDB 回溯:

#0  0x00007f361119db80 in ?? () from /usr/lib/libMesaOpenCL.so.1
#1 0x00007f36125dacb1 in clCreateCommandQueueWithProperties () from /usr/lib/libOpenCL.so.1
#2 0x0000557b2877dfec in OpenCL::createCommandQueue (ctx=..., dev=..., outOfOrderExec=false, profiling=false) at /home/***/OpenCL/Util.cpp:296
#3 0x0000557b2876f0cf in main (argc=1, argv=0x7ffd04fcdac8) at /home/***/main.cpp:27
#4 0x00007f361194cf4a in __libc_start_main () from /usr/lib/libc.so.6
#5 0x0000557b2876ecfa in _start ()

(我删掉了部分路径)这是产生错误的代码:

CommandQueue createCommandQueue(Context ctx, Device dev, bool outOfOrderExec, bool profiling) noexcept
{
cl_command_queue_properties props [3]= {CL_QUEUE_PROPERTIES, 0, 0};

if (outOfOrderExec)
{
props[1] |= CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
}

if (profiling)
{
props[1] |= CL_QUEUE_PROFILING_ENABLE;
}

int error = CL_SUCCESS;

cl_command_queue queue = clCreateCommandQueueWithProperties(ctx.get(), dev.get(), props, &error);

if (error != CL_SUCCESS)
{
std::cerr << "Error while creating command queue: " << OpenCL::getErrorString(error) << std::endl;
}

CommandQueue commQueue = CommandQueue(queue);
Session::get().registerQueue(commQueue);

return commQueue;
}

带有 clCreateCommandQueueWithProperties 的行是发生段错误的地方。

Context 是一个cl_context 的包装类,Context::get() 返回原始的cl_context:

class Context
{
private:
...
cl_context context;

public:
...

cl_context get() const noexcept;

...
};

Devicecl_device 的包装器,Device::get() 也返回 cl_device:

class Device
{
private:
...

cl_device_type type;
cl_device_id id;

public:
...

cl_device_id get() const noexcept;

cl_device_type getType () const noexcept;

...
};

这是主要功能:

int main (int argc, char* argv [])
{
OpenCL::Session::get().init();

for (const std::string& deviceAddress : OpenCL::Session::get().getAddresses())
{
std::cout << "[" << deviceAddress << "]: " << OpenCL::Session::get().getDevice(deviceAddress);
}

OpenCL::Context ctx = OpenCL::getContext();

std::cout << "OpenCL version: " << ctx.getVersionString() << std::endl;

OpenCL::Kernel kernel = OpenCL::createKernel(OpenCL::createProgram("src/Kernels/Hello.cl", ctx), "SAXPY");
OpenCL::CommandQueue queue = OpenCL::createCommandQueue(ctx, OpenCL::Session::get().getDevice(ctx.getAssociatedDevices()[0]));

unsigned int testDataSize = 1 << 13;

std::vector <float> a = std::vector <float> (testDataSize);
std::vector <float> b = std::vector <float> (testDataSize);

for (int i = 0; i < testDataSize; i++)
{
a[i] = static_cast<float>(i);
b[i] = 0.0;
}

OpenCL::Buffer aBuffer = OpenCL::allocateBuffer(ctx, a.data(), sizeof(float), a.size());
OpenCL::Buffer bBuffer = OpenCL::allocateBuffer(ctx, b.data(), sizeof(float), b.size());

kernel.setArgument(0, aBuffer);
kernel.setArgument(1, bBuffer);
kernel.setArgument(2, 2.0f);

OpenCL::Event saxpy_event = queue.enqueue(kernel, {testDataSize});
OpenCL::Event read_event = queue.read(bBuffer, b.data(), bBuffer.size());

std::cout << "SAXPY kernel took " << saxpy_event.getRunTime() << "ns to complete." << std::endl;
std::cout << "Read took " << read_event.getRunTime() << "ns to complete." << std::endl;

OpenCL::Session::get().cleanup();

return 0;
}

(分析将不起作用,因为我已禁用它(认为这是问题的原因),但是重新启用分析并不能解决问题)。

我正在使用 CLion,所以这是我的调试窗口的屏幕截图: CLion Debugging Window

最后是程序的控制台输出:

/home/***/cmake-build-debug/Main
[gpu0:0]: AMD - AMD OLAND (DRM 2.50.0 / 4.14.15-1-ARCH, LLVM 5.0.1): 6 compute units @ 825MHz
OpenCL version: OpenCL 1.1 Mesa 17.3.3
Signal: SIGSEGV (Segmentation fault)

上下文和设备对象似乎都是在没有任何问题的情况下创建的,所以我真的不知道是什么导致了段错误。

有没有可能是我在 Mesa 驱动程序中发现了错误,还是我遗漏了一些明显的东西?

编辑:This人似乎有类似的问题,不幸的是,他的问题只是一个 C 风格的忘记分配内存问题。

第二次编辑:我可能已经找到了这个问题的可能原因,CMake 正在查找、使用和链接 OpenCL 2.0,而我的 GPU 只支持 OpenCL 1.1。我会调查的。我还没有找到在 Arch Linux 上回滚到 OpenCL 1.1 的方法,但 clinfo 似乎工作正常,blender(依赖于 OpenCL)也是如此,所以我认为这不是问题所在。

这是 clinfo 的输出:

Number of platforms                               1
Platform Name Clover
Platform Vendor Mesa
Platform Version OpenCL 1.1 Mesa 17.3.3
Platform Profile FULL_PROFILE
Platform Extensions cl_khr_icd
Platform Extensions function suffix MESA

Platform Name Clover
Number of devices 1
Device Name AMD OLAND (DRM 2.50.0 / 4.14.15-1-ARCH, LLVM 5.0.1)
Device Vendor AMD
Device Vendor ID 0x1002
Device Version OpenCL 1.1 Mesa 17.3.3
Driver Version 17.3.3
Device OpenCL C Version OpenCL C 1.1
Device Type GPU
Device Available Yes
Device Profile FULL_PROFILE
Max compute units 6
Max clock frequency 825MHz
Max work item dimensions 3
Max work item sizes 256x256x256
Max work group size 256
Compiler Available Yes
Preferred work group size multiple 64
Preferred / native vector sizes
char 16 / 16
short 8 / 8
int 4 / 4
long 2 / 2
half 8 / 8 (cl_khr_fp16)
float 4 / 4
double 2 / 2 (cl_khr_fp64)
Half-precision Floating-point support (cl_khr_fp16)
Denormals No
Infinity and NANs Yes
Round to nearest Yes
Round to zero No
Round to infinity No
IEEE754-2008 fused multiply-add No
Support is emulated in software No
Single-precision Floating-point support (core)
Denormals No
Infinity and NANs Yes
Round to nearest Yes
Round to zero No
Round to infinity No
IEEE754-2008 fused multiply-add No
Support is emulated in software No
Correctly-rounded divide and sqrt operations No
Double-precision Floating-point support (cl_khr_fp64)
Denormals Yes
Infinity and NANs Yes
Round to nearest Yes
Round to zero Yes
Round to infinity Yes
IEEE754-2008 fused multiply-add Yes
Support is emulated in software No
Address bits 64, Little-Endian
Global memory size 2147483648 (2GiB)
Error Correction support No
Max memory allocation 1503238553 (1.4GiB)
Unified memory for Host and Device No
Minimum alignment for any data type 128 bytes
Alignment of base address 32768 bits (4096 bytes)
Global Memory cache type None
Image support No
Local memory type Local
Local memory size 32768 (32KiB)
Max constant buffer size 1503238553 (1.4GiB)
Max number of constant args 16
Max size of kernel argument 1024
Queue properties
Out-of-order execution No
Profiling Yes
Profiling timer resolution 0ns
Execution capabilities
Run OpenCL kernels Yes
Run native kernels No
Device Extensions cl_khr_byte_addressable_store cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_fp64 cl_khr_fp16

NULL platform behavior
clGetPlatformInfo(NULL, CL_PLATFORM_NAME, ...) Clover
clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, ...) Success [MESA]
clCreateContext(NULL, ...) [default] Success [MESA]
clCreateContextFromType(NULL, CL_DEVICE_TYPE_DEFAULT) Success (1)
Platform Name Clover
Device Name AMD OLAND (DRM 2.50.0 / 4.14.15-1-ARCH, LLVM 5.0.1)
clCreateContextFromType(NULL, CL_DEVICE_TYPE_CPU) No devices found in platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_GPU) Success (1)
Platform Name Clover
Device Name AMD OLAND (DRM 2.50.0 / 4.14.15-1-ARCH, LLVM 5.0.1)
clCreateContextFromType(NULL, CL_DEVICE_TYPE_ACCELERATOR) No devices found in platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_CUSTOM) No devices found in platform
clCreateContextFromType(NULL, CL_DEVICE_TYPE_ALL) Success (1)
Platform Name Clover
Device Name AMD OLAND (DRM 2.50.0 / 4.14.15-1-ARCH, LLVM 5.0.1)

ICD loader properties
ICD loader Name OpenCL ICD Loader
ICD loader Vendor OCL Icd free software
ICD loader Version 2.2.12
ICD loader Profile OpenCL 2.2

第三次编辑:我刚刚在我的 NVIDIA 机器上运行了代码,工作没有问题,这是控制台显示的内容:

[gpu0:0]: NVIDIA Corporation - GeForce GTX 970: 13 compute units @ 1253MHz
OpenCL version: OpenCL 1.2 CUDA 9.1.75
SAXPY kernel took 2368149686ns to complete.
Read took 2368158390ns to complete.

我还修复了 2 个问题 Andreas提到

最佳答案

clCreateCommandQueueWithProperties 已添加到 OpenCL 2.0 中。您不应将它用于低于 2.0 版的平台和设备(例如日志中显示的 1.1 和 1.2)。

关于c++ - 使用 clCreateCommandQueueWithProperties 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686503/

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