什么是Opencl make_Kernel真正返回以及如何存储它?

时间:2016-06-13 03:54:21

标签: c++11 opencl

下面我有以下代码,我试图将我编译的内核存储到一个变量函数中,然后可以在以后访问它。不幸的是,当我在结构中声明自动kernelTest时,它会抛出一个错误,说"非静态成员声明为auto"。 cl :: make_kernel实际返回什么,以及如何将其存储为私有变量?

struct OCLData
{
    cl::Device device;
    cl::Context context;
    cl::CommandQueue queue;

    cl::Program program;
    auto kernelTest; (PROBLEM)

    const char *kernelTestSource = MULTILINE(
    __kernel void kernelTest(const int N, __global float* A, __global float* B, __global float* C)
    {
       int i = get_global_id(0);
       int j = get_global_id(1);
    }
    );

    OCLData(){
        try{
            // Set Device
            cl_uint deviceIndex = 0;
            std::vector<cl::Device> devices;
            unsigned numDevices = getDeviceList(devices);
            if (deviceIndex >= numDevices)
            {
            std::cout << "Invalid device index (try '--list')\n";
            return;
            }
            this->device = devices[deviceIndex];

            // Set Context and Queue
            std::vector<cl::Device> chosen_device;
            chosen_device.push_back(device);
            this->context = cl::Context(chosen_device);
            this->queue = cl::CommandQueue(context, device);

            // Print Device Name
            std::string name;
            getDeviceName(this->device, name);
            std::cout << "\nUsing OpenCL device: " << name << "\n";

            // Compile GPU Code
            this->program = cl::Program(this->context, this->kernelTestSource, true);
            //auto kernel = cl::make_kernel<int, cl::Buffer, cl::Buffer, cl::Buffer>(this->program, "kernelTest");
            this->test = cl::make_kernel<int, cl::Buffer, cl::Buffer, cl::Buffer>(this->program, "kernelTest");
            //cl::make_kernel<int, cl::Buffer, cl::Buffer, cl::Buffer> naive_mmul(this->program, "kernelTest");
            std::cout << "GPU Code Compiled: " << "\n";

        } catch (cl::Error err)
            {
            std::cout << "Exception\n";
            std::cerr << "ERROR: "
                    << err.what()
                    << "("
                    << err_code(err.err())
                    << ")"
                    << std::endl;
            }

    }
};

2 个答案:

答案 0 :(得分:1)

cl::make_kernel<int, cl::Buffer, cl::Buffer, cl::Buffer>创建该类型的对象。

根据C ++ 11标准auto类成员必须也是static const,这意味着它必须被初始化,但是在cl::make_kerenl<...>之前有相当多的代码要执行被创造。 在这种情况下,您可以使用std::shared_ptr作为结构的成员类型:

std::shared_ptr<cl::make_kernel<int, cl::Buffer, cl::Buffer, cl::Buffer>> kernelTest;

然后在代码中:

this->kernelTest.reset(new cl::make_kernel<int, cl::Buffer, cl::Buffer, cl::Buffer>(this->program, "kernelTest"));

答案 1 :(得分:1)

我按照朋友的建议做了这个:

typedef cl::make_kernel <float, cl::Buffer&> kernelTestType;
std::function<kernelTestType::type_> kernelTest;
this->kernelTest = kernelTestType(this->program, "kernelTest");

看起来这样有效