opencl - 如何以编程方式在具有多个 GPU (OpenCL 1.1) 的平台上发现特定 GPU?

标签 opencl

我的 Mac Pro (OSX 10.7) 有两个 GPU。系统信息应用显示图形/显示器的以下详细信息:

    ATI Radeon HD 5770:
       Bus: PCIe
       Slot:    Slot-1
       Vendor:  ATI (0x1002)
       Device ID:   0x68b8
       ...

    ATI Radeon HD 5770:
      Bus:  PCIe
      Slot: Slot-2
      Device ID:    0x68b8
      Displays:
        LED Cinema Display:
          Main Display: Yes
          ...

I want to use the GPU not attached to the display for computation in a Java application with low-level bindings to OpenCL 1.1. How can I programmatically discover the GPU device in slot-1?

From my log file showing the results of device info queries:

... Device ATI Radeon HD 5770[AMD]: vendorId[1021b00] ...
... Device ATI Radeon HD 5770[AMD]: vendorId[2021b00] ...

相关帖子:How to match OpenCL devices with a specific GPU given PCI vendor, device and bus IDs in a multi-GPU system?

最佳答案

听起来您知道可以使用 clGetDeviceIds 获取系统中的设备,并且可以使用 clGetDeviceInfo< 向它们查询诸如 CL_DEVICE_NAME 之类的内容

不幸的是,我认为 OpenCL API 目前没有跨平台的方式来识别当前用于驱动显示器的计算设备。大多数时候,人们希望获得此设备,以便他们可以通过使用同一设备更快地共享 OpenGL/OpenCL。在您的情况下,您想知道是什么设备在驱动显示器,以便忽略它

但是,有一种特定于 Macintosh 的方法可以做到这一点。由于您提到您使用的是 Mac,因此流程如下:

  1. 使用您的 GPU 设备创建 OpenCL 环境。
  2. 向系统询问当前的 OpenGL 上下文。
  3. 通过扩展(来自 cl_gl_ext.h)询问 OpenCL 驱动显示器的设备。
  4. 使用供应商 ID 忽略该设备。

这是一个完整的程序,可以在 Mac 上执行此操作。我正在运行 Lion。

// compile with:
// clang -o test test.c -framework GLUT -framework OpenGL -framework OpenCL
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/CGLDevice.h>
#include <OpenCL/opencl.h>
#include <OpenCL/cl_gl_ext.h>
#include <stdio.h>

int main (int argc, char const *argv[]) {
  int i;
  cl_int error;

  // We need to do *something* to create a GL context:
  glutInit( &argc, (char**)argv );
  glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  glutCreateWindow( "OpenCL <-> OpenGL Test" );

  // So we can ask CGL for it:
  CGLContextObj gl_context = CGLGetCurrentContext();

  CGLShareGroupObj share_group = CGLGetShareGroup(gl_context);
  cl_context_properties properties[] = { CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, 
    (intptr_t)share_group, 0 };
  cl_context context = clCreateContext(properties, 0, NULL, 0, 0, &error);

  // And now we can ask OpenCL which particular device is being used by
  // OpenGL to do the rendering, currently:
  cl_device_id renderer;
  clGetGLContextInfoAPPLE(context, gl_context, 
    CL_CGL_DEVICE_FOR_CURRENT_VIRTUAL_SCREEN_APPLE, sizeof(renderer), 
    &renderer, NULL);

  cl_uint id_in_use;
  clGetDeviceInfo(renderer, CL_DEVICE_VENDOR_ID, sizeof(cl_uint), 
    &id_in_use, NULL);

  // Determine the number of devices:
  size_t size;
  cl_uint num_devices;
  clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &size);

  num_devices = size / sizeof(cl_device_id);
  cl_device_id devices[num_devices];
  clGetContextInfo(context, CL_CONTEXT_DEVICES, size, devices, NULL);

  // Now we have everything we need to use the device that IS NOT doing
  // rendering to the screen for our compute:
  char buf[128];
  cl_uint vendor_id;  
  for (i = 0; i < num_devices; i++) {
    clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL);
    clGetDeviceInfo(devices[i], CL_DEVICE_VENDOR_ID, sizeof(cl_uint), &vendor_id, NULL);
    fprintf(stdout, "%s (%x)", buf, vendor_id);
    if (vendor_id == id_in_use) {
      fprintf(stdout, " [ in use by GL for display ]\n");
    } else {
      fprintf(stdout, " [ totally free for compute! ]\n");
    }      
  }

  clReleaseContext(context);
  return 0;
}

当我在我的 iMac(一个 GPU)上尝试这个时,我得到:

ATI Radeon HD 6970M (1021b00) [ in use by GL for display ]

但是当我通过 ssh 在远程机器上尝试这个时:

ATI Radeon HD 5770 (1021b00) [ totally free for compute! ]

给我看看你的输出!我没有两个 GPU 盒子 :) 在我 friend 的多 GPU 机器上,运行 Mac OS 10.7.2:

GeForce GTX 285 (1022600) [ totally free for compute! ]
GeForce GT 120 (2022600) [ in use by GL for display ] 

请注意,可能有比 GLUT 更好的方法来启动和运行 GL。但 GLUT 并没有那么糟糕——您甚至不必在屏幕上显示一个窗口。这个程序没有。

关于opencl - 如何以编程方式在具有多个 GPU (OpenCL 1.1) 的平台上发现特定 GPU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8146056/

相关文章:

opencl - 高清处理器图形 (HD4000) 无法作为英特尔 OpenCL SDK 中的设备加载

python - 参数列表的长度 (3) 和 CL 生成的参数数量 (9) 不一致

c++ - OpenCL 计数器变量

eclipse - 如何使用 IDE(Netbeans、Eclipse)中的 optirun(Bumblebee)使用图形驱动程序运行构建?

opencl - float VS float N

opencv - OpenCL :Access proper index by using globalid(.)

gpu - CPU 和 GPU 内存共享

floating-point - NVIDIA GPU 的 OpenCL 中浮点值的原子加法?

c++ - OpenCL:从主机到设备缓冲区的并行写入?

c - 不应该在 GPU (OpenCL) 上更快地进行 3x3 卷积