Commit 2a44eee1 authored by Joseph Noir's avatar Joseph Noir

Move OpenCL error handling to specific header

parent 88ccfff7
...@@ -10,7 +10,9 @@ if (OPENCL_LIBRARIES) ...@@ -10,7 +10,9 @@ if (OPENCL_LIBRARIES)
set (LIBCAF_OPENCL_SRCS set (LIBCAF_OPENCL_SRCS
src/global.cpp src/global.cpp
src/opencl_metainfo.cpp src/opencl_metainfo.cpp
src/program.cpp) src/program.cpp
src/opencl_err.cpp
)
# build shared library if not compiling static only # build shared library if not compiling static only
if(NOT "${CAF_BUILD_STATIC_ONLY}" STREQUAL "yes") if(NOT "${CAF_BUILD_STATIC_ONLY}" STREQUAL "yes")
add_library(libcaf_opencl SHARED ${LIBCAF_OPENCL_SRCS} add_library(libcaf_opencl SHARED ${LIBCAF_OPENCL_SRCS}
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2014 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* Raphael Hiesgen <raphael.hiesgen (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_OPENCL_OPENCL_ERR_HPP
#define CAF_OPENCL_OPENCL_ERR_HPP
#include "caf/opencl/global.hpp"
#include "caf/detail/logging.hpp"
#define CAF_CLF(funname) #funname , funname
namespace caf {
namespace opencl {
void throwcl(const char* fname, cl_int err);
void pfn_notify(const char* errinfo, const void*, size_t, void*);
// call convention for simply calling a function
template <class F, class... Ts>
void v1callcl(const char* fname, F f, Ts&&... vs) {
throwcl(fname, f(std::forward<Ts>(vs)...));
}
// call convention for simply calling a function, not using the last argument
template <class F, class... Ts>
void v2callcl(const char* fname, F f, Ts&&... vs) {
throwcl(fname, f(std::forward<Ts>(vs)..., nullptr));
}
// call convention with `result` argument at the end returning `err`, not
// using the second last argument (set to nullptr) nor the one before (set to 0)
template <class R, class F, class... Ts>
R v1get(const char* fname, F f, Ts&&... vs) {
R result;
throwcl(fname, f(std::forward<Ts>(vs)..., cl_uint{0}, nullptr, &result));
return result;
}
// call convention with `err` argument at the end returning `result`
template <class F, class... Ts>
auto v2get(const char* fname, F f, Ts&&... vs)
-> decltype(f(std::forward<Ts>(vs)..., nullptr)) {
cl_int err;
auto result = f(std::forward<Ts>(vs)..., &err);
throwcl(fname, err);
return result;
}
// call convention with `result` argument at second last position (preceeded by
// its size) followed by an ingored void* argument (nullptr) returning `err`
template <class R, class F, class... Ts>
R v3get(const char* fname, F f, Ts&&... vs) {
R result;
throwcl(fname, f(std::forward<Ts>(vs)..., sizeof(R), &result, nullptr));
return result;
}
} // namespace opencl
} // namespace caf
#endif // CAF_OPENCL_OPENCL_ERR_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2014 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* Raphael Hiesgen <raphael.hiesgen (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/opencl/opencl_err.hpp"
namespace caf {
namespace opencl {
void throwcl(const char* fname, cl_int err) {
if (err != CL_SUCCESS) {
std::string errstr = fname;
errstr += ": ";
errstr += get_opencl_error(err);
throw std::runtime_error(std::move(errstr));
}
}
void pfn_notify(const char* errinfo, const void*, size_t, void*) {
CAF_LOGC_ERROR("caf::opencl::opencl_metainfo", "initialize",
"\n##### Error message via pfn_notify #####\n"
<< errinfo <<
"\n########################################");
}
} // namespace opencl
} // namespace caf
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* * * *
* Copyright (C) 2011 - 2014 * * Copyright (C) 2011 - 2014 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> * * Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* Raphael Hiesgen <raphael.hiesgen (at) haw-hamburg.de> *
* * * *
* Distributed under the terms and conditions of the BSD 3-Clause License or * * Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software * * (at your option) under the terms and conditions of the Boost Software *
...@@ -17,67 +18,12 @@ ...@@ -17,67 +18,12 @@
* http://www.boost.org/LICENSE_1_0.txt. * * http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/ ******************************************************************************/
#include "caf/opencl/opencl_err.hpp"
#include "caf/opencl/opencl_metainfo.hpp" #include "caf/opencl/opencl_metainfo.hpp"
#define CAF_CLF(funname) #funname , funname
namespace caf { namespace caf {
namespace opencl { namespace opencl {
namespace {
void throwcl(const char* fname, cl_int err) {
if (err != CL_SUCCESS) {
std::string errstr = fname;
errstr += ": ";
errstr += get_opencl_error(err);
throw std::runtime_error(std::move(errstr));
}
}
// call convention for simply calling a function, not using the last argument
template <class F, class... Ts>
void callcl(const char* fname, F f, Ts&&... vs) {
throwcl(fname, f(std::forward<Ts>(vs)..., nullptr));
}
// call convention with `result` argument at the end returning `err`, not
// using the second last argument (set to nullptr) nor the one before (set to 0)
template <class R, class F, class... Ts>
R v1get(const char* fname, F f, Ts&&... vs) {
R result;
throwcl(fname, f(std::forward<Ts>(vs)..., cl_uint{0}, nullptr, &result));
return result;
}
// call convention with `err` argument at the end returning `result`
template <class F, class... Ts>
auto v2get(const char* fname, F f, Ts&&... vs)
-> decltype(f(std::forward<Ts>(vs)..., nullptr)) {
cl_int err;
auto result = f(std::forward<Ts>(vs)..., &err);
throwcl(fname, err);
return result;
}
// call convention with `result` argument at second last position (preceeded by
// its size) followed by an ingored void* argument (nullptr) returning `err`
template <class R, class F, class... Ts>
R v3get(const char* fname, F f, Ts&&... vs) {
R result;
throwcl(fname, f(std::forward<Ts>(vs)..., sizeof(R), &result, nullptr));
return result;
}
void pfn_notify(const char* errinfo, const void*, size_t, void*) {
CAF_LOGC_ERROR("caf::opencl::opencl_metainfo", "initialize",
"\n##### Error message via pfn_notify #####\n"
<< errinfo <<
"\n########################################");
}
} // namespace <anonymous>
opencl_metainfo* opencl_metainfo::instance() { opencl_metainfo* opencl_metainfo::instance() {
auto sid = detail::singletons::opencl_plugin_id; auto sid = detail::singletons::opencl_plugin_id;
auto fac = [] { return new opencl_metainfo; }; auto fac = [] { return new opencl_metainfo; };
...@@ -94,7 +40,7 @@ void opencl_metainfo::initialize() { ...@@ -94,7 +40,7 @@ void opencl_metainfo::initialize() {
auto num_platforms = v1get<cl_uint>(CAF_CLF(clGetPlatformIDs)); auto num_platforms = v1get<cl_uint>(CAF_CLF(clGetPlatformIDs));
// get platform ids // get platform ids
std::vector<cl_platform_id> platforms(num_platforms); std::vector<cl_platform_id> platforms(num_platforms);
callcl(CAF_CLF(clGetPlatformIDs), num_platforms, platforms.data()); v2callcl(CAF_CLF(clGetPlatformIDs), num_platforms, platforms.data());
if (platforms.empty()) { if (platforms.empty()) {
throw std::runtime_error("no OpenCL platform found"); throw std::runtime_error("no OpenCL platform found");
} }
...@@ -113,7 +59,7 @@ void opencl_metainfo::initialize() { ...@@ -113,7 +59,7 @@ void opencl_metainfo::initialize() {
} }
// get available devices // get available devices
std::vector<cl_device_id> ds(num_devs); std::vector<cl_device_id> ds(num_devs);
callcl(CAF_CLF(clGetDeviceIDs), platform, dev_type, num_devs, ds.data()); v2callcl(CAF_CLF(clGetDeviceIDs), platform, dev_type, num_devs, ds.data());
std::vector<device_ptr> devices(num_devs); std::vector<device_ptr> devices(num_devs);
// lift raw pointer as returned by OpenCL to C++ smart pointers // lift raw pointer as returned by OpenCL to C++ smart pointers
auto lift = [](cl_device_id ptr) { return device_ptr{ptr, false}; }; auto lift = [](cl_device_id ptr) { return device_ptr{ptr, false}; };
...@@ -138,7 +84,7 @@ void opencl_metainfo::initialize() { ...@@ -138,7 +84,7 @@ void opencl_metainfo::initialize() {
auto max_wid = v3get<cl_uint>(CAF_CLF(clGetDeviceInfo), device.get(), auto max_wid = v3get<cl_uint>(CAF_CLF(clGetDeviceInfo), device.get(),
static_cast<unsigned>(CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS)); static_cast<unsigned>(CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS));
dim_vec max_wi_per_dim(max_wid); dim_vec max_wi_per_dim(max_wid);
callcl(CAF_CLF(clGetDeviceInfo), device.get(), v2callcl(CAF_CLF(clGetDeviceInfo), device.get(),
static_cast<unsigned>(CL_DEVICE_MAX_WORK_ITEM_SIZES), static_cast<unsigned>(CL_DEVICE_MAX_WORK_ITEM_SIZES),
sizeof(size_t) * max_wid, sizeof(size_t) * max_wid,
max_wi_per_dim.data()); max_wi_per_dim.data());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment