系统软件层

下面是一个使用 OpenCL API 列出系统中所有可用的 AMD 设备的示例代码。该代码将获取设备名称、驱动版本、计算单元数量和全局内存大小,并创建和销毁 OpenCL 上下文。

示例代码:

#include <iostream>
#include <CL/cl.h>

// Check the return value of OpenCL functions and print error message on failure
void checkOpenCLErrors(cl_int result) {
    if (result != CL_SUCCESS) {
        std::cerr << "OpenCL Error: " << result << std::endl;
        exit(EXIT_FAILURE);
    }
}

// Print information about an OpenCL device
void printDeviceInfo(cl_device_id device) {
    char deviceName[256];
    checkOpenCLErrors(clGetDeviceInfo(device, CL_DEVICE_NAME, sizeof(deviceName), deviceName, nullptr));
  
    cl_uint computeUnits;
    checkOpenCLErrors(clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(computeUnits), &computeUnits, nullptr));
  
    cl_uint driverVersionSize;
    checkOpenCLErrors(clGetDeviceInfo(device, CL_DRIVER_VERSION, 0, nullptr, &driverVersionSize));
    std::string driverVersion(driverVersionSize, '\0');
    checkOpenCLErrors(clGetDeviceInfo(device, CL_DRIVER_VERSION, driverVersionSize, &driverVersion[0], nullptr));

    cl_ulong globalMemorySize;
    checkOpenCLErrors(clGetDeviceInfo(device, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(globalMemorySize), &globalMemorySize, nullptr));

    // Print device details
    std::cout << "Device Name: " << deviceName << std::endl;
    std::cout << "Max Compute Units: " << computeUnits << std::endl;
    std::cout << "Driver Version: " << driverVersion << std::endl;
    std::cout << "Total Global Memory: " << globalMemorySize / (1024 * 1024) << " MB" << std::endl;
}

int main() {
    cl_int result;

    // Get the number of available OpenCL platforms
    cl_uint platformCount;
    result = clGetPlatformIDs(0, nullptr, &platformCount);
    checkOpenCLErrors(result);

    std::vector<cl_platform_id> platforms(platformCount);
    result = clGetPlatformIDs(platformCount, platforms.data(), nullptr);
    checkOpenCLErrors(result);

    cl_platform_id amdPlatform = nullptr;

    // Check for AMD platform
    for (cl_platform_id platform : platforms) {
        char platformName[256];
        checkOpenCLErrors(clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(platformName), platformName, nullptr));
        if (std::string(platformName).find("AMD") != std::string::npos) {
            amdPlatform = platform;
            break;
        }
    }

    if (amdPlatform == nullptr) {
        std::cerr << "No AMD platform found." << std::endl;
        return -1;
    }

    // Get the number of devices for the AMD platform
    cl_uint deviceCount;
    checkOpenCLErrors(clGetDeviceIDs(amdPlatform, CL_DEVICE_TYPE_GPU, 0, nullptr, &deviceCount));
    std::cout << "Number of AMD GPU Devices: " << deviceCount << std::endl;

    std::vector<cl_device_id> devices(deviceCount);
    checkOpenCLErrors(clGetDeviceIDs(amdPlatform, CL_DEVICE_TYPE_GPU, deviceCount, devices.data(), nullptr));

    // Iterate through each device and print its information
    for (cl_device_id device : devices) {
        printDeviceInfo(device);
        std::cout << std::endl;
    }

    // Create an OpenCL context
    cl_context context = clCreateContext(nullptr, deviceCount, devices.data(), nullptr, nullptr, &result);
    checkOpenCLErrors(result);
    std::cout << "OpenCL context created successfully." << std::endl;

    // Cleanup
    checkOpenCLErrors(clReleaseContext(context));

    return 0;
}

结果:

Number of AMD GPU Devices: 1
Device Name: AMD Radeon RX 7900 XTX