error: no viable conversion from 'std::array<int, n>' to 'const void *' using SYCL/DPC++
error: no viable conversion from 'std::array<int, n>' to 'const void *' using SYCL/DPC++
我是 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.
谁能帮我看看哪里出错了?
提前致谢。
memcpy() 的语法是
`void memcpy(void *Dest, const void *Src, size_t Count);`
您需要通过引用传递而不是值传递来传递第二个参数。您可以将 &h_a(或)&h_a[0](或)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;
}
我是 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.
谁能帮我看看哪里出错了?
提前致谢。
memcpy() 的语法是
`void memcpy(void *Dest, const void *Src, size_t Count);`
您需要通过引用传递而不是值传递来传递第二个参数。您可以将 &h_a(或)&h_a[0](或)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;
}