I can't get a basic opencl program to run

Hi,

I’m super new to opencl and an early intermediate at C++.

I’m trying to get just a simple opencl program to work so I can build from there.

I installed XCode 4 and cmake to my Mac (Os X), but my opencl code is still getting errors when I try to build it.

I tried the Hello World code from the book OpenCL Programming Guide By: Aaftab Munshi; Benedict R. Gaster; Timothy G. Mattson; James Fung; Dan Ginsburg

I get errors on all the opencl program terms like:
hello.cpp:16: error: ‘cl_context’ was not declared in this scope
hello.cpp:17: error: ‘cl_command_queue’ was not declared in this scope
hello.cpp:18: error: ‘cl_program’ was not declared in this scope
hello.cpp:19: error: ‘cl_device_id’ was not declared in this scope

Do I need to put an #include<opencl> or something like that?

Anyone know what I may have missed?

Can you give me a really simple code to start with?

Hi biokhar,

The basic includes for opencl are different for the mac versus the rest of the world. You will want to put something like the following at the beginning of your code:

#ifdef MAC
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif

Thank will include the right files for the mac and if you take your code to a PC.

Also The book you are using is very good(I use it all the time) but they only have sample code for four of the chapters. The other two opencl books have code from the very beginning which may be very helpful to you. It is not to say they are better all three of the books provide great information and I have all three open all the time. While I recommend buying all the books you can get the source for free from the authors websites at:

OpenCL in Action: The author really good simple programs that should get you started asap
Also he has his intro section available for free download which may help you get started. Also note he has code for the MAC, Linux and windows as separate downloads http://www.manning.com/scarpino2/

OpenCL Programming Guide:A bit more reference but absolutely wonderful as well. The source code is in a google repository at: http://code.google.com/p/opencl-book-samples/
Using the downloaded make file it should setup everything you need to get started including referencing OpenCL.

Again I want to stress that all three books are great and I use them all but I think the never book, OpenCL in Action takes a softer approach to OpenCL. The book you are looking at is great for getting up and running quickly and the case studies are really great but if you are having trouble another references is great. For now look at the source code and decide for yourself.

Thanks for the help.

I added the Mac specific stuff and a file named Opencl.h, then built it again and it said

error: Cl/cl.h: No such file or directory
error: ‘cl_context’ was not declared in this context and other opencl specific errors

so I removed

#else
#include <CL/cl.h>

and still got

error: ‘cl_context’ was not declared in this context and other opencl specific errors

Any suggestions?

biokhar, it is generally recommended to be proficient in C before attempting to use OpenCL.

I can only add here that I also program OpenCL on Apple (MacBook Pro / nVidia and Mac Pro / AMD), and I don’t think I’ve ever had to #include anything at all . . . I look now in my project, and I see that i do have OpenCL.framework under the “Frameworks->Linked Frameworks” tab. That’s all. I don’t know if this observation can help, but if it does, then cool!

On Mac the include directive should be:
#include <OpenCL/opencl.h>

This follows the general form Framework/framework.h.

To link the executable, the OpenCL.framework must be added to the target in an xcodeproject (or otherwise added to the linker command line).

Note that the filesystem might be case sensitive.

OpenCL in Action: The author really good simple programs that should get you started asap

Hello, i’ve installed OpenCL SDK 1.5 and AMD APP SDK under Windows7 and
try to compile VC2010 projects from the book samples, on my VC2010 Express Edition.
I didn’t tuch to the project properties and linkage seems ok
But after compilling troubles come from access violation :

‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\setupapi.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\cfgmgr32.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\oleaut32.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\ole32.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\devobj.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\dwmapi.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\imm32.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\msctf.dll’, Symbols loaded (source information stripped).
‘Ch1_matvec.exe’: Loaded ‘C:\Program Files (x86)\Intel\OpenCL SDK\1.1\bin\x86\clang_compiler.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Program Files (x86)\Intel\OpenCL SDK\1.1\bin\x86 ask_executor.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Program Files (x86)\Intel\OpenCL SDK\1.1\bin\x86 bb.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\dbghelp.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Program Files (x86)\Intel\OpenCL SDK\1.1\bin\x86 bbmalloc.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Program Files (x86)\Intel\OpenCL SDK\1.1\bin\x86\cpu_device.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\amdocl.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\aticaldd.dll’, Cannot find or open the PDB file
‘Ch1_matvec.exe’: Loaded ‘C:\Windows\SysWOW64\atigktxx.dll’, Cannot find or open the PDB file
First-chance exception at 0x685d13be in Ch1_matvec.exe: 0xC0000005: Access violation reading location 0xcccccccc.
Unhandled exception at 0x685d13be in Ch1_matvec.exe: 0xC0000005: Access violation reading location 0xcccccccc.

The missing the PDB file isn’t a problem but for the access violation i don’t know what to do or to search :shock:

Could u help please ?

Ok, forget my request for another forum rather… :wink:

I’ve had some strange issues with the AMD APP SDK on my Windows 7 64-bit machine. OpenCL would throw an unhandled exception when it was installed. There were a few DLLs that I had to rename (cant remember them right now) that didn’t get cleaned up from pre-2.5 version AMD APP SDK.