2014-12-06 43 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

檢查彙編程序:方法如下調用。編譯器生成的內容應該有所不同。