Skip to content

Commit 169df38

Browse files
committed
Stage two refactor to cache device offline mode setting
1 parent 4ae4e73 commit 169df38

File tree

7 files changed

+93
-93
lines changed

7 files changed

+93
-93
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: 23 additions & 5 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,13 +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;
15791597
// Cache context offline mode specified by environment variables
15801598
// CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA, CL_CONTEXT_MPSIM_DEVICE_INTELFPGA
15811599
// or CL_CONTEXT_MSIM_DEVICE_INTELFPGA
1582-
int offline_mode;
1600+
acl_context_offline_mode_t offline_mode;
15831601

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

src/acl_context.cpp

Lines changed: 6 additions & 24 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);
@@ -606,12 +605,11 @@ static cl_int l_load_properties(cl_context context,
606605
}
607606
}
608607

609-
// Environment variable can specify we always an offline device.
610-
if (!acl_platform.offline_device.empty()) {
611-
if (!l_find_device_by_name(acl_platform.offline_device))
612-
ERR_RET(CL_INVALID_VALUE, context,
613-
"Invalid offline device specified by environment variable "
614-
"CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA");
608+
// Check if environment variable specified offline device is valid
609+
if (acl_platform.has_offline_device < 0) {
610+
ERR_RET(CL_INVALID_VALUE, context,
611+
"Invalid offline device specified by environment variable "
612+
"CL_CONTEXT_OFFLINE_DEVICE_INTELFPGA");
615613
}
616614

617615
// Get default for program_library_root.
@@ -817,17 +815,6 @@ static cl_int l_load_properties(cl_context context,
817815
return CL_SUCCESS;
818816
}
819817

820-
static cl_device_id l_find_device_by_name(const std::string &name) {
821-
acl_assert_locked();
822-
823-
for (unsigned i = 0; i < acl_platform.num_devices; ++i) {
824-
if (name == acl_platform.device[i].def.autodiscovery_def.name) {
825-
return &(acl_platform.device[i]);
826-
}
827-
}
828-
return 0;
829-
}
830-
831818
// Initialize the given context.
832819
// Yes, this is like a "placement new".
833820
//
@@ -883,8 +870,6 @@ static cl_int l_init_context_with_devices(cl_context context,
883870
int num_present = 0;
884871
int num_absent = 0;
885872
for (cl_uint i = 0; i < num_devices; i++) {
886-
int usable = devices[i]->present;
887-
888873
// Can't mix both (actually) present and absent devices because there
889874
// is no consistent way to place device global memory.
890875
if (devices[i]->present) {
@@ -900,10 +885,7 @@ static cl_int l_init_context_with_devices(cl_context context,
900885
"Can't create a context with both offline and online devices");
901886
}
902887

903-
usable = usable || acl_platform.offline_device ==
904-
devices[i]->def.autodiscovery_def.name;
905-
906-
if (!usable)
888+
if (!devices[i]->present && !devices[i]->offline)
907889
ERR_RET(CL_DEVICE_NOT_AVAILABLE, context, "Device not available");
908890

909891
// Mark the device(s) as opened

src/acl_globals.cpp

Lines changed: 16 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,8 +232,8 @@ void acl_reset(void) {
223232

224233
l_reset_present_board();
225234

226-
acl_platform.offline_device = "";
227235
acl_platform.offline_mode = ACL_CONTEXT_OFFLINE_AND_AUTODISCOVERY;
236+
acl_platform.has_offline_device = 0;
228237
acl_platform.num_devices = 0;
229238
for (unsigned i = 0; i < ACL_MAX_DEVICE; ++i) {
230239
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-
(void)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.

0 commit comments

Comments
 (0)