ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
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-2015, 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 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  platform(cl_platform_id pf_id) : id_(pf_id) {}
64  platform(platform const & other) : id_(other.id_) {}
65 
66  void operator=(cl_platform_id pf_id) { id_ = pf_id; }
67 
68  cl_platform_id id() const { return id_; }
69 
71  std::string info() const
72  {
73  char buffer[1024];
74  cl_int err;
75  err = clGetPlatformInfo(id_, CL_PLATFORM_VENDOR, 1024 * sizeof(char), buffer, NULL);
76  VIENNACL_ERR_CHECK(err);
77 
78  std::stringstream ss;
79  ss << buffer << ": ";
80 
81  err = clGetPlatformInfo(id_, CL_PLATFORM_VERSION, 1024 * sizeof(char), buffer, NULL);
82  VIENNACL_ERR_CHECK(err);
83 
84  ss << buffer;
85 
86  return ss.str();
87  }
88 
90 
91  std::vector<device> devices(cl_device_type dtype = CL_DEVICE_TYPE_DEFAULT)
92  {
93  cl_int err;
94 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
95  std::cout << "ViennaCL: Querying devices available at current platform." << std::endl;
96 #endif
97  cl_device_id device_ids[VIENNACL_OCL_MAX_DEVICE_NUM];
98  cl_uint num_devices;
99  err = clGetDeviceIDs(id_, dtype, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
100  if (err == CL_DEVICE_NOT_FOUND && dtype == CL_DEVICE_TYPE_DEFAULT)
101  {
102  //workaround for ATI Stream SDK v2.3: No CPUs detected with default device type:
103  err = clGetDeviceIDs(id_, CL_DEVICE_TYPE_CPU, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
104  }
105 
106  VIENNACL_ERR_CHECK(err);
107 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
108  std::cout << "ViennaCL: Found " << num_devices << " devices." << std::endl;
109 #endif
110 
111  assert(num_devices > 0 && bool("Error in viennacl::ocl::platform::devices(): No OpenCL devices available!"));
112  std::vector<device> devices;
113 
114  for (cl_uint i=0; i<num_devices; ++i)
115  devices.push_back(device(device_ids[i]));
116 
117  return devices;
118  }
119 
120  private:
121  cl_platform_id id_;
122  };
123 
124  inline std::vector< platform > get_platforms()
125  {
126  std::vector< platform > ret;
127  cl_int err;
128  cl_uint num_platforms;
129  cl_platform_id ids[42]; //no more than 42 platforms supported...
130 #if defined(VIENNACL_DEBUG_ALL)
131  std::cout << "ViennaCL: Getting platform..." << std::endl;
132 #endif
133  err = clGetPlatformIDs(42, ids, &num_platforms);
134  VIENNACL_ERR_CHECK(err);
135 
136  for (cl_uint i = 0; i < num_platforms; ++i)
137  ret.push_back( platform(ids[i]) );
138 
139  return ret;
140  }
141 
142 }
143 }
144 
145 #endif
This file provides the forward declarations for the OpenCL layer of ViennaCL.
std::vector< platform > get_platforms()
Definition: platform.hpp:124
Represents an OpenCL device within ViennaCL.
Wrapper class for an OpenCL platform.
Definition: platform.hpp:45
std::vector< device > devices(cl_device_type dtype=CL_DEVICE_TYPE_DEFAULT)
Returns the available devices of the supplied device type.
Definition: platform.hpp:91
A class representing a compute device (e.g. a GPU)
Definition: device.hpp:49
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:681
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
platform(platform const &other)
Definition: platform.hpp:64
cl_platform_id id() const
Definition: platform.hpp:68
std::size_t vcl_size_t
Definition: forwards.h:75
#define VIENNACL_OCL_MAX_DEVICE_NUM
void operator=(cl_platform_id pf_id)
Definition: platform.hpp:66
platform(vcl_size_t pf_index=0)
Definition: platform.hpp:49
std::string info() const
Returns an information string.
Definition: platform.hpp:71
platform(cl_platform_id pf_id)
Definition: platform.hpp:63