![](/img/trans.png)
[英]How to specify particular GPU Device to be used at the time of running a program in SYCL/DPC++?
[英]SYCL/DPC++ cpu version gives correct result, but gpu gives incorrect data
我使用 intel dpc++ 編譯器編譯並運行了以下代碼。 使用 cpu 選擇器時我得到了正確的結果,但 gpu 選擇器給出了垃圾值。
我的代碼所做的只是一個名為數據的數組被初始化為全 1。 在 sycl kernel 中,此數組的訪問器乘以 3 並保存到結果數組中。 我嘗試在結果數組中打印值,該數組應該全是 3,但我得到的是垃圾值。
當我在 gpu 上執行代碼時得到垃圾值。我嘗試在 cpu 選擇器上運行,這里的代碼沒有問題。
我在 linux 和 windows 上試過這個。編譯器版本 dpcpp 2021.3
#include "iostream"
#include<CL/sycl.hpp>
#include <array>
using namespace std;
using namespace sycl;
int main() {
sycl::gpu_selector selector;
//using cpu selector as in the line below works
//sycl::cpu_selector selector;
sycl::queue q = sycl::queue(selector);
std::cout << q.get_device().get_info<sycl::info::device::name>();
constexpr int size = 3;
std::array<int, size> data{1,1,1};
std::array<int, size> resultarray;
range<1> num_items{ size };
buffer<int, 1> data_buff(data.data(), num_items);
buffer<int, 1> result(resultarray.data(), num_items);
q.submit([&](sycl::handler& cgh)
{
auto dataAccess = data_buff.get_access<access::mode::read_write>(cgh);
auto resultAccess = result.get_access<access::mode::write>(cgh);
cgh.parallel_for(num_items, [=](id<1> i)
{
resultAccess[i] = dataAccess[i] * 3;
});
}).wait();
std::cout <<"||"<< resultarray[0]<<"||"; //expected result ||3||
}
有人可以幫助為什么代碼會在 GPU 上給出錯誤的結果嗎?
您不會觸發復制回主機。 大概在 CPU 上,您的 SYCL 實現只是決定直接對輸入指針進行操作,因此您看不到問題所在。
想一想:SYCL 實現如何知道您的cout
中正在使用resultarray
並且必須將數據復制回來? 它不能,因為此 memory 訪問不通過任何 SYCL 構造 go。 因此它不知道它必須將數據復制回來。 wait()
只會讓主機等待 kernel 完成,它不會觸發副本。
觸發必要副本的最重要方法是:
{ }
中就足夠了,因為在您的cout
之前,緩沖區將從 scope 中的 go 開始,並且回寫被觸發。host_accessor
而不是直接訪問resultarray
handler::copy()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.