3 Replies Latest reply on Apr 3, 2014 9:16 AM by mdinger

    Hello world does not compile

    mdinger

      Hello,

       

      I am following this "Hello world" tutorial and it won't compile.  It is returning an unknown pragma warning on this command which seems to make the compile fail:

       

      #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable

       

      I am compiling through the "Developer Command Prompt for VS2013" using this command and I have copied the OpenCL.lib file into my local folder.

       

      cl /Fe"hello_world.exe" /I"C:\Program Files (x86)\AMD APP SDK\2

      .9\include" lesson.cpp lib/OpenCL.lib


      I have copied the file I'm using verbatim below in case I misunderstood something from the tutorial.  I have the complete APP SDK 2.9, an AMD A10 APU, and recent drivers.  I don't know why it's having issues unless the extension is no longer supported.  This is the closest I've seen to a list of supported extensions.  I'm not sure what the problem is.  I've included the complete compiler output at the bottom.


      Thank you if anyone can show me why it isn't working.


       

      lesson.cpp:


      """

      #include <utility>

      #define __NO_STD_VECTOR // Use cl::vector instead of STL version

      #include <CL/cl.hpp>

      #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;

          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 << "\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()");

       

          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(err, "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];

      }

      """



      Complete compiler output:

      """

      D:\Programs\openCL>cl /Fe"hello_world.exe" /I"C:\Program Files (x86)\AMD APP SDK

      \2.9\include" lesson.cpp lib/OpenCL.lib

      Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86

      Copyright (C) Microsoft Corporation.  All rights reserved.

       

      lesson.cpp

      C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\xlocale(337) : wa

      rning C4530: C++ exception handler used, but unwind semantics are not enabled. S

      pecify /EHsc

      lesson.cpp(81) : warning C4068: unknown pragma

      lesson.cpp(82) : error C2144: syntax error : 'char' should be preceded by ';'

      lesson.cpp(82) : error C4430: missing type specifier - int assumed. Note: C++ do

      es not support default-int

      lesson.cpp(82) : error C2373: 'hw' : redefinition; different type modifiers

              lesson.cpp(11) : see declaration of 'hw'

      lesson.cpp(83) : error C2144: syntax error : 'void' should be preceded by ';'

      lesson.cpp(83) : error C4430: missing type specifier - int assumed. Note: C++ do

      es not support default-int

      lesson.cpp(83) : error C2065: '__global' : undeclared identifier

      lesson.cpp(83) : error C2144: syntax error : 'char' should be preceded by ')'

      lesson.cpp(83) : error C2448: 'hello' : function-style initializer appears to be

      a function definition

      lesson.cpp(83) : error C2059: syntax error : ')'

      lesson.cpp(85) : error C2144: syntax error : 'size_t' should be preceded by '}'

      lesson.cpp(85) : error C2144: syntax error : 'size_t' should be preceded by ';'

      lesson.cpp(85) : error C3861: 'get_global_id': identifier not found

      lesson.cpp(86) : error C2057: expected constant expression

      lesson.cpp(86) : error C2466: cannot allocate an array of constant size 0

      lesson.cpp(86) : error C4430: missing type specifier - int assumed. Note: C++ do

      es not support default-int

      lesson.cpp(86) : error C2372: 'out' : redefinition; different types of indirecti

      on

              lesson.cpp(83) : see declaration of 'out'

      lesson.cpp(86) : error C2088: '[' : illegal for class

      lesson.cpp(87) : error C2059: syntax error : '}'

      lesson.cpp(87) : error C2143: syntax error : missing ';' before '}'

      """



       

        • Re: Hello world does not compile
          nou

          you must put kernel source to lesson1_kernels.cl and not directly to lesson.cpp

          in this lines it open this file and read it to string.

          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>()));

          1 of 1 people found this helpful
          • Re: Hello world does not compile
            amd_support

            Hi,

             

            Your program failed because you have written code kernel code in .cpp file. As per your host code, keep this kernel code into a separate "lesson1_kernel.cl" file and re-compile it again.

             

            To explain, OpenCL program is written in 2 separate parts.

            1. Host code: which can be written either in C, C++ or Java by using its corresponding binding.

            2. Device code: it is main kernel code which is executed on OpenCL devices (say for example GPU). It is written in OpenCL C language. It can be either in a separate .cl file or in host file. When writing kernel code in the host file, it must be written in a string of type char pointer.

             

            Change your code and update back to us if you still have some issue.

             

            Regards,

            AMD_Support

              • Re: Hello world does not compile
                mdinger

                Thank you.  That solved it.  I also found nou's answer helpful but only after I read your response for context.

                 

                I don't know if that tutorial page is modifiable but I think it would be improved if:

                1. It provided the 2 files: lesson.cpp and lesson1_kernel.cl as downloads because I obviously didn't noticed there should be 2 files.

                2. Write the compiling commands in some monospace font.  I'm not familiar with cl flags and /I (i) looks like /l (L)

                3. Add quotes around the variable in the commands where possible (/Fe"hello_world.exe")

                4. Mention "Developer Command Prompt for VS2013" in addition to VS "Command Window".  VS 2013 Express doesn't seem to have a command window.  I don't know why.

                 

                These changes probably would have saved me a lot of time.