Skip to content

Commit be2576f

Browse files
committed
Cache context offline device setting specified by environment variables
1 parent d188f5c commit be2576f

12 files changed

+107
-122
lines changed

include/acl_globals.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,8 @@ void acl_reset(void);
4444
// if it is not.
4545
cl_bool acl_init_from_hal_discovery(void);
4646

47-
// Looks at environment variable CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA.
48-
// If it exists and is prefixed by "+" then:
49-
// Return a pointer to the device name (without the "+" prefix).
50-
// Set *use_offline_ret_only = 1
51-
// If it exists and is not prefixed by "+" then
52-
// Return a pointer to the device name.
53-
// Set *use_offline_ret_only = 0
54-
#define ACL_CONTEXT_OFFLINE_AND_AUTODISCOVERY 0
55-
#define ACL_CONTEXT_OFFLINE_ONLY 1
56-
#define ACL_CONTEXT_MSIM 3
57-
#define ACL_CONTEXT_MPSIM 4
58-
const char *acl_get_offline_device_user_setting(int *use_offline_only_ret);
47+
acl_context_offline_mode_t
48+
acl_get_offline_device_user_setting(std::string *offline_device);
5949

6050
ACL_EXPORT
6151
extern struct _cl_platform_id acl_platform;

include/acl_types.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ typedef enum {
216216
ACL_COMPILER_MODE_NUM_MODES = CL_CONTEXT_COMPILER_MODE_NUM_MODES_INTELFPGA
217217
} acl_compiler_mode_t;
218218

