Skip to content

Commit

Permalink
Cache device_query results
Browse files Browse the repository at this point in the history
Since I'll be calling it more for #104
  • Loading branch information
lukeyeager committed May 18, 2015
1 parent ebb1635 commit 2e32ebe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
23 changes: 6 additions & 17 deletions digits/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def name():

def prompt_message(self):
s = 'Attached devices:\n'
for device_id, gpu in enumerate(self.query_gpus()):
for device_id, gpu in enumerate(device_query.get_devices()):
s += 'Device #%s:\n' % device_id
s += '\t%-20s %s\n' % ('Name', gpu.name)
s += '\t%-20s %s.%s\n' % ('Compute capability', gpu.major, gpu.minor)
Expand All @@ -431,19 +431,19 @@ def optional(self):
return True

def suggestions(self):
if len(self.query_gpus()) > 0:
if len(device_query.get_devices()) > 0:
return [Suggestion(
','.join([str(x) for x in xrange(len(self.query_gpus()))]),
','.join([str(x) for x in xrange(len(device_query.get_devices()))]),
'D', desc='default', default=True)]
else:
return []

@classmethod
def visibility(self):
if len(self.query_gpus()) == 0:
if len(device_query.get_devices()) == 0:
# Nothing to see here
return -1
if len(self.query_gpus()) == 1:
if len(device_query.get_devices()) == 1:
# Use just the one GPU by default
return 0
else:
Expand All @@ -455,7 +455,7 @@ def validate(cls, value):
return value

choices = []
gpus = cls.query_gpus()
gpus = device_query.get_devices()

if not gpus:
return ''
Expand Down Expand Up @@ -488,17 +488,6 @@ def convert_size(cls, size):
else:
return '0B'

@classmethod
def query_gpus(cls):
"""
Return a list of device_query.CudaDeviceProp objects which contain all
the available information on each device
Order is preserved in this list - item 0 is gpu #0
"""
if not hasattr(cls, 'device_list'):
cls.device_list = device_query.get_devices()
return cls.device_list

class JobsDirOption(ConfigOption):
@staticmethod
def name():
Expand Down
6 changes: 6 additions & 0 deletions digits/device_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,17 @@ class CudaDeviceProp(ctypes.Structure):
('multiGpuBoardGroupID', ctypes.c_int),
]

devices = None

def get_devices():
"""
Returns a list of CudaDeviceProp's
Prints an error and returns None if something goes wrong
"""
global devices
if devices is not None:
# Only query CUDA once
return devices
devices = []

# Load library
Expand Down

0 comments on commit 2e32ebe

Please sign in to comment.