这个社区有点冷,发个C和C++的查询平台和设备的简单实例代码~

piaopiaolanghua 2023-12-08 09:35:48
//#include <QCoreApplication>

#include <stdio.h>
#include <stdlib.h>

#include <CL/cl.h>

//static cl_int AllocateAndGetPlatformInfoString(cl_platform_id platformId,
//                                               cl_platform_info param_name,
//                                               char** pStr)
//{
//    cl_int errorCode = CL_SUCCESS;

//    size_t size = 0;
//    char* str = NULL;

//    if (errorCode == CL_SUCCESS)
//    {
//        errorCode = clGetPlatformInfo(platformId, param_name, 0, NULL, &size);
//    }

//    if (errorCode == CL_SUCCESS)
//    {
//        if (size != 0)
//        {
//            str = (char*)malloc(size);
//            if (str == NULL)
//            {
//                errorCode = CL_OUT_OF_HOST_MEMORY;
//            }
//        }
//    }

//    if (errorCode == CL_SUCCESS)
//    {
//        errorCode = clGetPlatformInfo(platformId, param_name, size, str, NULL);
//    }

//    if (errorCode != CL_SUCCESS)
//    {
//        free(str);
//    }
//    else
//    {
//        *pStr = str;
//    }

//    return errorCode;
//}

//static cl_int PrintPlatformInfoSummary(cl_platform_id platformId)
//{
//    cl_int errorCode = CL_SUCCESS;

//    char* platformName = NULL;
//    char* platformVendor = NULL;
//    char* platformVersion = NULL;

//    errorCode |= AllocateAndGetPlatformInfoString(platformId, CL_PLATFORM_NAME,
//                                                  &platformName);
//    errorCode |= AllocateAndGetPlatformInfoString(
//        platformId, CL_PLATFORM_VENDOR, &platformVendor);
//    errorCode |= AllocateAndGetPlatformInfoString(
//        platformId, CL_PLATFORM_VERSION, &platformVersion);

//    printf("\tName:           %s\n", platformName);
//    printf("\tVendor:         %s\n", platformVendor);
//    printf("\tDriver Version: %s\n", platformVersion);

//    free(platformName);
//    free(platformVendor);
//    free(platformVersion);

//    return errorCode;
//}

//static cl_int AllocateAndGetDeviceInfoString(cl_device_id device,
//                                             cl_device_info param_name,
//                                             char** pStr)
//{
//    cl_int errorCode = CL_SUCCESS;

//    size_t size = 0;
//    char* str = NULL;

//    if (errorCode == CL_SUCCESS)
//    {
//        errorCode = clGetDeviceInfo(device, param_name, 0, NULL, &size);
//    }

//    if (errorCode == CL_SUCCESS)
//    {
//        if (size != 0)
//        {
//            str = (char*)malloc(size);
//            if (str == NULL)
//            {
//                errorCode = CL_OUT_OF_HOST_MEMORY;
//            }
//        }
//    }

//    if (errorCode == CL_SUCCESS)
//    {
//        errorCode = clGetDeviceInfo(device, param_name, size, str, NULL);
//    }

//    if (errorCode != CL_SUCCESS)
//    {
//        free(str);
//    }
//    else
//    {
//        *pStr = str;
//    }

//    return errorCode;
//}

//static void PrintDeviceType(const char* label, cl_device_type type)
//{
//    printf("%s%s%s%s%s%s\n", label,
//           (type & CL_DEVICE_TYPE_DEFAULT) ? "DEFAULT " : "",
//           (type & CL_DEVICE_TYPE_CPU) ? "CPU " : "",
//           (type & CL_DEVICE_TYPE_GPU) ? "GPU " : "",
//           (type & CL_DEVICE_TYPE_ACCELERATOR) ? "ACCELERATOR " : "",
//           (type & CL_DEVICE_TYPE_CUSTOM) ? "CUSTOM " : "");
//}

//static cl_int PrintDeviceInfoSummary(cl_device_id* devices, cl_uint numDevices)
//{
//    cl_int errorCode = CL_SUCCESS;

//    cl_device_type deviceType;
//    char* deviceName = NULL;
//    char* deviceVendor = NULL;
//    char* deviceVersion = NULL;
//    char* deviceProfile = NULL;
//    char* driverVersion = NULL;

//    cl_uint i = 0;
//    for (i = 0; i < numDevices; i++)
//    {
//        errorCode |= clGetDeviceInfo(devices[i], CL_DEVICE_TYPE,
//                                     sizeof(deviceType), &deviceType, NULL);
//        errorCode |= AllocateAndGetDeviceInfoString(devices[i], CL_DEVICE_NAME,
//                                                    &deviceName);
//        errorCode |= AllocateAndGetDeviceInfoString(
//            devices[i], CL_DEVICE_VENDOR, &deviceVendor);
//        errorCode |= AllocateAndGetDeviceInfoString(
//            devices[i], CL_DEVICE_VERSION, &deviceVersion);
//        errorCode |= AllocateAndGetDeviceInfoString(
//            devices[i], CL_DEVICE_PROFILE, &deviceProfile);
//        errorCode |= AllocateAndGetDeviceInfoString(
//            devices[i], CL_DRIVER_VERSION, &driverVersion);

//        if (errorCode == CL_SUCCESS)
//        {
//            printf("Device[%u]:\n", i);

//            PrintDeviceType("\tType:           ", deviceType);

