ViennaCL - The Vienna Computing Library  1.5.0
viennacl/ocl/platform.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_OCL_PLATFORM_HPP_
00002 #define VIENNACL_OCL_PLATFORM_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2013, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00025 #ifdef __APPLE__
00026 #include <OpenCL/cl.h>
00027 #else
00028 #include <CL/cl.h>
00029 #endif
00030 
00031 #include <vector>
00032 #include "viennacl/ocl/forwards.h"
00033 #include "viennacl/ocl/device.hpp"
00034 
00035 namespace viennacl
00036 {
00037   namespace ocl
00038   {
00039 
00045     class platform
00046     {
00047 
00048       public:
00049         platform(vcl_size_t pf_index = 0)
00050         {
00051           cl_int err;
00052           cl_uint num_platforms;
00053           cl_platform_id ids[42];   //no more than 42 platforms supported...
00054           #if defined(VIENNACL_DEBUG_ALL)
00055           std::cout << "ViennaCL: Getting platform..." << std::endl;
00056           #endif
00057           err = clGetPlatformIDs(42, ids, &num_platforms);
00058           VIENNACL_ERR_CHECK(err);
00059           assert(num_platforms > pf_index && bool("ViennaCL: ERROR: Not enough platforms found!"));
00060           id_ = ids[pf_index];
00061           assert(num_platforms > 0 && bool("ViennaCL: ERROR: No platform found!"));
00062         }
00063 
00064         platform(cl_platform_id pf_id) : id_(pf_id) {}
00065 
00066         platform(platform const & other) : id_(other.id_) {}
00067 
00068         void operator=(cl_platform_id pf_id)
00069         {
00070           id_ = pf_id;
00071         }
00072 
00073         cl_platform_id id() const
00074         {
00075           return id_;
00076         }
00077 
00079         std::string info() const
00080         {
00081           char buffer[1024];
00082           cl_int err;
00083           err = clGetPlatformInfo(id_, CL_PLATFORM_VENDOR, 1024 * sizeof(char), buffer, NULL);
00084           VIENNACL_ERR_CHECK(err);
00085 
00086           std::stringstream ss;
00087           ss << buffer << ": ";
00088 
00089           err = clGetPlatformInfo(id_, CL_PLATFORM_VERSION, 1024 * sizeof(char), buffer, NULL);
00090           VIENNACL_ERR_CHECK(err);
00091 
00092           ss << buffer;
00093 
00094           return ss.str();
00095         }
00096 
00098 
00099         std::vector<device> devices(cl_device_type dtype = CL_DEVICE_TYPE_DEFAULT)
00100         {
00101           cl_int err;
00102           #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
00103           std::cout << "ViennaCL: Querying devices available at current platform." << std::endl;
00104           #endif
00105           cl_device_id device_ids[VIENNACL_OCL_MAX_DEVICE_NUM];
00106           cl_uint num_devices;
00107           err = clGetDeviceIDs(id_, dtype, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
00108           if (err == CL_DEVICE_NOT_FOUND && dtype == CL_DEVICE_TYPE_DEFAULT)
00109           {
00110             //workaround for ATI Stream SDK v2.3: No CPUs detected with default device type:
00111             err = clGetDeviceIDs(id_, CL_DEVICE_TYPE_CPU, VIENNACL_OCL_MAX_DEVICE_NUM, device_ids, &num_devices);
00112           }
00113 
00114           VIENNACL_ERR_CHECK(err);
00115           #if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
00116           std::cout << "ViennaCL: Found " << num_devices << " devices." << std::endl;
00117           #endif
00118 
00119           assert(num_devices > 0 && bool("Error in viennacl::ocl::platform::devices(): No OpenCL devices available!"));
00120           std::vector<device> devices;
00121 
00122           for (cl_uint i=0; i<num_devices; ++i)
00123             devices.push_back(device(device_ids[i]));
00124 
00125           return devices;
00126         }
00127 
00128       private:
00129         cl_platform_id id_;
00130     };
00131 
00132 
00133 
00134     inline std::vector< platform > get_platforms()
00135     {
00136       std::vector< platform > ret;
00137       cl_int err;
00138       cl_uint num_platforms;
00139       cl_platform_id ids[42];   //no more than 42 platforms supported...
00140       #if defined(VIENNACL_DEBUG_ALL)
00141       std::cout << "ViennaCL: Getting platform..." << std::endl;
00142       #endif
00143       err = clGetPlatformIDs(42, ids, &num_platforms);
00144       VIENNACL_ERR_CHECK(err);
00145 
00146       for (cl_uint i = 0; i < num_platforms; ++i)
00147         ret.push_back( platform(ids[i]) );
00148 
00149       return ret;
00150     }
00151   }
00152 }
00153 
00154 #endif