Skip to content

Commit 8ea53b9

Browse files
committed
Cache context offline device setting specified by environment variables
Currently runtime queries context offline device setting from environment variables whenever this setting is needed, therefore introduces performance overhead. The caching is done to avoid this performance overhead.
1 parent d188f5c commit 8ea53b9

File tree

10 files changed

+24
-39
lines changed

10 files changed

+24
-39
lines changed

include/acl_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,10 @@ typedef struct _cl_platform_id
15761576
// The setting of environment variable CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA, if
15771577
// any.
15781578
std::string offline_device;
1579+
// Cache context offline mode specified by environment variables
1580+
// CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA, CL_CONTEXT_MPSIM_DEVICE_INTELFPGA
1581+
// or CL_CONTEXT_MSIM_DEVICE_INTELFPGA
1582+
int offline_mode;
15791583

15801584
// Should we track and automatically release leaked objects?
15811585
// This helps immensely with the OpenCL conformance tests which tend to

src/acl_context.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ static cl_int l_finalize_context(cl_context context, cl_uint num_devices,
553553
static cl_int l_load_properties(cl_context context,
554554
const cl_context_properties *properties) {
555555
const char *default_compile_cmd = 0;
556-
int env_override = 0;
557556
acl_assert_locked();
558557

559558
// Set defaults.
@@ -717,8 +716,6 @@ static cl_int l_load_properties(cl_context context,
717716
// Always terminate list. After all, 'properties' might be empty!
718717
context->properties[context->num_property_entries++] = 0;
719718

720-
(void)acl_get_offline_device_user_setting(&env_override);
721-
722719
context->compiles_programs_incompletely = 0;
723720
switch (context->compiler_mode) {
724721
case static_cast<acl_compiler_mode_t>(
@@ -788,7 +785,7 @@ static cl_int l_load_properties(cl_context context,
788785
// We need backing store for the buffers.
789786
context->device_buffers_have_backing_store = 1;
790787

791-
if (env_override == ACL_CONTEXT_MPSIM) {
788+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
792789
// Simulator should support save/restore buffers around programming if
793790
// reprogramming on-the-fly is supported
794791
context->saves_and_restores_buffers_for_reprogramming = 1;

src/acl_device_binary.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,10 @@ cl_int acl_device_binary_t::load_binary_pkg(int validate_compile_options,
175175
#define FAILREAD_MSG "Could not read parts of the program binary."
176176
size_t data_len = 0;
177177

178-
int env_override = 0;
179-
180178
acl_assert_locked();
181179

182-
(void)acl_get_offline_device_user_setting(&env_override);
183-
184-
if (env_override == ACL_CONTEXT_MPSIM && !validate_compile_options &&
180+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM &&
181+
!validate_compile_options &&
185182
context->compiler_mode != CL_CONTEXT_COMPILER_MODE_OFFLINE_INTELFPGA &&
186183
get_binary_len() < 1024) {
187184
// IF the binary is ridiculously small (arbitrary number) we are going
@@ -258,7 +255,7 @@ cl_int acl_device_binary_t::load_binary_pkg(int validate_compile_options,
258255
// runtime.
259256
if (acl_pkg_section_exists(pkg, ".acl.rand_hash", &data_len) &&
260257
dev_prog->device->loaded_bin == nullptr &&
261-
env_override != ACL_CONTEXT_MPSIM) {
258+
acl_platform.offline_mode != ACL_CONTEXT_MPSIM) {
262259
std::vector<char> pkg_rand_hash(data_len + 1);
263260
AND_CHECK(acl_pkg_read_section(pkg, ".acl.rand_hash", pkg_rand_hash.data(),
264261
data_len + 1),
@@ -305,7 +302,7 @@ cl_int acl_device_binary_t::load_binary_pkg(int validate_compile_options,
305302
// For simulator flow, we treat as if the device has already been
306303
// programmed and check device global memory layout against
307304
// dev_prog->device->last_bin
308-
if (env_override == ACL_CONTEXT_MPSIM) {
305+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
309306
if (validate_memory_layout && dev_prog->device->last_bin) {
310307
AND_CHECK(get_devdef().autodiscovery_def.num_global_mem_systems <=
311308
1 ||
@@ -357,7 +354,7 @@ cl_int acl_device_binary_t::load_binary_pkg(int validate_compile_options,
357354
is_simulator = 0;
358355
if (status == CL_SUCCESS &&
359356
acl_pkg_section_exists(pkg, ".acl.simulator_object", &data_len)) {
360-
if (env_override != ACL_CONTEXT_MPSIM) {
357+
if (acl_platform.offline_mode != ACL_CONTEXT_MPSIM) {
361358
acl_context_callback(
362359
context,
363360
"aocx contains simulated kernel, but simulation mode not set!");
@@ -382,7 +379,7 @@ cl_int acl_device_binary_t::load_binary_pkg(int validate_compile_options,
382379
context,
383380
"aocx contains unsupported legacy opencl emulated kernel for windows!");
384381
}
385-
if (status == CL_SUCCESS && env_override == ACL_CONTEXT_MPSIM &&
382+
if (status == CL_SUCCESS && acl_platform.offline_mode == ACL_CONTEXT_MPSIM &&
386383
!is_simulator) {
387384
acl_context_callback(context,
388385
"Simulation mode set but aocx is for hardware!");

src/acl_globals.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ void acl_reset(void) {
224224
l_reset_present_board();
225225

226226
acl_platform.offline_device = "";
227+
acl_platform.offline_mode = ACL_CONTEXT_OFFLINE_AND_AUTODISCOVERY;
227228
acl_platform.num_devices = 0;
228229
for (unsigned i = 0; i < ACL_MAX_DEVICE; ++i) {
229230
acl_platform.device[i] = _cl_device_id();

src/acl_hal_mmd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
12481248
#endif
12491249

12501250
// Dynamically load board mmd & symbols
1251-
acl_get_offline_device_user_setting(&use_offline_only);
1251+
(void)acl_get_offline_device_user_setting(&use_offline_only);
12521252
if (use_offline_only == ACL_CONTEXT_MPSIM) {
12531253

12541254
// Substitute the simulator MMD layer.

src/acl_kernel.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,9 +2918,7 @@ static cl_int l_copy_and_adjust_arguments_for_device(
29182918
[needed_mem_id]);
29192919
#endif
29202920

2921-
int env_override = 0;
2922-
(void)acl_get_offline_device_user_setting(&env_override);
2923-
if (env_override == ACL_CONTEXT_MPSIM) {
2921+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
29242922
if (!acl_realloc_buffer_for_simulator(mem_obj, needed_physical_id,
29252923
needed_mem_id)) {
29262924
return CL_MEM_OBJECT_ALLOCATION_FAILURE;

src/acl_kernel_if.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ int acl_kernel_if_init(acl_kernel_if *kern, acl_bsp_io bsp_io,
706706
char description_size_lsb[KERNEL_ROM_SIZE_BYTES_READ + 1];
707707
unsigned int size_location, version, size;
708708
int result = 0;
709-
int use_offline_only = 0;
710709
acl_assert_locked();
711710

712711
assert(acl_bsp_io_is_valid(&bsp_io));
@@ -723,8 +722,7 @@ int acl_kernel_if_init(acl_kernel_if *kern, acl_bsp_io bsp_io,
723722

724723
// The simulator doesn't have any kernel interface information until the aocx
725724
// is loaded, which happens later.
726-
acl_get_offline_device_user_setting(&use_offline_only);
727-
if (use_offline_only == ACL_CONTEXT_MPSIM) {
725+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
728726
std::string err_msg;
729727
auto parse_result = acl_load_device_def_from_str(
730728
acl_shipped_board_cfgs[0].cfg, sysdef->device[0].autodiscovery_def,

src/acl_platform.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ const char *acl_platform_extensions() {
288288
// Initialize the internal bookkeeping based on the system definition
289289
// provided to us.
290290
void acl_init_platform(void) {
291-
int offline_mode = 0;
292291
acl_assert_locked();
293292

294293
acl_platform.dispatch = &acl_icd_dispatch;
@@ -304,9 +303,9 @@ void acl_init_platform(void) {
304303

305304
// Set offline_device property
306305
const char *offline_device =
307-
acl_get_offline_device_user_setting(&offline_mode);
306+
acl_get_offline_device_user_setting(&acl_platform.offline_mode);
308307
if (offline_device) {
309-
if (offline_mode == ACL_CONTEXT_MPSIM) {
308+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
310309
acl_platform.offline_device = ACL_MPSIM_DEVICE_NAME;
311310
} else {
312311
acl_platform.offline_device = offline_device;
@@ -383,7 +382,7 @@ void acl_init_platform(void) {
383382
// having.
384383
acl_platform.initial_board_def = acl_present_board_def();
385384

386-
switch (offline_mode) {
385+
switch (acl_platform.offline_mode) {
387386
case ACL_CONTEXT_OFFLINE_AND_AUTODISCOVERY:
388387
acl_platform.num_devices =
389388
acl_platform.initial_board_def->num_devices +
@@ -409,7 +408,7 @@ void acl_init_platform(void) {
409408
l_add_device(static_cast<int>(i));
410409
}
411410

412-
l_initialize_offline_devices(offline_mode);
411+
l_initialize_offline_devices(acl_platform.offline_mode);
413412

414413
// Device operation queue.
415414
acl_init_device_op_queue(&acl_platform.device_op_queue);
@@ -516,15 +515,12 @@ void acl_init_platform(void) {
516515

517516
void acl_finalize_init_platform(unsigned int num_devices,
518517
const cl_device_id *devices) {
519-
int offline_mode = 0;
520518
int have_single_bank_with_shared_memory;
521519
acl_assert_locked();
522520
assert(num_devices > 0);
523521

524-
(void)acl_get_offline_device_user_setting(&offline_mode);
525-
526-
l_initialize_devices(acl_present_board_def(), offline_mode, num_devices,
527-
devices);
522+
l_initialize_devices(acl_present_board_def(), acl_platform.offline_mode,
523+
num_devices, devices);
528524

529525
if (is_SOC_device()) {
530526
size_t cur_num_banks =

src/acl_program.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,9 +1638,7 @@ void acl_program_device(void *user_data, acl_device_op_t *op) {
16381638
dev_prog->device->def.autodiscovery_def =
16391639
dev_bin->get_devdef().autodiscovery_def;
16401640

1641-
int offline_mode = 0;
1642-
(void)acl_get_offline_device_user_setting(&offline_mode);
1643-
if (offline_mode == ACL_CONTEXT_MPSIM) {
1641+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
16441642
// Override the device name to the simulator.
16451643
// In function acl_device_binary_t::load_binary_pkg, the name member will
16461644
// be checked against the .acl.board section of the aocx file, which would

src/acl_usm.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ CL_API_ENTRY void *CL_API_CALL clHostMemAllocINTEL(
148148
{
149149
auto mmd_properties_it = mmd_properties.begin();
150150
if (mem_id) {
151-
int use_offline_only;
152-
acl_get_offline_device_user_setting(&use_offline_only);
153-
if (use_offline_only == ACL_CONTEXT_MPSIM) {
151+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
154152
*mmd_properties_it++ = AOCL_MMD_MEM_PROPERTIES_BUFFER_LOCATION;
155153
*mmd_properties_it++ = *mem_id;
156154
}
@@ -433,9 +431,7 @@ clSharedMemAllocINTEL(cl_context context, cl_device_id device,
433431
{
434432
auto mmd_properties_it = mmd_properties.begin();
435433
if (mem_id) {
436-
int use_offline_only;
437-
acl_get_offline_device_user_setting(&use_offline_only);
438-
if (use_offline_only == ACL_CONTEXT_MPSIM) {
434+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
439435
*mmd_properties_it++ = AOCL_MMD_MEM_PROPERTIES_BUFFER_LOCATION;
440436
*mmd_properties_it++ = *mem_id;
441437
}

0 commit comments

Comments
 (0)