Skip to content

Commit

Permalink
Use array for clUpdateMutableCommandsKHR
Browse files Browse the repository at this point in the history
Implementation change to prototype specification changes proposed
in KhronosGroup/OpenCL-Docs#1041

Uses OpenCL-Docs and OpenCL-Header changes from
* KhronosGroup/OpenCL-Docs#1045
* KhronosGroup/OpenCL-Headers#245
  • Loading branch information
EwanC committed Jan 17, 2024
1 parent e71008b commit 21514f0
Show file tree
Hide file tree
Showing 9 changed files with 693 additions and 1,283 deletions.
12 changes: 6 additions & 6 deletions source/cl/examples/MutableDispatchKHR/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ int main(const int argc, const char **argv) {
cl_mutable_dispatch_arg_khr arg_2{2, sizeof(cl_mem), &output_buffer};
cl_mutable_dispatch_arg_khr args[] = {arg_0, arg_1, arg_2};
cl_mutable_dispatch_config_khr dispatch_config{
CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR,
nullptr,
command_handle,
3 /* num_args */,
0 /* num_svm_arg */,
Expand All @@ -227,12 +225,14 @@ int main(const int argc, const char **argv) {
nullptr /* global_work_offset */,
nullptr /* global_work_size */,
nullptr /* local_work_size */};
cl_mutable_base_config_khr mutable_config{
CL_STRUCTURE_TYPE_MUTABLE_BASE_CONFIG_KHR, nullptr, 1,
&dispatch_config};

// Update the command buffer with the mutable configuration
error = clUpdateMutableCommandsKHR(command_buffer, &mutable_config);
cl_uint num_configs = 1;
cl_update_config_type_khr config_types[1] = {
CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR};
const void *configs[1] = {&dispatch_config};
error = clUpdateMutableCommandsKHR(command_buffer, num_configs,
config_types, configs);
CL_CHECK(error);
}

Expand Down
26 changes: 8 additions & 18 deletions source/cl/external/OpenCL-Headers/CL/cl_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ extern CL_API_ENTRY cl_int CL_API_CALL clGetCommandBufferInfoKHR(
#define CL_KHR_COMMAND_BUFFER_MUTABLE_DISPATCH_EXTENSION_NAME \
"cl_khr_command_buffer_mutable_dispatch"

typedef cl_uint cl_command_buffer_structure_type_khr;
typedef cl_uint cl_update_config_type_khr;
typedef cl_bitfield cl_mutable_dispatch_fields_khr;
typedef cl_uint cl_mutable_command_info_khr;
typedef struct _cl_mutable_dispatch_arg_khr {
Expand All @@ -286,8 +286,6 @@ typedef struct _cl_mutable_dispatch_exec_info_khr {
const void *param_value;
} cl_mutable_dispatch_exec_info_khr;
typedef struct _cl_mutable_dispatch_config_khr {
cl_command_buffer_structure_type_khr type;
const void *next;
cl_mutable_command_khr command;
cl_uint num_args;
cl_uint num_svm_args;
Expand All @@ -300,13 +298,6 @@ typedef struct _cl_mutable_dispatch_config_khr {
const size_t *global_work_size;
const size_t *local_work_size;
} cl_mutable_dispatch_config_khr;
typedef struct _cl_mutable_base_config_khr {
cl_command_buffer_structure_type_khr type;
const void *next;
cl_uint num_mutable_dispatch;
const cl_mutable_dispatch_config_khr *mutable_dispatch_list;
} cl_mutable_base_config_khr;

/* cl_command_buffer_flags_khr - bitfield */
#define CL_COMMAND_BUFFER_MUTABLE_KHR (1 << 1)

Expand Down Expand Up @@ -337,23 +328,22 @@ typedef struct _cl_mutable_base_config_khr {
#define CL_MUTABLE_DISPATCH_GLOBAL_WORK_SIZE_KHR 0x12A6
#define CL_MUTABLE_DISPATCH_LOCAL_WORK_SIZE_KHR 0x12A7

/* cl_command_buffer_structure_type_khr */
#define CL_STRUCTURE_TYPE_MUTABLE_BASE_CONFIG_KHR 0
#define CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR 1
/* cl_update_config_type_khr */
#define CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR 0

typedef cl_int(CL_API_CALL *clUpdateMutableCommandsKHR_fn)(
cl_command_buffer_khr command_buffer,
const cl_mutable_base_config_khr *mutable_config);
cl_command_buffer_khr command_buffer, cl_uint num_configs,
const cl_update_config_type_khr *config_types, const void **configs);

typedef cl_int(CL_API_CALL *clGetMutableCommandInfoKHR_fn)(
cl_mutable_command_khr command, cl_mutable_command_info_khr param_name,
size_t param_value_size, void *param_value, size_t *param_value_size_ret);

#ifndef CL_NO_PROTOTYPES

extern CL_API_ENTRY cl_int CL_API_CALL
clUpdateMutableCommandsKHR(cl_command_buffer_khr command_buffer,
const cl_mutable_base_config_khr *mutable_config);
extern CL_API_ENTRY cl_int CL_API_CALL clUpdateMutableCommandsKHR(
cl_command_buffer_khr command_buffer, cl_uint num_configs,
const cl_update_config_type_khr *config_types, const void **configs);

extern CL_API_ENTRY cl_int CL_API_CALL clGetMutableCommandInfoKHR(
cl_mutable_command_khr command, cl_mutable_command_info_khr param_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ struct _cl_command_buffer_khr final : public cl::base<_cl_command_buffer_khr> {
/// @param[in] mutable_config New configuration for one or more commands
///
/// @return CL_SUCCESS or appropriate OpenCL error code.
cl_int updateCommandBuffer(const cl_mutable_base_config_khr &mutable_config);
cl_int updateCommandBuffer(
cargo::array_view<const cl_mutable_dispatch_config_khr *>
&mutable_configs);

/// @brief Verifies whether a queue is compatible with the command-buffer.
///
Expand Down
30 changes: 13 additions & 17 deletions source/cl/source/extension/source/khr_command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,19 +1071,15 @@ cargo::expected<mux_descriptor_info_s, cl_int> createArgumentDescriptor(
} // anonymous namespace

[[nodiscard]] cl_int _cl_command_buffer_khr::updateCommandBuffer(
const cl_mutable_base_config_khr &mutable_config) {
cargo::array_view<const cl_mutable_dispatch_config_khr *>
&mutable_dispatch_configs) {
std::lock_guard<std::mutex> guard(mutex);
const cl_device_id device = command_queue->device;

cargo::array_view<const cl_mutable_dispatch_config_khr>
mutable_dispatch_configs(mutable_config.mutable_dispatch_list,
mutable_config.num_mutable_dispatch);

// Verify struct configures kernel arguments and return error if malformed
for (const auto &config : mutable_dispatch_configs) {
OCL_CHECK(config.type != CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR,
return CL_INVALID_VALUE);

for (const auto config_ptr : mutable_dispatch_configs) {
OCL_CHECK(config_ptr == nullptr, return CL_INVALID_VALUE);
cl_mutable_dispatch_config_khr config = *config_ptr;
OCL_CHECK(!config.command, return CL_INVALID_MUTABLE_COMMAND_KHR);
OCL_CHECK(config.command->command_buffer != this,
return CL_INVALID_MUTABLE_COMMAND_KHR);
Expand All @@ -1099,7 +1095,7 @@ cargo::expected<mux_descriptor_info_s, cl_int> createArgumentDescriptor(

for (auto config : mutable_dispatch_configs) {
unsigned update_index = 0;
const unsigned num_args = config.num_args + config.num_svm_args;
const unsigned num_args = config->num_args + config->num_svm_args;
UpdateInfo update_info;
if (update_info.descriptors.alloc(num_args)) {
return CL_OUT_OF_HOST_MEMORY;
Expand All @@ -1109,16 +1105,16 @@ cargo::expected<mux_descriptor_info_s, cl_int> createArgumentDescriptor(
return CL_OUT_OF_HOST_MEMORY;
}

if (update_info.pointers.alloc(config.num_svm_args)) {
if (update_info.pointers.alloc(config->num_svm_args)) {
return CL_OUT_OF_HOST_MEMORY;
}

const auto mutable_command = config.command;
const auto mutable_command = config->command;
update_info.id = mutable_command->id;
cargo::array_view<const cl_mutable_dispatch_arg_khr> args(config.arg_list,
config.num_args);
cargo::array_view<const cl_mutable_dispatch_arg_khr> args(config->arg_list,
config->num_args);

for (unsigned i = 0; i < config.num_args; ++i) {
for (unsigned i = 0; i < config->num_args; ++i) {
auto arg = args[i];
update_info.indices[update_index] = arg.arg_index;
auto descriptor =
Expand All @@ -1134,9 +1130,9 @@ cargo::expected<mux_descriptor_info_s, cl_int> createArgumentDescriptor(

#ifdef OCL_EXTENSION_cl_intel_unified_shared_memory
cargo::array_view<const cl_mutable_dispatch_arg_khr> svm_args(
config.arg_svm_list, config.num_svm_args);
config->arg_svm_list, config->num_svm_args);

for (unsigned i = 0; i < config.num_svm_args; ++i) {
for (unsigned i = 0; i < config->num_svm_args; ++i) {
// Unpack the argument.
const auto arg = svm_args[i];
const auto arg_index = arg.arg_index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ cl_int extension::khr_command_buffer_mutable_dispatch::GetDeviceInfo(
}

#ifdef OCL_EXTENSION_cl_khr_command_buffer_mutable_dispatch
CL_API_ENTRY cl_int CL_API_CALL
clUpdateMutableCommandsKHR(cl_command_buffer_khr command_buffer,
const cl_mutable_base_config_khr *mutable_config) {
CL_API_ENTRY cl_int CL_API_CALL clUpdateMutableCommandsKHR(
cl_command_buffer_khr command_buffer, cl_uint num_configs,
const cl_update_config_type_khr *config_types, const void **configs) {
tracer::TraceGuard<tracer::OpenCL> guard("clUpdateMutableCommandsKHR");
OCL_CHECK(!command_buffer, return CL_INVALID_COMMAND_BUFFER_KHR);
OCL_CHECK(!command_buffer->is_finalized, return CL_INVALID_OPERATION);
Expand All @@ -102,20 +102,24 @@ clUpdateMutableCommandsKHR(cl_command_buffer_khr command_buffer,
return CL_INVALID_OPERATION;
}

OCL_CHECK(!mutable_config, return CL_INVALID_VALUE);
OCL_CHECK(mutable_config->type != CL_STRUCTURE_TYPE_MUTABLE_BASE_CONFIG_KHR,
return CL_INVALID_VALUE);
OCL_CHECK(config_types && 0 == num_configs, return CL_INVALID_VALUE);
OCL_CHECK(!config_types && num_configs, return CL_INVALID_VALUE);

// Values for next would be defined by implementation of mutable mem commands
// layered extension. Later checks assume next is NULL and so the
// mutable_dispatch_list field must be set.
OCL_CHECK(mutable_config->next, return CL_INVALID_VALUE);
OCL_CHECK(configs && 0 == num_configs, return CL_INVALID_VALUE);
OCL_CHECK(!configs && num_configs, return CL_INVALID_VALUE);

OCL_CHECK(!mutable_config->mutable_dispatch_list ||
!mutable_config->num_mutable_dispatch,
return CL_INVALID_VALUE);
for (size_t i = 0; i < num_configs; i++) {
if (config_types[i] != CL_STRUCTURE_TYPE_MUTABLE_DISPATCH_CONFIG_KHR) {
return CL_INVALID_VALUE;
}
}

const cl_mutable_dispatch_config_khr **casted_configs =
reinterpret_cast<const cl_mutable_dispatch_config_khr **>(configs);
cargo::array_view<const cl_mutable_dispatch_config_khr *>
mutable_dispatch_configs(casted_configs, num_configs);

return command_buffer->updateCommandBuffer(*mutable_config);
return command_buffer->updateCommandBuffer(mutable_dispatch_configs);
}

CL_API_ENTRY cl_int CL_API_CALL clGetMutableCommandInfoKHR(
Expand Down
Loading

0 comments on commit 21514f0

Please sign in to comment.