I'm studying OpenCL now. I'd like to program like clinfo command. And I write code as follows on AMDAPP 2.8.1. but this code is wrong running.
Concretely, I get an information when this program call clGetDeviceInfo() on core i5-430UM and no GPU machine at first time,
code:
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_TYPE, MAX_MSG_SIZE, (void*)(&msg_uint), &msglen);
printf( " ret = %d, msg = %d\n", ret, msg_uint);
print:
ret = 0, msg = 2
but I don't get any information and return code -33 when this program call clGetDeviceInfo() at second time.
code:
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_VENDOR, MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " ret = %d, msg = %s\n, ret, msg);
print:
ret = -33, msg = ...(depends memory status)
Please teach me where is my code wrong.
Program Code:
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
#define MAX_MSG_SIZE 256
#define MAX_PLATFORMS 16
#define MAX_DEVICES 256
int main()
{
cl_platform_id platform_id[ MAX_PLATFORMS];
cl_platform_info platform_info;
cl_uint platforms_num;
cl_device_id device_id[ MAX_DEVICES];
cl_uint devices_num;
cl_uint dummy;
cl_int ret;
char msg[ MAX_MSG_SIZE];
size_t msglen;
cl_uint msg_uint;
cl_ulong msg_ulong;
size_t msg_dim[ MAX_MSG_SIZE];
ret = clGetPlatformIDs( MAX_PLATFORMS, platform_id, &platforms_num);
for ( int pf = 0; pf < platforms_num; pf++) {
printf( "Platform %d:\n", pf);
ret = clGetPlatformInfo( platform_id[pf], CL_PLATFORM_PROFILE,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " PLATFORM_PROFILE\t%s\n", msg);
ret = clGetPlatformInfo( platform_id[pf], CL_PLATFORM_VERSION,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " PLATFORM_VERSION\t%s\n", msg);
ret = clGetPlatformInfo( platform_id[pf], CL_PLATFORM_NAME,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " PLATFORM_NAME\t\t%s\n", msg);
ret = clGetPlatformInfo( platform_id[pf], CL_PLATFORM_VENDOR,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " PLATFORM_VENDOR\t%s\n", msg);
ret = clGetPlatformInfo( platform_id[pf], CL_PLATFORM_EXTENSIONS,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " PLATFORM_EXTENTIONS\t%s\n", msg);
ret = clGetDeviceIDs( platform_id[pf], CL_DEVICE_TYPE_ALL, MAX_DEVICES,
device_id, &devices_num);
for ( int dv = 0; dv < devices_num; dv++) {
printf( "platform %d: device %d:\n", pf, dv);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_TYPE,
MAX_MSG_SIZE, (void*)(&msg_uint), &msglen);
printf( " ret = %d, dvid = %d\n", ret, device_id[dv]);
printf( " DEVICE_TYPE\t%d ", msg_uint);
switch ( msg_uint) {
case CL_DEVICE_TYPE_CPU:
printf( "CPU\n");
break;
case CL_DEVICE_TYPE_GPU:
printf( "GPU\n");
break;
case CL_DEVICE_TYPE_ACCELERATOR:
printf( "ACCELERATOR\n");
break;
case CL_DEVICE_TYPE_CUSTOM:
printf( "CUSTOM\n");
break;
default:
printf( "\n");
}
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_VENDOR,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " ret = %d, dvid = %d\n", ret, device_id[dv]);
printf( " DEVICE_VENDOR\t%s\n", msg);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_NAME,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " DEVICE_NAME\t%s\n", msg);
ret = clGetDeviceInfo( device_id[dv], CL_DRIVER_VERSION,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " DRIVER_VERSION\t%s\n", msg);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_PROFILE,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " DEVICE_PROFILE\t%s\n", msg);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_VERSION,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " DEVICE_VERSION\t%s\n", msg);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_OPENCL_C_VERSION,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " DEVICE_OPENCL_C_VERSION\t%s\n", msg);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_EXTENSIONS,
MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " DEVICE_EXTENSIONS\t%s\n", msg);
}
/*
ret = clGetDeviceIDs( platform_id[ pf], CL_DEVICE_TYPE_ALL, 1,
&device_id, &devices_num);
ret = clGetDeviceIDs( platform_id[ pf], CL_Device_TYPE_ALL, devices_num,
&device_id, &dummy);
*/
}
return 0;
}
Solved! Go to Solution.
Your code is behaving bit strange. Probably I need to file a bug.
On linux machine, it was not giving any error in return code, but the loop is becoming infinite somehow. The code worked fine after these changes:
for ( int pf = 0; pf < platforms_num; pf++) {
ret = clGetDeviceIDs( platform_id[pf], CL_DEVICE_TYPE_ALL, MAX_DEVICES, device_id, &devices_num);
printf( " ret = %d pf:%d\n", ret,pf);
for ( int dv = 0; dv < devices_num; dv++) {
printf( "platform %d: device %d:\n", pf, dv);
cl_device_type devType;
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_TYPE, sizeof(cl_device_type), (void*)(&devType), NULL);
printf( " ret = %d, dvid = %d, msg_uint = %d\n", ret, msg_uint, device_id[dv]);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_VENDOR, MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " ret = %d, dvid = %d, msg = %s\n", ret, device_id[dv], msg);
scanf("%d",&ret);
}
}
Also you should check AMD APP SDK Samples to know how to use the API in a less confusing manner.
Check cl.h file for meaning of error code. Also check value of return after running the OpenCL APIs.
I undertand that return code -33 means "CL_INVALID_DEVICE"
However, I don't know about reason. When follows this code calls clGetDeviceInfo() at first, it runs right, but when it calls clGetDeviceInfo() at second, it runs wrong. Why?
rewrite simply code:
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
#define MAX_MSG_SIZE 256
#define MAX_PLATFORMS 16
#define MAX_DEVICES 256
int main()
{
cl_platform_id platform_id[ MAX_PLATFORMS];
cl_platform_info platform_info;
cl_uint platforms_num;
cl_device_id device_id[ MAX_DEVICES];
cl_uint devices_num;
cl_uint dummy;
cl_int ret;
char msg[ MAX_MSG_SIZE];
size_t msglen;
cl_uint msg_uint;
cl_ulong msg_ulong;
size_t msg_dim[ MAX_MSG_SIZE];
ret = clGetPlatformIDs( MAX_PLATFORMS, platform_id, &platforms_num);
for ( int pf = 0; pf < platforms_num; pf++) {
ret = clGetDeviceIDs( platform_id[pf], CL_DEVICE_TYPE_ALL, MAX_DEVICES, device_id, &devices_num);
for ( int dv = 0; dv < devices_num; dv++) {
printf( "platform %d: device %d:\n", pf, dv);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_TYPE, MAX_MSG_SIZE, (void*)(&msg_uint), &msglen);
printf( " ret = %d, dvid = %d, msg_uint = %d\n", ret, msg_uint, device_id[dv]);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_VENDOR, MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " ret = %d, dvid = %d, msg = %s\n", ret, device_id[dv], msg);
}
}
return 0;
}
results:
platform 0: device 0:
ret = 0, dvid = 0, msg_uint = 2
ret = -33, dvid = 0, msg =
Your code is behaving bit strange. Probably I need to file a bug.
On linux machine, it was not giving any error in return code, but the loop is becoming infinite somehow. The code worked fine after these changes:
for ( int pf = 0; pf < platforms_num; pf++) {
ret = clGetDeviceIDs( platform_id[pf], CL_DEVICE_TYPE_ALL, MAX_DEVICES, device_id, &devices_num);
printf( " ret = %d pf:%d\n", ret,pf);
for ( int dv = 0; dv < devices_num; dv++) {
printf( "platform %d: device %d:\n", pf, dv);
cl_device_type devType;
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_TYPE, sizeof(cl_device_type), (void*)(&devType), NULL);
printf( " ret = %d, dvid = %d, msg_uint = %d\n", ret, msg_uint, device_id[dv]);
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_VENDOR, MAX_MSG_SIZE, (void*)msg, &msglen);
printf( " ret = %d, dvid = %d, msg = %s\n", ret, device_id[dv], msg);
scanf("%d",&ret);
}
}
Also you should check AMD APP SDK Samples to know how to use the API in a less confusing manner.
The issue was with msg_uint variable passed for CL_DEVICE_TYPE.
msg_uint is a cl_uint which is 4 bytes on my machine. But CL_DEVICE_TYPE property is a cl_device_type object, which is 8 bytes. So a memory corruption was happening.
Your code did not gave any error for me on windows. I was using Win7 64, Cat 13.6 beta, HD 7870 GPU with SDK 2.8.1
I was preoccupied with the handling of the multi-device and misunderstood it about message size.
Your code runs right.
Thank you for advice.
himanshu.gautam による書き込み:
Your code is behaving bit strange. Probably I need to file a bug.
On linux machine, it was not giving any error in return code, but the loop is becoming infinite somehow. The code worked fine after these changes:
(snip)
cl_device_type devType;
ret = clGetDeviceInfo( device_id[dv], CL_DEVICE_TYPE, sizeof(cl_device_type), (void*)(&devType),
(snip)
Also you should check AMD APP SDK Samples to know how to use the API in a less confusing manner.