1
votes

I'm working on vectorAdd sample application which was provided along with oneAPI basekit. When I tried printing the sum inside kernel, I get the following error.

Please find attached source code and error when compiled.

//Source code 

    cgh.parallel_for<class VectorAdd>(num_items, [=](id<1> wiID) {
        sum_accessor[wiID] = addend_1_accessor[wiID] + addend_2_accessor[wiID];

        std::cout<<"Sum : "<<sum_accessor[wiID]<<std::endl;  // I want to print this sum

        });

During compilation I get the following error.

 //Error generated while compiling

usr/lib/gcc/x86_64-linux-gnu/7.4.0/../../../../include/c++/7.4.0/bits/ostream.tcc:359:25: error: SYCL kernel cannot have a class with a virtual function table
  extern template class basic_ostream<char>;
                        ^
vector-add.cpp:159:6: note: used here
            std::cout<<"Sum : "<<sum_accessor[wiID]<<std::endl;
            ^
vector-add.cpp:159:11: error: SYCL kernel cannot use a global variable
            std::cout<<"Sum : "<<sum_accessor[wiID]<<std::endl;
2

2 Answers

3
votes

Use the sycl::stream class to output logging in your SYCL code. There's a guide that explains how to do this.

Your code should look something like this

//Source code 
sycl::stream out(1024, 256, cgh);
cgh.parallel_for<class VectorAdd>(num_items, [=](id<1> wiID) {
    sum_accessor[wiID] = addend_1_accessor[wiID] + addend_2_accessor[wiID];
    out << "Sum : " << sum_accessor[wiID]  << cl::sycl::endl;  // I want to print this sum
    });
0
votes

Otherwise you can just use the host device to debug your program first. On the host device you should be able to use whatever C++ code you want. You can even use the normal host debug tools such as GDB, Valgrind, Helgrind, UBsan, etc. to debug the application including the kernel code.

Once the kernel is debugged on the host device, remove or comment the host-only debug code from the kernel and switch to the real accelerator device.