2014-12-06 47 views
0

我想创建周围的OpenCL下的定制类++包装来从可用设备的一些具体信息。例如,获取平台中可用的GPU,CPU等的数量。为了减少代码,我决定实施私有模板功能,如下图所示:私人C++模板函数 - OpenCL的

//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; 
    } 
}; 

当代码编译正确,的getInfo抛出一个CL_INVALID_VALUE错误。当我实现了使用定时功能相同的代码(而不是模板)的代码工作正常:

//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; 
    } 
}; 

有什么想法?

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; 
} 
+0

为什么要使用cl_uint法院地法在一环和cl_int在其他。这可能是原因。否则请张贴您如何调用该方法。另外:为什么在这里使用模板? – marc 2014-12-06 10:58:13

+0

对不起。这是传输代码时输入错误(我添加了try-catch块,并且由于err()是cl_int我不得不用cl_int替换cl_uint,但显然我跳过了这个...)。为了调用该方法,我使用了一个简单的C++代码,其中main()中包含以下内容:\t \t std :: vector availablePlatforms; \t \t cl :: Platform :: get(&availablePlatforms); \t \t设备d(availablePlatforms [0]); \t \t的std :: COUT << d.getTotalNumberOfGPUs()<<的std :: ENDL; – thanasis 2014-12-06 11:28:02

回答

1

的问题是,你的response变量没有在你的模板版本正确的类型。这是因为您正在将CL_DEVICE_TYPE_GPU传递给作为预处理器宏的模板化参数,因此不一定具有设备信息查询所需的正确类型。

一种解决方案是显式转换模板参数,以确保它具有正确的类型:

return countDevicesWithSpecificProperty(CL_DEVICE_TYPE, (cl_device_type)CL_DEVICE_TYPE_GPU); 
0

检查汇编程序:方法如下调用。编译器生成的内容应该有所不同。