Driver: some cleaning

* automatic generation of comparison operators
* better comments
This commit is contained in:
Philippe Tillet
2016-05-31 01:29:31 -04:00
parent 960cb59057
commit fdc6ff7907
16 changed files with 81 additions and 84 deletions

View File

@@ -35,7 +35,7 @@ namespace driver
{ {
// Buffer // Buffer
class ISAACAPI Buffer class ISAACAPI Buffer: public has_handle_comparators<Buffer>
{ {
public: public:
typedef HANDLE_TYPE(cl_mem, CUdeviceptr) handle_type; typedef HANDLE_TYPE(cl_mem, CUdeviceptr) handle_type;
@@ -43,7 +43,7 @@ public:
private: private:
friend class CommandQueue; friend class CommandQueue;
friend class Kernel; friend class Kernel;
//Wrapper to get CUDA context from Memory
static CUcontext context(CUdeviceptr h) static CUcontext context(CUdeviceptr h)
{ {
CUcontext res; CUcontext res;
@@ -52,15 +52,14 @@ private:
} }
public: public:
//Constructors
Buffer(CUdeviceptr h = 0, bool take_ownership = true); Buffer(CUdeviceptr h = 0, bool take_ownership = true);
Buffer(cl_mem Buffer = 0, bool take_ownership = true); Buffer(cl_mem Buffer = 0, bool take_ownership = true);
Buffer(Context const & context, size_t size); Buffer(Context const & context, size_t size);
Context const & context() const; //Accessors
bool operator<(Buffer const &) const;
bool operator==(Buffer const &) const;
bool operator!=(Buffer const &) const;
handle_type& handle(); handle_type& handle();
handle_type const & handle() const; handle_type const & handle() const;
Context const & context() const;
private: private:
backend_type backend_; backend_type backend_;
Context context_; Context context_;

View File

@@ -41,26 +41,31 @@ class NDRange;
class Buffer; class Buffer;
// Command Queue // Command Queue
class ISAACAPI CommandQueue class ISAACAPI CommandQueue: public has_handle_comparators<CommandQueue>
{ {
public: public:
typedef HANDLE_TYPE(cl_command_queue, CUstream) handle_type; typedef HANDLE_TYPE(cl_command_queue, CUstream) handle_type;
public: public:
//Constructors
CommandQueue(cl_command_queue const & queue, bool take_ownership = true); CommandQueue(cl_command_queue const & queue, bool take_ownership = true);
CommandQueue(Context const & context, Device const & device, cl_command_queue_properties properties = 0); CommandQueue(Context const & context, Device const & device, cl_command_queue_properties properties = 0);
//Accessors
handle_type & handle();
handle_type const & handle() const;
backend_type backend() const; backend_type backend() const;
Context const & context() const; Context const & context() const;
Device const & device() const; Device const & device() const;
//Synchronize
void synchronize(); void synchronize();
//Profiling
void enable_profiling(); void enable_profiling();
void disable_profiling(); void disable_profiling();
//Enqueue calls
void enqueue(Kernel const & kernel, NDRange global, driver::NDRange local, std::vector<Event> const *, Event *event); void enqueue(Kernel const & kernel, NDRange global, driver::NDRange local, std::vector<Event> const *, Event *event);
void write(Buffer const & buffer, bool blocking, std::size_t offset, std::size_t size, void const* ptr); void write(Buffer const & buffer, bool blocking, std::size_t offset, std::size_t size, void const* ptr);
void read(Buffer const & buffer, bool blocking, std::size_t offset, std::size_t size, void* ptr); void read(Buffer const & buffer, bool blocking, std::size_t offset, std::size_t size, void* ptr);
bool operator==(CommandQueue const & other) const;
bool operator<(CommandQueue const & other) const;
handle_type& handle();
private: private:
backend_type backend_; backend_type backend_;
Context context_; Context context_;

View File

@@ -35,7 +35,7 @@ namespace isaac
namespace driver namespace driver
{ {
class ISAACAPI Context class ISAACAPI Context: public has_handle_comparators<Context>
{ {
friend class Program; friend class Program;
friend class CommandQueue; friend class CommandQueue;
@@ -55,16 +55,15 @@ private:
} }
public: public:
//Constructors
explicit Context(CUcontext const & context, bool take_ownership = true); explicit Context(CUcontext const & context, bool take_ownership = true);
explicit Context(cl_context const & context, bool take_ownership = true); explicit Context(cl_context const & context, bool take_ownership = true);
explicit Context(Device const & device); explicit Context(Device const & device);
//Accessors
backend_type backend() const; backend_type backend() const;
Device const & device() const; Device const & device() const;
bool operator==(Context const &) const; handle_type const & handle() const;
bool operator<(Context const &) const;
handle_type const & handle() const { return h_; }
private: private:
DISABLE_MSVC_WARNING_C4251 DISABLE_MSVC_WARNING_C4251
backend_type backend_; backend_type backend_;

View File

@@ -34,7 +34,7 @@ namespace driver
{ {
// Device // Device
class ISAACAPI Device class ISAACAPI Device: public has_handle_comparators<Device>
{ {
private: private:
friend class Context; friend class Context;
@@ -43,6 +43,7 @@ private:
public: public:
typedef HANDLE_TYPE(cl_device_id, CUdevice) handle_type; typedef HANDLE_TYPE(cl_device_id, CUdevice) handle_type;
//Supported types
enum Type enum Type
{ {
GPU = CL_DEVICE_TYPE_GPU, GPU = CL_DEVICE_TYPE_GPU,
@@ -50,7 +51,7 @@ public:
ACCELERATOR = CL_DEVICE_TYPE_ACCELERATOR, ACCELERATOR = CL_DEVICE_TYPE_ACCELERATOR,
UNKNOWN UNKNOWN
}; };
//Supported vendors
enum class Vendor enum class Vendor
{ {
AMD, AMD,
@@ -58,7 +59,7 @@ public:
NVIDIA, NVIDIA,
UNKNOWN UNKNOWN
}; };
//Supported architectures
enum class Architecture enum class Architecture
{ {
//Intel //Intel
@@ -85,22 +86,21 @@ public:
}; };
private: private:
//Metaprogramming elper to get cuda info from attribute
template<CUdevice_attribute attr> template<CUdevice_attribute attr>
int cuGetInfo() const; int cuGetInfo() const;
public: public:
//Constructors
explicit Device(CUdevice const & device, bool take_ownership = true); explicit Device(CUdevice const & device, bool take_ownership = true);
explicit Device(cl_device_id const & device, bool take_ownership = true); explicit Device(cl_device_id const & device, bool take_ownership = true);
//Accessors
bool operator==(Device const &) const; handle_type const & handle() const;
bool operator<(Device const &) const;
Vendor vendor() const; Vendor vendor() const;
Architecture architecture() const; Architecture architecture() const;
std::string infos() const;
backend_type backend() const; backend_type backend() const;
//Informations
std::string infos() const;
size_t clock_rate() const; size_t clock_rate() const;
unsigned int address_bits() const; unsigned int address_bits() const;
driver::Platform platform() const; driver::Platform platform() const;

View File

@@ -33,18 +33,22 @@ namespace driver
{ {
// Event // Event
class ISAACAPI Event class ISAACAPI Event: public has_handle_comparators<Event>
{ {
private: private:
friend class CommandQueue; friend class CommandQueue;
public: public:
typedef HANDLE_TYPE(cl_event, cu_event_t) handle_type; typedef HANDLE_TYPE(cl_event, cu_event_t) handle_type;
public: public:
//Constructors
Event(cl_event const & event, bool take_ownership = true); Event(cl_event const & event, bool take_ownership = true);
Event(backend_type backend); Event(backend_type backend);
//Accessors
handle_type const & handle() const;
//Profiling
long elapsed_time() const; long elapsed_time() const;
handle_type& handle();
private: private:
backend_type backend_; backend_type backend_;

View File

@@ -33,11 +33,11 @@ namespace isaac
namespace driver namespace driver
{ {
struct cu_event_t{ struct cu_event_t{
operator bool() const { return first && second; } operator bool() const { return first && second; }
CUevent first; CUevent first;
CUevent second; CUevent second;
}; };
#define HANDLE_TYPE(CLTYPE, CUTYPE) Handle<CLTYPE, CUTYPE> #define HANDLE_TYPE(CLTYPE, CUTYPE) Handle<CLTYPE, CUTYPE>
@@ -64,11 +64,14 @@ private:
static void release(cl_program x); static void release(cl_program x);
public: public:
//Constructors
Handle(backend_type backend, bool take_ownership = true); Handle(backend_type backend, bool take_ownership = true);
backend_type backend() const; //Comparison
bool operator==(Handle const & other) const; bool operator==(Handle const & other) const;
bool operator!=(Handle const & other) const; bool operator!=(Handle const & other) const;
bool operator<(Handle const & other) const; bool operator<(Handle const & other) const;
//Accessors
backend_type backend() const;
CLType & cl(); CLType & cl();
CLType const & cl() const; CLType const & cl() const;
CUType & cu(); CUType & cu();
@@ -80,11 +83,22 @@ DISABLE_MSVC_WARNING_C4251
std::shared_ptr<CLType> cl_; std::shared_ptr<CLType> cl_;
std::shared_ptr<CUType> cu_; std::shared_ptr<CUType> cu_;
RESTORE_MSVC_WARNING_C4251 RESTORE_MSVC_WARNING_C4251
private: private:
backend_type backend_; backend_type backend_;
bool has_ownership_; bool has_ownership_;
}; };
//Helper for automatic implementation of comparison operators
template<class T>
class has_handle_comparators
{
public:
friend bool operator==(T const & x, T const & y) { return x.handle() == y.handle(); }
friend bool operator!=(T const & x, T const & y) { return x.handle() != y.handle(); }
friend bool operator<(T const & x, T const & y) { return x.handle() < y.handle(); }
};
} }
} }

View File

@@ -39,14 +39,18 @@ namespace driver
class Buffer; class Buffer;
// Kernel // Kernel
class ISAACAPI Kernel class ISAACAPI Kernel: public has_handle_comparators<Kernel>
{ {
friend class CommandQueue; friend class CommandQueue;
public: public:
typedef HANDLE_TYPE(cl_kernel, CUfunction) handle_type; typedef HANDLE_TYPE(cl_kernel, CUfunction) handle_type;
public: public:
//Constructors
Kernel(Program const & program, const char * name); Kernel(Program const & program, const char * name);
//Accessors
handle_type const & handle() const;
//Arguments setters
void setArg(unsigned int index, value_scalar const & scal); void setArg(unsigned int index, value_scalar const & scal);
void setArg(unsigned int index, std::size_t size, void* ptr); void setArg(unsigned int index, std::size_t size, void* ptr);
void setArg(unsigned int index, Buffer const &); void setArg(unsigned int index, Buffer const &);

View File

@@ -38,10 +38,11 @@ class Device;
class ISAACAPI Platform class ISAACAPI Platform
{ {
private:
public: public:
//Constructors
Platform(backend_type); Platform(backend_type);
Platform(cl_platform_id const &); Platform(cl_platform_id const &);
//Accessors
std::string name() const; std::string name() const;
std::string version() const; std::string version() const;
void devices(std::vector<Device> &) const; void devices(std::vector<Device> &) const;

View File

@@ -37,18 +37,20 @@ namespace driver
class Context; class Context;
class Device; class Device;
class ISAACAPI Program class ISAACAPI Program: public has_handle_comparators<Program>
{ {
public: public:
typedef HANDLE_TYPE(cl_program, CUmodule) handle_type; typedef HANDLE_TYPE(cl_program, CUmodule) handle_type;
private: private:
friend class Kernel; friend class Kernel;
public: public:
//Constructors
Program(Context const & context, std::string const & source); Program(Context const & context, std::string const & source);
//Accessors
handle_type const & handle() const;
Context const & context() const; Context const & context() const;
//Comparison operators
bool operator==(Program const & other) const;
bool operator<(Program const & other) const;
private: private:
DISABLE_MSVC_WARNING_C4251 DISABLE_MSVC_WARNING_C4251
backend_type backend_; backend_type backend_;

View File

@@ -35,10 +35,15 @@ namespace driver
class ISAACAPI ProgramCache class ISAACAPI ProgramCache
{ {
friend class backend; friend class backend;
public: public:
//Clearing the cache
void clear(); void clear();
//Adding a program to the cache
Program & add(Context const & context, std::string const & name, std::string const & src); Program & add(Context const & context, std::string const & name, std::string const & src);
//Finding a program in the cache
Program const *find(std::string const & name); Program const *find(std::string const & name);
private: private:
DISABLE_MSVC_WARNING_C4251 DISABLE_MSVC_WARNING_C4251
std::map<std::string, Program> cache_; std::map<std::string, Program> cache_;

View File

@@ -60,15 +60,6 @@ Buffer::Buffer(Context const & context, size_t size) : backend_(context.backend_
Context const & Buffer::context() const Context const & Buffer::context() const
{ return context_; } { return context_; }
bool Buffer::operator==(Buffer const & other) const
{ return h_==other.h_; }
bool Buffer::operator<(Buffer const & other) const
{ return h_<other.h_; }
bool Buffer::operator!=(Buffer const & other) const
{ return h_!=other.h_; }
HANDLE_TYPE(cl_mem, CUdeviceptr) & Buffer::handle() HANDLE_TYPE(cl_mem, CUdeviceptr) & Buffer::handle()
{ return h_; } { return h_; }

View File

@@ -147,13 +147,10 @@ void CommandQueue::read(Buffer const & buffer, bool blocking, std::size_t offset
} }
} }
bool CommandQueue::operator==(CommandQueue const & other) const CommandQueue::handle_type const & CommandQueue::handle() const
{ return h_ == other.h_; } { return h_; }
bool CommandQueue::operator<(CommandQueue const & other) const CommandQueue::handle_type & CommandQueue::handle()
{ return h_ < other.h_; }
HANDLE_TYPE(cl_command_queue, CUstream) & CommandQueue::handle()
{ return h_; } { return h_; }
} }

View File

@@ -86,15 +86,8 @@ Context::Context(Device const & device) : backend_(device.backend_), device_(dev
} }
} }
bool Context::operator==(Context const & other) const Context::handle_type const & Context::handle() const
{ { return h_; }
return h_==other.h_;
}
bool Context::operator<(Context const & other) const
{
return h_<other.h_;
}
Device const & Context::device() const Device const & Context::device() const
{ return device_; } { return device_; }

View File

@@ -53,18 +53,6 @@ Device::Device(cl_device_id const & device, bool take_ownership) : backend_(OPEN
} }
bool Device::operator==(Device const & other) const
{
return h_==other.h_;
}
bool Device::operator<(Device const & other) const
{
return h_<other.h_;
}
Device::Vendor Device::vendor() const Device::Vendor Device::vendor() const
{ {
std::string vname = vendor_str(); std::string vname = vendor_str();

View File

@@ -63,7 +63,7 @@ long Event::elapsed_time() const
} }
} }
HANDLE_TYPE(cl_event, cu_event_t) & Event::handle() Event::handle_type const & Event::handle() const
{ return h_; } { return h_; }
} }

View File

@@ -179,16 +179,11 @@ Program::Program(Context const & context, std::string const & source) : backend_
} }
} }
bool Program::operator==(Program const & other) const Program::handle_type const & Program::handle() const
{ return h_ == other.h_; } { return h_; }
bool Program::operator<(Program const & other) const
{ return h_ < other.h_; }
Context const & Program::context() const Context const & Program::context() const
{ { return context_; }
return context_;
}
} }