2021-01-04 11:29:40 +01:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2021 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2021-01-13 19:56:14 +01:00
|
|
|
#include <dlfcn.h>
|
2021-01-04 11:29:40 +01:00
|
|
|
#include <CL/cl.h>
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
// Util
|
2021-01-04 11:29:40 +01:00
|
|
|
namespace ircd::cl
|
|
|
|
{
|
2021-01-05 23:50:53 +01:00
|
|
|
static bool is_error(const int &code) noexcept;
|
|
|
|
static int throw_on_error(const int &code);
|
|
|
|
template<class func, class... args> static int call(func&&, args&&...);
|
|
|
|
template<class T = string_view, class F, class id, class param> static T info(F&&, const id &, const param &, const mutable_buffer &);
|
2021-04-03 21:22:56 +02:00
|
|
|
template<class T = string_view, class F, class id0, class id1, class param> static T info(F&&, const id0 &, const id1 &, const param &, const mutable_buffer &);
|
2021-01-04 11:29:40 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
// Runtime state
|
|
|
|
namespace ircd::cl
|
|
|
|
{
|
|
|
|
static const size_t
|
2021-01-13 19:56:14 +01:00
|
|
|
OPTION_MAX {8},
|
2021-01-05 23:50:53 +01:00
|
|
|
PLATFORM_MAX {8},
|
|
|
|
DEVICE_MAX {8};
|
|
|
|
|
|
|
|
static uint
|
2021-01-13 19:56:14 +01:00
|
|
|
options,
|
2021-01-05 23:50:53 +01:00
|
|
|
platforms,
|
|
|
|
devices[PLATFORM_MAX];
|
|
|
|
|
2021-01-13 19:56:14 +01:00
|
|
|
static char
|
|
|
|
option[OPTION_MAX][256];
|
|
|
|
|
|
|
|
static void
|
|
|
|
*linkage;
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
static cl_platform_id
|
|
|
|
platform[PLATFORM_MAX];
|
|
|
|
|
|
|
|
static cl_device_id
|
|
|
|
device[PLATFORM_MAX][DEVICE_MAX];
|
|
|
|
|
|
|
|
static cl_context
|
|
|
|
primary;
|
|
|
|
|
2021-03-26 02:36:36 +01:00
|
|
|
extern struct stats
|
|
|
|
primary_stats;
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
static cl_command_queue
|
|
|
|
queue[PLATFORM_MAX][DEVICE_MAX];
|
|
|
|
|
|
|
|
static void handle_notify(const char *, const void *, size_t, void *) noexcept;
|
|
|
|
}
|
|
|
|
|
2021-03-26 02:36:36 +01:00
|
|
|
struct ircd::cl::stats
|
|
|
|
{
|
|
|
|
template<class T>
|
|
|
|
using item = ircd::stats::item<T>;
|
|
|
|
|
|
|
|
item<uint64_t>
|
|
|
|
alloc_count,
|
|
|
|
alloc_bytes,
|
|
|
|
dealloc_count,
|
|
|
|
dealloc_bytes,
|
|
|
|
exec_tasks,
|
|
|
|
exec_kern_tasks,
|
|
|
|
exec_kern_threads,
|
|
|
|
exec_kern_groups,
|
|
|
|
exec_barrier_tasks;
|
|
|
|
};
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
decltype(ircd::cl::log)
|
|
|
|
ircd::cl::log
|
|
|
|
{
|
|
|
|
"cl"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::cl::version_api)
|
|
|
|
ircd::cl::version_api
|
2021-01-04 11:29:40 +01:00
|
|
|
{
|
|
|
|
"OpenCL", info::versions::API, CL_TARGET_OPENCL_VERSION,
|
|
|
|
{
|
2021-01-05 23:50:53 +01:00
|
|
|
#if defined(CL_VERSION_MAJOR) && defined(CL_VERSION_MINOR) && defined(CL_VERSION_PATCH)
|
|
|
|
CL_VERSION_MAJOR(CL_TARGET_OPENCL_VERSION),
|
|
|
|
CL_VERSION_MINOR(CL_TARGET_OPENCL_VERSION),
|
|
|
|
CL_VERSION_PATCH(CL_TARGET_OPENCL_VERSION),
|
|
|
|
#endif
|
2021-01-04 11:29:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
decltype(ircd::cl::version_abi)
|
|
|
|
ircd::cl::version_abi
|
2021-01-04 11:29:40 +01:00
|
|
|
{
|
2021-01-13 19:56:14 +01:00
|
|
|
"OpenCL", info::versions::ABI
|
2021-01-04 11:29:40 +01:00
|
|
|
};
|
2021-01-05 23:50:53 +01:00
|
|
|
|
2021-01-16 01:40:42 +01:00
|
|
|
decltype(ircd::cl::enable)
|
|
|
|
ircd::cl::enable
|
|
|
|
{
|
|
|
|
{ "name", "ircd.cl.enable" },
|
|
|
|
{ "default", false },
|
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
2021-03-20 21:41:25 +01:00
|
|
|
decltype(ircd::cl::profile_queue)
|
|
|
|
ircd::cl::profile_queue
|
|
|
|
{
|
|
|
|
{ "name", "ircd.cl.profile.queue" },
|
|
|
|
{ "default", false },
|
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
2021-03-26 02:36:36 +01:00
|
|
|
decltype(ircd::cl::primary_stats)
|
|
|
|
ircd::cl::primary_stats
|
|
|
|
{
|
|
|
|
{ { "name", "ircd.cl.alloc.count" } },
|
|
|
|
{ { "name", "ircd.cl.alloc.bytes" } },
|
|
|
|
{ { "name", "ircd.cl.dealloc.count" } },
|
|
|
|
{ { "name", "ircd.cl.dealloc.bytes" } },
|
|
|
|
{ { "name", "ircd.cl.exec.tasks" } },
|
|
|
|
{ { "name", "ircd.cl.exec.kern.tasks" } },
|
|
|
|
{ { "name", "ircd.cl.exec.kern.threads" } },
|
|
|
|
};
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::cl::init::init()
|
|
|
|
{
|
2021-01-16 01:40:42 +01:00
|
|
|
if(!enable)
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "OpenCL hardware acceleration is not available or enabled."
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-13 19:56:14 +01:00
|
|
|
const ctx::posix::enable_pthread enable_pthread;
|
|
|
|
|
|
|
|
// Setup options
|
|
|
|
strlcpy{option[options++], "LP_NUM_THREADS=0"};
|
|
|
|
strlcpy{option[options++], "MESA_GLSL_CACHE_DISABLE=true"};
|
|
|
|
strlcpy{option[options++], "AMD_DEBUG=nogfx"};
|
|
|
|
assert(options <= OPTION_MAX);
|
|
|
|
|
|
|
|
// Configure options into the environment. TODO: XXX don't overwrite
|
|
|
|
while(options--)
|
|
|
|
sys::call(putenv, option[options]);
|
|
|
|
|
|
|
|
// Load the pipe.
|
|
|
|
assert(!linkage);
|
|
|
|
if(!(linkage = dlopen("libOpenCL.so", RTLD_LAZY | RTLD_GLOBAL)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// OpenCL sez platform=null is implementation defined.
|
|
|
|
info(clGetPlatformInfo, nullptr, CL_PLATFORM_VERSION, version_abi.string);
|
|
|
|
|
|
|
|
// Get the platforms.
|
2021-01-05 23:50:53 +01:00
|
|
|
call(clGetPlatformIDs, PLATFORM_MAX, platform, &platforms);
|
|
|
|
|
|
|
|
char buf[4][128];
|
|
|
|
for(size_t i(0); i < platforms; ++i)
|
2021-01-08 09:53:12 +01:00
|
|
|
log::logf
|
2021-01-05 23:50:53 +01:00
|
|
|
{
|
2021-01-08 09:53:12 +01:00
|
|
|
log, log::level::DEBUG,
|
|
|
|
"OpenCL:%d [%u][*] :%s :%s :%s :%s",
|
2021-01-05 23:50:53 +01:00
|
|
|
CL_TARGET_OPENCL_VERSION,
|
|
|
|
i,
|
|
|
|
info(clGetPlatformInfo, platform[i], CL_PLATFORM_VERSION, buf[0]),
|
|
|
|
info(clGetPlatformInfo, platform[i], CL_PLATFORM_VENDOR, buf[1]),
|
|
|
|
info(clGetPlatformInfo, platform[i], CL_PLATFORM_NAME, buf[2]),
|
|
|
|
info(clGetPlatformInfo, platform[i], CL_PLATFORM_EXTENSIONS, buf[3]),
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t devices_total(0);
|
|
|
|
for(size_t i(0); i < platforms; ++i)
|
|
|
|
{
|
|
|
|
static const auto type
|
|
|
|
{
|
|
|
|
CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR
|
|
|
|
};
|
|
|
|
|
|
|
|
call(clGetDeviceIDs, platform[i], type, DEVICE_MAX, device[i], devices + i);
|
|
|
|
devices_total += devices[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i(0); i < platforms; ++i)
|
|
|
|
for(size_t j(0); j < devices[i]; ++j)
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "OpenCL:%d [%u][%u] :%s :%s :%s :%s",
|
|
|
|
CL_TARGET_OPENCL_VERSION,
|
|
|
|
i,
|
|
|
|
j,
|
|
|
|
info(clGetDeviceInfo, device[i][j], CL_DEVICE_VERSION, buf[1]),
|
|
|
|
info(clGetDeviceInfo, device[i][j], CL_DEVICE_VENDOR, buf[2]),
|
|
|
|
info(clGetDeviceInfo, device[i][j], CL_DEVICE_NAME, buf[3]),
|
2021-01-08 09:53:12 +01:00
|
|
|
info(clGetDeviceInfo, device[i][j], CL_DRIVER_VERSION, buf[0]),
|
2021-01-05 23:50:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Gather all devices we'll use.
|
|
|
|
size_t _devs {0};
|
|
|
|
cl_device_id _dev[DEVICE_MAX];
|
|
|
|
for(size_t i(0); i < platforms; ++i)
|
|
|
|
for(size_t j(0); j < devices[i]; ++j)
|
|
|
|
_dev[_devs++] = device[i][j];
|
|
|
|
|
|
|
|
// Create a context from gathered devices.
|
|
|
|
cl_int err {CL_SUCCESS};
|
|
|
|
cl_context_properties ctxprop {0};
|
|
|
|
primary = clCreateContext(&ctxprop, _devs, _dev, handle_notify, nullptr, &err);
|
|
|
|
throw_on_error(err);
|
|
|
|
|
|
|
|
// Create a queue for each device.
|
2021-03-21 01:45:20 +01:00
|
|
|
//cl_command_queue_properties qprop {0};
|
|
|
|
cl_queue_properties qprop {0};
|
|
|
|
qprop = (profile_queue? CL_QUEUE_PROFILING_ENABLE: cl_queue_properties(0));
|
|
|
|
//qprop |= CL_QUEUE_ON_DEVICE;
|
|
|
|
//qprop |= CL_QUEUE_ON_DEVICE_DEFAULT;
|
|
|
|
//qprop |= CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
|
2021-01-05 23:50:53 +01:00
|
|
|
for(size_t i(0); i < platforms; ++i)
|
|
|
|
for(size_t j(0); j < devices[i]; ++j)
|
|
|
|
{
|
2021-03-21 01:45:20 +01:00
|
|
|
//queue[i][j] = clCreateCommandQueue(primary, device[i][j], qprop, &err);
|
|
|
|
queue[i][j] = clCreateCommandQueueWithProperties(primary, device[i][j], &qprop, &err);
|
2021-01-05 23:50:53 +01:00
|
|
|
throw_on_error(err);
|
|
|
|
}
|
2021-03-11 18:11:57 +01:00
|
|
|
|
|
|
|
work::init();
|
2021-01-05 23:50:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2021-01-13 19:56:14 +01:00
|
|
|
if(!linkage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ctx::posix::enable_pthread enable_pthread;
|
2021-01-05 23:50:53 +01:00
|
|
|
if(primary)
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-01-05 23:50:53 +01:00
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Shutting down OpenCL...",
|
|
|
|
};
|
|
|
|
|
2021-03-11 18:11:57 +01:00
|
|
|
work::fini();
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
for(size_t i(0); i < PLATFORM_MAX; ++i)
|
|
|
|
for(size_t j(0); j < DEVICE_MAX; ++j)
|
|
|
|
if(queue[i][j])
|
|
|
|
{
|
|
|
|
call(clReleaseCommandQueue, queue[i][j]);
|
|
|
|
queue[i][j] = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(primary)
|
|
|
|
{
|
|
|
|
call(clReleaseContext, primary);
|
|
|
|
primary = nullptr;
|
|
|
|
}
|
2021-01-13 19:56:14 +01:00
|
|
|
|
|
|
|
dlclose(linkage);
|
2021-01-05 23:50:53 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
//
|
|
|
|
// interface
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::sync()
|
|
|
|
{
|
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
|
|
|
call
|
|
|
|
(
|
|
|
|
clFinish, q
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::flush()
|
|
|
|
{
|
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
|
|
|
call
|
|
|
|
(
|
|
|
|
clFlush, q
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// exec
|
|
|
|
//
|
|
|
|
|
2021-03-11 18:11:57 +01:00
|
|
|
namespace ircd::cl
|
|
|
|
{
|
2021-03-21 01:45:20 +01:00
|
|
|
static const size_t _deps_list_max {32};
|
2021-03-13 00:50:27 +01:00
|
|
|
static thread_local cl_event _deps_list[_deps_list_max];
|
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
static void handle_submitted(cl::exec *const &, const exec::opts &);
|
2021-03-13 00:50:27 +01:00
|
|
|
static vector_view<cl_event> make_deps_default(cl::work *const &, const exec::opts &);
|
|
|
|
static vector_view<cl_event> make_deps(cl::work *const &, const exec::opts &);
|
2021-03-11 18:11:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::cl::work>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::cl::work>::allocator
|
|
|
|
{};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::cl::work>::list)
|
|
|
|
ircd::util::instance_list<ircd::cl::work>::list
|
|
|
|
{
|
|
|
|
allocator
|
|
|
|
};
|
|
|
|
|
2021-03-13 00:50:27 +01:00
|
|
|
decltype(ircd::cl::exec::opts_default)
|
|
|
|
ircd::cl::exec::opts_default;
|
|
|
|
|
|
|
|
ircd::cl::exec::exec(const opts &opts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto deps
|
|
|
|
{
|
|
|
|
make_deps(this, opts)
|
|
|
|
};
|
|
|
|
|
2021-03-17 23:50:38 +01:00
|
|
|
assert(!this->handle);
|
2021-03-13 00:50:27 +01:00
|
|
|
call
|
|
|
|
(
|
|
|
|
clEnqueueBarrierWithWaitList,
|
|
|
|
q,
|
|
|
|
deps.size(),
|
|
|
|
deps.size()? deps.data(): nullptr,
|
|
|
|
reinterpret_cast<cl_event *>(&this->handle)
|
|
|
|
);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
|
|
|
primary_stats.exec_barrier_tasks += 1;
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-03-13 00:50:27 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Exec Barrier :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
ircd::cl::exec::exec(kern &kern,
|
2021-03-13 00:50:27 +01:00
|
|
|
const kern::range &work,
|
|
|
|
const opts &opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
size_t dim(0);
|
|
|
|
for(size_t i(0); i < work.global.size(); ++i)
|
2021-03-26 02:36:36 +01:00
|
|
|
dim += work.global[i] > 0 && dim == i;
|
2021-01-08 09:53:12 +01:00
|
|
|
|
2021-03-25 04:01:15 +01:00
|
|
|
if(!dim)
|
|
|
|
return;
|
|
|
|
|
2021-03-11 18:11:57 +01:00
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
2021-03-13 00:50:27 +01:00
|
|
|
const auto deps
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-03-13 00:50:27 +01:00
|
|
|
make_deps(this, opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
};
|
|
|
|
|
2021-03-17 23:50:38 +01:00
|
|
|
assert(!this->handle);
|
2021-01-08 09:53:12 +01:00
|
|
|
call
|
|
|
|
(
|
|
|
|
clEnqueueNDRangeKernel,
|
|
|
|
q,
|
2021-02-11 10:55:46 +01:00
|
|
|
cl_kernel(kern.handle),
|
2021-01-08 09:53:12 +01:00
|
|
|
dim,
|
|
|
|
work.offset.data(),
|
|
|
|
work.global.data(),
|
|
|
|
work.local.data(),
|
2021-03-13 00:50:27 +01:00
|
|
|
deps.size(),
|
|
|
|
deps.size()? deps.data(): nullptr,
|
2021-01-08 09:53:12 +01:00
|
|
|
reinterpret_cast<cl_event *>(&this->handle)
|
|
|
|
);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
|
|
|
size_t global_size(work.global[0]);
|
|
|
|
for(size_t i(1); i < dim; ++i)
|
|
|
|
global_size *= work.global[i];
|
|
|
|
|
|
|
|
size_t local_size(work.local[0]);
|
|
|
|
for(size_t i(1); i < dim; ++i)
|
|
|
|
local_size *= work.local[i];
|
|
|
|
|
|
|
|
primary_stats.exec_kern_tasks += 1;
|
|
|
|
primary_stats.exec_kern_threads += global_size;
|
|
|
|
primary_stats.exec_kern_groups += global_size / local_size;
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Exec Kern :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:13:20 +01:00
|
|
|
ircd::cl::exec::exec(data &dst,
|
|
|
|
const data &src,
|
|
|
|
const opts &opts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto deps
|
|
|
|
{
|
|
|
|
make_deps(this, opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t size
|
|
|
|
{
|
|
|
|
opts.size?:
|
|
|
|
std::min(dst.size(), src.size())
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(!this->handle);
|
|
|
|
call
|
|
|
|
(
|
|
|
|
clEnqueueCopyBuffer,
|
|
|
|
q,
|
|
|
|
cl_mem(src.handle),
|
|
|
|
cl_mem(dst.handle),
|
|
|
|
opts.offset[1],
|
|
|
|
opts.offset[0],
|
|
|
|
size,
|
|
|
|
deps.size(),
|
|
|
|
deps.size()? deps.data(): nullptr,
|
|
|
|
reinterpret_cast<cl_event *>(&this->handle)
|
|
|
|
);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-03-19 19:13:20 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Exec Copy :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
ircd::cl::exec::exec(data &data,
|
|
|
|
const mutable_buffer &buf,
|
2021-03-13 00:50:27 +01:00
|
|
|
const opts &opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
try
|
|
|
|
{
|
2021-03-11 18:11:57 +01:00
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
2021-03-28 04:48:03 +02:00
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
opts.size?: ircd::size(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!size)
|
|
|
|
return;
|
|
|
|
|
2021-03-13 00:50:27 +01:00
|
|
|
const auto deps
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-03-13 00:50:27 +01:00
|
|
|
make_deps(this, opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
};
|
|
|
|
|
2021-03-17 23:50:38 +01:00
|
|
|
assert(!this->handle);
|
2021-01-08 09:53:12 +01:00
|
|
|
call
|
|
|
|
(
|
|
|
|
clEnqueueReadBuffer,
|
|
|
|
q,
|
2021-02-11 10:55:46 +01:00
|
|
|
cl_mem(data.handle),
|
2021-03-13 00:50:27 +01:00
|
|
|
opts.blocking,
|
2021-03-19 19:13:20 +01:00
|
|
|
opts.offset[0],
|
2021-03-28 04:48:03 +02:00
|
|
|
size,
|
2021-01-08 09:53:12 +01:00
|
|
|
ircd::data(buf),
|
2021-03-13 00:50:27 +01:00
|
|
|
deps.size(),
|
|
|
|
deps.size()? deps.data(): nullptr,
|
2021-01-08 09:53:12 +01:00
|
|
|
reinterpret_cast<cl_event *>(&this->handle)
|
|
|
|
);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2021-04-02 21:55:13 +02:00
|
|
|
log, "Exec Read data:%p cl_mem:%p buf:%p,%zu :%s",
|
|
|
|
&data,
|
|
|
|
data.handle,
|
|
|
|
ircd::data(buf),
|
|
|
|
ircd::size(buf),
|
2021-01-08 09:53:12 +01:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::exec::exec(data &data,
|
|
|
|
const const_buffer &buf,
|
2021-03-13 00:50:27 +01:00
|
|
|
const opts &opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
try
|
|
|
|
{
|
2021-03-11 18:11:57 +01:00
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
2021-03-28 04:48:03 +02:00
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
opts.size?: ircd::size(buf)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!size)
|
|
|
|
return;
|
|
|
|
|
2021-03-13 00:50:27 +01:00
|
|
|
const auto deps
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-03-13 00:50:27 +01:00
|
|
|
make_deps(this, opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
};
|
|
|
|
|
2021-03-18 01:15:03 +01:00
|
|
|
assert(!this->handle);
|
2021-01-08 09:53:12 +01:00
|
|
|
call
|
|
|
|
(
|
2021-03-10 14:22:40 +01:00
|
|
|
clEnqueueWriteBuffer,
|
2021-01-08 09:53:12 +01:00
|
|
|
q,
|
2021-02-11 10:55:46 +01:00
|
|
|
cl_mem(data.handle),
|
2021-03-13 00:50:27 +01:00
|
|
|
opts.blocking,
|
2021-03-19 19:13:20 +01:00
|
|
|
opts.offset[0],
|
2021-03-28 04:48:03 +02:00
|
|
|
size,
|
2021-01-08 09:53:12 +01:00
|
|
|
mutable_cast(ircd::data(buf)),
|
2021-03-13 00:50:27 +01:00
|
|
|
deps.size(),
|
|
|
|
deps.size()? deps.data(): nullptr,
|
2021-01-08 09:53:12 +01:00
|
|
|
reinterpret_cast<cl_event *>(&this->handle)
|
|
|
|
);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2021-04-02 21:55:13 +02:00
|
|
|
log, "Exec Write data:%p cl_mem:%p buf:%p,%zu :%s",
|
|
|
|
&data,
|
|
|
|
data.handle,
|
|
|
|
ircd::data(buf),
|
|
|
|
ircd::size(buf),
|
2021-01-08 09:53:12 +01:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:15:03 +01:00
|
|
|
ircd::cl::exec::exec(data &data,
|
2021-03-29 19:26:57 +02:00
|
|
|
const pair<size_t, off_t> &slice,
|
2021-03-18 01:15:03 +01:00
|
|
|
const read_closure &closure,
|
|
|
|
const opts &opts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
2021-03-29 19:26:57 +02:00
|
|
|
const auto size
|
2021-03-18 01:15:03 +01:00
|
|
|
{
|
2021-03-29 19:26:57 +02:00
|
|
|
slice.first?:
|
|
|
|
opts.size?:
|
|
|
|
data.size()
|
2021-03-18 01:15:03 +01:00
|
|
|
};
|
|
|
|
|
2021-03-29 19:26:57 +02:00
|
|
|
const auto offset
|
2021-03-18 01:15:03 +01:00
|
|
|
{
|
2021-03-29 19:26:57 +02:00
|
|
|
slice.second?:
|
|
|
|
opts.offset[0]
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(size_t(size) <= data.size());
|
|
|
|
assert(size_t(offset) <= data.size());
|
|
|
|
|
|
|
|
const auto deps
|
|
|
|
{
|
|
|
|
make_deps(this, opts)
|
2021-03-18 01:15:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
cl_map_flags flags {0};
|
|
|
|
flags |= CL_MAP_READ;
|
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
|
|
|
assert(!this->handle);
|
|
|
|
void *const ptr
|
|
|
|
{
|
|
|
|
clEnqueueMapBuffer
|
|
|
|
(
|
|
|
|
q,
|
|
|
|
cl_mem(data.handle),
|
|
|
|
opts.blocking,
|
|
|
|
flags,
|
2021-03-29 19:26:57 +02:00
|
|
|
offset,
|
2021-03-18 01:15:03 +01:00
|
|
|
size,
|
|
|
|
deps.size(),
|
|
|
|
deps.size()? deps.data(): nullptr,
|
|
|
|
reinterpret_cast<cl_event *>(&this->handle),
|
|
|
|
&err
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
throw_on_error(err);
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-03-18 01:15:03 +01:00
|
|
|
assert(this->handle);
|
|
|
|
assert(ptr);
|
|
|
|
|
2021-03-18 19:15:49 +01:00
|
|
|
// Perform the unmapping on unwind. This is after the mapping event
|
|
|
|
// completed and the closure was called below. The unmapping event will
|
|
|
|
// replace the event handle for this exec instance until its actual dtor;
|
|
|
|
// thus the lifetime of the exec we are constructing actually represents
|
|
|
|
// the unmapping event.
|
2021-03-26 02:43:04 +01:00
|
|
|
const unwind unmap{[this, &data, &q, &ptr, &opts]
|
2021-03-18 01:15:03 +01:00
|
|
|
{
|
|
|
|
assert(!this->handle);
|
|
|
|
call
|
|
|
|
(
|
|
|
|
clEnqueueUnmapMemObject,
|
|
|
|
q,
|
|
|
|
cl_mem(data.handle),
|
|
|
|
ptr,
|
|
|
|
0, // deps
|
|
|
|
nullptr, // depslist
|
|
|
|
reinterpret_cast<cl_event *>(&this->handle)
|
|
|
|
);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-03-18 01:15:03 +01:00
|
|
|
}};
|
|
|
|
|
2021-03-18 19:15:49 +01:00
|
|
|
// After the closure is called below, or throws, or if wait() throws,
|
|
|
|
// we free the completed map event here to allow for the unmap event.
|
|
|
|
const unwind rehandle{[this]
|
|
|
|
{
|
|
|
|
assert(this->handle);
|
|
|
|
call(clReleaseEvent, cl_event(this->handle));
|
|
|
|
this->handle = nullptr;
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Wait for the mapping to complete before presenting the buffer.
|
2021-03-18 01:15:03 +01:00
|
|
|
wait();
|
|
|
|
closure(const_buffer
|
|
|
|
{
|
|
|
|
reinterpret_cast<const char *>(ptr), size
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Exec Read Closure :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::exec::exec(data &data,
|
2021-03-29 19:26:57 +02:00
|
|
|
const pair<size_t, off_t> &slice,
|
2021-03-18 01:15:03 +01:00
|
|
|
const write_closure &closure,
|
|
|
|
const opts &opts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &q
|
|
|
|
{
|
|
|
|
queue[0][0]
|
|
|
|
};
|
|
|
|
|
2021-03-29 19:26:57 +02:00
|
|
|
const auto size
|
2021-03-18 01:15:03 +01:00
|
|
|
{
|
2021-03-29 19:26:57 +02:00
|
|
|
slice.first?:
|
|
|
|
opts.size?:
|
|
|
|
data.size()
|
2021-03-18 01:15:03 +01:00
|
|
|
};
|
|
|
|
|
2021-03-29 19:26:57 +02:00
|
|
|
const auto offset
|
2021-03-18 01:15:03 +01:00
|
|
|
{
|
2021-03-29 19:26:57 +02:00
|
|
|
slice.second?:
|
|
|
|
opts.offset[0]
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(size_t(size) <= data.size());
|
|
|
|
assert(size_t(offset) <= data.size());
|
|
|
|
|
|
|
|
const auto deps
|
|
|
|
{
|
|
|
|
make_deps(this, opts)
|
2021-03-18 01:15:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
cl_map_flags flags {0};
|
|
|
|
flags |= CL_MAP_WRITE;
|
|
|
|
flags |= opts.duplex? CL_MAP_READ: 0;
|
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
|
|
|
assert(!this->handle);
|
|
|
|
void *const ptr
|
|
|
|
{
|
|
|
|
clEnqueueMapBuffer
|
|
|
|
(
|
|
|
|
q,
|
|
|
|
cl_mem(data.handle),
|
|
|
|
opts.blocking,
|
|
|
|
flags,
|
2021-03-29 19:26:57 +02:00
|
|
|
offset,
|
2021-03-18 01:15:03 +01:00
|
|
|
size,
|
|
|
|
deps.size(),
|
|
|
|
deps.size()? deps.data(): nullptr,
|
|
|
|
reinterpret_cast<cl_event *>(&this->handle),
|
|
|
|
&err
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
throw_on_error(err);
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-03-18 01:15:03 +01:00
|
|
|
assert(this->handle);
|
|
|
|
assert(ptr);
|
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
const unwind unmap{[this, &data, &q, &ptr, &opts]
|
2021-03-18 01:15:03 +01:00
|
|
|
{
|
|
|
|
assert(!this->handle);
|
|
|
|
call
|
|
|
|
(
|
|
|
|
clEnqueueUnmapMemObject,
|
|
|
|
q,
|
|
|
|
cl_mem(data.handle),
|
|
|
|
ptr,
|
|
|
|
0, // deps
|
|
|
|
nullptr, // depslist
|
|
|
|
reinterpret_cast<cl_event *>(&this->handle)
|
|
|
|
);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
handle_submitted(this, opts);
|
2021-03-18 01:15:03 +01:00
|
|
|
}};
|
|
|
|
|
2021-03-18 19:15:49 +01:00
|
|
|
const unwind rehandle{[this]
|
|
|
|
{
|
|
|
|
assert(this->handle);
|
|
|
|
call(clReleaseEvent, cl_event(this->handle));
|
|
|
|
this->handle = nullptr;
|
|
|
|
}};
|
|
|
|
|
2021-03-18 01:15:03 +01:00
|
|
|
wait();
|
|
|
|
closure(mutable_buffer
|
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(ptr), size
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Exec Write Closure :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2021-03-26 02:43:04 +01:00
|
|
|
void
|
|
|
|
ircd::cl::handle_submitted(cl::exec *const &exec,
|
|
|
|
const exec::opts &opts)
|
|
|
|
{
|
|
|
|
primary_stats.exec_tasks += 1;
|
|
|
|
|
|
|
|
if(opts.flush)
|
|
|
|
cl::flush();
|
|
|
|
|
|
|
|
if(opts.sync)
|
|
|
|
cl::sync();
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:50:27 +01:00
|
|
|
ircd::vector_view<cl_event>
|
|
|
|
ircd::cl::make_deps(cl::work *const &work,
|
|
|
|
const exec::opts &opts)
|
2021-03-11 18:11:57 +01:00
|
|
|
{
|
2021-03-21 01:45:20 +01:00
|
|
|
//TODO: for out-of-order queue
|
|
|
|
if((false) && empty(opts.deps) && !opts.indep)
|
2021-03-13 00:50:27 +01:00
|
|
|
return make_deps_default(work, opts);
|
|
|
|
|
2021-03-19 07:24:41 +01:00
|
|
|
if(empty(opts.deps))
|
|
|
|
return {};
|
|
|
|
|
2021-03-11 18:11:57 +01:00
|
|
|
size_t ret(0);
|
2021-03-13 00:50:27 +01:00
|
|
|
vector_view<cl_event> out(_deps_list);
|
|
|
|
for(auto &exec : opts.deps)
|
|
|
|
out.at(ret++) = cl_event(exec.handle);
|
|
|
|
|
|
|
|
return vector_view<cl_event>
|
|
|
|
{
|
|
|
|
out, ret
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::vector_view<cl_event>
|
|
|
|
ircd::cl::make_deps_default(cl::work *const &work,
|
|
|
|
const exec::opts &opts)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
vector_view<cl_event> out(_deps_list);
|
2021-03-11 18:11:57 +01:00
|
|
|
for(auto it(rbegin(cl::work::list)); it != rend(cl::work::list); ++it)
|
|
|
|
{
|
|
|
|
cl::work *const &other{*it};
|
|
|
|
if(other == work)
|
|
|
|
continue;
|
|
|
|
|
2021-03-17 23:50:38 +01:00
|
|
|
if(!other->handle)
|
|
|
|
continue;
|
|
|
|
|
2021-03-11 18:11:57 +01:00
|
|
|
if(other->context != ctx::current)
|
|
|
|
continue;
|
|
|
|
|
2021-03-13 00:50:27 +01:00
|
|
|
out.at(ret++) = cl_event(other->handle);
|
2021-03-11 18:11:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:50:27 +01:00
|
|
|
return vector_view<cl_event>
|
|
|
|
{
|
|
|
|
out, ret
|
|
|
|
};
|
2021-03-11 18:11:57 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
//
|
|
|
|
// kern
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::cl::kern::kern(code &code,
|
|
|
|
const string_view &name)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int err {CL_SUCCESS};
|
2021-02-11 10:55:46 +01:00
|
|
|
handle = clCreateKernel(cl_program(code.handle), name.c_str(), &err);
|
2021-01-08 09:53:12 +01:00
|
|
|
throw_on_error(err);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Kernel Create '%s' :%s",
|
|
|
|
name,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::kern::kern(kern &&o)
|
|
|
|
noexcept
|
|
|
|
:handle{std::move(o.handle)}
|
|
|
|
{
|
|
|
|
o.handle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::kern &
|
|
|
|
ircd::cl::kern::operator=(kern &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~kern();
|
|
|
|
handle = std::move(o.handle);
|
|
|
|
o.handle = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::kern::~kern()
|
|
|
|
noexcept try
|
|
|
|
{
|
2021-03-22 19:06:54 +01:00
|
|
|
if(likely(handle))
|
|
|
|
call(clReleaseKernel, cl_kernel(handle));
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "Kernel Release :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::kern::arg(const int i,
|
|
|
|
data &data)
|
|
|
|
{
|
2021-02-11 10:55:46 +01:00
|
|
|
const auto &data_handle
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-02-11 10:55:46 +01:00
|
|
|
cl_mem(data.handle)
|
2021-01-08 09:53:12 +01:00
|
|
|
};
|
|
|
|
|
2021-02-11 10:55:46 +01:00
|
|
|
call(clSetKernelArg, cl_kernel(handle), i, sizeof(cl_mem), &data_handle);
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 19:05:45 +01:00
|
|
|
void
|
|
|
|
ircd::cl::kern::arg(const int i,
|
|
|
|
const const_buffer &buf)
|
|
|
|
{
|
|
|
|
call(clSetKernelArg, cl_kernel(handle), i, ircd::size(buf), ircd::data(buf));
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
//
|
|
|
|
// code
|
|
|
|
//
|
|
|
|
|
2021-03-10 12:03:03 +01:00
|
|
|
ircd::cl::code::code(const string_view &src,
|
|
|
|
const string_view &build_opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
:code
|
|
|
|
{
|
2021-03-10 12:03:03 +01:00
|
|
|
vector_view<const string_view>(&src, 1),
|
|
|
|
build_opts
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:03:03 +01:00
|
|
|
ircd::cl::code::code(const vector_view<const string_view> &srcs,
|
|
|
|
const string_view &build_opts)
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
|
|
|
static const size_t iov_max
|
|
|
|
{
|
|
|
|
64 //TODO: ???
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(srcs.size() > iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Maximum number of sources exceeded: lim:%zu got:%zu",
|
|
|
|
iov_max,
|
|
|
|
srcs.size(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t count
|
|
|
|
{
|
|
|
|
std::min(srcs.size(), iov_max)
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t len[count];
|
|
|
|
const char *src[count];
|
|
|
|
for(size_t i(0); i < count; ++i)
|
|
|
|
src[i] = ircd::data(srcs[i]),
|
|
|
|
len[i] = ircd::size(srcs[i]);
|
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
|
|
|
handle = clCreateProgramWithSource(primary, count, src, len, &err);
|
|
|
|
throw_on_error(err);
|
2021-03-10 12:03:03 +01:00
|
|
|
|
|
|
|
if(!null(build_opts))
|
|
|
|
build(build_opts);
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 01:46:31 +01:00
|
|
|
ircd::cl::code::code(const vector_view<const const_buffer> &bins,
|
|
|
|
const string_view &build_opts)
|
|
|
|
{
|
|
|
|
static const size_t iov_max
|
|
|
|
{
|
|
|
|
64 //TODO: ???
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(bins.size() > iov_max))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Maximum number of binaries exceeded: lim:%zu got:%zu",
|
|
|
|
iov_max,
|
|
|
|
bins.size(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t count
|
|
|
|
{
|
|
|
|
std::min(bins.size(), iov_max)
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t len[iov_max + 1] {0};
|
|
|
|
const uint8_t *bin[iov_max + 1] {nullptr};
|
|
|
|
for(size_t i(0); i < count; ++i)
|
|
|
|
bin[i] = reinterpret_cast<const uint8_t *>(ircd::data(bins[i])),
|
|
|
|
len[i] = ircd::size(bins[i]);
|
|
|
|
|
|
|
|
size_t devs {0};
|
|
|
|
cl_device_id dev[DEVICE_MAX] {0};
|
|
|
|
for(size_t i(0); i < platforms; ++i)
|
|
|
|
for(size_t j(0); j < devices[i]; ++j)
|
|
|
|
dev[devs++] = device[i][j];
|
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
|
|
|
int binerr[iov_max + 1] {CL_SUCCESS};
|
|
|
|
handle = clCreateProgramWithBinary(primary, devs, dev, len, bin, binerr, &err);
|
|
|
|
throw_on_error(err);
|
|
|
|
for(size_t i(0); i < count; ++i)
|
|
|
|
throw_on_error(binerr[i]);
|
|
|
|
|
|
|
|
if(!null(build_opts))
|
|
|
|
build(build_opts);
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
ircd::cl::code::code(code &&o)
|
|
|
|
noexcept
|
|
|
|
:handle{std::move(o.handle)}
|
|
|
|
{
|
|
|
|
o.handle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::code &
|
|
|
|
ircd::cl::code::operator=(code &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~code();
|
|
|
|
handle = std::move(o.handle);
|
|
|
|
o.handle = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::code::~code()
|
|
|
|
noexcept try
|
|
|
|
{
|
2021-03-22 19:06:54 +01:00
|
|
|
if(likely(handle))
|
|
|
|
call(clReleaseProgram, cl_program(handle));
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "Program Release :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ircd::cl
|
|
|
|
{
|
2021-03-19 18:11:10 +01:00
|
|
|
static void build_handle_error_log_line(const string_view &line);
|
|
|
|
static void build_handle_error(code &, const opencl_error &);
|
|
|
|
static void build_handle(cl_program program, void *priv);
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::code::build(const string_view &opts)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const uint num_devices {0};
|
|
|
|
const cl_device_id *const device_list {nullptr};
|
|
|
|
call
|
|
|
|
(
|
|
|
|
clBuildProgram,
|
2021-02-11 10:55:46 +01:00
|
|
|
cl_program(handle),
|
2021-01-08 09:53:12 +01:00
|
|
|
num_devices,
|
|
|
|
device_list,
|
|
|
|
opts.c_str(),
|
2021-03-19 18:11:10 +01:00
|
|
|
cl::build_handle,
|
2021-01-08 09:53:12 +01:00
|
|
|
nullptr
|
|
|
|
);
|
|
|
|
}
|
2021-03-19 18:11:10 +01:00
|
|
|
catch(const opencl_error &e)
|
|
|
|
{
|
|
|
|
build_handle_error(*this, e);
|
|
|
|
throw;
|
|
|
|
}
|
2021-01-08 09:53:12 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2021-03-19 18:11:10 +01:00
|
|
|
log::error
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-03-19 18:11:10 +01:00
|
|
|
log, "code(%p) :Failed to build :%s",
|
|
|
|
this,
|
|
|
|
e.what(),
|
|
|
|
};
|
2021-01-08 09:53:12 +01:00
|
|
|
|
2021-03-19 18:11:10 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::build_handle(cl_program program,
|
|
|
|
void *const priv)
|
|
|
|
{
|
|
|
|
ircd::always_assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::build_handle_error(code &code,
|
|
|
|
const opencl_error &e)
|
|
|
|
{
|
|
|
|
const auto string_closure
|
|
|
|
{
|
|
|
|
[&code](const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
size_t len {0}; call
|
|
|
|
(
|
|
|
|
clGetProgramBuildInfo,
|
|
|
|
cl_program(code.handle),
|
|
|
|
device[0][0],
|
|
|
|
CL_PROGRAM_BUILD_LOG,
|
|
|
|
ircd::size(buf),
|
|
|
|
ircd::data(buf),
|
|
|
|
&len
|
|
|
|
);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
};
|
2021-01-08 09:53:12 +01:00
|
|
|
|
|
|
|
const auto error_message
|
|
|
|
{
|
2021-03-19 18:11:10 +01:00
|
|
|
ircd::string(8_KiB | SHRINK_TO_FIT, string_closure)
|
2021-01-08 09:53:12 +01:00
|
|
|
};
|
|
|
|
|
2021-03-19 18:11:10 +01:00
|
|
|
const auto lines
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-03-19 18:11:10 +01:00
|
|
|
ircd::tokens(error_message, '\n', build_handle_error_log_line)
|
|
|
|
};
|
|
|
|
}
|
2021-01-08 09:53:12 +01:00
|
|
|
|
2021-03-19 18:11:10 +01:00
|
|
|
void
|
|
|
|
ircd::cl::build_handle_error_log_line(const string_view &line)
|
|
|
|
{
|
|
|
|
// note last line is just a CR
|
|
|
|
if(line.size() <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto &[loc, line_] { split(line, ' ') };
|
|
|
|
const auto &[fac, msg] { split(line_, ' ') };
|
|
|
|
const auto &[fname, pos] { split(loc, ':') };
|
|
|
|
const auto &[row, col] { split(pos, ':') };
|
|
|
|
|
|
|
|
const auto level
|
|
|
|
{
|
|
|
|
startswith(fac, "warning")?
|
|
|
|
log::level::WARNING:
|
|
|
|
|
|
|
|
startswith(fac, "error")?
|
|
|
|
log::level::ERROR:
|
|
|
|
|
|
|
|
log::level::ERROR
|
|
|
|
};
|
|
|
|
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, level, "%s", line
|
|
|
|
};
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// data
|
|
|
|
//
|
|
|
|
|
2021-03-14 23:21:05 +01:00
|
|
|
ircd::cl::data::data(const size_t size_,
|
|
|
|
const mutable_buffer &buf,
|
2021-01-08 09:53:12 +01:00
|
|
|
const bool wonly)
|
|
|
|
{
|
2021-03-14 23:21:05 +01:00
|
|
|
const auto ptr
|
|
|
|
{
|
|
|
|
ircd::size(buf)? ircd::data(buf): nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
ircd::size(buf)?: size_
|
|
|
|
};
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
cl_mem_flags flags {0};
|
2021-03-29 19:29:20 +02:00
|
|
|
flags |= wonly? CL_MEM_WRITE_ONLY: CL_MEM_READ_WRITE;
|
2021-03-14 23:21:05 +01:00
|
|
|
flags |= ircd::size(buf)? CL_MEM_COPY_HOST_PTR: 0;
|
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
|
|
|
handle = clCreateBuffer(primary, flags, size, ptr, &err);
|
|
|
|
throw_on_error(err);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
|
|
|
primary_stats.alloc_count += 1;
|
|
|
|
primary_stats.alloc_bytes += size;
|
2021-03-14 23:21:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::data::data(const size_t size_,
|
|
|
|
const const_buffer &buf)
|
|
|
|
{
|
|
|
|
const auto ptr
|
|
|
|
{
|
|
|
|
ircd::size(buf)? ircd::data(buf): nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
ircd::size(buf)?: size_
|
|
|
|
};
|
|
|
|
|
|
|
|
cl_mem_flags flags {0};
|
|
|
|
flags |= CL_MEM_READ_ONLY;
|
|
|
|
flags |= ircd::size(buf)? CL_MEM_COPY_HOST_PTR: 0;
|
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
|
|
|
handle = clCreateBuffer(primary, flags, size, mutable_cast(ptr), &err);
|
2021-01-08 09:53:12 +01:00
|
|
|
throw_on_error(err);
|
2021-03-26 02:36:36 +01:00
|
|
|
|
|
|
|
primary_stats.alloc_count += 1;
|
|
|
|
primary_stats.alloc_bytes += size;
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::data::data(const mutable_buffer &buf,
|
|
|
|
const bool wonly)
|
|
|
|
{
|
|
|
|
cl_mem_flags flags {0};
|
|
|
|
flags |= CL_MEM_USE_HOST_PTR;
|
|
|
|
flags |= wonly? CL_MEM_WRITE_ONLY: CL_MEM_READ_WRITE;
|
2021-03-14 23:21:05 +01:00
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
2021-01-08 09:53:12 +01:00
|
|
|
handle = clCreateBuffer(primary, flags, ircd::size(buf), ircd::data(buf), &err);
|
|
|
|
throw_on_error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::data::data(const const_buffer &buf)
|
|
|
|
{
|
|
|
|
cl_mem_flags flags {0};
|
|
|
|
flags |= CL_MEM_USE_HOST_PTR;
|
|
|
|
flags |= CL_MEM_READ_ONLY;
|
2021-03-14 23:21:05 +01:00
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
2021-01-08 09:53:12 +01:00
|
|
|
handle = clCreateBuffer(primary, flags, ircd::size(buf), mutable_cast(ircd::data(buf)), &err);
|
|
|
|
throw_on_error(err);
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:56:45 +02:00
|
|
|
ircd::cl::data::data(data &master,
|
|
|
|
const pair<size_t, off_t> &slice)
|
|
|
|
{
|
|
|
|
cl_mem_flags flags {0};
|
|
|
|
|
|
|
|
cl_buffer_region region {0};
|
|
|
|
region.origin = slice.second;
|
|
|
|
region.size = slice.first;
|
|
|
|
|
|
|
|
int err {CL_SUCCESS};
|
|
|
|
handle = clCreateSubBuffer(cl_mem(master.handle), flags, CL_BUFFER_CREATE_TYPE_REGION, ®ion, &err);
|
|
|
|
throw_on_error(err);
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
ircd::cl::data::data(data &&o)
|
|
|
|
noexcept
|
|
|
|
:handle{std::move(o.handle)}
|
|
|
|
{
|
|
|
|
o.handle = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::data &
|
|
|
|
ircd::cl::data::operator=(data &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~data();
|
|
|
|
handle = std::move(o.handle);
|
|
|
|
o.handle = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::data::~data()
|
|
|
|
noexcept try
|
|
|
|
{
|
2021-03-22 19:06:54 +01:00
|
|
|
if(likely(handle))
|
2021-03-26 02:36:36 +01:00
|
|
|
{
|
|
|
|
const auto size
|
|
|
|
{
|
|
|
|
this->size()
|
|
|
|
};
|
|
|
|
|
2021-03-22 19:06:54 +01:00
|
|
|
call(clReleaseMemObject, cl_mem(handle));
|
2021-03-26 02:36:36 +01:00
|
|
|
|
|
|
|
primary_stats.dealloc_count += 1;
|
|
|
|
primary_stats.dealloc_bytes += size;
|
|
|
|
}
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "Memory Release :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:12:58 +01:00
|
|
|
char *
|
|
|
|
ircd::cl::data::ptr()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(handle);
|
|
|
|
|
|
|
|
char buf[sizeof(void *)] {0};
|
|
|
|
return info<char *>(clGetMemObjectInfo, cl_mem(mutable_cast(handle)), CL_MEM_SIZE, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::cl::data::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(handle);
|
|
|
|
|
|
|
|
char buf[sizeof(size_t)] {0};
|
|
|
|
return info<size_t>(clGetMemObjectInfo, cl_mem(mutable_cast(handle)), CL_MEM_SIZE, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint
|
|
|
|
ircd::cl::data::flags()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(handle);
|
|
|
|
|
|
|
|
char buf[sizeof(uint)] {0};
|
|
|
|
return info<uint>(clGetMemObjectInfo, cl_mem(mutable_cast(handle)), CL_MEM_FLAGS, buf);
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
//
|
|
|
|
// cl::work (event)
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::cl
|
|
|
|
{
|
2021-03-18 01:56:01 +01:00
|
|
|
struct completion;
|
2021-01-08 09:53:12 +01:00
|
|
|
|
|
|
|
static void handle_event(cl_event, cl_int, void *) noexcept;
|
2021-03-20 22:33:40 +01:00
|
|
|
static int wait_status(work &, const int, const int);
|
2021-03-11 18:11:57 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 01:56:01 +01:00
|
|
|
struct ircd::cl::completion
|
|
|
|
{
|
|
|
|
cl_event event {nullptr};
|
|
|
|
cl_int status {CL_COMPLETE};
|
|
|
|
ctx::dock dock;
|
|
|
|
};
|
|
|
|
|
2021-03-11 18:11:57 +01:00
|
|
|
void
|
|
|
|
ircd::cl::work::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::work::fini()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
cl::sync();
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:33:40 +01:00
|
|
|
//
|
|
|
|
// work::work
|
|
|
|
//
|
2021-03-11 18:11:57 +01:00
|
|
|
|
2021-03-20 22:33:40 +01:00
|
|
|
ircd::cl::work::work()
|
2021-03-11 18:11:57 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::work::work(void *const &handle)
|
|
|
|
{
|
|
|
|
call(clRetainEvent, cl_event(handle));
|
|
|
|
this->handle = handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::cl::work::~work()
|
|
|
|
noexcept try
|
|
|
|
{
|
2021-03-20 22:33:40 +01:00
|
|
|
if(!handle)
|
|
|
|
return;
|
|
|
|
|
2021-03-18 19:15:49 +01:00
|
|
|
const unwind free{[this]
|
|
|
|
{
|
2021-03-20 22:33:40 +01:00
|
|
|
assert(handle);
|
|
|
|
call(clReleaseEvent, cl_event(handle));
|
2021-03-18 19:15:49 +01:00
|
|
|
}};
|
|
|
|
|
2021-03-17 23:50:38 +01:00
|
|
|
wait();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-03-17 23:50:38 +01:00
|
|
|
log, "Work Release :%s",
|
|
|
|
e.what(),
|
2021-01-08 09:53:12 +01:00
|
|
|
};
|
|
|
|
|
2021-03-17 23:50:38 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:33:40 +01:00
|
|
|
void
|
|
|
|
ircd::cl::work::wait(const uint desired)
|
2021-03-18 21:21:10 +01:00
|
|
|
try
|
2021-03-17 23:50:38 +01:00
|
|
|
{
|
2021-03-20 22:33:40 +01:00
|
|
|
static_assert(CL_COMPLETE == 0);
|
|
|
|
assert(handle);
|
2021-03-17 23:50:38 +01:00
|
|
|
|
2021-03-18 21:21:10 +01:00
|
|
|
char buf[4];
|
|
|
|
int status
|
2021-01-08 09:53:12 +01:00
|
|
|
{
|
2021-03-17 23:50:38 +01:00
|
|
|
info<int>(clGetEventInfo, cl_event(handle), CL_EVENT_COMMAND_EXECUTION_STATUS, buf)
|
2021-03-11 18:11:57 +01:00
|
|
|
};
|
2021-01-08 09:53:12 +01:00
|
|
|
|
2021-03-20 22:33:40 +01:00
|
|
|
if(status > int(desired))
|
|
|
|
status = wait_status(*this, status, desired);
|
2021-01-08 09:53:12 +01:00
|
|
|
|
2021-03-18 21:21:10 +01:00
|
|
|
if(unlikely(status < 0))
|
|
|
|
throw_on_error(status);
|
|
|
|
|
2021-03-20 22:33:40 +01:00
|
|
|
assert(int(status) == int(desired));
|
2021-01-08 09:53:12 +01:00
|
|
|
}
|
2021-03-18 21:21:10 +01:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2021-03-20 22:33:40 +01:00
|
|
|
log, "work(%p)::wait(%u) :%s",
|
2021-03-18 21:21:10 +01:00
|
|
|
this,
|
2021-03-20 22:33:40 +01:00
|
|
|
desired,
|
2021-03-18 21:21:10 +01:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2021-01-08 09:53:12 +01:00
|
|
|
|
|
|
|
std::array<uint64_t, 4>
|
|
|
|
ircd::cl::work::profile()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto handle
|
|
|
|
{
|
2021-02-11 10:55:46 +01:00
|
|
|
cl_event(this->handle)
|
2021-01-08 09:53:12 +01:00
|
|
|
};
|
|
|
|
|
2021-04-02 21:53:49 +02:00
|
|
|
if(!profile_queue || !handle)
|
|
|
|
return {0};
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
char buf[4][8];
|
|
|
|
return std::array<uint64_t, 4>
|
|
|
|
{
|
|
|
|
info<size_t>(clGetEventProfilingInfo, handle, CL_PROFILING_COMMAND_QUEUED, buf[0]),
|
|
|
|
info<size_t>(clGetEventProfilingInfo, handle, CL_PROFILING_COMMAND_SUBMIT, buf[1]),
|
|
|
|
info<size_t>(clGetEventProfilingInfo, handle, CL_PROFILING_COMMAND_START, buf[2]),
|
|
|
|
info<size_t>(clGetEventProfilingInfo, handle, CL_PROFILING_COMMAND_END, buf[3]),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-20 22:33:40 +01:00
|
|
|
int
|
|
|
|
ircd::cl::wait_status(work &work,
|
|
|
|
const int status,
|
|
|
|
const int desired)
|
|
|
|
{
|
|
|
|
assert(status > desired);
|
|
|
|
assert(work.handle);
|
|
|
|
|
|
|
|
completion c
|
|
|
|
{
|
|
|
|
cl_event(work.handle),
|
|
|
|
status,
|
|
|
|
};
|
|
|
|
|
|
|
|
call
|
|
|
|
(
|
|
|
|
clSetEventCallback,
|
|
|
|
c.event,
|
|
|
|
desired,
|
|
|
|
&handle_event,
|
|
|
|
&c
|
|
|
|
);
|
|
|
|
|
|
|
|
c.dock.wait([&c, &desired]
|
|
|
|
{
|
|
|
|
return !c.event || c.status <= desired;
|
|
|
|
});
|
|
|
|
|
|
|
|
return c.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::handle_event(cl_event event,
|
|
|
|
cl_int status,
|
|
|
|
void *const priv)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
auto *const c
|
|
|
|
{
|
|
|
|
reinterpret_cast<completion *>(priv)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(event != nullptr);
|
|
|
|
c->status = status;
|
|
|
|
c->dock.notify_one();
|
|
|
|
}
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
//
|
|
|
|
// callback surface
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::cl::handle_notify(const char *errstr,
|
|
|
|
const void *token,
|
|
|
|
size_t cb,
|
|
|
|
void *priv)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(errstr)
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "OpenCL t:%p cb:%zu :%s",
|
|
|
|
token,
|
|
|
|
cb,
|
|
|
|
errstr,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class F,
|
|
|
|
class id,
|
|
|
|
class param>
|
|
|
|
T
|
|
|
|
ircd::cl::info(F&& func,
|
|
|
|
const id &i,
|
|
|
|
const param &p,
|
|
|
|
const mutable_buffer &out)
|
|
|
|
{
|
2021-01-08 09:53:12 +01:00
|
|
|
using ircd::data;
|
|
|
|
using ircd::size;
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
size_t len {0};
|
|
|
|
call(std::forward<F>(func), i, p, size(out), data(out), &len);
|
|
|
|
const string_view str
|
|
|
|
{
|
|
|
|
data(out), len
|
|
|
|
};
|
|
|
|
|
2021-01-08 09:53:12 +01:00
|
|
|
return byte_view<T>(str);
|
2021-01-05 23:50:53 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 21:22:56 +02:00
|
|
|
template<class T,
|
|
|
|
class F,
|
|
|
|
class id0,
|
|
|
|
class id1,
|
|
|
|
class param>
|
|
|
|
T
|
|
|
|
ircd::cl::info(F&& func,
|
|
|
|
const id0 &i0,
|
|
|
|
const id1 &i1,
|
|
|
|
const param &p,
|
|
|
|
const mutable_buffer &out)
|
|
|
|
{
|
|
|
|
using ircd::data;
|
|
|
|
using ircd::size;
|
|
|
|
|
|
|
|
size_t len {0};
|
|
|
|
call(std::forward<F>(func), i0, i1, p, size(out), data(out), &len);
|
|
|
|
const string_view str
|
|
|
|
{
|
|
|
|
data(out), len
|
|
|
|
};
|
|
|
|
|
|
|
|
return byte_view<T>(str);
|
|
|
|
}
|
|
|
|
|
2021-01-05 23:50:53 +01:00
|
|
|
template<class func,
|
|
|
|
class... args>
|
|
|
|
int
|
|
|
|
ircd::cl::call(func&& f,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
const int ret
|
|
|
|
{
|
|
|
|
f(std::forward<args>(a)...)
|
|
|
|
};
|
|
|
|
|
|
|
|
return throw_on_error(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ircd::cl::throw_on_error(const int &code)
|
|
|
|
{
|
|
|
|
if(unlikely(is_error(code)))
|
|
|
|
throw opencl_error
|
|
|
|
{
|
2021-03-19 18:11:10 +01:00
|
|
|
"(%d) :%s",
|
2021-01-05 23:50:53 +01:00
|
|
|
code,
|
|
|
|
reflect_error(code),
|
|
|
|
};
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::cl::is_error(const int &code)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return code < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::cl::reflect_error(const int code)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
switch(code)
|
|
|
|
{
|
|
|
|
case CL_SUCCESS: return "SUCCESS";
|
|
|
|
case CL_DEVICE_NOT_FOUND: return "DEVICE_NOT_FOUND";
|
|
|
|
case CL_DEVICE_NOT_AVAILABLE: return "DEVICE_NOT_AVAILABLE";
|
|
|
|
case CL_COMPILER_NOT_AVAILABLE: return "COMPILER_NOT_AVAILABLE";
|
|
|
|
case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "MEM_OBJECT_ALLOCATION_FAILURE";
|
|
|
|
case CL_OUT_OF_RESOURCES: return "OUT_OF_RESOURCES";
|
|
|
|
case CL_OUT_OF_HOST_MEMORY: return "OUT_OF_HOST_MEMORY";
|
|
|
|
case CL_PROFILING_INFO_NOT_AVAILABLE: return "PROFILING_INFO_NOT_AVAILABLE";
|
|
|
|
case CL_MEM_COPY_OVERLAP: return "MEM_COPY_OVERLAP";
|
|
|
|
case CL_IMAGE_FORMAT_MISMATCH: return "IMAGE_FORMAT_MISMATCH";
|
|
|
|
case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "IMAGE_FORMAT_NOT_SUPPORTED";
|
|
|
|
case CL_BUILD_PROGRAM_FAILURE: return "BUILD_PROGRAM_FAILURE";
|
|
|
|
case CL_MAP_FAILURE: return "MAP_FAILURE";
|
|
|
|
case CL_INVALID_VALUE: return "INVALID_VALUE";
|
|
|
|
case CL_INVALID_DEVICE_TYPE: return "INVALID_DEVICE_TYPE";
|
|
|
|
case CL_INVALID_PLATFORM: return "INVALID_PLATFORM";
|
|
|
|
case CL_INVALID_DEVICE: return "INVALID_DEVICE";
|
|
|
|
case CL_INVALID_CONTEXT: return "INVALID_CONTEXT";
|
|
|
|
case CL_INVALID_QUEUE_PROPERTIES: return "INVALID_QUEUE_PROPERTIES";
|
|
|
|
case CL_INVALID_COMMAND_QUEUE: return "INVALID_COMMAND_QUEUE";
|
|
|
|
case CL_INVALID_HOST_PTR: return "INVALID_HOST_PTR";
|
|
|
|
case CL_INVALID_MEM_OBJECT: return "INVALID_MEM_OBJECT";
|
|
|
|
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "INVALID_IMAGE_FORMAT_DESCRIPTOR";
|
|
|
|
case CL_INVALID_IMAGE_SIZE: return "INVALID_IMAGE_SIZE";
|
|
|
|
case CL_INVALID_SAMPLER: return "INVALID_SAMPLER";
|
|
|
|
case CL_INVALID_BINARY: return "INVALID_BINARY";
|
|
|
|
case CL_INVALID_BUILD_OPTIONS: return "INVALID_BUILD_OPTIONS";
|
|
|
|
case CL_INVALID_PROGRAM: return "INVALID_PROGRAM";
|
|
|
|
case CL_INVALID_PROGRAM_EXECUTABLE: return "INVALID_PROGRAM_EXECUTABLE";
|
|
|
|
case CL_INVALID_KERNEL_NAME: return "INVALID_KERNEL_NAME";
|
|
|
|
case CL_INVALID_KERNEL_DEFINITION: return "INVALID_KERNEL_DEFINITION";
|
|
|
|
case CL_INVALID_KERNEL: return "INVALID_KERNEL";
|
|
|
|
case CL_INVALID_ARG_INDEX: return "INVALID_ARG_INDEX";
|
|
|
|
case CL_INVALID_ARG_VALUE: return "INVALID_ARG_VALUE";
|
|
|
|
case CL_INVALID_ARG_SIZE: return "INVALID_ARG_SIZE";
|
|
|
|
case CL_INVALID_KERNEL_ARGS: return "INVALID_KERNEL_ARGS";
|
|
|
|
case CL_INVALID_WORK_DIMENSION: return "INVALID_WORK_DIMENSION";
|
|
|
|
case CL_INVALID_WORK_GROUP_SIZE: return "INVALID_WORK_GROUP_SIZE";
|
|
|
|
case CL_INVALID_WORK_ITEM_SIZE: return "INVALID_WORK_ITEM_SIZE";
|
|
|
|
case CL_INVALID_GLOBAL_OFFSET: return "INVALID_GLOBAL_OFFSET";
|
|
|
|
case CL_INVALID_EVENT_WAIT_LIST: return "INVALID_EVENT_WAIT_LIST";
|
|
|
|
case CL_INVALID_EVENT: return "INVALID_EVENT";
|
|
|
|
case CL_INVALID_OPERATION: return "INVALID_OPERATION";
|
|
|
|
case CL_INVALID_GL_OBJECT: return "INVALID_GL_OBJECT";
|
|
|
|
case CL_INVALID_BUFFER_SIZE: return "INVALID_BUFFER_SIZE";
|
|
|
|
case CL_INVALID_MIP_LEVEL: return "INVALID_MIP_LEVEL";
|
|
|
|
case CL_INVALID_GLOBAL_WORK_SIZE: return "INVALID_GLOBAL_WORK_SIZE";
|
|
|
|
|
|
|
|
#ifdef CL_VERSION_1_1
|
|
|
|
case CL_INVALID_PROPERTY: return "INVALID_PROPERTY";
|
|
|
|
case CL_MISALIGNED_SUB_BUFFER_OFFSET: return "MISALIGNED_SUB_BUFFER_OFFSET";
|
|
|
|
case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST: return "EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CL_VERSION_1_2
|
|
|
|
case CL_COMPILE_PROGRAM_FAILURE: return "COMPILE_PROGRAM_FAILURE";
|
|
|
|
case CL_LINKER_NOT_AVAILABLE: return "LINKER_NOT_AVAILABLE";
|
|
|
|
case CL_LINK_PROGRAM_FAILURE: return "LINK_PROGRAM_FAILURE";
|
|
|
|
case CL_DEVICE_PARTITION_FAILED: return "DEVICE_PARTITION_FAILED";
|
|
|
|
case CL_KERNEL_ARG_INFO_NOT_AVAILABLE: return "KERNEL_ARG_INFO_NOT_AVAILABLE";
|
|
|
|
case CL_INVALID_IMAGE_DESCRIPTOR: return "INVALID_IMAGE_DESCRIPTOR";
|
|
|
|
case CL_INVALID_COMPILER_OPTIONS: return "INVALID_COMPILER_OPTIONS";
|
|
|
|
case CL_INVALID_LINKER_OPTIONS: return "INVALID_LINKER_OPTIONS";
|
|
|
|
case CL_INVALID_DEVICE_PARTITION_COUNT: return "INVALID_DEVICE_PARTITION_COUNT";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CL_VERSION_2_0
|
|
|
|
case CL_INVALID_PIPE_SIZE: return "INVALID_PIPE_SIZE";
|
|
|
|
case CL_INVALID_DEVICE_QUEUE: return "INVALID_DEVICE_QUEUE";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CL_VERSION_2_2
|
|
|
|
case CL_INVALID_SPEC_ID: return "INVALID_SPEC_ID";
|
|
|
|
case CL_MAX_SIZE_RESTRICTION_EXCEEDED: return "MAX_SIZE_RESTRICTION_EXCEEDED";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return "???????";
|
|
|
|
}
|