All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
handle.hpp
Go to the documentation of this file.
#ifndef VIENNACL_OCL_HANDLE_HPP_
#define VIENNACL_OCL_HANDLE_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include <assert.h>
#include <string>
#include <iostream>
#include "viennacl/ocl/forwards.h"
#include "viennacl/ocl/error.hpp"
namespace viennacl
{
namespace ocl
{
template<class OCL_TYPE>
class handle_inc_dec_helper
{
typedef typename OCL_TYPE::ERROR_TEMPLATE_ARGUMENT_FOR_CLASS_INVALID ErrorType;
};
//cl_mem:
template<>
struct handle_inc_dec_helper<cl_mem>
{
static void inc(cl_mem & something)
{
cl_int err = clRetainMemObject(something);
VIENNACL_ERR_CHECK(err);
}
static void dec(cl_mem & something)
{
cl_int err = clReleaseMemObject(something);
VIENNACL_ERR_CHECK(err);
}
};
//cl_program:
template<>
struct handle_inc_dec_helper<cl_program>
{
static void inc(cl_program & something)
{
cl_int err = clRetainProgram(something);
VIENNACL_ERR_CHECK(err);
}
static void dec(cl_program & something)
{
(void)something;
#ifndef __APPLE__
cl_int err = clReleaseProgram(something);
VIENNACL_ERR_CHECK(err);
#endif
}
};
//cl_kernel:
template<>
struct handle_inc_dec_helper<cl_kernel>
{
static void inc(cl_kernel & something)
{
cl_int err = clRetainKernel(something);
VIENNACL_ERR_CHECK(err);
}
static void dec(cl_kernel & something)
{
(void)something;
#ifndef __APPLE__
cl_int err = clReleaseKernel(something);
VIENNACL_ERR_CHECK(err);
#endif
}
};
//cl_command_queue:
template<>
struct handle_inc_dec_helper<cl_command_queue>
{
static void inc(cl_command_queue & something)
{
cl_int err = clRetainCommandQueue(something);
VIENNACL_ERR_CHECK(err);
}
static void dec(cl_command_queue & something)
{
(void)something;
#ifndef __APPLE__
cl_int err = clReleaseCommandQueue(something);
VIENNACL_ERR_CHECK(err);
#endif
}
};
//cl_context:
template<>
struct handle_inc_dec_helper<cl_context>
{
static void inc(cl_context & something)
{
cl_int err = clRetainContext(something);
VIENNACL_ERR_CHECK(err);
}
static void dec(cl_context & something)
{
(void)something;
#ifndef __APPLE__
cl_int err = clReleaseContext(something);
VIENNACL_ERR_CHECK(err);
#endif
}
};
template<class OCL_TYPE>
class handle
{
public:
handle() : h_(0), p_context_(NULL) {}
handle(const OCL_TYPE & something, viennacl::ocl::context const & c) : h_(something), p_context_(&c) {}
handle(const handle & other) : h_(other.h_), p_context_(other.p_context_) { if (h_ != 0) inc(); }
~handle() { if (h_ != 0) dec(); }
handle & operator=(const handle & other)
{
if (h_ != 0)
dec();
h_ = other.h_;
p_context_ = other.p_context_;
inc();
return *this;
}
handle & operator=(const OCL_TYPE & something)
{
if (h_ != 0) dec();
h_ = something;
return *this;
}
handle & operator=(std::pair<OCL_TYPE, cl_context> p)
{
if (h_ != 0) dec();
h_ = p.first;
p_context_ = p.second;
return *this;
}
operator OCL_TYPE() const { return h_; }
const OCL_TYPE & get() const { return h_; }
viennacl::ocl::context const & context() const
{
assert(p_context_ != NULL && bool("Logic error: Accessing dangling context from handle."));
return *p_context_;
}
void context(viennacl::ocl::context const & c) { p_context_ = &c; }
handle & swap(handle & other)
{
OCL_TYPE tmp = other.h_;
other.h_ = this->h_;
this->h_ = tmp;
viennacl::ocl::context const * tmp2 = other.p_context_;
other.p_context_ = this->p_context_;
this->p_context_ = tmp2;
return *this;
}
void inc() { handle_inc_dec_helper<OCL_TYPE>::inc(h_); }
void dec() { handle_inc_dec_helper<OCL_TYPE>::dec(h_); }
private:
OCL_TYPE h_;
viennacl::ocl::context const * p_context_;
};
} //namespace ocl
} //namespace viennacl
#endif
forwards.h
This file provides the forward declarations for the OpenCL layer of ViennaCL.
viennacl::ocl::handle::~handle
~handle()
Definition: handle.hpp:157
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::ocl::handle_inc_dec_helper
Helper for OpenCL reference counting used by class handle.
Definition: handle.hpp:45
viennacl::ocl::handle::handle
handle(const handle &other)
Definition: handle.hpp:156
viennacl::ocl::handle::context
viennacl::ocl::context const & context() const
Definition: handle.hpp:193
VIENNACL_ERR_CHECK
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:681
viennacl::ocl::handle::operator=
handle & operator=(std::pair< OCL_TYPE, cl_context > p)
Wraps an OpenCL handle including its associated context. Decreases the reference count if the handle ...
Definition: handle.hpp:179
viennacl::ocl::handle::context
void context(viennacl::ocl::context const &c)
Definition: handle.hpp:198
viennacl::ocl::handle::inc
void inc()
Manually increment the OpenCL reference count. Typically called automatically, but is necessary if us...
Definition: handle.hpp:216
viennacl::ocl::handle::dec
void dec()
Manually decrement the OpenCL reference count. Typically called automatically, but might be useful wi...
Definition: handle.hpp:218
viennacl::ocl::handle::operator=
handle & operator=(const OCL_TYPE &something)
Wraps an OpenCL handle. Does not change the context of this handle object! Decreases the reference co...
Definition: handle.hpp:171
error.hpp
Error handling for the OpenCL layer of ViennaCL.
viennacl::ocl::handle::swap
handle & swap(handle &other)
Swaps the OpenCL handle of two handle objects.
Definition: handle.hpp:202
viennacl::ocl::handle::handle
handle()
Definition: handle.hpp:154
viennacl::ocl::handle::operator=
handle & operator=(const handle &other)
Copies the OpenCL handle from the provided handle. Does not take ownership like e.g. std::auto_ptr<>, so both handle objects are valid (more like shared_ptr).
Definition: handle.hpp:160
viennacl::traits::handle
viennacl::backend::mem_handle const & handle(T const &obj)
Returns the generic memory handle of an object. Const-version.
Definition: handle.hpp:48
viennacl::ocl::handle
Handle class the effectively represents a smart pointer for OpenCL handles.
Definition: forwards.h:51
viennacl::ocl::handle::handle
handle(const OCL_TYPE &something, viennacl::ocl::context const &c)
Definition: handle.hpp:155