【问题标题】:c void function invoking an OpenCL functions would not renewed valuec void 函数调用 OpenCL 函数不会更新值
【发布时间】:2022-06-19 16:35:57
【问题描述】:

我正在编写一个 void 函数来获取 OpenCL 设备的数量和 OpenCL 设备的列表。这两个在函数内部看起来是正确的,但是我在主函数中没有得到正确的值。

以下是生成问题的基本最小代码。

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

#define CL_TARGET_OPPENCL_VERSION 200
#include <CL/cl.h>


void getcldeviceinfo(int plfn, int dev_type, cl_uint ndev, cl_device_id devs[])
{  
   cl_int         cle;
   cl_uint        nplf;
   cl_platform_id plfs[2];

   // getting the number of platforms and platform ids
   cle   =  clGetPlatformIDs(0, NULL, &nplf);
   cle   =  clGetPlatformIDs(nplf, plfs, NULL);

   if (cle != CL_SUCCESS || nplf <= 0)
   {
      printf(\"Failed to find any OpenCL platforms\\n\");
   }


   // getting the number of devices and device ids
   // you need to specify which platform you wish to use
   if (dev_type != 0)
   {
      cle   =  clGetDeviceIDs(plfs[plfn], CL_DEVICE_TYPE_CPU, 0, NULL, &ndev);
      cle   =  clGetDeviceIDs(plfs[plfn], CL_DEVICE_TYPE_CPU, ndev, devs, NULL);
   }
   else
   {
      cle   =  clGetDeviceIDs(plfs[plfn], CL_DEVICE_TYPE_GPU, 0, NULL, &ndev);
      cle   =  clGetDeviceIDs(plfs[plfn], CL_DEVICE_TYPE_GPU, ndev, devs, NULL);
   }


   printf(\"number of devices inside the function : %u\\n\",ndev);
}


int main(int argc, char** argv)
{
    cl_device_id devices[10];
    cl_uint ndev = 0;

    printf(\"number of devices inside the main function before : %u\\n\",ndev);
    getcldeviceinfo(0, 0, ndev, devices);

    printf(\"number of devices inside the main function after  : %u\\n\",ndev);

    return 0;
}

当我运行上面的代码时,我希望得到以下输出。

number of devices inside the main function before : 0
number of devices inside the function : 2
number of devices inside the main function after  : 2

但我明白了

number of devices inside the main function before : 0
number of devices inside the function : 2
number of devices inside the main function after  : 0

我在这里做错了什么?

  • 您需要将 ndev 作为指向您的函数 clgetdeviceinfo 的指针传递。
  • 像 getcldeviceinfo(0, 0, &ndev, devices); ?
  • 是的,但是您需要相应地调整您的功能签名和功能架构。

标签: c opencl


【解决方案1】:

您需要函数参数 ndev 作为 call-by 引用(然后该函数可以访问存储原始 ndev 的内存位置),而不是 call-by-value(该函数创建自己的 @987654324 的私有副本@ 并且不能修改原始值):

void getcldeviceinfo(int plfn, int dev_type, cl_uint& ndev, cl_device_id* devs) { // note the "&"
    // ...
}

int main(int argc, char** argv) {
    cl_device_id devices[10];
    cl_uint ndev = 0;

    printf("number of devices inside the main function before : %u\n",ndev);
    getcldeviceinfo(0, 0, ndev, devices);

    printf("number of devices inside the main function after  : %u\n",ndev);

    return 0;
}

为了轻松开始使用 OpenCL,我创建了一个轻量级包装器,它极大地简化了 OpenCL C++ 绑定并消除了随之而来的整个代码开销。您可以使用get_devices() 获取所有可用设备的列表,并使用select_device_with_most_flops() 自动找到最快的设备。 https://github.com/ProjectPhysX/OpenCL-Wrapper

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    相关资源
    最近更新 更多