ViennaCL - The Vienna Computing Library  1.5.1
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-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #ifdef __APPLE__
26 #include <OpenCL/cl.h>
27 #else
28 #include <CL/cl.h>
29 #endif
30 
31 #include <vector>
32 #include "viennacl/ocl/forwards.h"
33 #include "viennacl/ocl/device.hpp"
34 
35 namespace viennacl
36 {
37  namespace ocl
38  {
39 
45  class platform
46  {
47 
48  public:
49  platform(vcl_size_t pf_index = 0)
50  {
51  cl_int err;
52  cl_uint num_platforms;
53  cl_platform_id ids[42]; //no more than 42 platforms supported...
54  #if defined(VIENNACL_DEBUG_ALL)
55  std::cout << "ViennaCL: Getting platform..." << std::endl;
56  #endif
57  err = clGetPlatformIDs(42, ids, &num_platforms);
58  VIENNACL_ERR_CHECK(err);
59  assert(num_platforms > pf_index && bool("ViennaCL: ERROR: Not enough platforms found!"));
60  id_ = ids[pf_index];
61  assert(num_platforms > 0 && bool("ViennaCL: ERROR: No platform found!"));
62  }
63 
64  platform(cl_platform_id pf_id) : id_(pf_id) {}
65 
66  platform(platform const & other) : id_(other.id_) {}
67 
68  void operator=(cl_platform_id pf_id)
69  {
70  id_ = pf_id;
71  }
72 
73  cl_platform_id id() const
74  {
75  return id_;
76  }
77 
79  std::string info() const
80  {
81  char buffer[1024];
82  cl_int err;
83  err = clGetPlatformInfo(id_, CL_PLATFORM_VENDOR, 1024 * sizeof(char), buffer, NULL);
84  VIENNACL_ERR_CHECK(err);
85 
86  std::stringstream ss;
87  ss << buffer << ": ";
88 
89  err = clGetPlatformInfo(id_, CL_PLATFORM_VERSION, 1024 * sizeof(char), buffer, NULL);
90  VIENNACL_ERR_CHECK(err);
91 
92  ss << buffer;
93 
94  return ss.str();
95  }
96 
98 
99  std::vector<device> devices(cl_device_type dtype = CL_DEVICE_TYPE_DEFAULT)
100  {
101  cl_int err;
102  #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
103  std::cout << "ViennaCL: Querying devices available at current platform." << std::endl;
104  #endif
105  cl_device_id device_ids[VIENNACL_OCL_MAX_DEVICE_NUM];
106  cl_uint num_devices;
107  err = clGetDeviceIDs(id_, dtype, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
108  if (err == CL_DEVICE_NOT_FOUND && dtype == CL_DEVICE_TYPE_DEFAULT)
109  {
110  //workaround for ATI Stream SDK v2.3: No CPUs detected with default device type:
111  err = clGetDeviceIDs(id_, CL_DEVICE_TYPE_CPU, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
112  }
113 
114  VIENNACL_ERR_CHECK(err);
115  #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
116  std::cout << "ViennaCL: Found " << num_devices << " devices." << std::endl;
117  #endif
118 
119  assert(num_devices > 0 && bool("Error in viennacl::ocl::platform::devices(): No OpenCL devices available!"));
120  std::vector<device> devices;
121 
122  for (cl_uint i=0; i<num_devices; ++i)
123  devices.push_back(device(device_ids[i]));
124 
125  return devices;
126  }
127 
128  private:
129  cl_platform_id id_;
130  };
131 
132 
133 
134  inline std::vector< platform > get_platforms()
135  {
136  std::vector< platform > ret;
137  cl_int err;
138  cl_uint num_platforms;
139  cl_platform_id ids[42]; //no more than 42 platforms supported...
140  #if defined(VIENNACL_DEBUG_ALL)
141  std::cout << "ViennaCL: Getting platform..." << std::endl;
142  #endif
143  err = clGetPlatformIDs(42, ids, &num_platforms);
144  VIENNACL_ERR_CHECK(err);
145 
146  for (cl_uint i = 0; i < num_platforms; ++i)
147  ret.push_back( platform(ids[i]) );
148 
149  return ret;
150  }
151  }
152 }
153 
154 #endif
This file provides the forward declarations for the OpenCL layer of ViennaCL.
std::size_t vcl_size_t
Definition: forwards.h:58
std::vector< platform > get_platforms()
Definition: platform.hpp:134
Represents an OpenCL device within ViennaCL.
Wrapper class for an OpenCL platform.
Definition: platform.hpp:45
platform(vcl_size_t pf_index=0)
Definition: platform.hpp:49
std::string info() const
Returns an information string.
Definition: platform.hpp:79
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
cl_platform_id id() const
Definition: platform.hpp:73
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:655
platform(cl_platform_id pf_id)
Definition: platform.hpp:64
std::vector< device > devices(cl_device_type dtype=CL_DEVICE_TYPE_DEFAULT)
Returns the available devices of the supplied device type.
Definition: platform.hpp:99
#define VIENNACL_OCL_MAX_DEVICE_NUM
Definition: device_utils.hpp:25
void operator=(cl_platform_id pf_id)
Definition: platform.hpp:68
platform(platform const &other)
Definition: platform.hpp:66