Skip to content

Commit dc4d345

Browse files
EdCauntmloubout
authored andcommitted
misc: Move get_visible_devices to archinfo
1 parent 77d226b commit dc4d345

2 files changed

Lines changed: 29 additions & 28 deletions

File tree

devito/arch/archinfo.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
from devito.logger import warning
1717
from devito.tools import as_tuple, all_equal, memoized_func
1818

19-
__all__ = ['platform_registry', 'get_cpu_info', 'get_gpu_info', 'get_nvidia_cc',
20-
'get_cuda_path', 'get_hip_path', 'check_cuda_runtime', 'get_m1_llvm_path',
21-
'get_advisor_path', 'Platform', 'Cpu64', 'Intel64', 'IntelSkylake', 'Amd',
22-
'Arm', 'Power', 'Device', 'NvidiaDevice', 'AmdDevice', 'IntelDevice',
19+
__all__ = ['platform_registry', 'get_cpu_info', 'get_gpu_info', 'get_visible_devices',
20+
'get_nvidia_cc', 'get_cuda_path', 'get_hip_path', 'check_cuda_runtime',
21+
'get_m1_llvm_path', 'get_advisor_path', 'Platform', 'Cpu64', 'Intel64',
22+
'IntelSkylake', 'Amd', 'Arm', 'Power', 'Device', 'NvidiaDevice',
23+
'AmdDevice', 'IntelDevice',
2324
# Brand-agnostic
2425
'ANYCPU', 'ANYGPU',
2526
# Intel CPUs
@@ -488,6 +489,27 @@ def parse_product_arch():
488489
return None
489490

490491

492+
def get_visible_devices():
493+
device_vars = (
494+
'CUDA_VISIBLE_DEVICES',
495+
'ROCR_VISIBLE_DEVICES',
496+
'HIP_VISIBLE_DEVICES'
497+
)
498+
for v in device_vars:
499+
try:
500+
return tuple(int(i) for i in os.environ[v].split(','))
501+
except ValueError:
502+
# Visible devices set via UUIDs or other non-integer identifiers.
503+
warning("Setting visible devices via UUIDs or other non-integer"
504+
" identifiers is currently unsupported: environment variable"
505+
f" {v}={os.environ[v]} ignored.")
506+
except KeyError:
507+
# Environment variable not set
508+
continue
509+
510+
return None
511+
512+
491513
@memoized_func
492514
def get_nvidia_cc():
493515
libnames = ('libcuda.so', 'libcuda.dylib', 'cuda.dll')

devito/operator/operator.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from collections import OrderedDict, namedtuple
32
from functools import cached_property
43
import ctypes
@@ -11,7 +10,8 @@
1110
import sympy
1211
import numpy as np
1312

14-
from devito.arch import ANYCPU, Device, compiler_registry, platform_registry
13+
from devito.arch import (ANYCPU, Device, compiler_registry, platform_registry,
14+
get_visible_devices)
1515
from devito.data import default_allocator
1616
from devito.exceptions import (CompilationError, ExecutionError, InvalidArgument,
1717
InvalidOperator)
@@ -1389,27 +1389,6 @@ def _get_nbytes(self, i):
13891389

13901390
return nbytes
13911391

1392-
@cached_property
1393-
def _visible_devices(self):
1394-
device_vars = (
1395-
'CUDA_VISIBLE_DEVICES',
1396-
'ROCR_VISIBLE_DEVICES',
1397-
'HIP_VISIBLE_DEVICES'
1398-
)
1399-
for v in device_vars:
1400-
try:
1401-
return tuple(int(i) for i in os.environ[v].split(','))
1402-
except ValueError:
1403-
# Visible devices set via UUIDs or other non-integer identifiers.
1404-
warning("Setting visible devices via UUIDs or other non-integer"
1405-
" identifiers is currently unsupported: environment variable"
1406-
f" {v}={os.environ[v]} ignored.")
1407-
except KeyError:
1408-
# Environment variable not set
1409-
continue
1410-
1411-
return None
1412-
14131392
@cached_property
14141393
def _physical_deviceid(self):
14151394
if isinstance(self.platform, Device):
@@ -1420,7 +1399,7 @@ def _physical_deviceid(self):
14201399
if self._visible_devices is None:
14211400
return logical_deviceid
14221401
else:
1423-
return self._visible_devices[logical_deviceid]
1402+
return get_visible_devices()[logical_deviceid]
14241403
else:
14251404
return None
14261405

0 commit comments

Comments
 (0)