问题描述
我尝试定义一个函数数组并传递给我定义为一个类的映射,然后如果我的设备是 cpu,则向量上的每个函数的执行都通过 cpu,如果不是,则通过通过SYCL部分并在GPU上执行。 同时,我想测量我的代码的性能,使用TBB库在cpu上执行我的代码的性能。 问题是,当我编译我的程序时,它向我显示了这个错误,我不知道如何修复它。 我应该指出,在定义函数数组之前,在 SYCL 部分定义函数的本地副本,我的代码运行良好,我得到了结果,但是在定义了一个函数数组之后,它出现了错误。 我的代码:
#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>
#include <chrono>
using namespace tbb;
template<class Tin,class Tout,class Function>
class Map {
private:
Function fun;
public:
Map() {}
Map(Function f):fun(f) {}
std::vector<Tout> operator()(bool use_tbb,std::vector<Tin>& v) {
std::vector<Tout> r(v.size());
if(use_tbb){
// Start measuring time
auto begin = std::chrono::high_resolution_clock::Now();
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]);
}
});
// Stop measuring time and calculate the elapsed time
auto end = std::chrono::high_resolution_clock::Now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Time measured: %.3f seconds.\n",elapsed.count() * 1e-9);
return r;
} else {
sycl::queue gpuQueue{sycl::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,sycl::write_only);
h.parallel_for(n_item,[=](sycl::id<1> index) {
out_accessor[index] = f(in_accessor[index]);
});
}).wait();
}
return r;
}
};
template<class Tin,class Function>
Map<Tin,Tout,Function> make_map(Function f) { return Map<Tin,Function>(f);}
typedef int(*func)(int x);
//define different functions
auto function = [](int x){ return x; };
auto functionTimesTwo = [](int x){ return (x*2); };
auto functionDivideByTwo = [](int x){ return (x/2); };
auto lambdaFunction = [](int x){return (++x);};
int main(int argc,char *argv[]) {
std::vector<int> v = {1,2,3,4,5,6,7,8,9};
//Array of functions
func functions[] =
{
function,functionTimesTwo,functionDivideByTwo,lambdaFunction
};
for(int i = 0; i< sizeof(functions); i++){
auto m1 = make_map<int,int>(functions[i]);
std::vector<int> r = m1(true,v);
//print the result
for(auto &e:r) {
std::cout << e << " ";
}
}
return 0;
}
错误:
SYCL kernel cannot call through a function pointer