wip dont use

This commit is contained in:
Concedo 2023-04-21 00:35:54 +08:00
parent 7ba36c2c6c
commit 07bb31b034
42 changed files with 8 additions and 7 deletions

View file

@ -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)

View file

@ -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);
}
}

View file

@ -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());
}
}
}
}

View file

@ -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
include/CL/Utils/Error.h Normal file
View file

@ -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);

View file

@ -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);
}
}
}

View file

@ -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
include/CL/Utils/Event.h Normal file
View file

@ -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);

View file

@ -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>() });
}
}
}

42
include/CL/Utils/File.h Normal file
View file

@ -0,0 +1,42 @@
#pragma once
// OpenCL Utils includes
#include "OpenCLUtils_Export.h"
// OpenCL includes
#include <CL/cl.h>
// read all the text file contents securely in ANSI C89
// return pointer to C-string with file contents
// can handle streams with no known size and no support for fseek
// based on https://stackoverflow.com/questions/14002954/ by Nominal Animal
UTILS_EXPORT
char* cl_util_read_text_file(const char* const filename, size_t* const length,
cl_int* const error);
// read all the binary file contents securely in ANSI C89
// return pointer to file contents
// can handle streams with no known size and no support for fseek
// based on https://stackoverflow.com/questions/14002954/ by Nominal Animal
UTILS_EXPORT
unsigned char* cl_util_read_binary_file(const char* const filename,
size_t* const length,
cl_int* const error);
// write binaries of OpenCL compiled program
// binaries are written as separate files for each device
// with file name "(program_file_name)_(name of device).bin"
// based on variant of Logan
// http://logan.tw/posts/2014/11/22/pre-compile-the-opencl-kernel-program-part-2/
UTILS_EXPORT
cl_int cl_util_write_binaries(const cl_program program,
const char* const program_file_name);
// read binaries of OpenCL compiled program
// from files of file names "(program_file_name)_(name of device).bin"
UTILS_EXPORT
cl_program cl_util_read_binaries(const cl_context context,
const cl_device_id* const devices,
const cl_uint num_devices,
const char* const program_file_name,
cl_int* const error);

49
include/CL/Utils/File.hpp Normal file
View file

@ -0,0 +1,49 @@
#pragma once
// OpenCL SDK includes
#include <CL/Utils/Utils.hpp>
// STL includes
#include <fstream>
#include <string>
namespace cl {
namespace util {
// Scott Meyers, Effective STL, Addison-Wesley Professional, 2001, Item 29
// with error handling
UTILSCPP_EXPORT
std::string read_text_file(const char* const filename, cl_int* const error)
{
std::ifstream in(filename);
if (in.good())
{
try
{
std::string red((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
if (in.good() && in.eof())
{
if (error != nullptr) *error = CL_SUCCESS;
return red;
}
else
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"File read error!");
return std::string();
}
} catch (std::bad_alloc& ex)
{
detail::errHandler(CL_OUT_OF_RESOURCES, error,
"Bad allocation!");
return std::string();
}
}
else
{
detail::errHandler(CL_INVALID_VALUE, error, "No file!");
return std::string();
}
}
}
}

View file

@ -0,0 +1,18 @@
#pragma once
#include "OpenCLUtilsCpp_Export.h"
#include <CL/Utils/Error.hpp>
#include <CL/opencl.hpp>
namespace cl {
namespace util {
vector<cl_context_properties>
UTILSCPP_EXPORT get_interop_context_properties(const cl::Device& plat,
cl_int* error = nullptr);
Context UTILSCPP_EXPORT get_interop_context(int plat_id, int dev_id,
cl_device_type type,
cl_int* error = nullptr);
}
}

View file

@ -0,0 +1,42 @@
#ifndef UTILSCPP_EXPORT_H
#define UTILSCPP_EXPORT_H
#ifdef OPENCLUTILSCPP_STATIC_DEFINE
# define UTILSCPP_EXPORT
# define OPENCLUTILSCPP_NO_EXPORT
#else
# ifndef UTILSCPP_EXPORT
# ifdef OpenCLUtilsCpp_EXPORTS
/* We are building this library */
# define UTILSCPP_EXPORT
# else
/* We are using this library */
# define UTILSCPP_EXPORT
# endif
# endif
# ifndef OPENCLUTILSCPP_NO_EXPORT
# define OPENCLUTILSCPP_NO_EXPORT
# endif
#endif
#ifndef OPENCLUTILSCPP_DEPRECATED
# define OPENCLUTILSCPP_DEPRECATED __declspec(deprecated)
#endif
#ifndef OPENCLUTILSCPP_DEPRECATED_EXPORT
# define OPENCLUTILSCPP_DEPRECATED_EXPORT UTILSCPP_EXPORT OPENCLUTILSCPP_DEPRECATED
#endif
#ifndef OPENCLUTILSCPP_DEPRECATED_NO_EXPORT
# define OPENCLUTILSCPP_DEPRECATED_NO_EXPORT OPENCLUTILSCPP_NO_EXPORT OPENCLUTILSCPP_DEPRECATED
#endif
#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef OPENCLUTILSCPP_NO_DEPRECATED
# define OPENCLUTILSCPP_NO_DEPRECATED
# endif
#endif
#endif /* UTILSCPP_EXPORT_H */

View file

@ -0,0 +1,42 @@
#ifndef UTILS_EXPORT_H
#define UTILS_EXPORT_H
#ifdef OPENCLUTILS_STATIC_DEFINE
# define UTILS_EXPORT
# define OPENCLUTILS_NO_EXPORT
#else
# ifndef UTILS_EXPORT
# ifdef OpenCLUtils_EXPORTS
/* We are building this library */
# define UTILS_EXPORT
# else
/* We are using this library */
# define UTILS_EXPORT
# endif
# endif
# ifndef OPENCLUTILS_NO_EXPORT
# define OPENCLUTILS_NO_EXPORT
# endif
#endif
#ifndef OPENCLUTILS_DEPRECATED
# define OPENCLUTILS_DEPRECATED __declspec(deprecated)
#endif
#ifndef OPENCLUTILS_DEPRECATED_EXPORT
# define OPENCLUTILS_DEPRECATED_EXPORT UTILS_EXPORT OPENCLUTILS_DEPRECATED
#endif
#ifndef OPENCLUTILS_DEPRECATED_NO_EXPORT
# define OPENCLUTILS_DEPRECATED_NO_EXPORT OPENCLUTILS_NO_EXPORT OPENCLUTILS_DEPRECATED
#endif
#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef OPENCLUTILS_NO_DEPRECATED
# define OPENCLUTILS_NO_DEPRECATED
# endif
#endif
#endif /* UTILS_EXPORT_H */

View file

@ -0,0 +1,16 @@
#pragma once
#include "OpenCLUtilsCpp_Export.h"
#include <CL/Utils/Error.hpp>
#include <CL/opencl.hpp>
namespace cl {
namespace util {
bool UTILSCPP_EXPORT supports_extension(const cl::Platform& platform,
const cl::string& extension);
bool UTILSCPP_EXPORT platform_version_contains(
const cl::Platform& platform, const cl::string& version_fragment);
}
}

11
include/CL/Utils/Utils.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
// OpenCL Utils includes
#include "OpenCLUtils_Export.h"
#include <CL/Utils/Error.h>
#include <CL/Utils/File.h>
#include <CL/Utils/Context.h>
// OpenCL includes
#include <CL/cl.h>

View file

@ -0,0 +1,14 @@
#pragma once
// OpenCL Utils includes
#include "OpenCLUtils_Export.h"
#include <CL/Utils/Detail.hpp>
#include <CL/Utils/Error.hpp>
#include <CL/Utils/Platform.hpp>
#include <CL/Utils/Device.hpp>
#include <CL/Utils/Context.hpp>
#include <CL/Utils/Event.hpp>
// OpenCL includes
#include <CL/opencl.hpp>