Skip to content

Commit

Permalink
integrated optional (experimentl) CLBlast support
Browse files Browse the repository at this point in the history
  • Loading branch information
LostRuins committed Apr 11, 2023
1 parent c9f1808 commit 23c675b
Show file tree
Hide file tree
Showing 53 changed files with 22,095 additions and 151 deletions.
50 changes: 50 additions & 0 deletions CL/Utils/Context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

// OpenCL Utils includes
#include "OpenCLUtils_Export.h"

// OpenCL includes
#include <CL/cl.h>

// STL includes
#include <time.h>

UTILS_EXPORT
cl_context cl_util_get_context(const cl_uint plat_id, const cl_uint dev_id,
const cl_device_type type, cl_int* const error);
UTILS_EXPORT
cl_device_id cl_util_get_device(const cl_uint plat_id, const cl_uint dev_id,
const cl_device_type type, cl_int* const error);

UTILS_EXPORT
cl_int cl_util_print_device_info(const cl_device_id device);

UTILS_EXPORT
char* cl_util_get_device_info(const cl_device_id device,
const cl_device_info info, cl_int* const error);
UTILS_EXPORT
char* cl_util_get_platform_info(const cl_platform_id platform,
const cl_platform_info info,
cl_int* const error);

// build program and show log if build is not successful
UTILS_EXPORT
cl_int cl_util_build_program(const cl_program pr, const cl_device_id dev,
const char* const opt);

#define GET_CURRENT_TIMER(time) \
struct timespec time; \
timespec_get(&time, TIME_UTC); \
{ \
}

#define TIMER_DIFFERENCE(dt, time1, time2) \
{ \
dt = (time2.tv_sec - time1.tv_sec) * 1000000000 \
+ (time2.tv_nsec - time1.tv_nsec); \
}

#define START_TIMER GET_CURRENT_TIMER(start_timer1)
#define STOP_TIMER(dt) \
GET_CURRENT_TIMER(stop_timer2) \
TIMER_DIFFERENCE(dt, start_timer1, stop_timer2)
17 changes: 17 additions & 0 deletions CL/Utils/Context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

// OpenCL SDK includes
#include "OpenCLUtilsCpp_Export.h"

#include <CL/Utils/Error.hpp>

// OpenCL includes
#include <CL/opencl.hpp>

namespace cl {
namespace util {
Context UTILSCPP_EXPORT get_context(cl_uint plat_id, cl_uint dev_id,
cl_device_type type,
cl_int* error = nullptr);
}
}
84 changes: 84 additions & 0 deletions CL/Utils/Detail.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#pragma once

// STL includes
#include <stddef.h>
#include <utility> // std::forward, std::integer_sequence
#include <tuple> // std::tuple, std::get
#include <initializer_list> // std::initializer_list

namespace cl {
namespace util {
namespace detail {
// Borrowed from:
// https://www.fluentcpp.com/2019/03/05/for_each_arg-applying-a-function-to-each-argument-of-a-function-in-cpp/
template <class F, class... Args> F for_each_arg(F f, Args&&... args)
{
(void)std::initializer_list<int>{ (
(void)f(std::forward<Args>(args)), 0)... };
return f;
}

namespace impl {
// Borrowed from: https://stackoverflow.com/a/16387374/1476661
template <typename T, typename F, int... Is>
void for_each_in_tuple(T&& t, F&& f,
std::integer_sequence<int, Is...>)
{
auto l = {
(std::forward<F>(f)(std::get<Is>(std::forward<T>(t))), 0)...
};
(void)l;
}
}
template <typename... Ts, typename F>
void for_each_in_tuple(std::tuple<Ts...> const& t, F&& f)
{
impl::for_each_in_tuple(
t, std::forward<F>(f),
std::make_integer_sequence<int, sizeof...(Ts)>());
}

namespace impl {
// Borrowed from
// https://codereview.stackexchange.com/questions/193420/apply-a-function-to-each-element-of-a-tuple-map-a-tuple
template <class F, typename Tuple, std::size_t... Is>
auto transform_tuple(Tuple&& t, F&& f, std::index_sequence<Is...>)
{
return std::make_tuple(std::forward<F>(f)(std::get<Is>(t))...);
}
}
template <class F, typename... Args>
auto transform_tuple(const std::tuple<Args...>& t, F&& f)
{
return impl::transform_tuple(
t, std::forward<F>(f),
std::make_index_sequence<sizeof...(Args)>{});
}

namespace impl {
// Borrowed from
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
// with modifications of Casey Carter at
// https://stackoverflow.com/a/51365112/1476661
template <typename F, typename Tuple, std::size_t... I>
auto apply(F&& f, Tuple&& args, std::index_sequence<I...>)
-> decltype(std::forward<F>(f)(
std::get<I>(std::forward<Tuple>(args))...))
{
return std::forward<F>(f)(
std::get<I>(std::forward<Tuple>(args))...);
}
}
template <typename F, typename Tuple,
typename Indices = std::make_index_sequence<
std::tuple_size<std::remove_reference_t<Tuple>>::value>>
auto apply(F&& f, Tuple&& args)
-> decltype(impl::apply(std::forward<F>(f),
std::forward<Tuple>(args), Indices()))
{
return impl::apply(std::forward<F>(f), std::forward<Tuple>(args),
Indices());
}
}
}
}
21 changes: 21 additions & 0 deletions CL/Utils/Device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "OpenCLUtilsCpp_Export.h"
#include <CL/Utils/Error.hpp>

#include <CL/opencl.hpp>

namespace cl {
namespace util {
bool UTILSCPP_EXPORT opencl_c_version_contains(
const cl::Device& device, const cl::string& version_fragment);

bool UTILSCPP_EXPORT supports_extension(const cl::Device& device,
const cl::string& extension);

#ifdef CL_VERSION_3_0
bool UTILSCPP_EXPORT supports_feature(const cl::Device& device,
const cl::string& feature_name);
#endif
}
}
88 changes: 88 additions & 0 deletions CL/Utils/Error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#pragma once

// OpenCL Utils includes
#include "OpenCLUtils_Export.h"

// OpenCL Utils includes
#include <CL/Utils/ErrorCodes.h>

// STL includes
#include <stdio.h> // fprintf

// OpenCL includes
#include <CL/cl.h>

// RET = function returns error code
// PAR = functions sets error code in the paremeter

#ifdef _DEBUG

#define OCLERROR_RET(func, err, label) \
do \
{ \
err = func; \
if (err != CL_SUCCESS) \
{ \
cl_util_print_error(err); \
fprintf(stderr, "on line %d, in file %s\n%s\n", __LINE__, \
__FILE__, #func); \
goto label; \
} \
} while (0)

#define OCLERROR_PAR(func, err, label) \
do \
{ \
func; \
if (err != CL_SUCCESS) \
{ \
cl_util_print_error(err); \
fprintf(stderr, "on line %d, in file %s\n%s\n", __LINE__, \
__FILE__, #func); \
goto label; \
} \
} while (0)

#define MEM_CHECK(func, err, label) \
do \
{ \
if ((func) == NULL) \
{ \
err = CL_OUT_OF_HOST_MEMORY; \
cl_util_print_error(err); \
fprintf(stderr, "on line %d, in file %s\n%s\n", __LINE__, \
__FILE__, #func); \
goto label; \
} \
} while (0)

#else

#define OCLERROR_RET(func, err, label) \
do \
{ \
err = func; \
if (err != CL_SUCCESS) goto label; \
} while (0)

#define OCLERROR_PAR(func, err, label) \
do \
{ \
func; \
if (err != CL_SUCCESS) goto label; \
} while (0)

#define MEM_CHECK(func, err, label) \
do \
{ \
if ((func) == NULL) \
{ \
err = CL_OUT_OF_HOST_MEMORY; \
goto label; \
} \
} while (0)

#endif

UTILS_EXPORT
void cl_util_print_error(cl_int error);
70 changes: 70 additions & 0 deletions CL/Utils/Error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

// OpenCL Utils includes
#include "OpenCLUtilsCpp_Export.h"

// OpenCL Utils includes
#include <CL/Utils/ErrorCodes.h>

// OpenCL includes
#include <CL/opencl.hpp>

namespace cl {
namespace util {
#if defined(CL_HPP_ENABLE_EXCEPTIONS)
/*! \brief Exception class
*
* This may be thrown by SDK utility functions when
* CL_HPP_ENABLE_EXCEPTIONS is defined.
*/
class Error : public std::exception {
private:
int err_;
const char* errStr_;

public:
/*! \brief Create a new SDK error exception for a given error code
* and corresponding message.
*
* \param err error code value.
*
* \param errStr a descriptive string that must remain in scope until
* handling of the exception has concluded. If set, it
* will be returned by what().
*/
Error(cl_int err, const char* errStr = NULL): err_(err), errStr_(errStr)
{}

~Error() throw() {}

/*! \brief Get error string associated with exception
*
* \return A memory pointer to the error message string.
*/
virtual const char* what() const throw()
{
if (errStr_ == NULL)
{
return "empty";
}
else
{
return errStr_;
}
}

/*! \brief Get error code associated with exception
*
* \return The error code.
*/
cl_int err(void) const { return err_; }
};
#endif

namespace detail {
UTILSCPP_EXPORT cl_int errHandler(cl_int err, cl_int* errPtr,
const char* errStr = nullptr);
}

}
}
5 changes: 5 additions & 0 deletions CL/Utils/ErrorCodes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#define CL_UTIL_INDEX_OUT_OF_RANGE -2000
#define CL_UTIL_DEVICE_NOT_INTEROPERABLE -2001
#define CL_UTIL_FILE_OPERATION_ERROR -2002
13 changes: 13 additions & 0 deletions CL/Utils/Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

// OpenCL Utils includes
#include "OpenCLUtils_Export.h"

// OpenCL includes
#include <CL/cl.h>

UTILS_EXPORT
cl_ulong cl_util_get_event_duration(const cl_event event,
const cl_profiling_info start,
const cl_profiling_info end,
cl_int* const error);
21 changes: 21 additions & 0 deletions CL/Utils/Event.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

// OpenCL SDK includes
#include "OpenCLUtilsCpp_Export.h"

// STL includes
#include <chrono>

// OpenCL includes
#include <CL/opencl.hpp>

namespace cl {
namespace util {
template <cl_int From, cl_int To, typename Dur = std::chrono::nanoseconds>
auto get_duration(cl::Event& ev)
{
return std::chrono::duration_cast<Dur>(std::chrono::nanoseconds{
ev.getProfilingInfo<To>() - ev.getProfilingInfo<From>() });
}
}
}
Loading

0 comments on commit 23c675b

Please sign in to comment.