84#ifndef NNGN_COMPUTE_COMPUTE_H
85#define NNGN_COMPUTE_COMPUTE_H
121 CPU = 1u << 0,
GPU = 1u << 1
176 constexpr explicit operator bool()
const {
return this->
id; }
212 const std::byte *
p = {};
216 constexpr auto begin()
const {
return this->
p; }
217 constexpr auto end()
const {
return this->p + this->
s; }
235 static std::unique_ptr<Compute>
create(
236 Backend b,
const void *params =
nullptr);
278 std::array<
std::
size_t, 3> buffer_origin,
279 std::array<
std::
size_t, 3> host_origin,
280 std::array<
std::
size_t, 3> region,
281 std::
size_t buffer_row_pitch,
std::
size_t buffer_slice_pitch,
282 std::
size_t host_row_pitch,
std::
size_t host_slice_pitch,
293 template<typename ...Ts>
361 template<typename ...Ts>
376 template<typename ...Ts>
400template<
std::ranges::range
T>
405 {
return Type::VECTOR_BEGIN <= t && t <= Type::VECTOR_END; }
407 {
return Type::HANDLE_BEGIN <= t && t <= Type::HANDLE_END; }
411inline auto arg_size(
const std::byte&) {
return sizeof(std::byte); }
417template<arithmetic T>
auto arg_size(
const T&) {
return sizeof(
T); }
423 auto s = std::span{
r};
424 return s.size_bytes();
427 {
return std::as_bytes(std::span{
r}).
data(); }
431template<Compute::Type t>
433 using T = std::underlying_type_t<Compute::Type>;
434 if constexpr(Type::SCALAR_BEGIN <= t && t < Type::VECTOR_BEGIN)
436 static_cast<T>(Compute::Type::VECTOR_BEGIN)
438 -
static_cast<T>(Compute::Type::BYTE));
449template<
typename ...Ts>
452 std::vector<std::byte>
data(std::reduce(cbegin(sizes), cend(sizes)));
454 auto copy = [p =
data.data(),
s = sizes.data()](
const auto &x)
mutable
460template<
typename ...Ts>
464 constexpr auto n =
sizeof...(Ts);
465 std::array<Type, n>
types = {};
466 std::array<std::size_t, n> sizes = {};
467 std::array<const std::byte*, n>
data = {};
469 [tp =
types.data(), sp = sizes.data(), dp =
data.data()]
470 <
typename T>(
const T &t)
mutable
478 program,
func,
n,
types.data(), sizes.data(),
data.data(), events);
481template<
typename ...Ts>
484 u32 n_dim,
const std::size_t *global_size,
486 constexpr auto n =
sizeof...(Ts);
487 std::array<Type, n>
types = {};
488 std::array<std::size_t, n> sizes = {};
489 std::array<const std::byte*, n>
data = {};
491 [tp =
types.data(), sp = sizes.data(), dp =
data.data()]
492 <
typename T>(
const T &t)
mutable
501 n,
types.data(), sizes.data(),
data.data(), events);
local class const
Definition animation.lua:7
Definition fundamental.h:28
assert
Definition debug.lua:3
local_size
Definition common.lua:28
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
local r
Definition gamma.lua:7
local data
Definition house0.lua:10
read_image
Definition img_common.lua:36
read_buffer
Definition img_common.lua:35
create_buffer
Definition img_common.lua:37
create_image
Definition img_common.lua:38
auto arg_ptr(const std::byte &b)
Definition compute.h:412
auto arg_size(const std::byte &)
Definition compute.h:411
std::uint32_t u32
Definition def.h:14
std::uint8_t u8
Definition def.h:12
std::uint64_t u64
Definition def.h:15
std::int32_t i32
Definition def.h:10
std::unique_ptr< Compute > compute_create_backend(const void *params)
auto as_bytes(const void *p)
Definition utils.h:158
func
Definition plot.lua:8
v[1]
Definition math.lua:22
#define NNGN_VIRTUAL(x)
Definition utils.h:27
local w
Definition strict.lua:12
static constexpr auto type
Definition compute.h:178
Argument type for raw memory passed to the execution kernel "by value".
Definition compute.h:210
constexpr DataArg(const T *t)
Convenience constructor to pass a single object as an argument.
Definition compute.h:215
constexpr auto begin() const
Definition compute.h:216
constexpr auto end() const
Definition compute.h:217
const std::byte * p
Definition compute.h:212
std::size_t s
Definition compute.h:211
Controls dependencies between operations.
Definition compute.h:190
std::size_t n_wait
Number of elements in wait_list.
Definition compute.h:192
Event *const * events
Buffer where the operation's resulting events will be placed.
Definition compute.h:201
const Event *const * wait_list
Events that must precede the execution of the operation.
Definition compute.h:194
Base class for handles to opaque compute objects.
Definition compute.h:174
u32 id
Definition compute.h:175
static constexpr auto type
Definition compute.h:179
Version version
Desired OpenCL version.
Definition compute.h:126
DeviceType preferred_device
Prefer this device type on initialization.
Definition compute.h:130
bool debug
Enables debugging, also required for profiling information.
Definition compute.h:128
static constexpr auto type
Definition compute.h:180
u32 major
Definition compute.h:123
u32 minor
Definition compute.h:123
Base class for computation back ends, which may be hardware-accelerated.
Definition compute.h:108
virtual std::size_t n_events(std::size_t n, const Type *types) const =0
Number of events generated by execute for arguments types.
virtual bool prof_info(ProfInfo info, std::size_t n, const Event *const *events, u64 *out) const =0
Collect profiling information from n events.
virtual size_t n_devices() const =0
virtual Kernel create_kernel(Program program, const char *func, std::size_t len, const Type *types, const std::size_t *sizes, const std::byte *const *data, Events events)=0
virtual bool release_kernel(Kernel k)=0
virtual bool release_buffer(Buffer b)=0
static constexpr bool is_vector_type(Type t)
Determines is t is one of the *V vector values in Type.
Definition compute.h:404
DeviceType
Definition compute.h:120
static constexpr bool is_handle_type(Type t)
Definition compute.h:406
virtual bool execute(Kernel kernel, ExecFlag flags, u32 n_dim, const std::size_t *global_size, const std::size_t *local_size, Events events) const =0
static std::unique_ptr< Compute > create(Backend b, const void *params=nullptr)
Creates a back end of the specified type.
Definition compute.cpp:7
bool write_struct(Buffer b, Events events, Ts &&...ts) const
Writes the variadic arguments sequentially to the buffer.
Definition compute.h:450
virtual bool write_buffer_rect(Buffer b, std::array< std::size_t, 3 > buffer_origin, std::array< std::size_t, 3 > host_origin, std::array< std::size_t, 3 > region, std::size_t buffer_row_pitch, std::size_t buffer_slice_pitch, std::size_t host_row_pitch, std::size_t host_slice_pitch, const std::byte *p, Events events) const =0
Writes a rectangular region of a buffer.
virtual bool release_image(Image i)=0
virtual std::string platform_name() const =0
virtual bool write_buffer(Buffer b, std::size_t off, std::size_t n, const std::byte *p, Events events) const =0
virtual Sampler create_sampler()=0
ProfInfo
Bit mask indicating which profiling data to collect/return.
Definition compute.h:169
@ QUEUED
Definition compute.h:170
@ PROF_INFO_MAX
Definition compute.h:171
@ SUBMIT
Definition compute.h:170
@ PROF_INFO_ALL
Definition compute.h:171
@ END
Definition compute.h:170
@ START
Definition compute.h:170
Limit
Indices for the values accessible via get_limits.
Definition compute.h:165
@ COMPUTE_UNITS
Definition compute.h:166
@ N
Definition compute.h:166
@ WORK_GROUP_SIZE
Definition compute.h:166
@ LOCAL_MEMORY
Definition compute.h:166
virtual bool fill_buffer(Buffer b, std::size_t off, std::size_t n, std::byte v, Events events) const =0
Fills the buffer with n copies of v.
virtual void * map_buffer(Buffer b, MemFlag flags, std::size_t off, std::size_t n, Events events) const =0
virtual bool release_events(std::size_t n, const Event *const *v) const =0
virtual bool fill_image(Image i, std::size_t w, std::size_t h, const void *v, Events events) const =0
Fills the image with copies of v.
virtual bool release_program(Program p)=0
static constexpr Type to_vector_type()
Transforms a scalar type into the equivalent vector type.
virtual Program create_program(std::string_view src, const char *opts)=0
Compiles source into a program using compilation options opts.
ExecFlag
Kernel execution flags.
Definition compute.h:155
@ BLOCKING
Block the execution until the operation finishes.
Definition compute.h:162
MemFlag
Properties of memory blocks.
Definition compute.h:151
@ READ_WRITE
Definition compute.h:152
@ READ_ONLY
Definition compute.h:152
@ WRITE_ONLY
Definition compute.h:152
virtual bool wait(std::size_t n, const Event *const *v) const =0
Block until the given events have completed.
static constexpr Type arg_type
Maps supported types to the equivalent Type value.
Definition compute.h:220
Type
Supported parameter types for kernel execution.
Definition compute.h:133
@ LOCAL
Device-local memory.
@ DATA
Pointer to raw memory.
Backend
Indicates which type of back end to create.
Definition compute.h:110
@ PSEUDOCOMP
No-op back end which can be used in lieu of a real one.
@ OPENCL_BACKEND
OpenCL 1.2 back end.
virtual size_t n_platforms() const =0
virtual std::string device_name() const =0
virtual bool init()=0
Must be called before the back end can be used.
virtual bool unmap_buffer(Buffer b, void *p, Events events) const =0
std::vector< u64 > get_limits() const
Convenience overload that allocates the required memory.
Definition compute.h:443
virtual bool release_sampler(Sampler s)=0
std::chrono::seconds s
Definition timing.cpp:6