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

wjo*_*son 3 opencl

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

    ATI Radeon HD 5770:
       总线:PCIe
       插槽:Slot-1
       供应商:ATI (0x1002)
       设备 ID:0x68b8
       ...

    ATI Radeon HD 5770:
      总线:PCIe
      插槽:Slot-2
      设备 ID:0x68b8
      显示:
        LED影院显示:
          主显示器:是
          ...

我想使用未连接到显示器的 GPU 在 Java 应用程序中使用低级绑定到 OpenCL 1.1 进行计算。如何以编程方式发现插槽 1 中的 GPU 设备?

从我的日志文件中显示设备信息查询的结果:

... Device ATI Radeon HD 5770[AMD]: vendorId[1021b00] ...
... Device ATI Radeon HD 5770[AMD]: vendorId[2021b00] ...
Run Code Online (Sandbox Code Playgroud)

相关文章:如何在多 GPU 系统中给定 PCI 供应商、设备和总线 ID,将 OpenCL 设备与特定 GPU 匹配?

Jam*_*mes 5

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

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

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

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

这是一个完整的程序,可以在 Mac 上执行此操作。我在跑狮子。

// 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;
}
Run Code Online (Sandbox Code Playgroud)

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

ATI Radeon HD 6970M (1021b00) [ in use by GL for display ]
Run Code Online (Sandbox Code Playgroud)

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

ATI Radeon HD 5770 (1021b00) [ totally free for compute! ]
Run Code Online (Sandbox Code Playgroud)

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

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

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