本文整理汇总了Python中pynvml.nvmlDeviceGetCount方法的典型用法代码示例。如果您正苦于以下问题:Python pynvml.nvmlDeviceGetCount方法的具体用法?Python pynvml.nvmlDeviceGetCount怎么用?Python pynvml.nvmlDeviceGetCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pynvml
的用法示例。
在下文中一共展示了pynvml.nvmlDeviceGetCount方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getFreeId
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def getFreeId():
import pynvml
pynvml.nvmlInit()
def getFreeRatio(id):
handle = pynvml.nvmlDeviceGetHandleByIndex(id)
use = pynvml.nvmlDeviceGetUtilizationRates(handle)
ratio = 0.5*(float(use.gpu+float(use.memory)))
return ratio
deviceCount = pynvml.nvmlDeviceGetCount()
available = []
for i in range(deviceCount):
if getFreeRatio(i)<70:
available.append(i)
gpus = ''
for g in available:
gpus = gpus+str(g)+','
gpus = gpus[:-1]
return gpus
示例2: getGPUUsage
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def getGPUUsage():
try:
pynvml.nvmlInit()
count = pynvml.nvmlDeviceGetCount()
if count == 0:
return None
result = {"driver": pynvml.nvmlSystemGetDriverVersion(),
"gpu_count": int(count)
}
i = 0
gpuData = []
while i<count:
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
mem = pynvml.nvmlDeviceGetMemoryInfo(handle)
gpuData.append({"device_num": i, "name": pynvml.nvmlDeviceGetName(handle), "total": round(float(mem.total)/1000000000, 2), "used": round(float(mem.used)/1000000000, 2)})
i = i+1
result["devices"] = jsonpickle.encode(gpuData, unpicklable=False)
except Exception as e:
result = {"driver": "No GPU!", "gpu_count": 0, "devices": []}
return result
示例3: getFreeId
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def getFreeId():
import pynvml
pynvml.nvmlInit()
def getFreeRatio(id):
handle = pynvml.nvmlDeviceGetHandleByIndex(id)
use = pynvml.nvmlDeviceGetUtilizationRates(handle)
ratio = 0.5 * (float(use.gpu + float(use.memory)))
return ratio
deviceCount = pynvml.nvmlDeviceGetCount()
available = []
for i in range(deviceCount):
if getFreeRatio(i) < 70:
available.append(i)
gpus = ''
for g in available:
gpus = gpus + str(g) + ','
gpus = gpus[:-1]
return gpus
示例4: get_appropriate_cuda
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def get_appropriate_cuda(task_scale='s'):
if task_scale not in {'s','m','l'}:
logger.info('task scale wrong!')
exit(2)
import pynvml
pynvml.nvmlInit()
total_cuda_num = pynvml.nvmlDeviceGetCount()
for i in range(total_cuda_num):
logger.info(i)
handle = pynvml.nvmlDeviceGetHandleByIndex(i) # 这里的0是GPU id
memInfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
utilizationInfo = pynvml.nvmlDeviceGetUtilizationRates(handle)
logger.info(i, 'mem:', memInfo.used / memInfo.total, 'util:',utilizationInfo.gpu)
if memInfo.used / memInfo.total < 0.15 and utilizationInfo.gpu <0.2:
logger.info(i,memInfo.used / memInfo.total)
return 'cuda:'+str(i)
if task_scale=='s':
max_memory=2000
elif task_scale=='m':
max_memory=6000
else:
max_memory = 9000
max_id = -1
for i in range(total_cuda_num):
handle = pynvml.nvmlDeviceGetHandleByIndex(0) # 这里的0是GPU id
memInfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
utilizationInfo = pynvml.nvmlDeviceGetUtilizationRates(handle)
if max_memory < memInfo.free:
max_memory = memInfo.free
max_id = i
if id == -1:
logger.info('no appropriate gpu, wait!')
exit(2)
return 'cuda:'+str(max_id)
# if memInfo.used / memInfo.total < 0.5:
# return
示例5: device_count_for
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def device_count_for():
try:
return pynvml.nvmlDeviceGetCount()
except pynvml.NVMlError:
return None
示例6: record_measurements_to
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def record_measurements_to(async_reporting_func, polling_interval=1):
try:
deviceCount = pynvml.nvmlDeviceGetCount()
while True:
measurement = await aggregate_measurements(deviceCount)
await async_reporting_func(measurement)
await asyncio.sleep(polling_interval)
except CancelledError:
_logger().info("Logging cancelled")
示例7: _get_device_count
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def _get_device_count(self):
""" Detect the number of GPUs attached to the system and allocate to
:attr:`_device_count`. """
if self._is_plaidml:
self._device_count = self._plaid.device_count
elif IS_MACOS:
self._device_count = pynvx.cudaDeviceGetCount(ignore=True)
else:
try:
self._device_count = pynvml.nvmlDeviceGetCount()
except pynvml.NVMLError:
self._device_count = 0
self._log("debug", "GPU Device count: {}".format(self._device_count))
示例8: get_gpu_metrics
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def get_gpu_metrics() -> List:
try:
pynvml.nvmlInit()
device_count = pynvml.nvmlDeviceGetCount()
results = []
for i in range(device_count):
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
results += metrics_dict_to_list(query_gpu(handle))
return results
except pynvml.NVMLError:
logger.debug("Failed to collect gpu resources", exc_info=True)
return []
示例9: _find_gpu
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def _find_gpu(self):
device_count = pynvml.nvmlDeviceGetCount()
for i in range(device_count):
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
gpu_processes = pynvml.nvmlDeviceGetComputeRunningProcesses(handle)
for gpu_process in gpu_processes:
if gpu_process.pid == self.pid:
self.gpu = handle
self.accounting_enabled = pynvml.nvmlDeviceGetAccountingMode(self.gpu) == pynvml.NVML_FEATURE_ENABLED
# Clear accounting statistics (requires root privileges)
#pynvml.nvmlDeviceSetAccountingMode(self.gpu, pynvml.NVML_FEATURE_DISABLED)
#pynvml.nvmlDeviceSetAccountingMode(self.gpu, pynvml.NVML_FEATURE_ENABLED)
示例10: _crawl_in_system
# 需要导入模块: import pynvml [as 别名]
# 或者: from pynvml import nvmlDeviceGetCount [as 别名]
def _crawl_in_system(self):
'''
nvidia-smi returns following: MEMORY, UTILIZATION, ECC, TEMPERATURE,
POWER, CLOCK, COMPUTE, PIDS, PERFORMANCE, SUPPORTED_CLOCKS,
PAGE_RETIREMENT, ACCOUNTING
currently, following are requested based on dlaas requirements:
utilization.gpu, utilization.memory,
memory.total, memory.free, memory.used
nvidia-smi --query-gpu=utilization.gpu,utilization.memory,\
memory.total,memory.free,memory.used --format=csv,noheader,nounits
'''
if self._init_nvml() == -1:
return
self.inspect_arr = exec_dockerps()
num_gpus = pynvml.nvmlDeviceGetCount()
for gpuid in range(0, num_gpus):
gpuhandle = pynvml.nvmlDeviceGetHandleByIndex(gpuid)
temperature = pynvml.nvmlDeviceGetTemperature(
gpuhandle, pynvml.NVML_TEMPERATURE_GPU)
memory = pynvml.nvmlDeviceGetMemoryInfo(gpuhandle)
mem_total = memory.total / 1024 / 1024
mem_used = memory.used / 1024 / 1024
mem_free = memory.free / 1024 / 1024
power_draw = pynvml.nvmlDeviceGetPowerUsage(gpuhandle) / 1000
power_limit = pynvml.nvmlDeviceGetEnforcedPowerLimit(
gpuhandle) / 1000
util = pynvml.nvmlDeviceGetUtilizationRates(gpuhandle)
util_gpu = util.gpu
util_mem = util.memory
entry = {
'utilization': {'gpu': util_gpu, 'memory': util_mem},
'memory': {'total': mem_total, 'free': mem_free,
'used': mem_used},
'temperature': temperature,
'power': {'draw': power_draw, 'limit': power_limit}
}
key = self._get_feature_key(gpuhandle, gpuid)
if gpuid == num_gpus - 1:
self._shutdown_nvml()
yield (key, entry, 'gpu')
return