Function 私人C++;常规类中的模板函数-OpenCL 我试图在OpenCL C++包装器上创建一个定制类,从可用的设备中获取一些特定的信息。例如,获取平台中可用GPU、CPU等的数量。为了减少代码,我决定实现一个私有模板函数,如下所示: //Devices.hpp class Devices { public: Devices(const cl::Platform& inputPlatform) { inputPlatform.getDevices(CL_DEVICE_TYPE_ALL, &availableDevices); } cl_int getTotalNumberOfDevices() { return availableDevices.size(); } cl_int getTotalNumberOfGPUs() { return countDevicesWithSpecificProperty(CL_DEVICE_TYPE, CL_DEVICE_TYPE_GPU); } private: std::vector<cl::Device> availableDevices; template <typename T> cl_int countDevicesWithSpecificProperty( const cl_device_info& deviceInfo, const T& searchPropertyValue) { cl_int totalNumberOfDevices = getTotalNumberOfDevices(); T response; cl_int count = 0; for (cl_int i = 0; i < totalNumberOfDevices; ++i) { try { availableDevices.at(i).getInfo(deviceInfo, &response); } catch (cl::Error e) { return e.err(); } if (response == searchPropertyValue) ++count; } return count; } }; //Devices.hpp 类设备 { 公众: 设备(常数cl::平台和输入平台) { inputPlatform.getDevices(CL_设备类型所有和可用设备); } cl_int GetTotalNumber of Devices() { 返回可用的设备。大小(); } cl_int GetTotalNumber of Gpus() { 返回具有特定属性的计数设备(CL\U设备类型、CL\U设备类型\U GPU); } 私人: std::矢量可用设备; 模板 具有特定属性的计数设备( const cl_设备信息和设备信息, const T和searchPropertyValue) { cl_int totalNumberOfDevices=getTotalNumberOfDevices(); T反应; cl_int count=0; 对于(cl_int i=0;i
当代码正确编译时,getInfo抛出CL_INVALID_值错误。当我使用常规函数(而不是模板)实现相同的代码时,代码运行良好:Function 私人C++;常规类中的模板函数-OpenCL 我试图在OpenCL C++包装器上创建一个定制类,从可用的设备中获取一些特定的信息。例如,获取平台中可用GPU、CPU等的数量。为了减少代码,我决定实现一个私有模板函数,如下所示: //Devices.hpp class Devices { public: Devices(const cl::Platform& inputPlatform) { inputPlatform.getDevices(CL_DEVICE_TYPE_ALL, &availableDevices); } cl_int getTotalNumberOfDevices() { return availableDevices.size(); } cl_int getTotalNumberOfGPUs() { return countDevicesWithSpecificProperty(CL_DEVICE_TYPE, CL_DEVICE_TYPE_GPU); } private: std::vector<cl::Device> availableDevices; template <typename T> cl_int countDevicesWithSpecificProperty( const cl_device_info& deviceInfo, const T& searchPropertyValue) { cl_int totalNumberOfDevices = getTotalNumberOfDevices(); T response; cl_int count = 0; for (cl_int i = 0; i < totalNumberOfDevices; ++i) { try { availableDevices.at(i).getInfo(deviceInfo, &response); } catch (cl::Error e) { return e.err(); } if (response == searchPropertyValue) ++count; } return count; } }; //Devices.hpp 类设备 { 公众: 设备(常数cl::平台和输入平台) { inputPlatform.getDevices(CL_设备类型所有和可用设备); } cl_int GetTotalNumber of Devices() { 返回可用的设备。大小(); } cl_int GetTotalNumber of Gpus() { 返回具有特定属性的计数设备(CL\U设备类型、CL\U设备类型\U GPU); } 私人: std::矢量可用设备; 模板 具有特定属性的计数设备( const cl_设备信息和设备信息, const T和searchPropertyValue) { cl_int totalNumberOfDevices=getTotalNumberOfDevices(); T反应; cl_int count=0; 对于(cl_int i=0;i,function,templates,opencl,Function,Templates,Opencl,当代码正确编译时,getInfo抛出CL_INVALID_值错误。当我使用常规函数(而不是模板)实现相同的代码时,代码运行良好: //Devices.hpp class Devices { public: Devices(const cl::Platform& inputPlatform) { inputPlatform.getDevices(CL_DEVICE_TYPE_ALL, &availableDevices); } cl_
//Devices.hpp
class Devices
{
public:
Devices(const cl::Platform& inputPlatform)
{
inputPlatform.getDevices(CL_DEVICE_TYPE_ALL, &availableDevices);
}
cl_int getTotalNumberOfDevices()
{
return availableDevices.size();
}
cl_int getTotalNumberOfGPUs()
{
return countDevicesWithSpecificProperty(CL_DEVICE_TYPE, CL_DEVICE_TYPE_GPU);
}
private:
std::vector<cl::Device> availableDevices;
cl_int countDevicesWithSpecificProperty
(const cl_device_info& deviceInfo,
const cl_device_type& searchPropertyValue)
{
cl_int totalNumberOfDevices = getTotalNumberOfDevices();
cl_device_type response;
cl_int count = 0;
for (cl_int i = 0; i < totalNumberOfDevices; ++i)
{
try
{
availableDevices.at(i).getInfo(deviceInfo, &response);
}
catch (cl::Error e)
{
return e.err();
}
if (response == searchPropertyValue) ++count;
}
return count;
}
};
//Devices.hpp
类设备
{
公众:
设备(常数cl::平台和输入平台)
{
inputPlatform.getDevices(CL_设备类型所有和可用设备);
}
cl_int GetTotalNumber of Devices()
{
返回可用的设备。大小();
}
cl_int GetTotalNumber of Gpus()
{
返回具有特定属性的计数设备(CL\U设备类型、CL\U设备类型\U GPU);
}
私人:
std::矢量可用设备;
具有特定属性的计数设备
(const cl_设备信息和设备信息,
const cl_设备类型和搜索属性值)
{
cl_int totalNumberOfDevices=getTotalNumberOfDevices();
cl_装置_型响应;
cl_int count=0;
对于(cl_int i=0;i
有什么想法吗
PS:方法调用如下:
//main.cpp
#define __CL_ENABLE_EXCEPTIONS
#include <iostream>
#include <vector>
#include <CL/cl.hpp>
#include "Devices.hpp"
int main()
{
try
{
std::vector<cl::Platform> availablePlatforms;
cl::Platform::get(&availablePlatforms);
Devices d(availablePlatforms[0]);
std::cout << d.getTotalNumberOfGPUs() << std::endl;
}
catch (cl::Error e)
{
std::cout << e.what() << std::endl << e.err() << std::endl;
}
return 0;
}
//main.cpp
#定义\uu CL\u启用\u异常
#包括
#包括
#包括
#包括“Devices.hpp”
int main()
{
尝试
{
std::矢量可用平台;
cl::Platform::get(&availablePlatforms);
设备d(可用平台[0]);
std::cout检查两种情况下的汇编程序列表。编译器生成的内容应该有所不同。问题是您的响应变量在模板版本中没有正确的类型。这是因为您正在将CL\u设备类型\u GPU
传递给模板参数,该参数是预处理器macro和so不一定具有设备信息查询所需的正确类型
一种解决方案是显式强制转换模板参数,以确保其类型正确:
return countDevicesWithSpecificProperty(CL_DEVICE_TYPE, (cl_device_type)CL_DEVICE_TYPE_GPU);
为什么在一个循环中使用cl_-uint fori,在另一个循环中使用cl_-int。这可能是原因。否则,请也发布如何调用该方法。另外:为什么在此处使用模板?对此感到抱歉。这是在传输代码时的一个输入错误(我添加了try-catch块和自err()调用ClyIn必须用ClIt替换CluUnt,但我忽略了这个……。调用该方法,我使用了一个简单的C++代码,其中,在主()中,我包括以下内容:STD::vector可用平台;Cl::平台::GET(和可用平台);设备D(可用平台(0));STD::CUT