Hi, I have a problem with OpenCL on my Ubuntu 12.04 box and a NVIDIA 7100 GS card
I use this very basic source :
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#include <CL/cl.h>
#include <CL/cl.hpp>
int main(int argc, char *argv[])
{
int err;
cl_uint platforms;
cl_platform_id platform = NULL;
char cBuffer[1024];
err = clGetPlatformIDs( 1, &platform, &platforms );
if (err != CL_SUCCESS)
{
printf("Error in OpenCL call!
");
return EXIT_FAILURE;
}
printf("Number of platforms: %d
", platforms);
err = clGetPlatformInfo( platform, CL_PLATFORM_NAME, sizeof(cBuffer), cBuffer, NULL );
if (err != CL_SUCCESS)
{
printf("Error in OpenCL call!
");
return EXIT_FAILURE;
}
printf("CL_PLATFORM_NAME : %s
", cBuffer);
err = clGetPlatformInfo( platform, CL_PLATFORM_VERSION, sizeof(cBuffer), cBuffer, NULL );
if (err != CL_SUCCESS)
{
printf("Error in OpenCL call!
");
return EXIT_FAILURE;
}
printf("CL_PLATFORM_VERSION : %s
", cBuffer);
}
And compile it using this makefile :
all : test1
test1 : test1.cpp
g++ test1.cpp -L/etc/alternatives -lOpenCL -o test1
clean : test1
rm test1
This compile “fine” (cf. only the CL_USE_DEPRECATED_OPENCL_1_1_APIS warning, but I have a lot of compilations errors without this …)
yannoo@Ubuntoo:~/OPENCL/v2$ make
g++ test1.cpp -L/etc/alternatives -lOpenCL -o test1
In file included from /usr/include/CL/opencl.h:43:0,
from /usr/include/CL/cl.hpp:164,
from test1.cpp:6:
/usr/include/CL/cl_gl.h:107:2: attention : #warning CL_USE_DEPRECATED_OPENCL_1_1_APIS is defined. These APIs are unsupported and untested in OpenCL 1.2! [-Wcpp]
But when I run the executable, it’s like it cannot find a valid OpenCL context
yannoo@Ubuntoo:~/OPENCL/v2$ ./test1
Error in OpenCL call!
=> so the problem is here
err = clGetPlatformIDs( 1, &platform, &platforms );
if (err != CL_SUCCESS)
{
printf("Error in OpenCL call!
");
return EXIT_FAILURE;
}
Note that I have the same type of error with another source lesson1.cpp :
#include <utility>
#define __NO_STD_VECTOR // Use cl::vector instead of STL version
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#include <CL/cl.h>
#include <CL/cl.hpp>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>
const std::string hw("Hello World
");
inline void
checkErr(cl_int err, const char * name)
{
if (err != CL_SUCCESS) {
std::cerr << "ERROR : " << name
<< " (" << err << ")" << std::endl;
exit(EXIT_FAILURE);
}
}
int
main(void)
{
cl_int err;
cl::vector< cl::Platform > platformList;
cl::Platform::get(&platformList);
checkErr(platformList.size()!=0 ? CL_SUCCESS : -1, "cl::Platform::get");
std::cerr << "Platform number is: " << platformList.size() << std::endl;
std::string platformVendor;
platformList[0].getInfo((cl_platform_info)CL_PLATFORM_VENDOR, &platformVendor);
std::cerr << "Platform is by: " << platformVendor << "
";
cl_context_properties cprops[3] =
{CL_CONTEXT_PLATFORM, (cl_context_properties)(platformList[0])(), 0};
cl::Context context(
CL_DEVICE_TYPE_CPU,
cprops,
NULL,
NULL,
&err);
checkErr(err, "Conext::Context()");
char * outH = new char[hw.length()+1];
cl::Buffer outCL(
context,
CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,
hw.length()+1,
outH,
&err);
checkErr(err, "Buffer::Buffer()");
cl::vector<cl::Device> devices;
devices = context.getInfo<CL_CONTEXT_DEVICES>();
checkErr(
devices.size() > 0 ? CL_SUCCESS : -1, "devices.size() > 0");
std::ifstream file("lesson1_kernels.cl");
checkErr(file.is_open() ? CL_SUCCESS:-1, "lesson1_kernel.cl");
std::string prog(
std::istreambuf_iterator<char>(file),
(std::istreambuf_iterator<char>()));
cl::Program::Sources source(
1,
std::make_pair(prog.c_str(), prog.length()+1));
cl::Program program(context, source);
err = program.build(devices,"");
checkErr(file.is_open() ? CL_SUCCESS : -1, "Program::build()");
cl::Kernel kernel(program, "hello", &err);
checkErr(err, "Kernel::Kernel()");
err = kernel.setArg(0, outCL);
checkErr(err, "Kernel::setArg()");
cl::CommandQueue queue(context, devices[0], 0, &err);
checkErr(err, "CommandQueue::CommandQueue()");
cl::Event event;
err = queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange(hw.length()+1),
cl::NDRange(1, 1),
NULL,
&event);
checkErr(err, "ComamndQueue::enqueueNDRangeKernel()");
event.wait();
err = queue.enqueueReadBuffer(
outCL,
CL_TRUE,
0,
hw.length()+1,
outH);
checkErr(err, "ComamndQueue::enqueueReadBuffer()");
std::cout << outH;
return EXIT_SUCCESS;
}
lesson1_kernel.cl
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
__constant char hw[] = "Hello World
";
__kernel void hello(__global char * out)
{
size_t tid = get_global_id(0);
out[tid] = hw[tid];
}
Using this makefile :
all : lesson1
lesson1 : lesson1.cpp
g++ lesson1.cpp -L/etc/alternatives -lOpenCL -o lesson1
clean : lesson1
rm lesson1
That give at the execution :
yannoo@Ubuntoo:~/OPENCL$ make
g++ lesson1.cpp -L/etc/alternatives -lOpenCL -o lesson1
In file included from /usr/include/CL/opencl.h:43:0,
from /usr/include/CL/cl.hpp:164,
from lesson1.cpp:6:
/usr/include/CL/cl_gl.h:107:2: attention : #warning CL_USE_DEPRECATED_OPENCL_1_1_APIS is defined. These APIs are unsupported and untested in OpenCL 1.2! [-Wcpp]
yannoo@Ubuntoo:~/OPENCL$ ./lesson1
*ERROR : cl::Platform::get (-1)
And a third code test :
#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
int main(void) {
// Get platform and device information
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret;
ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
printf("plateform_id=%x, ret_num_plareform=%d, ret=%d
", (int)platform_id, ret_num_platforms, ret);
ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);
printf("platform_id=%x, device_id=%x , ret_num_devices=%x, ret=%d
", (int)platform_id, (int)device_id, ret_num_devices, ret);
return(0);
}
That give at execution :
plateform_id=0, ret_num_plareform=0, ret=-1001
platform_id=0, device_id=0 , ret_num_devices=1, ret=-32
My graphic card is not compatible with OpenCL or it’s another thing ?
yannoo@Ubuntoo:~/OPENCL$ lspci | grep VGA
01:00.0 VGA compatible controller: NVIDIA Corporation NV44 [GeForce 7100 GS] (rev a1)