【发布时间】: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); ?
-
是的,但是您需要相应地调整您的功能签名和功能架构。