【发布时间】:2013-03-08 03:07:06
【问题描述】:
我正在尝试开始使用 OpenCL(英特尔 opencl-1.2-3.0.56860)。我设法在 Ubuntu 12.05 下安装了来自 Intel 的 OpenCL SDK(使用“alien”将 rpm 包转换为 *.deb 包)。现在我尝试运行我的第一个简单的 OpenCL 程序...要运行我需要使用的程序,请设置 LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=/opt/intel/opencl/lib64/
我的问题是,我在调用 clCreateContext(...) 时总是收到错误“CL_DEVICE_NOT_AVAILABLE”。
这是我的源代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <CL/cl.h>
#include <CL/cl_platform.h>
#include <string.h>
// Enable double precision values
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
// OpenCL kernel. Each work item takes care of one element of c
const char *kernelSource = "\n" \
"__kernel void vecAdd( __global double *a, \n" \
" __global double *b, \n" \
" __global double *c, \n" \
" const unsigned int n) \n" \
"{ \n" \
" //Get our global thread ID \n" \
" int id = get_global_id(0); \n" \
" \n" \
" //Make sure we do not go out of bounds \n" \
" if (id < n) \n" \
" c[id] = a[id] + b[id]; \n" \
"} \n" \
"\n" ;
const char* err2str(cl_int err) {
switch(err) {
case CL_SUCCESS: return "CL_SUCCESS";
case CL_INVALID_PROGRAM: return "CL_INVALID_PROGRAM";
case CL_INVALID_VALUE: return "CL_INVALID_VALUE";
case CL_INVALID_DEVICE: return "CL_INVALID_DEVICE";
case CL_INVALID_BINARY: return "CL_INVALID_BINARY";
case CL_INVALID_BUILD_OPTIONS: return "CL_INVALID_BUILD_OPTIONS";
case CL_INVALID_OPERATION: return "CL_INVALID_OPERATION";
case CL_DEVICE_NOT_AVAILABLE: return "CL_DEVICE_NOT_AVAILABLE";
default: return "unknown";
}
}
void pfn_notify(const char *errinfo, const void *private_info, size_t cb, void *user_data)
{
fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo);
}
cl_platform_id GetIntelOCLPlatform()
{
cl_platform_id pPlatforms[10] = { 0 };
char pPlatformName[128] = { 0 };
cl_uint uiPlatformsCount = 0;
cl_int err = clGetPlatformIDs(10, pPlatforms, &uiPlatformsCount);
for (cl_uint ui = 0; ui < uiPlatformsCount; ++ui)
{
err = clGetPlatformInfo(pPlatforms[ui], CL_PLATFORM_NAME, 128 * sizeof(char), pPlatformName, NULL);
if ( err != CL_SUCCESS )
{
printf("ERROR: Failed to retreive platform vendor name.\n", ui);
return NULL;
}
if (!strcmp(pPlatformName, "Intel(R) OpenCL"))
return pPlatforms[ui];
}
return NULL;
}
int main( int argc, char* argv[] )
{
cl_platform_id cpPlatform; // OpenCL platform
cl_device_id device_id; // device ID
cl_context context; // context
cl_command_queue queue; // command queue
cl_program program; // program
cl_kernel kernel; // kernel
size_t cb;
cl_device_id devices[100];
cl_uint devices_n = 0;
cl_int err;
cl_platform_id intel_platform_id = GetIntelOCLPlatform();
if( intel_platform_id == NULL ) {
printf("ERROR: Failed to find Intel OpenCL platform.\n");
return -1;
}
// Get Device Info...
err = clGetDeviceIDs(intel_platform_id, CL_DEVICE_TYPE_ALL, 100, devices, &devices_n);
printf("ERR(clGetDeviceIDs) = %d\n", err);
printf("#DEVICES: %d\n", devices_n);
for (int i=0; i<devices_n; i++) {
char buffer[10240];
cl_uint buf_uint;
cl_ulong buf_ulong;
printf(" -- %d --\n", i);
err = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(buffer), buffer, NULL);
printf(" DEVICE_NAME = %s\n", buffer);
err = clGetDeviceInfo(devices[i], CL_DEVICE_VENDOR, sizeof(buffer), buffer, NULL);
printf(" DEVICE_VENDOR = %s\n", buffer);
err = clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, sizeof(buffer), buffer, NULL);
printf(" DEVICE_VERSION = %s\n", buffer);
err = clGetDeviceInfo(devices[i], CL_DRIVER_VERSION, sizeof(buffer), buffer, NULL);
printf(" DRIVER_VERSION = %s\n", buffer);
err = clGetDeviceInfo(devices[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(buf_uint), &buf_uint, NULL);
printf(" DEVICE_MAX_COMPUTE_UNITS = %u\n", (unsigned int)buf_uint);
err = clGetDeviceInfo(devices[i], CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(buf_uint), &buf_uint, NULL);
printf(" DEVICE_MAX_CLOCK_FREQUENCY = %u\n", (unsigned int)buf_uint);
err = clGetDeviceInfo(devices[i], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(buf_ulong), &buf_ulong, NULL);
printf(" DEVICE_GLOBAL_MEM_SIZE = %llu\n", (unsigned long long)buf_ulong);
}
context = clCreateContext(NULL, 1, devices, &pfn_notify, NULL, &err);
printf("ERR(clCreateContext) = %d [%s]\n", err, err2str(err));
// Create a context
cl_context_properties context_properties[3] = {CL_CONTEXT_PLATFORM, (cl_context_properties)intel_platform_id, (cl_context_properties)NULL };
// context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
context = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_CPU, NULL, NULL, &err);
printf("ERR(clCreateContextFromType) = %d [%s]\n", err, err2str(err));
if (err != CL_SUCCESS) exit(0);
if (context == (cl_context)0) {
printf("Illegal context?");
exit(0);
}
return 0;
}
输出是:
ERR(clGetDeviceIDs) = 0
#DEVICES: 1
-- 0 --
DEVICE_NAME = Intel(R) Core(TM)2 Duo CPU P7450 @ 2.13GHz
DEVICE_VENDOR = Intel(R) Corporation
DEVICE_VERSION = OpenCL 1.2 (Build 56860)
DRIVER_VERSION = 1.2
DEVICE_MAX_COMPUTE_UNITS = 2
DEVICE_MAX_CLOCK_FREQUENCY = 2130
DEVICE_GLOBAL_MEM_SIZE = 3152510976
ERR(clCreateContext) = -2 [CL_DEVICE_NOT_AVAILABLE]
ERR(clCreateContextFromType) = -2 [CL_DEVICE_NOT_AVAILABLE]
任何想法如何解决这个问题?
问候, 斯蒂芬
---- 编辑 -----
CPU 有 sse4_1 标志(也有:sse、sse2、sss3)。
cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 23
model name : Intel(R) Core(TM)2 Duo CPU P7450 @ 2.13GHz
stepping : 6
microcode : 0x60c
cpu MHz : 800.000
cache size : 3072 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 10
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl aperfmperf pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm sse4_1 lahf_lm dtherm
bogomips : 4255.56
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
[ same for the other core]
【问题讨论】:
-
首先确保您的 CPU 受 Intel 的 SDK 支持。你的程序在我的机器上运行良好。来自英特尔的 OpenCL 1.1。
-
感谢测试。你使用什么样的硬件?这是这台电脑中唯一的CPU吗?我在 GPU 上成功运行了代码。同一台 PC,但 Nvidia 平台。