C++11 错误:SYCL内核无法通过函数指针DPCPP、TBB调用

C++11 错误:SYCL内核无法通过函数指针DPCPP、TBB调用,c++11,tbb,sycl,dpc++,C++11,Tbb,Sycl,Dpc++,我试图定义一个函数数组并将其传递给map,我将其定义为一个类,然后如果我的设备是CPU,那么向量上的每个函数的执行都会通过CPU,如果不是,则通过SYCL部分并在GPU上执行。 同时,我想测量我的代码的性能,用TBB库在CPU上执行代码的性能。 问题是,当我编译我的程序时,它向我显示了这个错误,我不知道如何修复它。 我应该指出,在定义函数数组之前,在SYCL部分定义函数的本地副本时,我的代码运行良好,我得到了结果,但在定义函数数组之后,它会导致错误。 我的代码: 你不能。引用规范: 本规范定义的

我试图定义一个函数数组并将其传递给map,我将其定义为一个类,然后如果我的设备是CPU,那么向量上的每个函数的执行都会通过CPU,如果不是,则通过SYCL部分并在GPU上执行。 同时,我想测量我的代码的性能,用TBB库在CPU上执行代码的性能。 问题是,当我编译我的程序时,它向我显示了这个错误,我不知道如何修复它。 我应该指出,在定义函数数组之前,在SYCL部分定义函数的本地副本时,我的代码运行良好,我得到了结果,但在定义函数数组之后,它会导致错误。 我的代码:

你不能。引用规范:

本规范定义的SYCL设备代码不支持 虚拟函数调用、一般的函数指针、异常、, 运行时类型信息或可能的C++库的全组 依赖于这些功能或特定主机编译器的功能


您不应该忘记,您的目标是各种设备,每个设备都执行自己版本的功能

首先,使用函数指针没有意义,因为它只有一个值,并且不能同时表示同一函数的CPU和GPU版本


其次,如果您使用的函数不是获取函数指针的函数,并且您使用的编程模型不需要将函数声明为内核(如CUDA),那么我看不出编译器如何确定要为不同的设备编译哪些函数。

您是对的,但在定义函数数组之前,使用函数的本地副本,我有可能在设备上执行它,我的意思是在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, h, 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 Tout, class Function>
Map<Tin, Tout, Function> make_map(Function f) { return Map<Tin, Tout, 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