Skip to content

Commit

Permalink
enable selecting gpu
Browse files Browse the repository at this point in the history
  • Loading branch information
brokenhammer committed Dec 23, 2024
1 parent 34dbac0 commit b53e110
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions desc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __getattr__(name):
config = {"device": None, "avail_mem": None, "kind": None}


def set_device(kind="cpu"):
def set_device(kind="cpu",gpuid=None):
"""Sets the device to use for computation.
If kind==``'gpu'``, checks available GPUs and selects the one with the most
Expand Down Expand Up @@ -127,11 +127,15 @@ def set_device(kind="cpu"):
set_device(kind="cpu")
return
devices = [dev for dev in devices if dev["index"] in gpu_ids]
for dev in devices:
mem = dev["mem_total"] - dev["mem_used"]
if mem > maxmem:
maxmem = mem
selected_gpu = dev

if (not (gpuid == None)) and (str(gpuid) in gpu_ids):
selected_gpu = [dev for dev in devices if dev["index"] == str(gpuid)][0]

Check warning on line 132 in desc/__init__.py

View check run for this annotation

Codecov / codecov/patch

desc/__init__.py#L131-L132

Added lines #L131 - L132 were not covered by tests
else:
for dev in devices:
mem = dev["mem_total"] - dev["mem_used"]
if mem > maxmem:
maxmem = mem
selected_gpu = dev

Check warning on line 138 in desc/__init__.py

View check run for this annotation

Codecov / codecov/patch

desc/__init__.py#L134-L138

Added lines #L134 - L138 were not covered by tests
config["device"] = selected_gpu["type"] + " (id={})".format(
selected_gpu["index"]
)
Expand Down

0 comments on commit b53e110

Please sign in to comment.