/*************************************************************************** ocl_device.h ------------------- W. Michael Brown Utilities for dealing with OpenCL devices __________________________________________________________________________ This file is part of the Geryon Unified Coprocessor Library (UCL) __________________________________________________________________________ begin : Mon Dec 23 2009 copyright : (C) 2009 by W. Michael Brown email : brownw@ornl.gov ***************************************************************************/ /* ----------------------------------------------------------------------- Copyright (2009) Sandia Corporation. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain rights in this software. This software is distributed under the Simplified BSD License. ----------------------------------------------------------------------- */ #ifndef OCL_DEVICE #define OCL_DEVICE #include #include #include #ifdef __APPLE__ #include #include #else #include #include #endif #include "ocl_macros.h" #include "ucl_types.h" namespace ucl_opencl { // -------------------------------------------------------------------------- // - COMMAND QUEUE STUFF // -------------------------------------------------------------------------- typedef cl_command_queue command_queue; typedef cl_context context_type; inline void ucl_sync(cl_command_queue &cq) { CL_SAFE_CALL(clFinish(cq)); } inline bool _shared_mem_device(cl_device_type &device_type) { return (device_type==CL_DEVICE_TYPE_CPU); } struct OCLProperties { std::string name; cl_device_type device_type; cl_ulong global_mem; cl_ulong shared_mem; cl_ulong const_mem; cl_uint compute_units; cl_uint clock; size_t work_group_size; size_t work_item_size[3]; bool double_precision; int alignment; size_t timer_resolution; bool ecc_support; std::string c_version; bool partition_equal, partition_counts, partition_affinity; cl_uint max_sub_devices; }; /// Class for looking at data parallel device properties /** \note Calls to change the device outside of the class results in incorrect * behavior * \note There is no error checking for indexing past the number of devices **/ class UCL_Device { public: /// Collect properties for every device on the node /** \note You must set the active GPU with set() before using the device **/ inline UCL_Device(); inline ~UCL_Device(); /// Return the number of platforms (0 if error or no platforms) inline int num_platforms() { return _num_platforms; } /// Return a string with name and info of the current platform inline std::string platform_name(); /// Delete any contexts/data and set the platform number to be used inline int set_platform(const int pid); /// Return the number of devices that support OpenCL inline int num_devices() { return _num_devices; } /// Set the OpenCL device to the specified device number /** A context and default command queue will be created for the device * * Returns UCL_SUCCESS if successful or UCL_ERROR if the device could not * be allocated for use. clear() is called to delete any contexts and * associated data from previous calls to set(). **/ inline int set(int num); /// Delete any context and associated data stored from a call to set() inline void clear(); /// Get the current device number inline int device_num() { return _device; } /// Returns the context for the current device inline cl_context & context() { return _context; } /// Returns the default stream for the current device inline command_queue & cq() { return cq(_default_cq); } /// Returns the stream indexed by i inline command_queue & cq(const int i) { return _cq[i]; } /// Set the default command queue /** \param i index of the command queue (as added by push_command_queue()) If i is 0, the command queue created with device initialization is used **/ inline void set_command_queue(const int i) { _default_cq=i; } /// Block until all commands in the default stream have completed inline void sync() { sync(_default_cq); } /// Block until all commands in the specified stream have completed inline void sync(const int i) { ucl_sync(cq(i)); } /// Get the number of command queues currently available on device inline int num_queues() { return _cq.size(); } /// Add a command queue for device computations (with profiling enabled) inline void push_command_queue() { cl_int errorv; _cq.push_back(cl_command_queue()); #ifdef CL_VERSION_2_0 cl_queue_properties props[] = {CL_QUEUE_PROPERTIES, CL_QUEUE_PROFILING_ENABLE, 0}; _cq.back()=clCreateCommandQueueWithProperties(_context, _cl_device, props, &errorv); #else _cq.back()=clCreateCommandQueue(_context, _cl_device, CL_QUEUE_PROFILING_ENABLE, &errorv); #endif if (errorv!=CL_SUCCESS) { std::cerr << "Could not create command queue on device: " << name() << std::endl; UCL_GERYON_EXIT; } } /// Remove a stream for device computations /** \note You cannot delete the default stream **/ inline void pop_command_queue() { if (_cq.size()<2) return; CL_SAFE_CALL(clReleaseCommandQueue(_cq.back())); _cq.pop_back(); } /// Get the current OpenCL device name inline std::string name() { return name(_device); } /// Get the OpenCL device name inline std::string name(const int i) { return std::string(_properties[i].name); } /// Get a string telling the type of the current device inline std::string device_type_name() { return device_type_name(_device); } /// Get a string telling the type of the device inline std::string device_type_name(const int i); /// Get current device type (UCL_CPU, UCL_GPU, UCL_ACCELERATOR, UCL_DEFAULT) inline int device_type() { return device_type(_device); } /// Get device type (UCL_CPU, UCL_GPU, UCL_ACCELERATOR, UCL_DEFAULT) inline int device_type(const int i); /// Returns true if host memory is efficiently addressable from device inline bool shared_memory() { return shared_memory(_device); } /// Returns true if host memory is efficiently addressable from device inline bool shared_memory(const int i) { return _shared_mem_device(_properties[i].device_type); } /// Returns true if double precision is support for the current device inline bool double_precision() { return double_precision(_device); } /// Returns true if double precision is support for the device inline bool double_precision(const int i) {return _properties[i].double_precision;} /// Get the number of compute units on the current device inline unsigned cus() { return cus(_device); } /// Get the number of compute units inline unsigned cus(const int i) { return _properties[i].compute_units; } /// Get the gigabytes of global memory in the current device inline double gigabytes() { return gigabytes(_device); } /// Get the gigabytes of global memory inline double gigabytes(const int i) { return static_cast(_properties[i].global_mem)/1073741824; } /// Get the bytes of global memory in the current device inline size_t bytes() { return bytes(_device); } /// Get the bytes of global memory inline size_t bytes(const int i) { return _properties[i].global_mem; } /// Return the GPGPU revision number for current device //inline double revision() { return revision(_device); } /// Return the GPGPU revision number //inline double revision(const int i) // { return //static_cast(_properties[i].minor)/10+_properties[i].major;} /// Clock rate in GHz for current device inline double clock_rate() { return clock_rate(_device); } /// Clock rate in GHz inline double clock_rate(const int i) { return _properties[i].clock*1e-3;} /// Return the address alignment in bytes inline int alignment() { return alignment(_device); } /// Return the address alignment in bytes inline int alignment(const int i) { return _properties[i].alignment; } /// Return the timer resolution inline size_t timer_resolution() { return timer_resolution(_device); } /// Return the timer resolution inline size_t timer_resolution(const int i) { return _properties[i].timer_resolution; } /// Get the maximum number of threads per block inline size_t group_size() { return group_size(_device); } /// Get the maximum number of threads per block inline size_t group_size(const int i) { return _properties[i].work_group_size; } /// Return the maximum memory pitch in bytes for current device inline size_t max_pitch() { return max_pitch(_device); } /// Return the maximum memory pitch in bytes inline size_t max_pitch(const int i) { return 0; } /// Returns false if accelerator cannot be shared by multiple processes /** If it cannot be determined, true is returned **/ inline bool sharing_supported() { return sharing_supported(_device); } /// Returns false if accelerator cannot be shared by multiple processes /** If it cannot be determined, true is returned **/ inline bool sharing_supported(const int i) { return true; } /// True if splitting device into equal subdevices supported inline bool fission_equal() { return fission_equal(_device); } /// True if splitting device into equal subdevices supported inline bool fission_equal(const int i) { return _properties[i].partition_equal; } /// True if splitting device into subdevices by specified counts supported inline bool fission_by_counts() { return fission_by_counts(_device); } /// True if splitting device into subdevices by specified counts supported inline bool fission_by_counts(const int i) { return _properties[i].partition_counts; } /// True if splitting device into subdevices by affinity domains supported inline bool fission_by_affinity() { return fission_by_affinity(_device); } /// True if splitting device into subdevices by affinity domains supported inline bool fission_by_affinity(const int i) { return _properties[i].partition_affinity; } /// Maximum number of subdevices allowed from device fission inline int max_sub_devices() { return max_sub_devices(_device); } /// Maximum number of subdevices allowed from device fission inline int max_sub_devices(const int i) { return _properties[i].max_sub_devices; } /// List all devices along with all properties inline void print_all(std::ostream &out); /// Return the OpenCL type for the device inline cl_device_id & cl_device() { return _cl_device; } /// Select the platform that has accelerators inline int set_platform_accelerator(int pid=-1); private: int _num_platforms; // Number of platforms int _platform; // UCL_Device ID for current platform cl_platform_id _cl_platform; // OpenCL ID for current platform cl_platform_id _cl_platforms[20]; // OpenCL IDs for all platforms cl_context _context; // Context used for accessing the device std::vector _cq;// The default command queue for this device int _device; // UCL_Device ID for current device cl_device_id _cl_device; // OpenCL ID for current device std::vector _cl_devices; // OpenCL IDs for all devices int _num_devices; // Number of devices std::vector _properties; // Properties for each device inline void add_properties(cl_device_id); inline int create_context(); int _default_cq; }; // Grabs the properties for all devices UCL_Device::UCL_Device() { _device=-1; // --- Get Number of Platforms cl_uint nplatforms; cl_int errorv=clGetPlatformIDs(20,_cl_platforms,&nplatforms); if (errorv!=CL_SUCCESS) { _num_platforms=0; return; } else _num_platforms=static_cast(nplatforms); // note that platform 0 may not necessarily be associated with accelerators set_platform_accelerator(); } UCL_Device::~UCL_Device() { clear(); } void UCL_Device::clear() { _properties.clear(); _cl_devices.clear(); if (_device>-1) { for (size_t i=0; i<_cq.size(); i++) { CL_DESTRUCT_CALL(clReleaseCommandQueue(_cq.back())); _cq.pop_back(); } CL_DESTRUCT_CALL(clReleaseContext(_context)); } _device=-1; } int UCL_Device::set_platform(int pid) { clear(); cl_int errorv; _cl_device=0; _device=-1; _num_devices=0; _default_cq=0; #ifdef UCL_DEBUG assert(pid