OpenCL header file not found - opencl

So please bear with me if the question is very trivial.
I wanted to install Intel implementation of OpenCL to code in integrated intel GPU.
I installed the OpenCL driver using Beignet. It successfully installed and clinfo shows the number of platforms and other details.
I have the following code,
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#include <CL\cl.hpp>
#include<iostream>
int main() {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
std::cout << "Total platforms including cpu: " << platforms.size() << std::endl;
if (platforms.size() == 0) {
std::cout << " No platforms found. Check OpenCL installation!\n";
exit(1);
}
for (int j = 0; j < platforms.size(); j++) {
auto p = platforms[j];//Change platform from 0,1 and 2
std::vector <cl::Device> devices;
p.getDevices(CL_DEVICE_TYPE_ALL, &devices);
for (int i = 0; i < devices.size(); i++) {
auto device = devices[i];
auto vendor = device.getInfo<CL_DEVICE_VENDOR>();
std::cout << vendor << std::endl;
auto version = device.getInfo<CL_DEVICE_VERSION>();
}
std::cout << "----------------------\n";
}
}
When I compile using $g++ -o test test.cpp -lOpenCL it throws the following error
fatal error: CL\cl.hpp: No such file or directory
#include <CL\cl.hpp>
I linked the library as follows,
sudo ln -s /usr/lib/x86_64-linux-gnu/libOpenCL.so /usr/local/lib/libOpenCl.so
I'm not sure how to proceed further. Please help

Related

SIGTSTP in codechef ide

I've written a code to solve Buying new tablet problem from codechef. Although everything goes well in CLION and Visual Studio, even outputs are correct, codechef compiler says runtime error SIGTSTP. I'm new to this site, can not understand what it says. I've done a little research on Google, but nothing.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int T,N,B,W,H,P;
bool isGr = false;
cin >> T;
while (T--){
cin >> N;
cin >> B;
vector <int> tablets;
while (N--){
cin >> W;
cin >> H;
cin >> P;
if (P <= B) {
tablets.push_back(W*H);
isGr = true;
}
else isGr = false;
}
if (isGr){
sort(tablets.begin(),tablets.end());
cout << tablets.back() << endl;
tablets = {};
} else cout << "no tablet" << endl;
isGr = false;
}
}
The condition
'''else isGr=false;'''
in second while loop, is not required....

Problems with OpenCL helloworld

i´m new to OpenCl and i´m trying to learn it right know.
I installed Intel® SDK for OpenCL™ Applications and now i´m trying to use it with visual studio 2015.
When i´m trying to run an hello world example it returns an error at the "program.build" -part in the following codepart.
Can somebody tell me what i´m missing?
Thanks :)
The consol returns this:
Using platform: Intel<R> OpenCL
Using device: Intel<R> Core<TM> i7-3770 CPU e 3.40 GHz
-44
Error building:
Examplecode:
//get all platforms (drivers)
std::vector<cl::Platform> all_platforms;
cl::Platform::get(&all_platforms);
if (all_platforms.size() == 0) {
std::cout << " No platforms found. Check OpenCL installation!\n";
exit(1);
}
cl::Platform default_platform = all_platforms[0];
std::cout << "Using platform: " << default_platform.getInfo<CL_PLATFORM_NAME>() << "\n";
//get default device of the default platform
std::vector<cl::Device> all_devices;
default_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices);
if (all_devices.size() == 0) {
std::cout << " No devices found. Check OpenCL installation!\n";
exit(1);
}
cl::Device default_device = all_devices[0];
std::cout << "Using device: " << default_device.getInfo<CL_DEVICE_NAME>() << "\n";
cl::Context context({ default_device });
cl::Program::Sources sources;
// kernel calculates for each element C=A+B
std::string kernel_code =
" void kernel simple_add(global const int* A, global const int* B, global int* C){ "
" C[get_global_id(0)]=A[get_global_id(0)]+B[get_global_id(0)]; "
" } ";
sources.push_back({ kernel_code.c_str(),kernel_code.length() });
cl::Program program(context, sources);
if (**program.build({ default_device }) != CL_SUCCESS**) {
std::cout << program.build({ default_device }) <<"\n";
std::cout << " Error building: " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(default_device) << "\n";
exit(1);
}
Error code -44 means that "the program object is invalid". However this is inconclusive and hints to some other issue that corrupts memory. Also see this question.
Calling program.build() twice is - although not good practice - not the issue here, but maybe some threading issues? The problem certainly is not in the code snipped you provided.

Unable to read shared memory data using boost created by Qt

