无法从 SYCL/DPC++ 中的内核代码获取值

Unable to get values from kernel code in SYCL/DPC++

我是 SYCL/DPC++ 的初学者。我创建了一个数组,并通过使用缓冲区更新了设备代码中的值,但是 当我尝试在 kernel/device 代码中打印更新值时出现错误。我能够通过访问器和数组打印更新的值。 有人可以帮我解决如何 print/get 来自内核代码的值吗?

这是我的代码。

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

我在编译代码时遇到了这些错误

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.

您不能在 SYCL 内核中使用标准打印方法。 您需要使用流 class。 例如

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

}

在指南中查看更多信息here and here