ViennaCL - The Vienna Computing Library  1.5.1
infos.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_OCL_INFOS_HPP_
2 #define VIENNACL_OCL_INFOS_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2012, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 #ifdef __APPLE__
25 #include <OpenCL/cl.h>
26 #else
27 #include <CL/cl.h>
28 #endif
29 #include <vector>
30 #include "viennacl/ocl/forwards.h"
31 #include "viennacl/ocl/error.hpp"
32 
33 namespace viennacl{
34 
35  namespace ocl{
36 
38  namespace detail{
39 
41  template<typename T>
42  struct info;
43 
45  template<>
46  struct info<cl_mem>{
47  typedef cl_mem_info type;
48  static void get(cl_mem mem, cl_mem_info param_name,size_t param_value_size,void *param_value,size_t *param_value_size_ret){
49  cl_int err = clGetMemObjectInfo(mem,param_name,param_value_size,param_value,param_value_size_ret);
50  VIENNACL_ERR_CHECK(err);
51  }
52  };
53 
54  template<>
55  struct info<cl_device_id>{
56  typedef cl_device_info type;
57  static void get(cl_device_id device, cl_device_info param_name,size_t param_value_size,void *param_value,size_t *param_value_size_ret){
58  cl_int err = clGetDeviceInfo(device,param_name,param_value_size,param_value,param_value_size_ret);
59  VIENNACL_ERR_CHECK(err);
60  }
61  };
62 
63  template<>
64  struct info<cl_kernel>{
65  typedef cl_kernel_info type;
66  static void get(cl_kernel kernel, cl_kernel_info param_name,size_t param_value_size,void *param_value,size_t *param_value_size_ret){
67  cl_int err = clGetKernelInfo(kernel,param_name,param_value_size,param_value,param_value_size_ret);
68  VIENNACL_ERR_CHECK(err);
69  }
70 
71  static void get(cl_kernel kernel, cl_device_id dev_id, cl_kernel_work_group_info param_name,size_t param_value_size,void *param_value,size_t *param_value_size_ret){
72  cl_int err = clGetKernelWorkGroupInfo(kernel, dev_id, param_name,param_value_size,param_value,param_value_size_ret);
73  VIENNACL_ERR_CHECK(err);
74  }
75  };
76 
77  template<>
78  struct info<cl_context>{
79  typedef cl_context_info type;
80  static void get(cl_context context, cl_context_info param_name,size_t param_value_size,void *param_value,size_t *param_value_size_ret){
81  cl_int err = clGetContextInfo(context,param_name,param_value_size,param_value,param_value_size_ret);
82  VIENNACL_ERR_CHECK(err);
83  }
84  };
85 
86  template<>
87  struct info<cl_program>{
88  typedef cl_program_info type;
89  static void get(cl_program context, cl_program_info param_name,size_t param_value_size,void *param_value,size_t *param_value_size_ret){
90  cl_int err = clGetProgramInfo(context,param_name,param_value_size,param_value,param_value_size_ret);
91  VIENNACL_ERR_CHECK(err);
92  }
93  };
94 
95  template<class RES_T>
96  struct get_info_impl{
97 
98  template<class MEM_T, class INFO_T>
99  RES_T operator()(MEM_T const & mem, INFO_T const & info){
100  RES_T res;
101  detail::info<MEM_T>::get(mem,info,sizeof(RES_T),&res,NULL);
102  return res;
103  }
104 
105  template<class MEM_T, class MEM2_T, class INFO_T>
106  RES_T operator()(MEM_T const & mem, MEM2_T const & mem2, INFO_T const & info){
107  RES_T res;
108  detail::info<MEM_T>::get(mem,mem2, info,sizeof(RES_T),&res,NULL);
109  return res;
110  }
111  };
112 
113  template<>
114  struct get_info_impl<std::string>{
115 
116  template<class MEM_T, class INFO_T>
117  std::string operator()(const MEM_T &mem, const INFO_T &info){
118  char buff[1024];
119  detail::info<MEM_T>::get(mem,info,1024,buff,NULL);
120  return std::string(buff);
121  }
122  };
123 
124  template<class T>
125  struct get_info_impl<std::vector<T> >{
126  template<class MEM_T, class INFO_T>
127  std::vector<T> operator()(const MEM_T &mem, const INFO_T &info){
128  size_t vec_size;
129  detail::info<MEM_T>::get(mem,info,0,NULL,&vec_size);
130  std::vector<T> res(vec_size/sizeof(T));
131  detail::info<MEM_T>::get(mem,info,vec_size,res.data(),NULL);
132  return res;
133  }
134  };
135 
136  template<typename T, typename info<T>::type param>
137  struct return_type;
141  #define SET_INFO_RETURN_TYPE(DATA_TYPE,NAME,RETURN_TYPE) template<> struct return_type<DATA_TYPE, NAME> { typedef RETURN_TYPE Result; }
142 
143  SET_INFO_RETURN_TYPE(cl_mem,CL_MEM_TYPE, cl_mem_object_type);
144  SET_INFO_RETURN_TYPE(cl_mem,CL_MEM_FLAGS, cl_mem_flags);
145  SET_INFO_RETURN_TYPE(cl_mem,CL_MEM_SIZE, size_t);
146  SET_INFO_RETURN_TYPE(cl_mem,CL_MEM_HOST_PTR, void*);
147  SET_INFO_RETURN_TYPE(cl_mem,CL_MEM_MAP_COUNT, cl_uint);
148  SET_INFO_RETURN_TYPE(cl_mem,CL_MEM_REFERENCE_COUNT, cl_uint);
149  SET_INFO_RETURN_TYPE(cl_mem,CL_MEM_CONTEXT, cl_context);
150 
151  SET_INFO_RETURN_TYPE(cl_program,CL_PROGRAM_REFERENCE_COUNT,cl_uint);
152 
153  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_ADDRESS_BITS, cl_uint);
154  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_AVAILABLE, cl_bool);
155  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_COMPILER_AVAILABLE, cl_bool);
156 // SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_DOUBLE_FP_CONFIG, cl_device_fp_config);
157  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_ENDIAN_LITTLE, cl_bool);
158  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_ERROR_CORRECTION_SUPPORT, cl_bool);
159  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_EXECUTION_CAPABILITIES, cl_device_exec_capabilities);
160  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_EXTENSIONS, std::string);
161  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_GLOBAL_MEM_CACHE_SIZE, cl_ulong);
162  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, cl_uint);
163  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_GLOBAL_MEM_SIZE, cl_ulong);
164 // SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_HALF_FP_CONFIG, cl_device_fp_config);
165  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_IMAGE_SUPPORT, cl_bool);
166  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_IMAGE2D_MAX_HEIGHT , size_t);
167  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_IMAGE2D_MAX_WIDTH , size_t);
168  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_IMAGE3D_MAX_DEPTH , size_t);
169  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_IMAGE3D_MAX_HEIGHT , size_t);
170  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_IMAGE3D_MAX_WIDTH , size_t);
171  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_LOCAL_MEM_SIZE, cl_ulong);
172  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_LOCAL_MEM_TYPE, cl_device_local_mem_type);
173  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_CLOCK_FREQUENCY , cl_uint);
174  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_COMPUTE_UNITS , cl_uint); //The minimum value is 1
175  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_CONSTANT_ARGS , cl_uint); //The minimum value is 8
176  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE , cl_ulong); //The minimum value is 64 KB
177  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_MEM_ALLOC_SIZE , cl_ulong); //The minimum value is max (1/4th of CL_DEVICE_GLOBAL_MEM_SIZE, 128*1024*1024)
178  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_PARAMETER_SIZE , size_t);
179  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_READ_IMAGE_ARGS , cl_uint);
180  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_SAMPLERS , cl_uint);
181  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_WORK_GROUP_SIZE , size_t);
182  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS , cl_uint);
183  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_WORK_ITEM_SIZES , std::vector<size_t>);
184  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MAX_WRITE_IMAGE_ARGS , cl_uint);
185  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MEM_BASE_ADDR_ALIGN , cl_uint);
186  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE , cl_uint);
187  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_NAME , std::string);
188  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PLATFORM , cl_platform_id);
189  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR , cl_uint);
190  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT , cl_uint);
191  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT , cl_uint);
192  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT , cl_uint);
193  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE , cl_uint);
194  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PROFILE , std::string);
195  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_PROFILING_TIMER_RESOLUTION , size_t);
196  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_QUEUE_PROPERTIES , cl_command_queue_properties);
197  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_SINGLE_FP_CONFIG , cl_device_fp_config);
198  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_TYPE , cl_device_type);
199  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_VENDOR , std::string);
200  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_VENDOR_ID , cl_uint);
201  SET_INFO_RETURN_TYPE(cl_device_id, CL_DEVICE_VERSION , std::string);
202  SET_INFO_RETURN_TYPE(cl_device_id, CL_DRIVER_VERSION , std::string);
203 
204 
205  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_FUNCTION_NAME, std::string);
206  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_NUM_ARGS, cl_uint);
207  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_REFERENCE_COUNT, cl_uint);
208  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_CONTEXT, cl_context);
209  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_PROGRAM, cl_program);
210 
211 
212  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_WORK_GROUP_SIZE, size_t);
213 // SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_COMPILE_WORK_GROUP_SIZE, size_t[3]);
214  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_LOCAL_MEM_SIZE, cl_ulong);
215  SET_INFO_RETURN_TYPE(cl_kernel,CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, size_t);
216 
217  SET_INFO_RETURN_TYPE(cl_context, CL_CONTEXT_NUM_DEVICES, cl_uint);
218  SET_INFO_RETURN_TYPE(cl_context, CL_CONTEXT_REFERENCE_COUNT, cl_uint);
219  SET_INFO_RETURN_TYPE(cl_context, CL_CONTEXT_PROPERTIES, cl_context_properties);
220 
221  #undef SET_INFO_RETURN_TYPE
222 
224  }
225 
226  template<cl_device_info param>
227  typename detail::return_type<cl_device_id, param>::Result info(cl_device_id const & handle){
228  typedef typename detail::return_type<cl_device_id, param>::Result res_t;
229  return detail::get_info_impl<res_t>()(handle,param);
230  }
231 
232  template<cl_mem_info param>
233  typename detail::return_type<cl_mem, param>::Result info(cl_mem const & handle){
234  typedef typename detail::return_type<cl_mem, param>::Result res_t;
235  return detail::get_info_impl<res_t>()(handle,param);
236  }
237  template<cl_program_info param>
238  typename detail::return_type<cl_program, param>::Result info(cl_program const & handle){
239  typedef typename detail::return_type<cl_program, param>::Result res_t;
240  return detail::get_info_impl<res_t>()(handle,param);
241  }
242 
243 // template<cl_kernel_info param>
244 // typename detail::return_type<cl_kernel, param>::Result info(cl_kernel const & handle){
245 // typedef typename detail::return_type<cl_kernel, param>::Result res_t;
246 // return detail::get_info_impl<res_t>()(handle,param);
247 // }
248 
249 // template<cl_kernel_work_group_info param>
250 // typename detail::return_type<cl_kernel, param>::Result info(cl_kernel const & handle, cl_device_id const & handle2){
251 // typedef typename detail::return_type<cl_kernel, param>::Result res_t;
252 // return detail::get_info_impl<res_t>()(handle,handle2,param);
253 // }
254 
255  template<cl_context_info param>
256  typename detail::return_type<cl_context, param>::Result info(cl_context const & handle){
257  typedef typename detail::return_type<cl_context, param>::Result res_t;
258  return detail::get_info_impl<res_t>()(handle,param);
259  }
260 
261  template<class OCL_TYPE, typename detail::info<OCL_TYPE>::type param>
262  typename detail::return_type<OCL_TYPE, param>::Result info(OCL_TYPE const & handle){
263  return viennacl::ocl::info(handle.get());
264  }
265 
266  }
267 }
268 #endif // INFOS_HPP
This file provides the forward declarations for the OpenCL layer of ViennaCL.
detail::return_type< cl_device_id, param >::Result info(cl_device_id const &handle)
Definition: infos.hpp:227
Helper class for obtaining informations from the OpenCL backend. Deprecated!
Definition: infos.hpp:42
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:655
Error handling for the OpenCL layer of ViennaCL.
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:41
Handle class the effectively represents a smart pointer for OpenCL handles.
Definition: forwards.h:51