-2

我是 SYCL/DPC++ 的初学者。我已经使用 USM(统一共享内存)编写了一个代码,并采用了两个数组主机和设备数组,我想在其中将主机数组值复制到设备数组并执行基本操作并打印这些值。但是在编译的时候,我得到了一个错误。

这是我的代码。

#include <CL/sycl.hpp>
#include <array>
#include<iostream>
using namespace std;
using namespace sycl;
const int n = 6;

int main() {
  queue q;

  std::array<int,n> h_a;
  int *d_a = malloc_device<int>(n, q);
  for (int i = 0; i < n; i++)
    h_a[i] = n;

  q.submit([&](handler& h) {
      h.memcpy(d_a,h_a,n * sizeof(int));
    });
  q.wait();

  q.submit([&](handler& h) {
      h.parallel_for(n, [=](id<1> i) {
          d_a[i] = d_a[i] * 2;
          d_a[i] = d_a[i] + 2;
          d_a[i] = d_a[i] - 2;
          d_a[i] = d_a[i] + 2;
        });
    });
  q.wait();

  q.submit([&](handler& h) {
      h.memcpy(h_a,d_a,n * sizeof(int));
    });
  q.wait();
  for(int i = 0;i < n;i++){
    cout<<h_a[i]<<" "<<d_a[i]<<" ";
  }
  cout<<"\n";

  free(d_a, q);
  return 0;
}

编译错误

simpleope.cpp:17:20: error: no viable conversion from 'std::array<int, n>' to 'const void *'
      h.memcpy(d_a,h_a,n * sizeof(int));
                   
/opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/handler.hpp:2171:39: note: passing argument to parameter 'Src' here
  void memcpy(void *Dest, const void *Src, size_t Count);
                                      
simpleope.cpp:32:16: error: no viable conversion from 'std::array<int, n>' to 'void *'
      h.memcpy(h_a,d_a,n * sizeof(int));
               
/opt/intel/oneapi/compiler/2021.3.0/linux/bin/../include/sycl/CL/sycl/handler.hpp:2171:21: note: passing argument to parameter 'Dest' here
  void memcpy(void *Dest, const void *Src, size_t Count);
                    
2 errors generated.

有人可以帮我在哪里出错吗?

提前致谢。

4

1 回答 1

1

memcpy() 的语法是

`void memcpy(void *Dest, const void *Src, size_t Count);`

您需要将第二个参数作为按引用传递而不是按值传递传递。您可以将 &h_a (or) &h_a[0] (or) h_a.data() 中的任何一个作为参数传递,以消除代码中的这些错误。修改后的代码是

#include <CL/sycl.hpp>
#include <array>
#include<iostream>
using namespace std;
using namespace sycl;
const int n = 6; int main() {​​​​
  queue q;   std::array<int,n> h_a;
  int *d_a = malloc_device<int>(n, q);
  for (int i = 0; i < n; i++)
    h_a[i] = n;   
q.submit([&](handler& h) {​​​​
      h.memcpy(d_a,&h_a,n * sizeof(int));
    }​​​​);
  q.wait();   
q.submit([&](handler& h) {​​​​
      h.parallel_for(n, [=](id<1> i) {​​​​
          d_a[i] = d_a[i] * 2;
          d_a[i] = d_a[i] + 2;
          d_a[i] = d_a[i] - 2;
          d_a[i] = d_a[i] + 2;
        }​​​​);
    }​​​​);

  q.wait();   
q.submit([&](handler& h) {​​​​
      h.memcpy(&h_a,d_a,n * sizeof(int));
    }​​​​);
  q.wait();
  for(int i = 0;i < n;i++){​​​​
    cout<<h_a[i]<<" "<<d_a[i]<<" ";
  }​​​​
  cout<<"\n";   free(d_a, q);
  return 0;
}​​​​
于 2021-09-30T10:47:58.960 回答