Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

Commit 37adcdbf authored by schultezub's avatar schultezub
Browse files

* Added CLWrapper<T>::getInfo() and CLWrapper<T>::getInfoString() (using...

 * Added CLWrapper<T>::getInfo() and CLWrapper<T>::getInfoString() (using CLWrapperTraits<T>::InfoType and CLWrapperTraits<T>::getInfo())
 * Introducing cllib::Program

git-svn-id: https://camplinux.in.tum.de/svn/campvis/trunk@239 bb408c1c-ae56-11e1-83d9-df6b3e0c105e
parent 75d28e14
......@@ -16,132 +16,192 @@ namespace cllib {
*/
template<typename T>
struct CLWrapperTraits {};
#if defined(CL_VERSION_1_2)
// OpenCL 1.2 devices do have retain/release.
template <>
struct CLWrapperTraits<cl_device_id>
{
/**
* Retain the device.
* \param device A valid device created using createSubDevices
* \return
* CL_SUCCESS if the function executed successfully.
* CL_INVALID_DEVICE if device was not a valid subdevice
* CL_OUT_OF_RESOURCES
* CL_OUT_OF_HOST_MEMORY
*/
static cl_int retain(cl_device_id device) {
return ::clRetainDevice(device);
}
/**
* Retain the device.
* \param device A valid device created using createSubDevices
* \return
* CL_SUCCESS if the function executed successfully.
* CL_INVALID_DEVICE if device was not a valid subdevice
* CL_OUT_OF_RESOURCES
* CL_OUT_OF_HOST_MEMORY
*/
static cl_int release(cl_device_id device) {
return ::clReleaseDevice(device);
}
};
#else // #if defined(CL_VERSION_1_2)
// OpenCL 1.1 devices do not have retain/release.
template <>
struct CLWrapperTraits<cl_device_id> {
// cl_device_id does not have retain().
static cl_int retain(cl_device_id) {
return CL_SUCCESS;
}
// cl_device_id does not have release().
static cl_int release(cl_device_id) {
return CL_SUCCESS;
}
};
#endif // #if defined(CL_VERSION_1_2)
template <>
struct CLWrapperTraits<cl_platform_id> {
// cl_platform_id does not have retain().
static cl_int retain(cl_platform_id) {
return CL_SUCCESS;
}
// cl_platform_id does not have release().
static cl_int release(cl_platform_id) {
return CL_SUCCESS;
}
};
template <>
struct CLWrapperTraits<cl_context> {
static cl_int retain(cl_context context) {
return ::clRetainContext(context);
}
static cl_int release(cl_context context) {
return ::clReleaseContext(context);
}
};
template <>
struct CLWrapperTraits<cl_command_queue> {
static cl_int retain(cl_command_queue queue) {
return ::clRetainCommandQueue(queue);
}
static cl_int release(cl_command_queue queue) {
return ::clReleaseCommandQueue(queue);
}
};
template <>
struct CLWrapperTraits<cl_mem> {
static cl_int retain(cl_mem memory) {
return ::clRetainMemObject(memory);
}
static cl_int release(cl_mem memory) {
return ::clReleaseMemObject(memory);
}
};
template <>
struct CLWrapperTraits<cl_sampler> {
static cl_int retain(cl_sampler sampler) {
return ::clRetainSampler(sampler);
}
static cl_int release(cl_sampler sampler) {
return ::clReleaseSampler(sampler);
}
};
template <>
struct CLWrapperTraits<cl_program> {
static cl_int retain(cl_program program) {
return ::clRetainProgram(program);
}
static cl_int release(cl_program program) {
return ::clReleaseProgram(program);}
};
template <>
struct CLWrapperTraits<cl_kernel> {
static cl_int retain(cl_kernel kernel) {
return ::clRetainKernel(kernel);
}
static cl_int release(cl_kernel kernel) {
return ::clReleaseKernel(kernel);
}
};
template <>
struct CLWrapperTraits<cl_event> {
static cl_int retain(cl_event event) {
return ::clRetainEvent(event);
}
static cl_int release(cl_event event) {
return ::clReleaseEvent(event);
}
#if defined(CL_VERSION_1_2)
// OpenCL 1.2 devices do have retain/release.
template <>
struct CLWrapperTraits<cl_device_id>
{
typedef cl_device_info InfoType;
/**
* Retain the device.
* \param device A valid device created using createSubDevices
* \return
* CL_SUCCESS if the function executed successfully.
* CL_INVALID_DEVICE if device was not a valid subdevice
* CL_OUT_OF_RESOURCES
* CL_OUT_OF_HOST_MEMORY
*/
static cl_int retain(cl_device_id device) {
return ::clRetainDevice(device);
}
/**
* Retain the device.
* \param device A valid device created using createSubDevices
* \return
* CL_SUCCESS if the function executed successfully.
* CL_INVALID_DEVICE if device was not a valid subdevice
* CL_OUT_OF_RESOURCES
* CL_OUT_OF_HOST_MEMORY
*/
static cl_int release(cl_device_id device) {
return ::clReleaseDevice(device);
}
static cl_int getInfo(cl_device_id id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetDeviceInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
#else // #if defined(CL_VERSION_1_2)
// OpenCL 1.1 devices do not have retain/release.
template <>
struct CLWrapperTraits<cl_device_id> {
typedef cl_device_info InfoType;
// cl_device_id does not have retain().
static cl_int retain(cl_device_id) {
return CL_SUCCESS;
}
// cl_device_id does not have release().
static cl_int release(cl_device_id) {
return CL_SUCCESS;
}
static cl_int getInfo(cl_device_id id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetDeviceInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
#endif // #if defined(CL_VERSION_1_2)
template <>
struct CLWrapperTraits<cl_platform_id> {
typedef cl_platform_info InfoType;
// cl_platform_id does not have retain().
static cl_int retain(cl_platform_id) {
return CL_SUCCESS;
}
// cl_platform_id does not have release().
static cl_int release(cl_platform_id) {
return CL_SUCCESS;
}
static cl_int getInfo(cl_platform_id id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetPlatformInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
template <>
struct CLWrapperTraits<cl_context> {
typedef cl_context_info InfoType;
static cl_int retain(cl_context context) {
return ::clRetainContext(context);
}
static cl_int release(cl_context context) {
return ::clReleaseContext(context);
}
static cl_int getInfo(cl_context id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetContextInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
template <>
struct CLWrapperTraits<cl_command_queue> {
typedef cl_command_queue_info InfoType;
static cl_int retain(cl_command_queue queue) {
return ::clRetainCommandQueue(queue);
}
static cl_int release(cl_command_queue queue) {
return ::clReleaseCommandQueue(queue);
}
static cl_int getInfo(cl_command_queue id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetCommandQueueInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
template <>
struct CLWrapperTraits<cl_mem> {
typedef cl_mem_info InfoType;
static cl_int retain(cl_mem memory) {
return ::clRetainMemObject(memory);
}
static cl_int release(cl_mem memory) {
return ::clReleaseMemObject(memory);
}
static cl_int getInfo(cl_mem id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetMemObjectInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
template <>
struct CLWrapperTraits<cl_sampler> {
typedef cl_sampler_info InfoType;
static cl_int retain(cl_sampler sampler) {
return ::clRetainSampler(sampler);
}
static cl_int release(cl_sampler sampler) {
return ::clReleaseSampler(sampler);
}
static cl_int getInfo(cl_sampler id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetSamplerInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
template <>
struct CLWrapperTraits<cl_program> {
typedef cl_program_info InfoType;
static cl_int retain(cl_program program) {
return ::clRetainProgram(program);
}
static cl_int release(cl_program program) {
return ::clReleaseProgram(program);}
static cl_int getInfo(cl_program id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetProgramInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
template <>
struct CLWrapperTraits<cl_kernel> {
typedef cl_kernel_info InfoType;
static cl_int retain(cl_kernel kernel) {
return ::clRetainKernel(kernel);
}
static cl_int release(cl_kernel kernel) {
return ::clReleaseKernel(kernel);
}
static cl_int getInfo(cl_kernel id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetKernelInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
template <>
struct CLWrapperTraits<cl_event> {
typedef cl_event_info InfoType;
static cl_int retain(cl_event event) {
return ::clRetainEvent(event);
}
static cl_int release(cl_event event) {
return ::clReleaseEvent(event);
}
static cl_int getInfo(cl_event id, InfoType info, size_t paramValueSize, void* paramValue, size_t* paramValueSizeRet) {
return clGetEventInfo(id, info, paramValueSize, paramValue, paramValueSizeRet);
}
};
}
......@@ -207,64 +267,66 @@ namespace cllib {
* Wrapper for OpenCL objects that maintain an internal OpenCL ID and regard the internal OpenCL reference counting.
* \sa CLWrapperTraits
*/
template <typename T>
class CLWrapper {
public:
/// Typedef for the type OpenCL id.
typedef T cl_type;
/**
* Default constructor for an object without id.
*/
explicit CLWrapper()
: _id(0)
{ }
/**
* Constructor initializing the internal handle with \a id.
* \param id ID of the internal OpenCL handle.
*/
explicit CLWrapper(const cl_type& id)
: _id(id)
{ }
/**
* Destructor, releases the internal handle.
*/
virtual ~CLWrapper() {
if (_id != 0)
LCL_ERROR(release());
}
/**
* Copy constructor, regards the internal reference counting.
* \param rhs Source object
*/
CLWrapper(const CLWrapper<cl_type>& rhs)
{
_id = rhs._id;
if (_id != 0)
LCL_ERROR(retain());
}
/**
* Assignment operator, regards the internal reference counting.
* \param rhs Source object
* \return *this
*/
CLWrapper<cl_type>& operator=(const CLWrapper<cl_type>& rhs)
{
template <typename T>
class CLWrapper {
public:
/// Typedef for the type OpenCL id.
typedef T cl_type;
typedef typename CLWrapperTraits<T>::InfoType InfoType;
/**
* Default constructor for an object without id.
*/
explicit CLWrapper()
: _id(0)
{ }
/**
* Constructor initializing the internal handle with \a id.
* \param id ID of the internal OpenCL handle.
*/
explicit CLWrapper(const cl_type& id)
: _id(id)
{ }
/**
* Destructor, releases the internal handle.
*/
virtual ~CLWrapper() {
if (_id != 0)
LCL_ERROR(release());
}
/**
* Copy constructor, regards the internal reference counting.
* \param rhs Source object
*/
CLWrapper(const CLWrapper<cl_type>& rhs)
{
_id = rhs._id;
if (_id != 0)
LCL_ERROR(retain());
}
/**
* Assignment operator, regards the internal reference counting.
* \param rhs Source object
* \return *this
*/
CLWrapper<cl_type>& operator=(const CLWrapper<cl_type>& rhs)
{
if (_id != rhs._id) {
if (_id != 0)
LCL_ERROR(release());
_id = rhs._id;
if (_id != 0)
LCL_ERROR(retain());
}
return *this;
}
if (_id != 0)
LCL_ERROR(release());
_id = rhs._id;
if (_id != 0)
LCL_ERROR(retain());
}
return *this;
}
/**
* Gets the handle to internal OpenCl object.
* \return _id
......@@ -272,30 +334,55 @@ namespace cllib {
cl_type getId() const {
return _id;
}
protected:
/**
* Internally retains this object by using CLWrapperTraits<cl_type>.
* \param id Object to retain.
* \return The Error code of the retain function call.
*/
cl_int retain() const {
return CLWrapperTraits<cl_type>::retain(_id);
}
/**
* Internally releases this object by using CLWrapperTraits<cl_type>.
* \return The Error code of the release function call.
*/
cl_int release() const {
return CLWrapperTraits<cl_type>::release(_id);
}
cl_type _id; ///< Handle to internal OpenCL object.
};
protected:
/**
* Internally retains this object by using CLWrapperTraits<cl_type>.
* \param id Object to retain.
* \return The Error code of the retain function call.
*/
cl_int retain() const {
return CLWrapperTraits<cl_type>::retain(_id);
}
/**
* Internally releases this object by using CLWrapperTraits<cl_type>.
* \return The Error code of the release function call.
*/
cl_int release() const {
return CLWrapperTraits<cl_type>::release(_id);
}
/**
* Get the information about this object from OpenCL.
* \note Do \b use it with std::string, use getStringInfo() instead.
* \param info The information to query.
*/
template<class R>
R getInfo(InfoType info) const {
R ret;
LCL_ERROR(CLWrapperTraits<cl_type>::getInfo(_id, info, sizeof(ret), &ret, 0));
return ret;
}
/**
* Get the information about this object from OpenCL as string representation.
* \param info The information to query.
*/
std::string getStringInfo(InfoType info) const {
size_t retSize;
LCL_ERROR(CLWrapperTraits<T>::getInfo(_id, info, 0, 0, &retSize));
char* buffer = new char[retSize + 1];
LCL_ERROR(CLWrapperTraits<T>::getInfo(_id, info, retSize, buffer, 0));
std::string ret(buffer);
delete[] buffer;
return ret;
}
cl_type _id; ///< Handle to internal OpenCL object.
};
}
#endif // CLLIB_H__
......@@ -17,7 +17,7 @@ namespace cllib {
tgtAssert(_id != 0, "OpenCL device id must not be 0.");
// parse device profile
std::string profileString = getInfo<std::string>(CL_DEVICE_PROFILE);
std::string profileString = getStringInfo(CL_DEVICE_PROFILE);
if (profileString == "FULL_PROFILE")
_profile = FULL_PROFILE;
else if (profileString == "EMBEDDED_PROFILE") {
......@@ -29,13 +29,13 @@ namespace cllib {
}
// get other device information strings
_name = getInfo<std::string>(CL_DEVICE_NAME);
_name = getStringInfo(CL_DEVICE_NAME);
LINFO("Name: " << _name);
_vendor = getInfo<std::string>(CL_DEVICE_VENDOR);
_version = ClVersion(getInfo<std::string>(CL_DEVICE_VERSION));
_vendor = getStringInfo(CL_DEVICE_VENDOR);
_version = ClVersion(getStringInfo(CL_DEVICE_VERSION));
//explode extensions string with space as delimiter and insert them into set:
std::vector<std::string> exploded = TUMVis::StringUtils::split(getInfo<std::string>(CL_DEVICE_EXTENSIONS), " ");
std::vector<std::string> exploded = TUMVis::StringUtils::split(getStringInfo(CL_DEVICE_EXTENSIONS), " ");
_extensions.insert(exploded.begin(), exploded.end());
_deviceType = getInfo<cl_device_type>(CL_DEVICE_TYPE);
......@@ -105,18 +105,6 @@ namespace cllib {
return _version;
}
//template specialization for strings:
template<>
std::string Device::getInfo(cl_device_info info) const {
size_t retSize;
LCL_ERROR(clGetDeviceInfo(_id, info, 0, 0, &retSize));
char* buffer = new char[retSize];
LCL_ERROR(clGetDeviceInfo(_id, info, retSize, buffer, 0));
std::string ret(buffer);
delete[] buffer;
return ret;
}
cl_device_type Device::getDeviceType() const {
return _deviceType;
}
......
......@@ -83,9 +83,6 @@ namespace cllib {
bool supportsGlSharing() const;
protected:
template<class T>
T getInfo(cl_device_info info) const;
const Platform* _platform; ///< Parent platform of the device.
Profile _profile; ///< supported OpenCL profile
......@@ -109,20 +106,7 @@ namespace cllib {
static const std::string loggerCat_;
};
// ================================================================================================
template<class T>
T Device::getInfo(cl_device_info info) const {
T ret;
LCL_ERROR(clGetDeviceInfo(_id, info, sizeof(ret), &ret, 0));
return ret;
}
//template specialization for strings:
template<>
std::string Device::getInfo(cl_device_info info) const;
}
#endif // DEVICE_H__
......@@ -14,11 +14,6 @@ namespace cllib {
}
Event::~Event() {
}
EventList::EventList() {
_events = 0;
_size = 0;
......
......@@ -18,11 +18,6 @@ namespace cllib {
*/
Event(cl_event id);