Skip to content

Commit

Permalink
Set default values for kernel image static part and skip CSR write if…
Browse files Browse the repository at this point in the history
… no change
  • Loading branch information
sophimao committed Apr 10, 2024
1 parent 425a29b commit 11e307f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/acl_kernel_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ typedef struct {
// CRA address offset for backwards compatibility
unsigned int cra_address_offset = 8;

// Kernel static image cache for trackinig changed work dimensions, etc.
std::vector<std::unique_ptr<char[]>> static_img_cache;
// Kernel argument cache for trackinig changed arguments
std::vector<std::unique_ptr<char[]>> accel_arg_cache;
} acl_kernel_if;
Expand Down
15 changes: 14 additions & 1 deletion include/acl_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ class acl_device_program_info_t {
// don't expect it.
#pragma pack(push, 4)
// These are the bytes written to global memory for a kernel invocation.
typedef struct {
typedef struct acl_dev_kernel_invocation_image {
// The activation_id is the index into the device op queue.
// The value at acl_platform.device_op_queue[activation_id] will be
// updated asynchronously by the HAL, so its address must remain stable.
Expand Down Expand Up @@ -486,6 +486,19 @@ typedef struct {
char *arg_value;
size_t arg_value_size;

// Define constructor to initialize the invocation image to default values
// Hard code for now
acl_dev_kernel_invocation_image()
: activation_id(0), accel_id(0), work_dim(1), work_group_size(1),
padding(0), arg_value(NULL), arg_value_size(0) {
for (unsigned i = 0; i < 3; ++i) {
global_work_size[i] = 1;
num_groups[i] = 1;
local_work_size[i] = 1;
global_work_offset[i] = 0;
}
}

} acl_dev_kernel_invocation_image_t;

// Invocation image structure that matches the 18.1 CRA layout.
Expand Down
25 changes: 23 additions & 2 deletions src/acl_kernel_if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,23 @@ int acl_kernel_if_update(const acl_device_def_autodiscovery_t &devdef,
if (kern->num_accel > 0) {
kern->accel_job_ids.resize(kern->num_accel);
kern->accel_invoc_queue_depth.resize(kern->num_accel);
kern->static_img_cache.resize(kern->num_accel);
kern->accel_arg_cache.resize(kern->num_accel);

// Kernel IRQ is a separate thread. Need to use circular buffer to make this
// multithread safe.
kern->accel_queue_front.resize(kern->num_accel);
kern->accel_queue_back.resize(kern->num_accel);

acl_dev_kernel_invocation_image_t default_invocation;
size_t image_size_static =
(size_t)((uintptr_t) & (default_invocation.arg_value) - (uintptr_t) &
(default_invocation.work_dim));

for (unsigned a = 0; a < kern->num_accel; ++a) {
kern->static_img_cache[a] = std::make_unique<char[]>(image_size_static);
memcpy(kern->static_img_cache[a].get(),
(char *)(&(default_invocation.work_dim)), image_size_static);
unsigned int max_same_accel_launches =
devdef.accel[a].fast_launch_depth + 1;
// +1, because fast launch depth does not account for the running kernel
Expand Down Expand Up @@ -1153,8 +1162,19 @@ void acl_kernel_if_launch_kernel_on_custom_sof(
// it is in dynamic memory. Only write the static part of the invocation
// image if this kernel uses CRA control.
if (!kern->streaming_control_signal_names[accel_id]) {
acl_kernel_cra_write_block(kern, accel_id, offset, (unsigned int *)image_p,
image_size_static);
if (kern->csr_version == CSR_VERSION_ID_18_1) {
// Just write everything for older CSR version
acl_kernel_cra_write_block(kern, accel_id, offset,
(unsigned int *)image_p, image_size_static);
} else {
char *img_cache_ptr = kern->static_img_cache[accel_id].get();
assert(img_cache_ptr && "kernel image cache not initialized!");
if (memcmp(img_cache_ptr, (char *)image_p, image_size_static) != 0) {
acl_kernel_cra_write_block(kern, accel_id, offset,
(unsigned int *)image_p, image_size_static);
memcpy(img_cache_ptr, (char *)image_p, image_size_static);
}
}
}

bool accel_has_agent_args = false;
Expand Down Expand Up @@ -1692,6 +1712,7 @@ void acl_kernel_if_close(acl_kernel_if *kern) {
kern->accel_invoc_queue_depth.clear();
kern->accel_queue_front.clear();
kern->accel_queue_back.clear();
kern->static_img_cache.clear();
kern->accel_arg_cache.clear();
kern->autorun_profiling_kernel_id = -1;
}
Expand Down

0 comments on commit 11e307f

Please sign in to comment.