简体   繁体   English

在创建 OpenCL 上下文之前设置线程关联会导致段错误

[英]Setting thread affinity before creating OpenCL context leads to Segfault

I'm setting the affinity of the main thread to core 0 but the OpenCl context creation ends with a segfault.我将主线程的关联设置为核心 0,但 OpenCl 上下文创建以段错误结束。 (I joined a code snippet to reproduce the problem, I removed vector size check to keep it concise as possible). (我加入了一个代码片段来重现问题,我删除了矢量大小检查以使其尽可能简洁)。

If I define more than one core in my cpuset everything works fine.如果我在我的 cpuset 中定义了多个核心,一切正常。

I have gut feeling about what could be wrong.我对什么可能是错的有直觉。 But I would prefer a concrete explanation.但我更喜欢具体的解释。

And then an error could be nicer than a raw segfault.然后错误可能比原始段错误更好。

Open CL info:打开 CL 信息:

  • Device Version Intel OpenCL 1.2 (Build 475)设备版本英特尔 OpenCL 1.2(内部版本 475)
  • Driver Version 1.2.0.475驱动程序版本1.2.0.475
#include <vector>

#include <CL/cl.hpp>


int main(int argc, char** argv)
{
    // Set affinity to core 0
    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(0, &cpuset);
    pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);

    // Retrieve first platform
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    cl::Platform platform = platforms[0];

    // Retrieve first CPU device
    std::vector<cl::Device> devices;
    platform.getDevices(CL_DEVICE_TYPE_CPU, &devices);
    cl::Device device = devices[0];

    // Create context
    cl::Context ctx(device);

    return 0;
}

Edit: add OpenCL version编辑:添加 OpenCL 版本

I just tried your example with two Intel OpenCL SDKs, but didn't have your exact version at hand.我刚刚用两个英特尔 OpenCL SDK 尝试了您的示例,但手头没有您的确切版本。 Both ran without errors, so: cannot reproduce .两者都运行没有错误,所以:无法重现

This one seems a bit older than yours (by the build number):这个似乎比你的老一点(按内部版本号):

CL_DEVICE_VERSION:          OpenCL 1.2 (Build 43)
CL_DRIVER_VERSION:          1.2.0.43
CL_DEVICE_OPENCL_C_VERSION: OpenCL C 1.2 

The second one is, I think, still the most recent Intel CPU driver:第二个是,我认为,仍然是最新的 Intel CPU 驱动程序:

CL_DEVICE_VERSION:          OpenCL 2.1 (Build 0)
CL_DRIVER_VERSION:          18.1.0.0920
CL_DEVICE_OPENCL_C_VERSION: OpenCL C 2.0 

I used GCC 7.4.0 on Ubuntu 18.4.我在 Ubuntu 18.4 上使用了 GCC 7.4.0。

To my experience it is not a problem to set affinity with the Intel OpenCL Runtime for CPUs .根据我的经验,为 CPU 设置与 Intel OpenCL Runtime 的关联不是问题

I used it together with MPI on supercomputers, where you would have multiple, eg 4, MPI processes per compute node and then have the runtime set each process' CPU mask to a disjunct subset of the available cores.我将它与超级计算机上的 MPI 一起使用,其中每个计算节点有多个(例如 4 个)MPI 进程,然后让运行时将每个进程的CPU 掩码设置为可用内核的分离子集。 The Intel OpenCL Runtime (using Intel Threading Building Blocks under the hood) inside each process would then spawn one thread per assigned core, ie adhere to the host process' CPU mask as expected.每个进程内的英特尔 OpenCL 运行时(在引擎盖下使用英特尔线程构建块)然后将为每个分配的内核生成一个线程,即按照预期遵循主机进程的 CPU 掩码。 Maybe the result is different if you set the first thread's affinity, and also depends on where in the program you set (relative to where the OpenCL runtime spawns it's threads).如果您设置第一个线程的关联性,结果可能会有所不同,并且还取决于您在程序中设置的位置(相对于 OpenCL 运行时生成线程的位置)。

As you seem to already have run your code in a debugger and narrowed things down to clcreatecontext .因为您似乎已经在调试器中运行了代码并将范围缩小到clcreatecontext I'd suggest you try using a newer OpenCL driver.我建议您尝试使用较新的 OpenCL 驱动程序。

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

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