gpt4 book ai didi

tbb - 错误 : implicit capture of 'this' is not allowed for kernel functions, SYCL,DPCPP

转载 作者:行者123 更新时间:2023-12-04 07:43:06 28 4
gpt4 key购买 nike

我尝试编写一种“映射”类,它通过一些指定目标类型(CPU 或 GPU/加速器)的参数来包装 OneAPI 调用以隐藏硬件定位问题。
映射,将代码定向到 SYCL 内核或 TBB 以通过并行 for 实现映射操作。
它以设备类型、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”

最佳答案

您正在尝试访问 fun在你的内核中,一个成员变量 Map .在 C++ 中使用 this 访问成员变量指针。默认情况下,Lambda 在 C++ 中不捕获 this 指针,因此会出现错误消息。
但是,即使您要捕获 this在您的内核中它不起作用,因为 this将指向通常在设备上无法访问的主机内存。
对此的一个非常简单的解决方法通常是在内核中使用本地副本:

class X {
void run(sycl::queue& q){
q.submit([&](sycl::handler& cgh){
int local_var = var; // Note: This can also be expressed using the lambda capture list
cgh.parallel_for(..., [=](...){ /* use local_var here*/});
});
}

int var;
};
从 C++17 开始,您还可以通过复制来捕获类: [*this](...){...} .
您的代码更根本的问题是 SYCL 规范不允许使用 std::function内部设备代码。在某些情况下,对于某些 SYCL 实现,它可能会起作用(例如对于主机后端),但这是一个扩展。问题在于 std::function的执行通常使用设备不支持的机制进行类型删除,例如动态多态。
一种解决方案可能是在类模板参数中包含函数的类型,而不是使用 std::function .

关于tbb - 错误 : implicit capture of 'this' is not allowed for kernel functions, SYCL,DPCPP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67349424/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com