|
1 | 1 | import cloudpickle as pickle |
2 | 2 |
|
3 | 3 | import pytest |
| 4 | +import os |
4 | 5 | import numpy as np |
5 | 6 | import sympy |
6 | 7 | import scipy.sparse |
|
10 | 11 | Dimension, MatrixSparseTimeFunction, SparseTimeFunction, |
11 | 12 | SubDimension, SubDomain, SubDomainSet, TimeFunction, exp, |
12 | 13 | Operator, configuration, switchconfig, TensorTimeFunction, |
13 | | - Buffer, assign) |
| 14 | + Buffer, assign, switchenv) |
14 | 15 | from devito.arch import get_gpu_info, get_cpu_info, Device, Cpu64 |
15 | 16 | from devito.exceptions import InvalidArgument |
16 | 17 | from devito.ir import (Conditional, Expression, Section, FindNodes, FindSymbols, |
@@ -74,6 +75,111 @@ def test_autopad_with_platform_switch(self): |
74 | 75 | assert f.shape_allocated[1] == 64 |
75 | 76 |
|
76 | 77 |
|
| 78 | +class TestDeviceID: |
| 79 | + """ |
| 80 | + Test that device IDs and associated environment variables such as |
| 81 | + CUDA_VISIBLE_DEVICES are correctly handled. |
| 82 | + """ |
| 83 | + |
| 84 | + @pytest.mark.parametrize('env_variables', [{"cuda_visible_devices": "1"}, |
| 85 | + {"cuda_visible_devices": "1,2"}, |
| 86 | + {"cuda_visible_devices": "1,0"}, |
| 87 | + {"rocr_visible_devices": "1"}, |
| 88 | + {"hip_visible_devices": " 1"}]) |
| 89 | + def test_visible_devices(self, env_variables): |
| 90 | + """ |
| 91 | + Test that physical device IDs used for querying memory on a device via |
| 92 | + nvidia-smi correctly account for visible-device environment variables. |
| 93 | + """ |
| 94 | + grid = Grid(shape=(10, 10)) |
| 95 | + u = Function(name='u', grid=grid) |
| 96 | + |
| 97 | + eq = Eq(u, u+1) |
| 98 | + |
| 99 | + # Save previous environment to verify switchenv works as intended |
| 100 | + previous_environ = dict(os.environ) |
| 101 | + |
| 102 | + with switchenv(**env_variables): |
| 103 | + op1 = Operator(eq) |
| 104 | + |
| 105 | + argmap1 = op1.arguments() |
| 106 | + # All variants in parameterisation should yield deviceid 1 |
| 107 | + assert argmap1._physical_deviceid == 1 |
| 108 | + |
| 109 | + # Make sure the switchenv doesn't somehow persist |
| 110 | + assert dict(os.environ) == previous_environ |
| 111 | + |
| 112 | + # Check that physical deviceid is 0 when no environment variables set |
| 113 | + op2 = Operator(eq) |
| 114 | + |
| 115 | + argmap2 = op2.arguments() |
| 116 | + # Default physical deviceid expected to be 0 |
| 117 | + assert argmap2._physical_deviceid == 0 |
| 118 | + |
| 119 | + @pytest.mark.parallel(mode=2) |
| 120 | + @pytest.mark.parametrize('visible_devices', ["1,2", "1,0", "0,2,3"]) |
| 121 | + def test_visible_devices_mpi(self, visible_devices, mode): |
| 122 | + """ |
| 123 | + Test that physical device IDs used for querying memory on a device via |
| 124 | + nvidia-smi correctly account for visible-device environment variables |
| 125 | + when using MPI. |
| 126 | + """ |
| 127 | + |
| 128 | + grid = Grid(shape=(10, 10)) |
| 129 | + rank = grid.distributor.myrank |
| 130 | + u = Function(name='u', grid=grid) |
| 131 | + |
| 132 | + eq = Eq(u, u+1) |
| 133 | + |
| 134 | + with switchenv(cuda_visible_devices=visible_devices): |
| 135 | + op1 = Operator(eq) |
| 136 | + argmap1 = op1.arguments() |
| 137 | + |
| 138 | + devices = [int(i) for i in visible_devices.split(',')] |
| 139 | + |
| 140 | + assert argmap1._physical_deviceid == devices[rank] |
| 141 | + |
| 142 | + # In default case, physical deviceid will equal rank |
| 143 | + op2 = Operator(eq) |
| 144 | + argmap2 = op2.arguments() |
| 145 | + assert argmap2._physical_deviceid == rank |
| 146 | + |
| 147 | + def test_visible_devices_with_devito_deviceid(self): |
| 148 | + """Test interaction between CUDA_VISIBLE_DEVICES and DEVITO_DEVICEID""" |
| 149 | + grid = Grid(shape=(10, 10)) |
| 150 | + u = Function(name='u', grid=grid) |
| 151 | + |
| 152 | + eq = Eq(u, u+1) |
| 153 | + |
| 154 | + with switchenv(cuda_visible_devices="1,3"), switchconfig(deviceid=1): |
| 155 | + op = Operator(eq) |
| 156 | + |
| 157 | + argmap = op.arguments() |
| 158 | + # deviceid should see the world from within CUDA_VISIBLE_DEVICES |
| 159 | + # So should be the second of the two visible devices specified (3) |
| 160 | + assert argmap._physical_deviceid == 3 |
| 161 | + |
| 162 | + @pytest.mark.parallel(mode=2) |
| 163 | + def test_deviceid_per_rank(self, mode): |
| 164 | + """ |
| 165 | + Test that Device IDs set by the user on a per-rank basis do not |
| 166 | + get modifed. |
| 167 | + """ |
| 168 | + # Reversed order to ensure it is different to default |
| 169 | + user_set_deviceids = (1, 0) |
| 170 | + |
| 171 | + grid = Grid(shape=(10, 10)) |
| 172 | + u = Function(name='u', grid=grid) |
| 173 | + |
| 174 | + rank = grid.distributor.myrank |
| 175 | + deviceid = user_set_deviceids[rank] |
| 176 | + |
| 177 | + op = Operator(Eq(u, u+1)) |
| 178 | + |
| 179 | + argmap = op.arguments(deviceid=deviceid) |
| 180 | + assert argmap._physical_deviceid == deviceid |
| 181 | + |
| 182 | + |
77 | 183 | class TestCodeGeneration: |
78 | 184 |
|
79 | 185 | def test_maxpar_option(self): |
|
0 commit comments