ViennaCL - The Vienna Computing Library  1.2.0
platform.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_OCL_PLATFORM_HPP_
2 #define VIENNACL_OCL_PLATFORM_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, 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 
30 #include <vector>
31 #include "viennacl/ocl/forwards.h"
32 #include "viennacl/ocl/device.hpp"
33 
34 namespace viennacl
35 {
36  namespace ocl
37  {
38  class platform
39  {
40 
41  public:
43  {
44  cl_int err;
45  cl_uint num_platforms;
46  cl_platform_id ids[3];
47  #if defined(VIENNACL_DEBUG_ALL)
48  std::cout << "ViennaCL: Getting platform..." << std::endl;
49  #endif
50  err = clGetPlatformIDs(1, ids, &num_platforms);
51  VIENNACL_ERR_CHECK(err);
52  id_ = ids[0];
53  assert(num_platforms > 0 && "ViennaCL: ERROR: No platform found!");
54  }
55 
56  cl_platform_id id() const
57  {
58  return id_;
59  }
60 
62  std::string info() const
63  {
64  char buffer[1024];
65  cl_int err;
66  err = clGetPlatformInfo(id_, CL_PLATFORM_VENDOR, 1024 * sizeof(char), buffer, NULL);
67  VIENNACL_ERR_CHECK(err);
68 
69  std::stringstream ss;
70  ss << buffer << ": ";
71 
72  err = clGetPlatformInfo(id_, CL_PLATFORM_VERSION, 1024 * sizeof(char), buffer, NULL);
73  VIENNACL_ERR_CHECK(err);
74 
75  ss << buffer;
76 
77  return ss.str();
78  }
79 
81 
82  std::vector<device> devices(cl_device_type dtype = CL_DEVICE_TYPE_DEFAULT)
83  {
84  cl_int err;
85  #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
86  std::cout << "ViennaCL: Querying devices available at current platform." << std::endl;
87  #endif
88  cl_device_id device_ids[VIENNACL_OCL_MAX_DEVICE_NUM];
89  cl_uint num_devices;
90  err = clGetDeviceIDs(id_, dtype, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
91  if (err == CL_DEVICE_NOT_FOUND && dtype == CL_DEVICE_TYPE_DEFAULT)
92  {
93  //workaround for ATI Stream SDK v2.3: No CPUs detected with default device type:
94  err = clGetDeviceIDs(id_, CL_DEVICE_TYPE_CPU, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
95  }
96 
97  VIENNACL_ERR_CHECK(err);
98  #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
99  std::cout << "ViennaCL: Found " << num_devices << " devices." << std::endl;
100  #endif
101 
102  assert(num_devices > 0 && "Error in viennacl::ocl::platform::devices(): No OpenCL devices available!");
103  std::vector<device> devices;
104 
105  for (cl_uint i=0; i<num_devices; ++i)
106  devices.push_back(device(device_ids[i]));
107 
108  return devices;
109  }
110 
111  private:
112  cl_platform_id id_;
113  };
114 
115  }
116 }
117 
118 #endif