11 Replies Latest reply on Jun 23, 2010 4:42 AM by sine

    Hello World program

    sine
      Hello World with the SDK 2.1 on Linux

      Hello,

      The HelloCL from sample files works correctly, and I tested successfully a few other with --device cpu.

      Following the tutorial for the latest SDK ( http://developer.amd.com/GPU/ATISTREAMSDK/pages/TutorialOpenCL.aspx ). I tried to compile the Hello World program. I get the following error:

      $ g++ -o hello_world -I/usr/local/ati-stream-sdk-v2.1-lnx32/include/ -L/usr/local/ati-stream-sdk-v2.1-lnx32/lib/ test.cpp -lOpenCL
      test.cpp: In function ‘int main()’:
      test.cpp:35: error: no matching function for call to ‘cl:latform::getInfo(int, std::string*)’
      /usr/local/ati-stream-sdk-v2.1-lnx32/include/CL/cl.hpp:1388: note: candidates are: cl_int cl:latform::getInfo(cl_platform_info, cl::STRING_CLASS*) const
      test.cpp: At global scope:
      test.cpp:112: error: expected constructor, destructor, or type conversion before ‘char’
      test.cpp:113: error: expected constructor, destructor, or type conversion before ‘void’
      $

       

      The details of the platform are as follows:

      $ uname -a
      Linux office 2.6.31.13-desktop586-1mnb #1 SMP Tue Apr 27 20:38:56 EDT 2010 i686 Intel(R) Core(TM)2 CPU          4400  @ 2.00GHz GNU/Linux
      $  gcc --version
      gcc (GCC) 4.4.1

       

      SDK is installed in /usr/local/ati-stream-sdk-v2.1-lnx32

      LD_LIBRARY_PATH includes /usr/local/ati-stream-sdk-v2.1-lnx32/lib/x86

      Could anyone please correct the code or any other mistake please?

      TIA,

      sine

       

       

      #include <utility> #define __NO_STD_VECTOR // Use cl::vector and cl::string and #define __NO_STD_STRING // not STL versions, more on this later #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_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(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]; }

        • Hello World program
          nou

          you define NO_STD_STRING so you you must use cl::string and not std::string

          std::string platformVendor;

            • Hello World program
              sine

              Changed all the std::string to cl::string. But it doesn't work.

               

              hello1.cpp:13: warning: deprecated conversion from string constant to ‘char*’ hello1.cpp: In function ‘int main()’: hello1.cpp:36: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cerr)), ((const char*)"Platform is by: ")) << platformVendor’ /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i586-manbo-linux-gnu/4.4.1/../../../../include/c++/4.4.1/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>] hello1.cpp:67: error: no matching function for call to ‘cl::string::string(std::istreambuf_iterator<char, std::char_traits<char> >, std::istreambuf_iterator<char, std::char_traits<char> >)’ /usr/local/ati-stream-sdk-v2.1-lnx32/include/CL/cl.hpp:519: note: candidates are: cl::string::string(const cl::string&) /usr/local/ati-stream-sdk-v2.1-lnx32/include/CL/cl.hpp:482: note: cl::string::string(char*) /usr/local/ati-stream-sdk-v2.1-lnx32/include/CL/cl.hpp:468: note: cl::string::string(char*, size_t) /usr/local/ati-stream-sdk-v2.1-lnx32/include/CL/cl.hpp:464: note: cl::string::string() hello1.cpp: At global scope: hello1.cpp:112: error: expected constructor, destructor, or type conversion before ‘char’ hello1.cpp:113: error: expected constructor, destructor, or type conversion before ‘void’

                • Hello World program
                  sine

                  I tried another 'Hello World' program. Error with that too. Though, encouragingly, it is smaller. It was taken from:

                  http://forums.amd.com/forum/messageview.cfm?catid=390&threadid=125792&enterthread=y

                   

                  The code and the errors are attached.

                  Help please!

                  Thanking in advance,

                  -Sine

                   

                  <code> #include <iostream> #include <fstream> #include <cstdlib> #include <CL/cl.h> #include <string> #include <ctime> #include <vector> void errcheck(int error,std::string error_code){ if(error!=CL_SUCCESS){ std::cout<<"Error: "<<error_code<<"("<<error<<")"<<std::endl; exit(-1); } } int main(int argc,char** argv) { cl_int error=-1; //Platform info cl_uint numPlatforms; cl_platform_id platform = NULL; clGetPlatformIDs(0, NULL, &numPlatforms); if (0 < numPlatforms){ cl_platform_id* platforms = new cl_platform_id[numPlatforms]; clGetPlatformIDs(numPlatforms, platforms, NULL); for (unsigned i = 0; i < numPlatforms; ++i){ char pbuf[100]; clGetPlatformInfo(platforms[i],CL_PLATFORM_VENDOR,sizeof(pbuf),pbuf,NULL); platform = platforms[i]; // if (!strcmp(pbuf, "Advanced Micro Devices, Inc."))break; } delete[] platforms; } cl_context_properties cps[3] ={CL_CONTEXT_PLATFORM,(cl_context_properties)platform,0}; cl_context_properties* cprops =(platform==NULL) ? NULL : cps; //create Context cl_context hContext; hContext=clCreateContextFromType(cprops,CL_DEVICE_TYPE_GPU,0,0,&error);errcheck(error,"clCreateContextFromType"); //query all OpenCL device & context size_t nContextDescriptorSize; clGetContextInfo(hContext,CL_CONTEXT_DEVICES,0,0,&nContextDescriptorSize); cl_device_id *aDevices =(cl_device_id*)malloc(nContextDescriptorSize); clGetContextInfo(hContext,CL_CONTEXT_DEVICES,nContextDescriptorSize,aDevices,0); //create command queue cl_command_queue hCmdQueue; hCmdQueue=clCreateCommandQueue(hContext,aDevices[0],0,0); //read program std::ifstream file("OpenCL_Test_1.cl"); std::string prog(std::istreambuf_iterator<char>(file),(std::istreambuf_iterator<char>())); const char *sProgramSource=prog.c_str(); //create & compile program cl_program hProgram; hProgram =clCreateProgramWithSource(hContext,1,(const char **)&sProgramSource,0,&error); errcheck(error,"clCreateProgramWithSource"); error=clBuildProgram(hProgram,0,0,0,0,0); errcheck(error,"clBuildProgram"); //create Kernel cl_kernel hKernel; hKernel=clCreateKernel(hProgram,"Vec_ADD",0); //allocate Hostmemory cl_int wsize=10; //error=clGetDeviceInfo(aDevices[0],CL_DEVICE_MAX_WORK_GROUP_SIZE,sizeof(int),(void*)&wsize,NULL); errcheck(error,"clGetDeviceInfo"); size_t worksize[]={wsize,1,1}; cl_int *a=new cl_int[wsize]; cl_int *b=new cl_int[wsize]; cl_int *c=new cl_int[wsize]; //set Hostmemory for(int i=0;i<wsize;i++){ a[i]=i; b[i]=wsize-i; } //allocate Devicememory cl_mem CL1,CL2,CL3; CL1=clCreateBuffer(hContext,CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR,sizeof(cl_int)*wsize,a,0); CL2=clCreateBuffer(hContext,CL_MEM_READ_ONLY |CL_MEM_COPY_HOST_PTR,sizeof(cl_int)*wsize,b,0); CL3=clCreateBuffer(hContext,CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR,sizeof(cl_int)*wsize,c,0); //setKernelarg error=clSetKernelArg(hKernel,0,sizeof(cl_mem),(void *)&CL1);errcheck(error,"clSetKernelArg_1"); error=clSetKernelArg(hKernel,1,sizeof(cl_mem),(void *)&CL2);errcheck(error,"clSetKernelArg_2"); error=clSetKernelArg(hKernel,2,sizeof(cl_mem),(void *)&CL3);errcheck(error,"clSetKernelArg_3"); //run Kernel clock_t time=clock(); clFinish(hCmdQueue); error=clEnqueueNDRangeKernel(hCmdQueue, hKernel,1,0,worksize,0,0,0,0); errcheck(error,"clEnqueueNDRangeKernel"); //copy mem back error=clEnqueueReadBuffer(hCmdQueue,CL3,CL_TRUE,0,sizeof(cl_int)*wsize,c,0,0,0); errcheck(error,"clEnqueueReadBuffer"); clFinish(hCmdQueue); time=clock()-time; std::cout<<std::endl<<"Ausgabe:"<<std::endl; for(int i=0;i<wsize;i++){ std::cout<<c[i]<<" "; } std::cout<<std::endl<<std::endl; std::cout<<std::endl<<"Zeit: "<<time<<"ms"<<std::endl<<std::endl; delete [] a; delete [] b; delete [] c; clReleaseMemObject(CL1); clReleaseMemObject(CL2); clReleaseMemObject(CL3); return 0; } //Kernel: __kernel void Vec_ADD(__global const int *a,__global const int *b,__global int *c){ int gid = get_global_id(0); c[gid]=a[gid]+b[gid]; } </code> <error> $ gcc -o hello_world -I/usr/local/ati-stream-sdk-v2.1-lnx32/include/ -L/usr/local/ati-stream-sdk-v2.1-lnx32/lib hello2.cpp -lOpenCL hello2.cpp:113: error: expected constructor, destructor, or type conversion before ‘void’ </error>

                    • Hello World program
                      omkaranathan

                       

                      I tried another 'Hello World' program. Error with that too. Though, encouragingly, it is smaller. It was taken from:

                       

                       



                      Did you copy paste the whole code to your .cpp file? Or created .cl file as expected in the given example?

                        • Hello World program
                          sine

                           

                          Originally posted by: omkaranathan
                          I tried another 'Hello World' program. Error with that too. Though, encouragingly, it is smaller. It was taken from:

                           

                           

                           

                           

                           

                          Did you copy paste the whole code to your .cpp file? Or created .cl file as expected in the given example?

                           



                          Yeah, the .cl was missing. But I moved the // Kernel and below part of that code into the correct cl file. (OpenCL_Test_1.cl) It compiles now, and shows that numPlatforms is '1', but dies with the following error:

                          "Error: clCreateContextFromType(-1)"

                          What could be going wrong here?

                          TIA,

                          Sine

                            • Hello World program
                              omkaranathan

                              Which GPU are you using? Did you install the latest drivers? Please post the output of CLInfo sample.

                                • Hello World program
                                  sine

                                  ,

                                  Originally posted by: omkaranathan Which GPU are you using? Did you install the latest drivers? Please post the output of CLInfo sample.

                                   

                                  Pardon my ignorence I am struggling to compile and run my first opencl applications. I have the latest SDK. The CLInfo is attached. I have no GPU but I think it should work as I have x86 SDK. Shouldn't it?

                                  Thanks again,

                                  Sine

                                  Number of platforms: 1 Platform Profile: FULL_PROFILE Platform Version: OpenCL 1.0 ATI-Stream-v2.1 (145) Platform Name: ATI Stream Platform Vendor: Advanced Micro Devices, Inc. Platform Extensions: cl_khr_icd Platform Name: ATI Stream Number of devices: 1 Device Type: CL_DEVICE_TYPE_CPU Device ID: 4098 Max compute units: 2 Max work items dimensions: 3 Max work items[0]: 1024 Max work items[1]: 1024 Max work items[2]: 1024 Max work group size: 1024 Preferred vector width char: 16 Preferred vector width short: 8 Preferred vector width int: 4 Preferred vector width long: 2 Preferred vector width float: 4 Preferred vector width double: 0 Max clock frequency: 2000Mhz Address bits: 32 Max memory allocation: 536870912 Image support: No Max size of kernel argument: 4096 Alignment (bits) of base address: 1024 Minimum alignment (bytes) for any datatype: 128 Single precision floating point capability Denorms: Yes Quiet NaNs: Yes Round to nearest even: Yes Round to zero: No Round to +ve and infinity: No IEEE754-2008 fused multiply-add: No Cache type: Read/Write Cache line size: 0 Cache size: 0 Global memory size: 1073741824 Constant buffer size: 65536 Max number of constant args: 8 Local memory type: Global Local memory size: 32768 Profiling timer resolution: 1 Device endianess: Little Available: Yes Compiler available: Yes Execution capabilities: Execute OpenCL kernels: Yes Execute native function: No Queue properties: Out-of-Order: No Profiling : Yes Platform ID: 0x40f47304 Name: Intel(R) Core(TM)2 CPU 4400 @ 2.00GHz Vendor: GenuineIntel Driver version: 1.1 Profile: FULL_PROFILE Version: OpenCL 1.0 ATI-Stream-v2.1 (145) Extensions: cl_khr_icd cl_amd_fp64 cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_byte_addressable_store cl_khr_gl_sharing cl_ext_device_fission cl_amd_device_attribute_query cl_amd_printf Passed!

                          • Hello World program
                            omkaranathan

                             

                            Modify the line 

                            platformList[0].getInfo(CL_PLATFORM_VENDOR, &platformVendor);

                            to 

                            platformList[0].getInfo((cl_platform_info)CL_PLATFORM_VENDOR, &platformVendor);

                             

                            Also remove the line #define __NO_STD_STRING added above and use std::string

                             

                              • Hello World program
                                sine

                                Thanks omkaranathan. Removing the #define from the original example worked for me. Also there was a typo for the .cl file. I am a bit surprised errors were not fixed in the AMD blog. I wish the examples were given as a zip file in the tutorial. There was another typo (of closing a "}") in checkErr function too. Thanks again.
                                Sine