219+
// Looks at environment variable CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA.
220+
// If it exists and is prefixed by "+" then:
221+
// Return a pointer to the device name (without the "+" prefix).
222+
// Set *use_offline_ret_only = 1
223+
// If it exists and is not prefixed by "+" then
224+
// Return a pointer to the device name.
225+
// Set *use_offline_ret_only = 0
226+
typedef enum {
227+
ACL_CONTEXT_OFFLINE_AND_AUTODISCOVERY,
228+
ACL_CONTEXT_OFFLINE_ONLY,
229+
ACL_CONTEXT_MSIM,
230+
ACL_CONTEXT_MPSIM
231+
} acl_context_offline_mode_t;
232+
219233
/* When a feature is still in development, it might need enum values
220234
* that are distinct from all published enums in the OpenCL token registry.
221235
* There is a reserved range for values for such "experimental" features.
@@ -1226,7 +1240,8 @@ typedef struct _cl_device_id {
12261240

12271241
unsigned int address_bits; // cache address bits to avoid GetDeviceInfo calls
12281242

1229-
int present; // Is the device present in the host system?
1243+
bool present; // Is the device present in the host system?
1244+
bool offline; // Is the device a real (i.e., not simulator) offline device?
12301245

12311246
// Error notification callback.
12321247
CL_EXCEPTION_TYPE_INTEL device_exception_status;
@@ -1573,9 +1588,16 @@ typedef struct _cl_platform_id
15731588
int device_exception_platform_counter; // indicates number of devices with at
15741589
// least one exception
15751590

1576-
// The setting of environment variable CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA, if
1577-
// any.
1578-
std::string offline_device;
1591+
// Record whether the platform has (non-simulator) offline device,
1592+
// value will be:
1593+
// 1: if there is a valid offline device specified by environment variable
1594+
// 0: if there is no offline device specified by environment variable
1595+
// -1: if the offline device specified by environment variable is invalid
1596+
int has_offline_device;
1597+
// Cache context offline mode specified by environment variables
1598+
// CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA, CL_CONTEXT_MPSIM_DEVICE_INTELFPGA
1599+
// or CL_CONTEXT_MSIM_DEVICE_INTELFPGA
1600+
acl_context_offline_mode_t offline_mode;
15791601

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

src/acl_context.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ static void
4848
l_init_kernel_invocation_wrapper(acl_kernel_invocation_wrapper_t *wrapper,
4949
unsigned i);
5050
static void l_forcibly_release_allocations(cl_context context);
51-
static cl_device_id l_find_device_by_name(const std::string &name);
5251
static cl_int l_update_program_library_root(cl_context context,
5352
const char *new_root);
5453
static cl_int l_update_compile_command(cl_context context, const char *new_cmd);
@@ -553,7 +552,6 @@ static cl_int l_finalize_context(cl_context context, cl_uint num_devices,
553552
static cl_int l_load_properties(cl_context context,
554553
const cl_context_properties *properties) {
555554
const char *default_compile_cmd = 0;
556-
int env_override = 0;
557555
acl_assert_locked();
558556

559557
// Set defaults.
@@ -607,9 +605,8 @@ static cl_int l_load_properties(cl_context context,
607605
}
608606
}
609607

610-
// Environment variable can specify we always an offline device.
611-
if (!acl_platform.offline_device.empty()) {
612-
if (!l_find_device_by_name(acl_platform.offline_device))
608+
// Check if environment variable specified offline device is valid
609+
if (acl_platform.has_offline_device < 0) {
613610
ERR_RET(CL_INVALID_VALUE, context,
614611
"Invalid offline device specified by environment variable "
615612
"CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA");
@@ -717,8 +714,6 @@ static cl_int l_load_properties(cl_context context,
717714
// Always terminate list. After all, 'properties' might be empty!
718715
context->properties[context->num_property_entries++] = 0;
719716

720-
(void)acl_get_offline_device_user_setting(&env_override);
721-
722717
context->compiles_programs_incompletely = 0;
723718
switch (context->compiler_mode) {
724719
case static_cast<acl_compiler_mode_t>(
@@ -788,7 +783,7 @@ static cl_int l_load_properties(cl_context context,
788783
// We need backing store for the buffers.
789784
context->device_buffers_have_backing_store = 1;
790785

791-
if (env_override == ACL_CONTEXT_MPSIM) {
786+
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
792787
// Simulator should support save/restore buffers around programming if
793788
// reprogramming on-the-fly is supported
794789
context->saves_and_restores_buffers_for_reprogramming = 1;
@@ -820,17 +815,6 @@ static cl_int l_load_properties(cl_context context,
820815
return CL_SUCCESS;
821816
}
822817

823-
static cl_device_id l_find_device_by_name(const std::string &name) {
824-
acl_assert_locked();
825-
826-
for (unsigned i = 0; i < acl_platform.num_devices; ++i) {
827-
if (name == acl_platform.device[i].def.autodiscovery_def.name) {
828-
return &(acl_platform.device[i]);
829-
}
830-
}
831-
return 0;
832-
}
833-
834818
// Initialize the given context.
835819
// Yes, this is like a "placement new".
836820
//
@@ -886,8 +870,6 @@ static cl_int l_init_context_with_devices(cl_context context,
886870
int num_present = 0;
887871
int num_absent = 0;
888872
for (cl_uint i = 0; i < num_devices; i++) {
889-
int usable = devices[i]->present;
890-
891873
// Can't mix both (actually) present and absent devices because there
892874
// is no consistent way to place device global memory.
893875
if (devices[i]->present) {
@@ -903,10 +885,7 @@ static cl_int l_init_context_with_devices(cl_context context,
903885
"Can't create a context with both offline and online devices");
904886
}
905887

906-
usable = usable || acl_platform.offline_device ==
907-
devices[i]->def.autodiscovery_def.name;
908-
909-
if (!usable)
888+
if (!devices[i]->present && !devices[i]->offline)
910889
ERR_RET(CL_DEVICE_NOT_AVAILABLE, context, "Device not available");
911890

912891
// Mark the device(s) as opened

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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ static void l_reset_present_board() {
7373
// If it's prefixed by "+", then it's in addition to any auto-discovered
7474
// devices.
7575
// If not, then we don't even probe for auto-discovered devices.
76-
const char *acl_get_offline_device_user_setting(int *use_offline_only_ret) {
77-
int use_offline_only = 0;
76+
acl_context_offline_mode_t
77+
acl_get_offline_device_user_setting(std::string *offline_device) {
78+
acl_context_offline_mode_t use_offline_only =
79+
ACL_CONTEXT_OFFLINE_AND_AUTODISCOVERY;
7880
const char *setting = 0;
7981
const char *setting_deprecated = 0;
8082
const char *result = 0;
@@ -136,8 +138,15 @@ const char *acl_get_offline_device_user_setting(int *use_offline_only_ret) {
136138
}
137139
}
138140

139-
*use_offline_only_ret = use_offline_only;
140-
return result;
141+
if (offline_device) {
142+
if (use_offline_only == ACL_CONTEXT_MPSIM) {
143+
*offline_device = ACL_MPSIM_DEVICE_NAME;
144+
} else if (result) {
145+
*offline_device = result;
146+
}
147+
}
148+
149+
return use_offline_only;
141150
}
142151

143152
int acl_init(const acl_system_def_t *newsys) {
@@ -166,11 +175,11 @@ int acl_init(const acl_system_def_t *newsys) {
166175
// This function returns CL_TRUE if a hal is initialized and CL_FALSE
167176
// if it is not.
168177
cl_bool acl_init_from_hal_discovery(void) {
169-
int use_offline_only = 0;
170178
const acl_hal_t *board_hal;
171179
acl_assert_locked();
172180

173-
(void)acl_get_offline_device_user_setting(&use_offline_only);
181+
acl_context_offline_mode_t use_offline_only =
182+
acl_get_offline_device_user_setting(NULL);
174183

175184
// Two jobs:
176185
// 1. Set the HAL from the linked-in HAL library.
@@ -223,7 +232,8 @@ void acl_reset(void) {
223232

224233
l_reset_present_board();
225234

226-
acl_platform.offline_device = "";
235+
acl_platform.offline_mode = ACL_CONTEXT_OFFLINE_AND_AUTODISCOVERY;
236+
acl_platform.has_offline_device = 0;
227237
acl_platform.num_devices = 0;
228238
for (unsigned i = 0; i < ACL_MAX_DEVICE; ++i) {
229239
acl_platform.device[i] = _cl_device_id();

src/acl_hal_mmd.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,6 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
11991199
buf[MAX_BOARD_NAMES_LEN]; // This is a bit subtle, pointers to device
12001200
// names might get cached by various routines
12011201
char *ptr, *saveptr;
1202-
int use_offline_only;
12031202

12041203
#ifdef _WIN32
12051204
// We're really relying on this being called before anything else
@@ -1248,7 +1247,8 @@ acl_mmd_get_system_definition(acl_system_def_t *sys,
12481247
#endif
12491248

12501249
// Dynamically load board mmd & symbols
1251-
acl_get_offline_device_user_setting(&use_offline_only);
1250+
acl_context_offline_mode_t use_offline_only =
1251+
acl_get_offline_device_user_setting(NULL);
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,

0 commit comments

Comments
 (0)