123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #ifndef BOOST_COMPUTE_PLATFORM_HPP
- #define BOOST_COMPUTE_PLATFORM_HPP
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <boost/algorithm/string/split.hpp>
- #include <boost/algorithm/string/classification.hpp>
- #include <boost/compute/cl.hpp>
- #include <boost/compute/device.hpp>
- #include <boost/compute/detail/get_object_info.hpp>
- namespace boost {
- namespace compute {
- class platform
- {
- public:
-
- explicit platform(cl_platform_id id)
- : m_platform(id)
- {
- }
-
- platform(const platform &other)
- : m_platform(other.m_platform)
- {
- }
-
- platform& operator=(const platform &other)
- {
- if(this != &other){
- m_platform = other.m_platform;
- }
- return *this;
- }
-
- ~platform()
- {
- }
-
- cl_platform_id id() const
- {
- return m_platform;
- }
-
- std::string name() const
- {
- return get_info<std::string>(CL_PLATFORM_NAME);
- }
-
- std::string vendor() const
- {
- return get_info<std::string>(CL_PLATFORM_VENDOR);
- }
-
- std::string profile() const
- {
- return get_info<std::string>(CL_PLATFORM_PROFILE);
- }
-
- std::string version() const
- {
- return get_info<std::string>(CL_PLATFORM_VERSION);
- }
-
- std::vector<std::string> extensions() const
- {
- std::string extensions_string =
- get_info<std::string>(CL_PLATFORM_EXTENSIONS);
- std::vector<std::string> extensions_vector;
- boost::split(extensions_vector,
- extensions_string,
- boost::is_any_of("\t "),
- boost::token_compress_on);
- return extensions_vector;
- }
-
-
- bool supports_extension(const std::string &name) const
- {
- const std::vector<std::string> extensions = this->extensions();
- return std::find(
- extensions.begin(), extensions.end(), name) != extensions.end();
- }
-
- std::vector<device> devices(cl_device_type type = CL_DEVICE_TYPE_ALL) const
- {
- size_t count = device_count(type);
- if(count == 0){
-
- return std::vector<device>();
- }
- std::vector<cl_device_id> device_ids(count);
- cl_int ret = clGetDeviceIDs(m_platform,
- type,
- static_cast<cl_uint>(count),
- &device_ids[0],
- 0);
- if(ret != CL_SUCCESS){
- BOOST_THROW_EXCEPTION(opencl_error(ret));
- }
- std::vector<device> devices;
- for(cl_uint i = 0; i < count; i++){
- devices.push_back(device(device_ids[i]));
- }
- return devices;
- }
-
- size_t device_count(cl_device_type type = CL_DEVICE_TYPE_ALL) const
- {
- cl_uint count = 0;
- cl_int ret = clGetDeviceIDs(m_platform, type, 0, 0, &count);
- if(ret != CL_SUCCESS){
- if(ret == CL_DEVICE_NOT_FOUND){
-
- return 0;
- }
- else {
-
- BOOST_THROW_EXCEPTION(opencl_error(ret));
- }
- }
- return count;
- }
-
-
-
- template<class T>
- T get_info(cl_platform_info info) const
- {
- return detail::get_object_info<T>(clGetPlatformInfo, m_platform, info);
- }
-
- template<int Enum>
- typename detail::get_object_info_type<platform, Enum>::type
- get_info() const;
-
-
- void* get_extension_function_address(const char *function_name) const
- {
- #ifdef CL_VERSION_1_2
- return clGetExtensionFunctionAddressForPlatform(m_platform,
- function_name);
- #else
- return clGetExtensionFunctionAddress(function_name);
- #endif
- }
-
- void unload_compiler()
- {
- #ifdef CL_VERSION_1_2
- clUnloadPlatformCompiler(m_platform);
- #else
- clUnloadCompiler();
- #endif
- }
-
- bool operator==(const platform &other) const
- {
- return m_platform == other.m_platform;
- }
-
- bool operator!=(const platform &other) const
- {
- return m_platform != other.m_platform;
- }
- private:
- cl_platform_id m_platform;
- };
- BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(platform,
- ((std::string, CL_PLATFORM_PROFILE))
- ((std::string, CL_PLATFORM_VERSION))
- ((std::string, CL_PLATFORM_NAME))
- ((std::string, CL_PLATFORM_VENDOR))
- ((std::string, CL_PLATFORM_EXTENSIONS))
- )
- inline boost::compute::platform device::platform() const
- {
- return boost::compute::platform(get_info<CL_DEVICE_PLATFORM>());
- }
- }
- }
- #endif
|