I am a beginner in SYCL/DPC . I have created an array and by using buffers I am updating the values in the device code but when I try to print the updated values in the kernel/device code I am getting error. I am able to print the updated values through accessors and arrays. Can someone help me out how can I print/get the values from kernel code?
Here is my code.
#include <CL/sycl.hpp>
#include <array>
#include<iostream>
using namespace std;
using namespace sycl;
const int n = 6;
int main() {
array<int,n> a;
for (int i = 0; i < n; i )
a[i] = 0;
queue q;
buffer b(a);
q.submit([&](handler &h) {
accessor acc(b, h);
h.parallel_for(n, [=](id<1> i) {
acc[i] = (acc[i] n) / 2;
cout<<acc[i]<<" ";
});
cout<<"Updated"<<"\n";
});
q.wait();
host_accessor h_acc(b);
for (int i = 0; i < n; i ){
cout << h_acc[i] << " ";
}
cout <<"From Host Accessor Updated"<< "\n";
for (int i = 0; i < n; i ) {
cout << a[i] << " ";
}
cout << "From Array Updated"<<"\n";
}
I got these errors while compiling the code
print.cpp:19:1: error: SYCL kernel cannot use a non-const global variable
cout<<acc[i]<<" ";
^
/opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/handler.hpp:2303:7: note: called by 'operator()'
KernelFunc(Arg);
^
/opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/handler.hpp:899:5: note: called by 'kernel_parallel_for<sycl::detail::__pf_kernel_wrapper<(lambda at print.cpp:17:19)>, sycl::id<1>, (lambda at /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/handler.hpp:2299:12)>'
KernelFunc(detail::Builder::getElement(detail::declptr<ElementType>()));
^
In file included from print.cpp:1:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl.hpp:11:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/ONEAPI/atomic.hpp:11:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/ONEAPI/atomic_accessor.hpp:11:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/ONEAPI/atomic_enums.hpp:12:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/access/access.hpp:10:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/detail/common.hpp:109:
In file included from /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/iostream:39:
In file included from /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/ostream:38:
In file included from /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/ios:44:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/bits/basic_ios.h:158:15: error: SYCL kernel cannot call an undefined function without SYCL_EXTERNAL attribute
{ this->clear(this->rdstate() | __state); }
^
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/bits/basic_ios.h:148:7: note: 'clear' declared here
clear(iostate __state = goodbit);
^
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/bits/basic_ios.h:157:7: note: called by 'setstate'
setstate(iostate __state)
^
In file included from print.cpp:1:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl.hpp:11:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/ONEAPI/atomic.hpp:11:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/ONEAPI/atomic_accessor.hpp:11:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/ONEAPI/atomic_enums.hpp:12:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/access/access.hpp:10:
In file included from /opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/detail/common.hpp:109:
In file included from /usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/iostream:39:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/ostream:611:2: error: SYCL kernel cannot call an undefined function without SYCL_EXTERNAL attribute
__ostream_insert(__out, __s,
^
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/bits/ostream_insert.h:77:5: note: '__ostream_insert<char, std::char_traits<char>>' declared here
__ostream_insert(basic_ostream<_CharT, _Traits>& __out,
^
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/ostream:606:5: note: called by 'operator<<<std::char_traits<char>>'
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
print.cpp:19:5: error: SYCL kernel cannot call an undefined function without SYCL_EXTERNAL attribute
cout<<acc[i]<<" ";
^
/usr/lib/gcc/x86_64-linux-gnu/11/../../../../include/c /11/ostream:189:7: note: 'operator<<' declared here
operator<<(int __n);
^
print.cpp:17:19: note: called by 'operator()'
h.parallel_for(n, [=](id<1> i) {
^
print.cpp:19:1: error: SYCL kernel cannot use a non-const global variable
cout<<acc[i]<<" ";
^
/opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/handler.hpp:899:5: note: called by 'kernel_parallel_for<(lambda at print.cpp:17:19), sycl::id<1>, (lambda at print.cpp:17:19)>'
KernelFunc(detail::Builder::getElement(detail::declptr<ElementType>()));
^
5 errors generated.
CodePudding user response:
You can't use standard print methods inside a SYCL kernel. You need to use the stream class. For example
#include <CL/sycl.hpp>
namespace sycl = cl::sycl;
int main(int, char**) {
sycl::queue queue(sycl::default_selector{});
queue.submit([&] (sycl::handler& cgh) {
sycl::stream out(1024, 256, cgh);
cgh.single_task<class printkernel>([=] {
out << "Hello stream!" << sycl::endl;
} );
} );
return 0;
}