1 #include "HalideRuntime.h"
2 #include "scoped_spin_lock.h"
3 
4 // Runtime settings for opencl and cuda device selection
5 namespace Halide {
6 namespace Runtime {
7 namespace Internal {
8 
9 WEAK int halide_gpu_device = 0;
10 WEAK ScopedSpinLock::AtomicFlag halide_gpu_device_lock = 0;
11 WEAK bool halide_gpu_device_initialized = false;
12 
13 }  // namespace Internal
14 }  // namespace Runtime
15 }  // namespace Halide
16 
17 extern int atoi(const char *);
18 extern char *getenv(const char *);
19 
20 extern "C" {
21 
halide_set_gpu_device(int d)22 WEAK void halide_set_gpu_device(int d) {
23     halide_gpu_device = d;
24     halide_gpu_device_initialized = true;
25 }
halide_get_gpu_device(void * user_context)26 WEAK int halide_get_gpu_device(void *user_context) {
27     ScopedSpinLock lock(&halide_gpu_device_lock);
28     if (!halide_gpu_device_initialized) {
29         const char *var = getenv("HL_GPU_DEVICE");
30         if (var) {
31             halide_gpu_device = atoi(var);
32         } else {
33             halide_gpu_device = -1;
34         }
35         halide_gpu_device_initialized = true;
36     }
37     return halide_gpu_device;
38 }
39 }
40