Skip to content

Commit e2aebdd

Browse files
authored
env variable control over device selector in DPNPC (#577)
1 parent 211404a commit e2aebdd

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

dpnp/backend/examples/example3.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int main(int, char**)
4242
{
4343
const size_t size = 256;
4444

45-
dpnp_queue_initialize_c(QueueOptions::CPU_SELECTOR);
45+
dpnp_queue_initialize_c();
4646
std::cout << "SYCL queue is CPU: " << dpnp_queue_is_cpu_c() << std::endl;
4747

4848
int* array1 = (int*)dpnp_memory_alloc_c(size * sizeof(int));

dpnp/backend/include/dpnp_iface.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
enum class QueueOptions : uint32_t
6969
{
7070
CPU_SELECTOR, /**< CPU side execution mode */
71-
GPU_SELECTOR /**< Intel GPU side execution mode */
71+
GPU_SELECTOR, /**< Intel GPU side execution mode */
72+
AUTO_SELECTOR /**< Automatic selection based on environment variable with @ref CPU_SELECTOR default */
7273
};
7374

7475
/**
@@ -77,9 +78,9 @@ enum class QueueOptions : uint32_t
7778
*
7879
* Global SYCL queue initialization.
7980
*
80-
* @param [in] selector Select type @ref QueueOptions of the SYCL queue.
81+
* @param [in] selector Select type @ref QueueOptions of the SYCL queue. Default @ref AUTO_SELECTOR
8182
*/
82-
INP_DLLEXPORT void dpnp_queue_initialize_c(QueueOptions selector);
83+
INP_DLLEXPORT void dpnp_queue_initialize_c(QueueOptions selector = QueueOptions::AUTO_SELECTOR);
8384

8485
/**
8586
* @ingroup BACKEND_API

dpnp/backend/src/queue_sycl.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,42 @@ cl::sycl::queue* backend_sycl::queue = nullptr;
3535
#endif
3636
mkl_rng::mt19937* backend_sycl::rng_engine = nullptr;
3737

38+
static void show_avalable_sycl_devices()
39+
{
40+
const std::vector<cl::sycl::device> devices = cl::sycl::device::get_devices();
41+
42+
std::cout << "Available SYCL devices:" << std::endl;
43+
for (std::vector<cl::sycl::device>::const_iterator it = devices.cbegin(); it != devices.cend(); ++it)
44+
{
45+
std::cout
46+
// not yet implemented error << " " << it->has(sycl::aspect::usm_shared_allocations) << " "
47+
<< " - id=" << it->get_info<cl::sycl::info::device::vendor_id>()
48+
<< ", type=" << static_cast<pi_uint64>(it->get_info<cl::sycl::info::device::device_type>())
49+
<< ", gws=" << it->get_info<cl::sycl::info::device::max_work_group_size>()
50+
<< ", cu=" << it->get_info<cl::sycl::info::device::max_compute_units>()
51+
<< ", name=" << it->get_info<cl::sycl::info::device::name>() << std::endl;
52+
}
53+
}
54+
55+
static cl::sycl::device get_default_sycl_device()
56+
{
57+
int dpnpc_queue_gpu = 0;
58+
cl::sycl::device dev = cl::sycl::device(cl::sycl::cpu_selector());
59+
60+
const char* dpnpc_queue_gpu_var = getenv("DPNPC_QUEUE_GPU");
61+
if (dpnpc_queue_gpu_var != NULL)
62+
{
63+
dpnpc_queue_gpu = atoi(dpnpc_queue_gpu_var);
64+
}
65+
66+
if (dpnpc_queue_gpu)
67+
{
68+
dev = cl::sycl::device(cl::sycl::gpu_selector());
69+
}
70+
71+
return dev;
72+
}
73+
3874
/**
3975
* Function push the SYCL kernels to be linked (final stage of the compilation) for the current queue
4076
*
@@ -89,14 +125,22 @@ void backend_sycl::backend_sycl_queue_init(QueueOptions selector)
89125

90126
cl::sycl::device dev;
91127

128+
#if not defined(NDEBUG)
129+
show_avalable_sycl_devices();
130+
#endif
131+
92132
if (QueueOptions::CPU_SELECTOR == selector)
93133
{
94134
dev = cl::sycl::device(cl::sycl::cpu_selector());
95135
}
96-
else
136+
else if (QueueOptions::GPU_SELECTOR == selector)
97137
{
98138
dev = cl::sycl::device(cl::sycl::gpu_selector());
99139
}
140+
else
141+
{
142+
dev = get_default_sycl_device();
143+
}
100144

101145
queue = new cl::sycl::queue(dev, exception_handler);
102146

0 commit comments

Comments
 (0)