我尝试编写一种“映射”类,它通过一些指定目标类型(CPU 或 GPU/加速器)的参数来包装 OneAPI 调用隐藏硬件定位问题。map,将代码引导到SYCL内核或TBB以通过parallel for实现map操作。它以设备类型、CPU 或 GPU 和函数作为参数,并应用于集合中的所有项目。但是在内核函数中,我有一个错误,即不允许隐式捕获。我无法理解我的错误是什么。这是我的代码:
#include <CL/sycl.hpp>
#include <iostream>
#include <tbb/tbb.h>
#include <tbb/parallel_for.h>
#include <vector>
#include <string>
#include <queue>
#include<tbb/blocked_range.h>
#include <tbb/global_control.h>
using namespace std;
using namespace cl::sycl;
using namespace tbb;
template<typename Tin, typename Tout>
class Map {
private:
function<Tout(Tin)> fun;
string device_type;
public:
Map() {}
Map(function<Tout(Tin)> f):fun(f) {}
void f(function<Tout(Tin)> ff) {
fun = ff;
}
void set_device(string dev) {
device_type = dev;
}
vector<Tout> operator()(vector<Tin>& v) {
device *my_dev = new device();
if(device_type == "cpu"){
if(my_dev->is_cpu()) {
vector<Tout> r(0);
tbb::parallel_for(tbb::blocked_range<Tin>(0, v.size()),
[&](tbb::blocked_range<Tin> t) {
for (int index = t.begin(); index < t.end(); ++index){
r[index] = fun(v[index]);
}
});
return r;
}
}else if(device_type == "gpu"){
if(my_dev->is_gpu()) {
vector<Tout> r(v.size());
sycl::queue gpuQueue{gpu_selector()};
sycl::range<1> n_item{v.size()};
sycl::buffer<Tin, 1> in_buffer(&v[0], n_item);
sycl::buffer<Tout, 1> out_buffer(&r[0], n_item);
gpuQueue.submit([&](sycl::handler& h){
//local copy of fun
//auto f = fun;
sycl::accessor in_accessor(in_buffer, h, sycl::read_only);
sycl::accessor out_accessor(out_buffer, h, sycl::write_only);
h.parallel_for(n_item, [=](sycl::id<1> index) {
out_accessor[index] = fun(in_accessor[index]);
});
}).wait();
return r;
}
}
}
};
int main(int argc, char *argv[]) {
vector<int> v = {1,2,3,4,5,6,7,8};
auto f = [](int x){return (++x);};
sycl::device dev = sycl::cpu_selector().select_device();
string dev_type = argv[1];
Map <int,int> m(f);
m.set_device(dev_type);
auto r = m(v);
for(auto &e:r) {
cout << e << "\n";
}
return 0;
}
当我在 Eclipse 的控制台中检查问题时,它显示了这个错误:
1- 内核函数不允许隐式捕获“this”