get_object_info.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_DETAIL_GET_OBJECT_INFO_HPP
  11. #define BOOST_COMPUTE_DETAIL_GET_OBJECT_INFO_HPP
  12. #include <string>
  13. #include <vector>
  14. #include <boost/preprocessor/seq/for_each.hpp>
  15. #include <boost/preprocessor/tuple/elem.hpp>
  16. #include <boost/throw_exception.hpp>
  17. #include <boost/compute/cl.hpp>
  18. #include <boost/compute/exception/opencl_error.hpp>
  19. namespace boost {
  20. namespace compute {
  21. namespace detail {
  22. template<class Function, class Object, class AuxInfo>
  23. struct bound_info_function
  24. {
  25. bound_info_function(Function function, Object object, AuxInfo aux_info)
  26. : m_function(function),
  27. m_object(object),
  28. m_aux_info(aux_info)
  29. {
  30. }
  31. template<class Info>
  32. cl_int operator()(Info info, size_t input_size, const void *input,
  33. size_t size, void *value, size_t *size_ret) const
  34. {
  35. return m_function(
  36. m_object, m_aux_info, info,
  37. input_size, input, size, value, size_ret
  38. );
  39. }
  40. template<class Info>
  41. cl_int operator()(Info info, size_t size, void *value, size_t *size_ret) const
  42. {
  43. return m_function(m_object, m_aux_info, info, size, value, size_ret);
  44. }
  45. Function m_function;
  46. Object m_object;
  47. AuxInfo m_aux_info;
  48. };
  49. template<class Function, class Object>
  50. struct bound_info_function<Function, Object, void>
  51. {
  52. bound_info_function(Function function, Object object)
  53. : m_function(function),
  54. m_object(object)
  55. {
  56. }
  57. template<class Info>
  58. cl_int operator()(Info info, size_t size, void *value, size_t *size_ret) const
  59. {
  60. return m_function(m_object, info, size, value, size_ret);
  61. }
  62. Function m_function;
  63. Object m_object;
  64. };
  65. template<class Function, class Object>
  66. inline bound_info_function<Function, Object, void>
  67. bind_info_function(Function f, Object o)
  68. {
  69. return bound_info_function<Function, Object, void>(f, o);
  70. }
  71. template<class Function, class Object, class AuxInfo>
  72. inline bound_info_function<Function, Object, AuxInfo>
  73. bind_info_function(Function f, Object o, AuxInfo j)
  74. {
  75. return bound_info_function<Function, Object, AuxInfo>(f, o, j);
  76. }
  77. // default implementation
  78. template<class T>
  79. struct get_object_info_impl
  80. {
  81. template<class Function, class Info>
  82. T operator()(Function function, Info info) const
  83. {
  84. T value;
  85. cl_int ret = function(info, sizeof(T), &value, 0);
  86. if(ret != CL_SUCCESS){
  87. BOOST_THROW_EXCEPTION(opencl_error(ret));
  88. }
  89. return value;
  90. }
  91. template<class Function, class Info>
  92. T operator()(Function function, Info info,
  93. const size_t input_size, const void* input) const
  94. {
  95. T value;
  96. cl_int ret = function(info, input_size, input, sizeof(T), &value, 0);
  97. if(ret != CL_SUCCESS){
  98. BOOST_THROW_EXCEPTION(opencl_error(ret));
  99. }
  100. return value;
  101. }
  102. };
  103. // specialization for bool
  104. template<>
  105. struct get_object_info_impl<bool>
  106. {
  107. template<class Function, class Info>
  108. bool operator()(Function function, Info info) const
  109. {
  110. cl_bool value;
  111. cl_int ret = function(info, sizeof(cl_bool), &value, 0);
  112. if(ret != CL_SUCCESS){
  113. BOOST_THROW_EXCEPTION(opencl_error(ret));
  114. }
  115. return value == CL_TRUE;
  116. }
  117. };
  118. // specialization for std::string
  119. template<>
  120. struct get_object_info_impl<std::string>
  121. {
  122. template<class Function, class Info>
  123. std::string operator()(Function function, Info info) const
  124. {
  125. size_t size = 0;
  126. cl_int ret = function(info, 0, 0, &size);
  127. if(ret != CL_SUCCESS){
  128. BOOST_THROW_EXCEPTION(opencl_error(ret));
  129. }
  130. if(size == 0){
  131. return std::string();
  132. }
  133. std::string value(size - 1, 0);
  134. ret = function(info, size, &value[0], 0);
  135. if(ret != CL_SUCCESS){
  136. BOOST_THROW_EXCEPTION(opencl_error(ret));
  137. }
  138. return value;
  139. }
  140. };
  141. // specialization for std::vector<T>
  142. template<class T>
  143. struct get_object_info_impl<std::vector<T> >
  144. {
  145. template<class Function, class Info>
  146. std::vector<T> operator()(Function function, Info info) const
  147. {
  148. size_t size = 0;
  149. cl_int ret = function(info, 0, 0, &size);
  150. if(ret != CL_SUCCESS){
  151. BOOST_THROW_EXCEPTION(opencl_error(ret));
  152. }
  153. if(size == 0) return std::vector<T>();
  154. std::vector<T> vector(size / sizeof(T));
  155. ret = function(info, size, &vector[0], 0);
  156. if(ret != CL_SUCCESS){
  157. BOOST_THROW_EXCEPTION(opencl_error(ret));
  158. }
  159. return vector;
  160. }
  161. template<class Function, class Info>
  162. std::vector<T> operator()(Function function, Info info,
  163. const size_t input_size, const void* input) const
  164. {
  165. #ifdef BOOST_COMPUTE_CL_VERSION_2_1
  166. // For CL_KERNEL_LOCAL_SIZE_FOR_SUB_GROUP_COUNT in clGetKernelSubGroupInfo
  167. // we can't get param_value_size using param_value_size_ret
  168. if(info == CL_KERNEL_LOCAL_SIZE_FOR_SUB_GROUP_COUNT)
  169. {
  170. std::vector<T> vector(3);
  171. cl_int ret = function(
  172. info, input_size, input,
  173. sizeof(T) * vector.size(), &vector[0], 0
  174. );
  175. if(ret != CL_SUCCESS){
  176. BOOST_THROW_EXCEPTION(opencl_error(ret));
  177. }
  178. return vector;
  179. }
  180. #endif
  181. size_t size = 0;
  182. cl_int ret = function(info, input_size, input, 0, 0, &size);
  183. if(ret != CL_SUCCESS){
  184. BOOST_THROW_EXCEPTION(opencl_error(ret));
  185. }
  186. std::vector<T> vector(size / sizeof(T));
  187. ret = function(info, input_size, input, size, &vector[0], 0);
  188. if(ret != CL_SUCCESS){
  189. BOOST_THROW_EXCEPTION(opencl_error(ret));
  190. }
  191. return vector;
  192. }
  193. };
  194. // returns the value (of type T) from the given clGet*Info() function call.
  195. template<class T, class Function, class Object, class Info>
  196. inline T get_object_info(Function f, Object o, Info i)
  197. {
  198. return get_object_info_impl<T>()(bind_info_function(f, o), i);
  199. }
  200. template<class T, class Function, class Object, class Info, class AuxInfo>
  201. inline T get_object_info(Function f, Object o, Info i, AuxInfo j)
  202. {
  203. return get_object_info_impl<T>()(bind_info_function(f, o, j), i);
  204. }
  205. template<class T, class Function, class Object, class Info, class AuxInfo>
  206. inline T get_object_info(Function f, Object o, Info i, AuxInfo j, const size_t k, const void * l)
  207. {
  208. return get_object_info_impl<T>()(bind_info_function(f, o, j), i, k, l);
  209. }
  210. // returns the value type for the clGet*Info() call on Object with Enum.
  211. template<class Object, int Enum>
  212. struct get_object_info_type;
  213. // defines the object::get_info<Enum>() specialization
  214. #define BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATION(object_type, result_type, value) \
  215. namespace detail { \
  216. template<> struct get_object_info_type<object_type, value> { typedef result_type type; }; \
  217. } \
  218. template<> inline result_type object_type::get_info<value>() const \
  219. { \
  220. return get_info<result_type>(value); \
  221. }
  222. // used by BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS()
  223. #define BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_IMPL(r, data, elem) \
  224. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATION( \
  225. data, BOOST_PP_TUPLE_ELEM(2, 0, elem), BOOST_PP_TUPLE_ELEM(2, 1, elem) \
  226. )
  227. // defines the object::get_info<Enum>() specialization for each
  228. // (result_type, value) tuple in seq for object_type.
  229. #define BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(object_type, seq) \
  230. BOOST_PP_SEQ_FOR_EACH( \
  231. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_IMPL, object_type, seq \
  232. )
  233. } // end detail namespace
  234. } // end compute namespace
  235. } // end boost namespace
  236. #endif // BOOST_COMPUTE_DETAIL_GET_OBJECT_INFO_HPP