I've created a Qt shared memory program to write a string into shared memory. Now After writing, I need to read it from Boost program. I tried using simple programs, but I couldn't read the string using Boost interprocess.
Here is the Qt code that is writing into the shared memory. And I'm double checking if the string is written by reading from the shared memory from the same program.
void CDialog::loadString()
{
if(sharedMemory.isAttached())
{
if(!sharedMemory.detach())
{
lbl->setText("Unable to detach from Shared Memory");
return;
}
}
lbl->setText("Click on Top Button");
char sString[] = "my string";
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
out << sString;
int size = buffer.size();
qDebug() << size;
if(!sharedMemory.create(size))
{
lbl->setText("Unable to create shared memory segment");
qDebug() << lbl->text();
}
sharedMemory.lock();
char *to = (char *) sharedMemory.data();
const char *from = buffer.data();
memcpy(to, from, qMin(sharedMemory.size(), size));
sharedMemory.unlock();
char * str;
QDataStream in(&buffer);
sharedMemory.lock();
buffer.setData((char *)sharedMemory.constData(), sharedMemory.size());
buffer.open(QBuffer::ReadOnly);
in >> str;
sharedMemory.unlock();
qDebug() << str;
}
And I'm reading it from boost using the same key which I've provided in the Qt program.
Below is the Boost program code -
int main()
{
boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_only, "Highscore", boost::interprocess::read_only);
boost::interprocess::offset_t size;
if (shdmem.get_size(size))
std::cout << "Shared Mem Size: " << size << std::endl;
boost::interprocess::mapped_region region2(shdmem, boost::interprocess::read_only);
char *i2 = static_cast<char *>(region2.get_address());
std::cout << i2 << std::endl;
return 0;
}
Kindly help me in reading the shared memory data from Boost program.
Thank you.
From the Qt docs:
Warning: QSharedMemory changes the key in a Qt-specific way. It is therefore currently not possible to use the shared memory of non-Qt applications with QSharedMemory.
You will probably need to use Boost on both sides.

OpenCL 1.2 C++ Wrapper - undefined reference to clReleaseDevice

I am trying to use the OpenCL C++ wrapper API for the following program :
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#include <cstdio>
#include <cstdlib>
#include <iostream>
const char helloStr [] = "__kernel void "
"hello(void) "
"{ "
" "
"} ";
int
main(void)
{
cl_int err = CL_SUCCESS;
try {
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if (platforms.size() == 0) {
std::cout << "Platform size 0\n";
return -1;
}
cl_context_properties properties[] =
{ CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
cl::Context context(CL_DEVICE_TYPE_CPU, properties);
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
cl::Program::Sources source(1,
std::make_pair(helloStr,strlen(helloStr)));
cl::Program program_ = cl::Program(context, source);
program_.build(devices);
cl::Kernel kernel(program_, "hello", &err);
cl::Event event;
cl::CommandQueue queue(context, devices[0], 0, &err);
queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange(4,4),
cl::NullRange,
NULL,
&event);
event.wait();
}
catch (cl::Error err) {
std::cerr
<< "ERROR: "
<< err.what()
<< "("
<< err.err()
<< ")"
<< std::endl;
}
return EXIT_SUCCESS;
}
I use the same kernel file from that blog post, anyways that is not the issue since I can't get past compilation.
I'm compiling the program with the following command :
g++ example.cpp -o example -l OpenCL
and I get the following error message :
/tmp/ccbUf7dB.o: In function `cl::detail::ReferenceHandler<_cl_device_id*>::release(_cl_device_id*)':
example.cpp:(.text._ZN2cl6detail16ReferenceHandlerIP13_cl_device_idE7releaseES3_[_ZN2cl6detail16ReferenceHandlerIP13_cl_device_idE7releaseES3_]+0x14): undefined reference to `clReleaseDevice'
collect2: error: ld returned 1 exit status
I've read stuff about clReleaseDevice not working for legacy devices (see for example this question), but my graphics card is pretty recent (NVidia GTX 660 Ti, supports OpenCL 1.2). Where can I go from there?
I am running this on Ubuntu 13.04 x64 with nvidia-opencl-dev and opencl-headers installed from ubuntu repositories.
The root cause
The problem is that the OpenCL library that you are linking against does not support OpenCL 1.2. Speaking generically, until an OpenCL implementation supporting the version you want to use becomes available for a specific platform, you will have this problem when linking against the OpenCL shared library provided with it. There are two solutions:
Download the version of cl.hpp from Khronos that matches the OpenCL version provided by your chosen hardware and continue using the library provided by your device's manufacturer.
Link against an OpenCL shared library that implements the latest OpenCL standard, but write multiple code paths - one for each OpenCL version, ensuring that each code path only uses OpenCL functions that are supported by that version. This route is harder and I have no idea how you can do it with the C++ wrapper. If you try to call an OpenCL 1.2 function on a platform that doesn't support OpenCL 1.2 you will get a segfault, that's why different code paths are needed.
Nvidia specific
Nvidia has been very slow in delivering OpenCL 1.2 support. As a result, their OpenCL library did not provide the OpenCL 1.2 functions the linker was looking for, resulting in the errors.
At the end of May 2015 Nvidia released drivers that support OpenCL 1.2, see the comments by Z Boson below. Updating your drivers should resolve the linker error. GeForce GTX 6xx and later cards (except for rebrands of earlier generations) support OpenCL 1.2. You can check the conformant products list on the Khronos OpenCL site to make sure. The GTX 660 Ti is listed so you're in luck.
Yeah. I have never seen OpenCL 1.2 on Nvidia devices. Compile this on your system, and look at the "OpenCL C Version":
#include <iostream>
#include <vector>
#include <CL/cl.hpp>
int main() {
// Get the platforms
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
// Loop over the number of platforms
for ( size_t i = 0; i < platforms.size(); ++i ) {
// Display the platform information
std::cout << "Platform " << i+1 << ": "
<< platforms[i].getInfo<CL_PLATFORM_NAME>()
<< "\n----------------------------------------------"
<< "\nVendor : " << platforms[i].getInfo<CL_PLATFORM_VENDOR>()
<< "\nVersion : " << platforms[i].getInfo<CL_PLATFORM_VERSION>();
// Get the devices on the current platform
std::vector <cl::Device> devices;
platforms[i].getDevices( CL_DEVICE_TYPE_ALL , & devices);
// Loop over the devices
std::cout << "\n----------------------------------------------\n";
for ( size_t j = 0; j < devices.size(); ++j ) {
// Display the device information
std::cout
<< "\n Device " << j+1 << ": "
<< devices[j].getInfo< CL_DEVICE_NAME >()
<< "\n\t Device Version : "
<< devices[j].getInfo< CL_DEVICE_VERSION >()
<< "\n\t OpenCL C Version : "
<< devices[j].getInfo< CL_DEVICE_OPENCL_C_VERSION >()
<< "\n\t Compute Units : "
<< devices[j].getInfo< CL_DEVICE_MAX_COMPUTE_UNITS >()
<< "\n\t Max Work Group Size: "
<< devices[j].getInfo< CL_DEVICE_MAX_WORK_GROUP_SIZE >()
<< "\n\t Clock Frequency : "
<< devices[j].getInfo< CL_DEVICE_MAX_CLOCK_FREQUENCY >()
<< "\n\t Local Memory Size : "
<< devices[j].getInfo< CL_DEVICE_LOCAL_MEM_SIZE >()
<< "\n\t Global Memory Size : "
<< devices[j].getInfo< CL_DEVICE_GLOBAL_MEM_SIZE >();
// Check if the device supports double precision
std::string str = devices[j].getInfo<CL_DEVICE_EXTENSIONS>();
size_t found = str.find("cl_khr_fp64");
std::cout << "\n\t Double Precision : ";
if ( found != std::string::npos ){ std::cout << "yes\n"; }
else { std::cout << "no\n"; }
}
std::cout << "\n----------------------------------------------\n";
}
// std::cin.ignore();
return 0;
}

