MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / NVMLContext

Class NVMLContext

tensorpack/utils/nvml.py:149–206  ·  view source on GitHub ↗

Creates a context to query information Example: with NVMLContext() as ctx: num_gpus = ctx.num_devices() for device in ctx.devices(): print(device.memory()) print(device.utilization())

Source from the content-addressed store, hash-verified

147
148
149class NVMLContext(object):
150 """Creates a context to query information
151
152 Example:
153
154 with NVMLContext() as ctx:
155 num_gpus = ctx.num_devices()
156 for device in ctx.devices():
157 print(device.memory())
158 print(device.utilization())
159
160 """
161 def __enter__(self):
162 """Create a new context """
163 _NVML.load()
164 _check_return(_NVML.get_function("nvmlInit_v2")())
165 return self
166
167 def __exit__(self, type, value, tb):
168 """Destroy current context"""
169 _check_return(_NVML.get_function("nvmlShutdown")())
170
171 def num_devices(self):
172 """Get number of devices """
173 c_count = c_uint()
174 _check_return(_NVML.get_function(
175 "nvmlDeviceGetCount_v2")(byref(c_count)))
176 return c_count.value
177
178 def devices(self):
179 """
180 Returns:
181 [NvidiaDevice]: a list of devices
182 """
183 return [self.device(i) for i in range(self.num_devices())]
184
185 def device(self, idx):
186 """Get a specific GPU device
187
188 Args:
189 idx: index of device
190
191 Returns:
192 NvidiaDevice: single GPU device
193 """
194 num_dev = self.num_devices()
195 assert idx < num_dev, "Cannot obtain device {}: NVML only found {} devices.".format(idx, num_dev)
196
197 class GpuDevice(Structure):
198 pass
199
200 c_nvmlDevice_t = POINTER(GpuDevice)
201
202 c_index = c_uint(idx)
203 device = c_nvmlDevice_t()
204 _check_return(_NVML.get_function(
205 "nvmlDeviceGetHandleByIndex_v2")(c_index, byref(device)))
206 return NvidiaDevice(device)

Callers 4

get_num_gpuFunction · 0.85
nvml.pyFile · 0.85
workerMethod · 0.85
collect_env_infoFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…