我正在尝试在OpenCL中运行我的第一个代码。我编写了以下程序来检测计算机中可用的平台和设备。代码在我的计算机上运行良好,该计算机具有Intel CPU和NVIDIA GPU。它可以正确检测每个平台和设备。但是,当我在装有AMD-FX 770K和Radeon R7 240的计算机上运行时,输出如下图所示。至少,它必须将Radeon R7 240 GPU显示为此平台中的设备,但事实并非如此。知道为什么代码在AMD平台上不能正常工作吗?
以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CL/cl.h>
int main(){
cl_int err, i, j;
cl_platform_id *platforms;
cl_device_id *devices;
cl_uint num_platforms, num_devices;
size_t plat_name_size, devi_name_size;
char *plat_name_data, *devi_name_data;
err = clGetPlatformIDs(1, NULL, &num_platforms);
if (err < 0){
perror("No platform is found");
exit(1);
}
platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id)*num_platforms);
clGetPlatformIDs(num_platforms, platforms, NULL);
printf("Number of found platforms is %d\n ", num_platforms);
for (i = 0; i < num_platforms; i++){
err = clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 0, NULL, &plat_name_size);
if (err < 0){
perror("Couldn't read platform name.");
exit(1);
}
plat_name_data = (char*)malloc(plat_name_size);
clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, plat_name_size, plat_name_data, NULL);
printf("Platform No.%d is: %s\n", i, plat_name_data);
free(plat_name_data);
err = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 1, NULL, &num_devices);
if (err < 0){
perror("No device is found in this platform");
exit(1);
}
devices = (cl_device_id*)malloc(sizeof(cl_device_id)*(num_devices));
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL);
printf("Number of devices found in this platform is: %d\n", num_devices);
for (j = 0; j < num_devices; j++){
err = clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 0, NULL, &devi_name_size);
if (err < 0){
perror("Couldn't read the device name.");
exit(1);
}
devi_name_data = (char*)malloc(devi_name_size);
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, devi_name_size, devi_name_data, NULL);
printf("Device No.%d name is: %s\n", j, devi_name_data);
free(devi_name_data);
}
}
return 0;
}
答案 0 :(得分:2)
请在以下两个电话中将num_entries设置为“0”:
clGetPlatformIDs( 0 / * num_entries * / _,NULL,&amp; num_platforms);
clGetDeviceIDs(platforms [i],CL_DEVICE_TYPE_ALL, 0 / * num_entries * /,NULL,&amp; num_devices);