Undefined reference to boost::thread in Qt

I'm trying to create a boost::thread application in Qt.
Here is my code:
#include <iostream>
#include "boost/thread.hpp"
#include "boost/bind.hpp"
using namespace std;
class A {
public:
void tf() {
for (int i = 0; i < 100; ++i) {
cout << i << endl;
}
}
};
int main()
{
boost::shared_ptr<A> aPtr;
cout << "Hello World!" << endl;
boost::thread t = boost::thread(boost::bind(&A::tf, aPtr.get()));
cout << "Thread started" << endl;
return 0;
}
And the corresponding .pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= qt
LIBS += -L"C:/Program Files (x86)/boost/boost_1_49/lib"
DEPENDPATH += "C:/Program Files (x86)/boost/boost_1_49"
INCLUDEPATH += "C:/Program Files (x86)/boost/boost_1_49"
SOURCES += main.cpp
When i try to compile it i get:
{{path}}\main.cpp:21: error: undefined reference to `_imp___ZN5boost6threadD1Ev'
{{path}}\main.o:-1: In function `ZN5boost6threadC1INS_3_bi6bind_tIvNS_4_mfi3mf0Iv1AEENS2_5list1INS2_5valueIPS6_EEEEEEEET_NS_10disable_ifINS_14is_convertibleIRSE_NS_6detail13thread_move_tISE_EEEEPNS0_5dummyEE4typeE':
c:\Program Files (x86)\boost\boost_1_49\boost\thread\detail\thread.hpp:205: error: undefined reference to `_imp___ZN5boost6thread12start_threadEv'
collect2.exe:-1: error: error: ld returned 1 exit status
Whats the problem?
What am i missing?
M.
You aren't linking to the Boost Thread library, you are just telling Qt where it is.
LIBS += -L"C:/Program Files (x86)/boost/boost_1_49/lib" -lboost_thread

Resources