Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Getting started on MacOs X 10.6

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    6

    Getting started on MacOs X 10.6

    Hi alltogether,

    as I am writing to this board I am quite new to programming with OpenCL - I recently made some attempts to get some C++ sample Code from the tutorials compiled under MacOS X SnowLeopard but unfortunatetly none of them succeeded.

    The main problem is that the normal way to include the openCL framework headers which is described in the tutorials (use #include OpenCL/opencl.h) does not work on my system.
    The only place the header files are located is /System/Library/Frameworks/OpenCL.framework/Versions/A/Headers/ which is not in the search path assumed - I tried to fix that by switching
    to the commandline interface , using the -I and -L switches in combination with the "-framework opencl" option - but this did not work either. The error message always follows the pattern:
    firstCL.cpp: In function ‘int main()’:
    firstCL.cpp:28: error: ‘cl’ has not been declared
    firstCL.cpp:28: error: ‘cl’ has not been declared
    firstCL.cpp:28: error: ‘platformList’ was not declared in this scope
    firstCL.cpp:29: error: ‘Platform’ has not been declared
    firstCL.cpp:39: error: ‘cl’ has not been declared
    firstCL.cpp:39: error: expected `;' before ‘context’
    firstCL.cpp:46: error: ‘cl’ has not been declared
    firstCL.cpp:46: error: ‘cl’ has not been declared
    firstCL.cpp:46: error: ‘devices’ was not declared in this scope
    firstCL.cpp:47: error: ‘context’ was not declared in this scope

    what in my interpretation means that the cl:: namespace qualifier is not found in that context

    Did anybody run into the same problem and can give me some guidance how to proceed from here.
    Do I ,for example, have to install a certain package - c++ bindings ?
    the code I am using for testing follows below.

    Best regards + many thanks in advance
    Stefan


    #include <stdlib.h>
    #include <math.h>
    #include <OpenCL/opencl.h>

    int main (int argc, const char * argv[]) {

    // some code

    cl_device_id device_id; //declaring a device id gives no errors

    int err = clGetDeviceIDs(NULL, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);

    // insert code here...
    printf("Hello, World!\n");
    printf("Device ID is : %s", device_id);
    printf(" err is %d", err);

    return 0;
    }
    */

    #include <utility>

    #define __NO_STD_VECTOR // Use cl::vector instead of STL version
    #include <OpenCL/opencl.h>

    #include <cstdio>
    #include <cstdlib>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iterator>

    const std::string hw("Hello World\n");

    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;
    vector< cl::Platform > platformList;
    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 << "\n";
    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()");
    cl::vector<cl:evice> 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;

    }

    #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
    __constant char hw[] = "Hello World\n";
    __kernel void hello(__global char * out)
    {
    size_t tid = get_global_id(0);
    out[tid] = hw[tid];
    }

  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    3

    Re: Getting started on MacOs X 10.6

    The framework header file OpenCL/opencl.h includes the OpenCL 1.0 Khronos C language headers. The source code posted above appears to use C++ wrappers which are not part of the OpenCL framework in SnowLeopard.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    6

    Re: Getting started on MacOs X 10.6

    HI Board,

    after some months of absence I am returning to the topic - it its somewhat strange - that I could not find any tutorial on how to install OpenCL correctly on MacOS X 10.6 - I guess most of the board members who have it up and running have done something different or have a different (Developer) Version of MacOS - (My MacBook Pro runs on Mac OS X 10.6.6 (10J567) ) I am writing this because in contrary to what I am reading on the topic on my System (for example) the directory /System/Library/Frameworks/OpenCL.framework does not exist (and a search with "find" or the Finder does not return a positive result) - and that after I went trough the complete install process of the Developers DVD at least 2 times. And this (please correct me if I am wrong ) seems to be essential to make development with OpenCL possible.

    My concluding question is: what needs to be done to get a MacOs X 10.6 ready to compile OpenCL code - are there additional repositories of OpenCL.framework - which are not part of the Apple distribution - do other images / version of the Apple Developers DVD which are proven/known to come with OpenCL.framework . (If so please give me a link)

    Best regards + Many thanks in advance
    Stefan

  4. #4
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Getting started on MacOs X 10.6

    Back in the day all I did was dowload and install XCode. Everything worked out of the box.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    6

    Re: Getting started on MacOs X 10.6

    Ok. - Many Thanks David for your reply !,

    after my reinstall i finally got OpenCL installed on my box (That is the OpenCL C Examples from the Apple Developers Page run without problems) - the bad news is that I still have trouble to compile programs that use the C++ Bindings - What I have done up to now is that I downloaded the cl.hpp file from Khronos and copied into the directories /System/Library/Frameworks/OpenCl.framework/Headers and /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/OpenCL.framework/Versions/A/Headers/cl.hpp

    Sadly I found that this is not working as expected - I got "file not found" - (in Xcode)
    After trying this I also copied the cl.hpp file into the sources directories of my examples from various tutorials I found in the web - the result is that I got a clean compile on the command line - and a runtime error
    "clBuildProgram(-11)" while trying to run the binary - and in Xcode no parse errors but 24 linking errors - one for every OpenCL c++ binding used in the example code.

    looks like this:

    Ld build/Debug/OpenCLTest3 normal x86_64
    cd /Users/steve/Documents/OpenCLTest3
    setenv MACOSX_DEPLOYMENT_TARGET 10.6
    /Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -L/Users/steve/Documents/OpenCLTest3/build/Debug -F/Users/steve/Documents/OpenCLTest3/build/Debug -filelist /Users/steve/Documents/OpenCLTest3/build/OpenCLTest3.build/Debug/OpenCLTest3.build/Objects-normal/x86_64/OpenCLTest3.LinkFileList -mmacosx-version-min=10.6 -o /Users/steve/Documents/OpenCLTest3/build/Debug/OpenCLTest3

    Undefined symbols:
    "_clCreateKernel", referenced from:
    cl::Kernel::Kernel(cl::Program const&, char const*, int*)in OpenCLTest3.o
    "_clRetainMemObject", referenced from:
    cl::detail::ReferenceHandler<_cl_mem*>::retain(_cl _mem*)in OpenCLTest3.o
    "_clWaitForEvents", referenced from:
    cl::Event::wait() constin OpenCLTest3.o
    "_clReleaseKernel", referenced from:
    cl::detail::ReferenceHandler<_cl_kernel*>::release (_cl_kernel*)in OpenCLTest3.o
    "_clGetProgramBuildInfo", referenced from:
    int cl::Program::getBuildInfo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >(cl:evice const&, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*) constin OpenCLTest3.o
    "_clCreateCommandQueue", referenced from:
    cl::CommandQueue::CommandQueue(cl::Context const&, cl:evice const&, unsigned long long, int*)in OpenCLTest3.o
    "_clCreateBuffer", referenced from:
    cl::Buffer::Buffer(cl::Context const&, unsigned long long, unsigned long, void*, int*)in

    etc. etc.

    As I understand the issue ld does not find the library which is referenced by the cl.hpp wrapper - and I up to now have no idea which of the libs that come with OpenCL I should take - I already tried to give ld a hint by adding the option -L /System/Library/Frameworks/OpenCl.framework/Libraries to my commandline - but this did not change anything.

    Any more ideas ? - what's wrong with my build environment ?

    Thank you in advance
    Best regards
    Stefan

  6. #6
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: Getting started on MacOs X 10.6

    Did you add the OpenCL framework to your XCode project? That's all you should need to do. Try following a simple tutorial on XCode and OpenCL, such as this.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    6

    Re: Getting started on MacOs X 10.6

    Hi David,

    yes I did - that' how I have got my code examples - the presentation you gave me the link on lacks hints on my concrete problem - C++ is only referenced to in the sense that OpenCL is not designed to support true (memory) objects - but that is what the wrapper (C++ Bindings) is for - interfacing between C Objects and C++ Class Definitions. But the C++ Bindings and XCode are not subject of the presentation. As I told you in my previous post I followed the tutorials and copied the cl.hpp in the directories where the tutorials say they should be found - what makes it hard to find a resolution to the resulting errors is that all of the tutorials are written by guys that work for AMD or use the AMD Implementation or have a Mac what seems to make them assuming that everything is working right out of the box - and therefore aren't bothering any more the setup of Xcode etc. If you have an Idea what steps are necessary to make Xcode aware of the C++ Bindings please tell me
    - What puzzles me in that case is that OpenCL C works - and if I copy the cl.hpp header in the source directory becomes involved in the compiler-run but the linker fails to get the references straight - strange because it's a hpp and therefore no additional library is needed to link the implementation is already in the header file - it might be the case that Xcode takes the cl.hpp for a mere header and therefore does not compile binary code out of it - but I have switched to XCode for this project and up to now have not found any menu options that allow to modify the build environment like it is possible in eclipse.

    So Again - Is there a way to make Xcode aware of cl.hpp in the correct manner (compiling the implementation into the binary and linking correctly) -As I read somewhere without giving noticeto it too much is that there is a switch that enables(or disables) compiling .hpp files in Xcode - actually I cannot find this switch - but it might be a hint. (Any suggestions ?)


    Thanks in advance
    Best Regards
    Stefan

  8. #8
    Member
    Join Date
    Mar 2010
    Location
    Raleigh, NC
    Posts
    55

    Re: Getting started on MacOs X 10.6

    I don't have a mac, so I'm taking a stab here - but make sure that you are using the right OpenCL version (1.0 vs 1.1). If you are attempting to use the 1.1 .hpp file, and your OpenCL version on your mac is 1.0, would that cause problems?

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    6

    Re: Getting started on MacOs X 10.6

    Hi General,
    thanks for your post but as indicated on the Khronos API Registry page the header cl.hpp works for both - 1.0 and 1.1 -I made the test with diff - both links on the page point to the same file.

    Best regards
    Stefan

  10. #10
    Junior Member
    Join Date
    Jan 2011
    Posts
    29

    Re: Getting started on MacOs X 10.6

    The undefined symbol errors above suggest that the project doesn't include the OpenCL.framework. Here are the steps to get started with the C++ bindings in Xcode3:

    1.) Open XCode3
    2.) File -> New Project; select Application -> Command Line Tool; select Type: C++
    3.) Save the project 'Untitled' somewhere
    4.) Expand Targets; right click on 'Untitled' -> Add -> Existing Frameworks
    5.) Scroll down and select OpenCL.framework; click Add
    6.) Open Safari and navigate to cl.hpp on khronos.org; File -> Save As; place the file in the 'Untitled' project folder (same directory as main.cpp).
    7.) Right click on 'Untitled' project; -> Add -> Existing Files; Select 'cl.hpp' from the project folder.
    8.) Edit main.cpp. Add #include "cl.hpp". In the main() function, add some C++ bindings test code.
    9.) Build -> Build and Run.

    If you aren't using the optional C++ bindings then only steps 1-5 are needed (plus including <OpenCL/opencl.h> instead of "cl.hpp"). If you work outside of Xcode, e.g. using Makefiles, use the flag "-framework OpenCL" to link with the framework and include its headers.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 08-13-2010, 09:19 AM
  2. Replies: 3
    Last Post: 11-19-2009, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •