Skip to content

Commit

Permalink
Merge pull request #22 from CNugteren/minor_fixes
Browse files Browse the repository at this point in the history
Various minor fixes
  • Loading branch information
CNugteren committed May 25, 2015
2 parents 0f21d60 + 4cc9dbd commit f5afa53
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

Version 1.6.2
- Fixed another exception-related bug
- Further improved reporting of failed runs
- Updated C++11 OpenCL API

Version 1.6.1
- Fixed a couple of issues related to exceptions
- Improved reporting of failed runs
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ cmake_minimum_required(VERSION 2.8)
project("cltune" CXX)
set(cltune_VERSION_MAJOR 1)
set(cltune_VERSION_MINOR 6)
set(cltune_VERSION_PATCH 1)
set(cltune_VERSION_PATCH 2)

# Options
option(SAMPLES "Enable compilation of sample programs" ON)
Expand Down
61 changes: 41 additions & 20 deletions include/internal/clpp11.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class Object {
protected:

// Error handling
[[noreturn]] void Error(const std::string &message) {
[[noreturn]] void Error(const std::string &message) const {
throw std::runtime_error("Internal OpenCL error: "+message);
}
[[noreturn]] void Error(const cl_int status) {
[[noreturn]] void Error(const cl_int status) const {
throw std::runtime_error("Internal OpenCL error with status: "+std::to_string(status));
}
};
Expand All @@ -75,10 +75,10 @@ class Event: public Object {
public:

// Constructor based on the plain C data-type
Event(const cl_event event): event_(event) { }
explicit Event(const cl_event event): event_(event) { }

// New event
Event() {}
Event(): event_() {}

// Public functions
size_t GetProfilingStart() const {
Expand Down Expand Up @@ -113,10 +113,10 @@ class Platform: public Object {
public:

// Constructor based on the plain C data-type
Platform(const cl_platform_id platform): platform_(platform) { }
explicit Platform(const cl_platform_id platform): platform_(platform) { }

// Initialize the platform. Note that this constructor can throw exceptions!
Platform(const size_t platform_id) {
explicit Platform(const size_t platform_id) {
auto num_platforms = cl_uint{0};
auto status = clGetPlatformIDs(0, nullptr, &num_platforms);
if (status != CL_SUCCESS) { Error(status); }
Expand All @@ -142,10 +142,10 @@ class Device: public Object {
public:

// Constructor based on the plain C data-type
Device(const cl_device_id device): device_(device) { }
explicit Device(const cl_device_id device): device_(device) { }

// Initialize the device. Note that this constructor can throw exceptions!
Device(const Platform &platform, const cl_device_type type, const size_t device_id) {
explicit Device(const Platform &platform, const cl_device_type type, const size_t device_id) {
auto num_devices = cl_uint{0};
auto status = clGetDeviceIDs(platform(), type, 0, nullptr, &num_devices);
if (status != CL_SUCCESS) { Error(status); }
Expand All @@ -159,6 +159,8 @@ class Device: public Object {

// Public functions
std::string Version() const { return GetInfoString(CL_DEVICE_VERSION); }
cl_device_type Type() const { return GetInfo<cl_device_type>(CL_DEVICE_TYPE); }
std::string Vendor() const { return GetInfoString(CL_DEVICE_VENDOR); }
std::string Name() const { return GetInfoString(CL_DEVICE_NAME); }
std::string Extensions() const { return GetInfoString(CL_DEVICE_EXTENSIONS); }
size_t MaxWorkGroupSize() const { return GetInfo<size_t>(CL_DEVICE_MAX_WORK_GROUP_SIZE); }
Expand Down Expand Up @@ -225,12 +227,12 @@ class Context: public ObjectWithState {
public:

// Constructor based on the plain C data-type
Context(const cl_context context): context_(context) {
explicit Context(const cl_context context): context_(context) {
clRetainContext(context_);
}

// Memory management
Context(const Device &device) {
explicit Context(const Device &device) {
auto status = CL_SUCCESS;
const cl_device_id dev = device();
context_ = clCreateContext(nullptr, 1, &dev, nullptr, nullptr, &status);
Expand Down Expand Up @@ -267,7 +269,7 @@ class Program: public ObjectWithState {
// Note that there is no constructor based on the plain C data-type because of extra state

// Memory management
Program(const Context &context, const std::string &source):
explicit Program(const Context &context, const std::string &source):
length_(source.length()) {
std::copy(source.begin(), source.end(), back_inserter(source_));
source_ptr_ = source_.data();
Expand All @@ -289,6 +291,16 @@ class Program: public ObjectWithState {
swap(*this, other);
return *this;
}
/*
TODO: Implement move construction/assignment?
Program(Program &&other) {
clRetainProgram(program_);
swap(*this, other);
}
Program& operator=(Program &&other) {
swap(*this, other);
return *this;
}*/
friend void swap(Program &first, Program &second) {
std::swap(first.length_, second.length_);
std::swap(first.source_, second.source_);
Expand Down Expand Up @@ -326,12 +338,12 @@ class Kernel: public ObjectWithState {
public:

// Constructor based on the plain C data-type
Kernel(const cl_kernel kernel): kernel_(kernel) {
explicit Kernel(const cl_kernel kernel): kernel_(kernel) {
clRetainKernel(kernel_);
}

// Memory management
Kernel(const Program &program, const std::string &name) {
explicit Kernel(const Program &program, const std::string &name) {
auto status = CL_SUCCESS;
kernel_ = clCreateKernel(program(), name.c_str(), &status);
if (status != CL_SUCCESS) { Error(status); }
Expand All @@ -352,11 +364,11 @@ class Kernel: public ObjectWithState {
}

// Public functions
template <typename T>
cl_int SetArgument(const cl_uint index, const T value) {
template <typename T> // Note: doesn't work with T=Buffer
cl_int SetArgument(const cl_uint index, const T &value) {
return clSetKernelArg(kernel_, index, sizeof(T), &value);
}
size_t LocalMemUsage(const Device &device) {
size_t LocalMemUsage(const Device &device) const {
auto bytes = size_t{0};
clGetKernelWorkGroupInfo(kernel_, device(), CL_KERNEL_LOCAL_MEM_SIZE, 0, nullptr, &bytes);
auto result = size_t{0};
Expand All @@ -378,12 +390,12 @@ class CommandQueue: public ObjectWithState {
public:

// Constructor based on the plain C data-type
CommandQueue(const cl_command_queue queue): queue_(queue) {
explicit CommandQueue(const cl_command_queue queue): queue_(queue) {
clRetainCommandQueue(queue_);
}

// Memory management
CommandQueue(const Context &context, const Device &device) {
explicit CommandQueue(const Context &context, const Device &device) {
auto status = CL_SUCCESS;
queue_ = clCreateCommandQueue(context(), device(), CL_QUEUE_PROFILING_ENABLE, &status);
if (status != CL_SUCCESS) { Error(status); }
Expand Down Expand Up @@ -441,12 +453,12 @@ class Buffer: public ObjectWithState {
public:

// Constructor based on the plain C data-type
Buffer(const cl_mem buffer): buffer_(buffer) {
explicit Buffer(const cl_mem buffer): buffer_(buffer) {
clRetainMemObject(buffer_);
}

// Memory management
Buffer(const Context &context, const cl_mem_flags flags, const size_t bytes) {
explicit Buffer(const Context &context, const cl_mem_flags flags, const size_t bytes) {
auto status = CL_SUCCESS;
buffer_ = clCreateBuffer(context(), flags, bytes, nullptr, &status);
if (status != CL_SUCCESS) { Error(status); }
Expand Down Expand Up @@ -483,6 +495,15 @@ class Buffer: public ObjectWithState {
cl_int WriteBuffer(const CommandQueue &queue, const size_t bytes, const std::vector<T> &host) {
return WriteBuffer(queue, bytes, &host[0]);
}
size_t GetSize() const {
auto bytes = size_t{0};
auto status = clGetMemObjectInfo(buffer_, CL_MEM_SIZE, 0, nullptr, &bytes);
if (status != CL_SUCCESS) { Error(status); }
auto result = size_t{0};
status = clGetMemObjectInfo(buffer_, CL_MEM_SIZE, bytes, &result, nullptr);
if (status != CL_SUCCESS) { Error(status); }
return result;
}

// Accessors to the private data-member
cl_mem operator()() const { return buffer_; }
Expand Down
4 changes: 3 additions & 1 deletion src/cltune.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ template <typename T>
void Tuner::AddArgumentInput(const std::vector<T> &source) {
auto device_buffer = Buffer(pimpl->context(), CL_MEM_READ_ONLY, source.size()*sizeof(T));
auto status = device_buffer.WriteBuffer(pimpl->queue(), source.size()*sizeof(T), source);
if (status != CL_SUCCESS) { throw std::runtime_error("Write buffer error: " + status); }
if (status != CL_SUCCESS) {
throw std::runtime_error("Write buffer error: " + std::to_string(status));
}
auto argument = TunerImpl::MemArgument{pimpl->argument_counter_++, source.size(),
pimpl->GetType<T>(), device_buffer};
pimpl->arguments_input_.push_back(argument);
Expand Down
6 changes: 3 additions & 3 deletions src/tuner_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ void TunerImpl::Tune() {
// Stores the parameters and the timing-result
tuning_result.configuration = permutation;
tuning_results_.push_back(tuning_result);
if (!tuning_result.status) {
PrintResult(stdout, tuning_result, kMessageWarning);
}
if (tuning_result.time == std::numeric_limits<double>::max()) {
tuning_result.time = 0.0;
PrintResult(stdout, tuning_result, kMessageFailure);
tuning_result.time = std::numeric_limits<double>::max();
}
else if (!tuning_result.status) {
PrintResult(stdout, tuning_result, kMessageWarning);
}
}

// Prints a log of the searching process. This is disabled per default, but can be enabled
Expand Down

0 comments on commit f5afa53

Please sign in to comment.