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

View File

@@ -41,26 +41,31 @@ class NDRange;
class Buffer;
// Command Queue
class ISAACAPI CommandQueue
class ISAACAPI CommandQueue: public has_handle_comparators<CommandQueue>
{
public:
typedef HANDLE_TYPE(cl_command_queue, CUstream) handle_type;
public:
//Constructors
CommandQueue(cl_command_queue const & queue, bool take_ownership = true);
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;
Context const & context() const;
Device const & device() const;
//Synchronize
void synchronize();
//Profiling
void enable_profiling();
void disable_profiling();
//Enqueue calls
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 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:
backend_type backend_;
Context context_;

View File

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

View File

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

View File

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

View File

@@ -33,11 +33,11 @@ namespace isaac
namespace driver
{
struct cu_event_t{
operator bool() const { return first && second; }
CUevent first;
CUevent second;
};
struct cu_event_t{
operator bool() const { return first && second; }
CUevent first;
CUevent second;
};
#define HANDLE_TYPE(CLTYPE, CUTYPE) Handle<CLTYPE, CUTYPE>
@@ -64,11 +64,14 @@ private:
static void release(cl_program x);
public:
//Constructors
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;
//Accessors
backend_type backend() const;
CLType & cl();
CLType const & cl() const;
CUType & cu();
@@ -80,11 +83,22 @@ DISABLE_MSVC_WARNING_C4251
std::shared_ptr<CLType> cl_;
std::shared_ptr<CUType> cu_;
RESTORE_MSVC_WARNING_C4251
private:
backend_type backend_;
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;
// Kernel
class ISAACAPI Kernel
class ISAACAPI Kernel: public has_handle_comparators<Kernel>
{
friend class CommandQueue;
public:
typedef HANDLE_TYPE(cl_kernel, CUfunction) handle_type;
public:
//Constructors
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, std::size_t size, void* ptr);
void setArg(unsigned int index, Buffer const &);

View File

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

View File

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

View File

@@ -35,10 +35,15 @@ namespace driver
class ISAACAPI ProgramCache
{
friend class backend;
public:
//Clearing the cache
void clear();
//Adding a program to the cache
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);
private:
DISABLE_MSVC_WARNING_C4251
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
{ 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()
{ 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
{ return h_ == other.h_; }
CommandQueue::handle_type const & CommandQueue::handle() const
{ return h_; }
bool CommandQueue::operator<(CommandQueue const & other) const
{ return h_ < other.h_; }
HANDLE_TYPE(cl_command_queue, CUstream) & CommandQueue::handle()
CommandQueue::handle_type & CommandQueue::handle()
{ 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
{
return h_==other.h_;
}
bool Context::operator<(Context const & other) const
{
return h_<other.h_;
}
Context::handle_type const & Context::handle() const
{ return h_; }
Device const & Context::device() const
{ 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
{
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_; }
}

View File

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