//            printf("\tName:           %s\n", deviceName);
//            printf("\tVendor:         %s\n", deviceVendor);
//            printf("\tDevice Version: %s\n", deviceVersion);
//            printf("\tDevice Profile: %s\n", deviceProfile);
//            printf("\tDriver Version: %s\n", driverVersion);
//        }
//        else
//        {
//            fprintf(stderr, "Error getting device info for device %u.\n", i);
//        }

//        free(deviceName);
//        free(deviceVendor);
//        free(deviceVersion);
//        free(deviceProfile);
//        free(driverVersion);
//    }

//    return errorCode;
//}

//int main0(int argc, char** argv)
//{
//    cl_uint numPlatforms = 0;
//    clGetPlatformIDs(0, NULL, &numPlatforms);
//    printf("Enumerated %u platforms.\n\n", numPlatforms);

//    cl_platform_id* platforms =
//        (cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));
//    clGetPlatformIDs(numPlatforms, platforms, NULL);

//    cl_uint i = 0;
//    for (i = 0; i < numPlatforms; i++)
//    {
//        printf("Platform[%u]:\n", i);
//        PrintPlatformInfoSummary(platforms[i]);

//        cl_uint numDevices = 0;
//        clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);

//        cl_device_id* devices =
//            (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
//        clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices, devices,
//                       NULL);

//        PrintDeviceInfoSummary(devices, numDevices);
//        printf("\n");

//        free(devices);
//    }

//    free(platforms);

//    printf("Done.\n");

//    return 0;
//}


//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------

#include <iostream>
#include <string>
#include <vector>

#include <CL/opencl.hpp>

static cl_int PrintPlatformInfoSummary(cl::Platform platform)
{
    std::cout << "\tName:           " << platform.getInfo<CL_PLATFORM_NAME>()
              << "\n";
    std::cout << "\tVendor:         " << platform.getInfo<CL_PLATFORM_VENDOR>()
              << "\n";
    std::cout << "\tDriver Version: " << platform.getInfo<CL_PLATFORM_VERSION>()
              << "\n";

    return CL_SUCCESS;
}

static void PrintDeviceType(const std::string& label, cl_device_type type)
{
    std::cout << label << ((type & CL_DEVICE_TYPE_DEFAULT) ? "DEFAULT " : "")
              << ((type & CL_DEVICE_TYPE_CPU) ? "CPU " : "")
              << ((type & CL_DEVICE_TYPE_GPU) ? "GPU " : "")
              << ((type & CL_DEVICE_TYPE_ACCELERATOR) ? "ACCELERATOR " : "")
              << ((type & CL_DEVICE_TYPE_CUSTOM) ? "CUSTOM " : "") << "\n";
}

static cl_int PrintDeviceInfoSummary(const std::vector<cl::Device> devices)
{
    for (size_t i = 0; i < devices.size(); i++)
    {
        std::cout << "Device[" << i << "]:\n";

        cl_device_type deviceType = devices[i].getInfo<CL_DEVICE_TYPE>();
        PrintDeviceType("\tType:           ", deviceType);

        std::cout << "\tName:           "
                  << devices[i].getInfo<CL_DEVICE_NAME>() << "\n";
        std::cout << "\tVendor:         "
                  << devices[i].getInfo<CL_DEVICE_VENDOR>() << "\n";
        std::cout << "\tDevice Version: "
                  << devices[i].getInfo<CL_DEVICE_VERSION>() << "\n";
        std::cout << "\tOCL  C Version: "
                  << devices[i].getInfo<CL_DEVICE_OPENCL_C_VERSION>() << "\n";
        std::cout << "\tDevice Profile: "
                  << devices[i].getInfo<CL_DEVICE_PROFILE>() << "\n";
        std::cout << "\tDriver Version: "
                  << devices[i].getInfo<CL_DRIVER_VERSION>() << "\n";
        std::cout << "\tDevices  Units: "
                  << devices[i].getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << "\n";
        std::cout << "\tDevices MaxMem: "
                  << devices[i].getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>()/1024/1024 << "MB\n";
        std::cout << "\tDevices LocMem: "
                  << devices[i].getInfo<CL_DEVICE_LOCAL_MEM_SIZE>()/1024 << "KB\n";

//        std::cout << "\tDevice Extensions: "
//                  << devices[i].getInfo<CL_DEVICE_EXTENSIONS>() << "\n";




    }

    return CL_SUCCESS;
}

int main(int argc, char** argv)
{
    std::vector<cl::Platform> platforms;
    cl::Platform::get(&platforms);
    std::cout << "Enumerated " << platforms.size() << " platforms.\n\n";

    for (size_t i = 0; i < platforms.size(); i++)
    {
        std::cout << "Platform[" << i << "]:\n";
        PrintPlatformInfoSummary(platforms[i]);

        std::vector<cl::Device> devices;
        platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices);

        PrintDeviceInfoSummary(devices);
        std::cout << "\n";
    }

    std::cout << "Done.\n";

    return 0;
}

 

Qt pro的配置增加一下两行,配合include和lib
LIBS += -LD:/XXXXXXXX/OpenCL-SDK-v2023.04.17-Win-x64/lib/ -lOpenCL
INCLUDEPATH += D:/XXXXXXXX/OpenCL-SDK-v2023.04.17-Win-x64/include
 
...全文
126 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

606

社区成员

发帖
与我相关
我的任务
社区描述
异构开发技术
社区管理员
  • OpenCL和异构编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