Go to the documentation of this file.00001 #ifndef VIENNACL_OCL_PLATFORM_HPP_
00002 #define VIENNACL_OCL_PLATFORM_HPP_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00024 #ifdef __APPLE__
00025 #include <OpenCL/cl.h>
00026 #else
00027 #include <CL/cl.h>
00028 #endif
00029
00030 #include <vector>
00031 #include "viennacl/ocl/forwards.h"
00032 #include "viennacl/ocl/device.hpp"
00033
00034 namespace viennacl
00035 {
00036 namespace ocl
00037 {
00038 class platform
00039 {
00040
00041 public:
00042 platform()
00043 {
00044 cl_int err;
00045 cl_uint num_platforms;
00046 cl_platform_id ids[3];
00047 #if defined(VIENNACL_DEBUG_ALL)
00048 std::cout << "ViennaCL: Getting platform..." << std::endl;
00049 #endif
00050 err = clGetPlatformIDs(1, ids, &num_platforms);
00051 VIENNACL_ERR_CHECK(err);
00052 id_ = ids[0];
00053 assert(num_platforms > 0 && "ViennaCL: ERROR: No platform found!");
00054 }
00055
00056 cl_platform_id id() const
00057 {
00058 return id_;
00059 }
00060
00062 std::string info() const
00063 {
00064 char buffer[1024];
00065 cl_int err;
00066 err = clGetPlatformInfo(id_, CL_PLATFORM_VENDOR, 1024 * sizeof(char), buffer, NULL);
00067 VIENNACL_ERR_CHECK(err);
00068
00069 std::stringstream ss;
00070 ss << buffer << ": ";
00071
00072 err = clGetPlatformInfo(id_, CL_PLATFORM_VERSION, 1024 * sizeof(char), buffer, NULL);
00073 VIENNACL_ERR_CHECK(err);
00074
00075 ss << buffer;
00076
00077 return ss.str();
00078 }
00079
00081
00082 std::vector<device> devices(cl_device_type dtype = CL_DEVICE_TYPE_DEFAULT)
00083 {
00084 cl_int err;
00085 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
00086 std::cout << "ViennaCL: Querying devices available at current platform." << std::endl;
00087 #endif
00088 cl_device_id device_ids[VIENNACL_OCL_MAX_DEVICE_NUM];
00089 cl_uint num_devices;
00090 err = clGetDeviceIDs(id_, dtype, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
00091 if (err == CL_DEVICE_NOT_FOUND && dtype == CL_DEVICE_TYPE_DEFAULT)
00092 {
00093
00094 err = clGetDeviceIDs(id_, CL_DEVICE_TYPE_CPU, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
00095 }
00096
00097 VIENNACL_ERR_CHECK(err);
00098 #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
00099 std::cout << "ViennaCL: Found " << num_devices << " devices." << std::endl;
00100 #endif
00101
00102 assert(num_devices > 0 && "Error in viennacl::ocl::platform::devices(): No OpenCL devices available!");
00103 std::vector<device> devices;
00104
00105 for (cl_uint i=0; i<num_devices; ++i)
00106 devices.push_back(device(device_ids[i]));
00107
00108 return devices;
00109 }
00110
00111 private:
00112 cl_platform_id id_;
00113 };
00114
00115 }
00116 }
00117
00118 #endif