# Using specific GPUs in a shared machine

**URL:** <https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274>\
**Category:** Ray Tune\
**Created:** [March 3, 2022, 9:58am UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274 "2022-03-03T09:58:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lesolorzanov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lesolorzanov/32/2217_2.png) [@lesolorzanov](https://discuss.ray.io/u/lesolorzanov)\
**Post date:** [March 3, 2022, 9:58am UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274/1 "2022-03-03T09:58:09Z")

</div>

At work we share the computers with GPUs. A colleague needs to use 2 out of the 4 gpus and I need the other 2. She uses 0 and 1 and I need to use 2 and 3.

I read here ([using ray with GPU](https://docs.ray.io/en/master/ray-core/using-ray-with-gpus.html)) that “_Ray will automatically set the `CUDA_VISIBLE_DEVICES` environment variable._”.

I don’t like this behavior. We usually run scripts that set the devices o she has _0,1_ and I use _2,3_.

Is there a way to solve this?

---

<div class="post-metadata">

**Author:** ![xwjiang2010](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xwjiang2010/32/1476_2.png) [@xwjiang2010](https://discuss.ray.io/u/xwjiang2010)\
**Post date:** [March 3, 2022, 10:09pm UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274/2 "2022-03-03T22:09:33Z")

</div>

Does this work for your case?

> <https://github.com/ray-project/ray/issues/9863>
>
> \### What is your question?
> 
> Say, I have 4 GPUs with ids=\[0, 1, 2, 3\] and I onl…y want to run all trials for Tune on id=2 and id=3 only. That means I can only maximize the use of the third and fourth GPU without touching the first two GPUs. How can I achieve this? 
> 
> \`\`\`ray.init(num\_cpus=num\_cpus, num\_gpus=num\_gpus, temp\_dir=ray\_log)\`\`\`
> 
> The attribute \`\`\`num\_gpus\`\`\` is the number of GPUs ray can use. When setting \`\`\`num\_gpus=1\`\`\`, all the trials run on the first device (GPU id=0). When increasing \`\`\`num\_gpus\`\`\`, all the trials will ordinally use GPUs from id=0 to id=3... I want to know how to specify the exact GPU ids, e.g., all trials run on id=2 and id=3.
> 
> I've tried specifying GPU id in the training functions, but raised \`\`\`RuntimeError: CUDA error: invalid device ordinal\`\`\`. 
> 
> I'm still new to this great project. Appreciate your warm help!
> 
> \*Ray version and other system information (Python version, TensorFlow version, OS):\*
> OS: Linux
> Python: 3.7.4
> Ray: 0.8.6

---

<div class="post-metadata">

**Author:** ![lesolorzanov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lesolorzanov/32/2217_2.png) [@lesolorzanov](https://discuss.ray.io/u/lesolorzanov)\
**Post date:** [March 4, 2022, 12:38pm UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274/3 "2022-03-04T12:38:21Z")

</div>

Thank you. Yes ideally this should work. But:

1. Ray tune docs say that the variable is reset and changed.
2. if I write gpus=2 it uses always 0 and 1, and not 2 and 3, for example.

So I was wondering if Ray tune has the hability that one could say, use\_only=[2,3] or something

I’ll try to make it work with the environment variable in the meantime.

---

<div class="post-metadata">

**Author:** ![xwjiang2010](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xwjiang2010/32/1476_2.png) [@xwjiang2010](https://discuss.ray.io/u/xwjiang2010)\
**Post date:** [March 16, 2022, 8:05pm UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274/4 "2022-03-16T20:05:08Z")

</div>

Hey @lesolorzanov  
I took another look at our documentation with code. I believe that user could set CUDA\_VISIBLE\_DEVICE through env var. And if that is set, Ray will respect that in the sense that only GPUs in this list will be returned.  
See [worker.py - ray-project/ray - Sourcegraph](https://sourcegraph.com/github.com/ray-project/ray/-/blob/python/ray/worker.py?L544)

---

<div class="post-metadata">

**Author:** ![xwjiang2010](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xwjiang2010/32/1476_2.png) [@xwjiang2010](https://discuss.ray.io/u/xwjiang2010)\
**Post date:** [March 16, 2022, 8:05pm UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274/5 "2022-03-16T20:05:29Z")

</div>

Please give it a try and let me know if you run into any issues.

---

<div class="post-metadata">

**Author:** ![lesolorzanov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/lesolorzanov/32/2217_2.png) [@lesolorzanov](https://discuss.ray.io/u/lesolorzanov)\
**Post date:** [March 23, 2022, 3:29pm UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274/6 "2022-03-23T15:29:56Z")

</div>

Thank you yes, indeed I can set it at the beginning, I use:

`os.environ["CUDA_VISIBLE_DEVICES"]="0,1,3"`

in my python code.

**Edit: nevermind it seems that if I don’t write cpu in resources per trial it works now, it is placing a trial per gpu.**

thank you.

The problem I have now is that it is trying to put all the trials in the same gpu at the same time and I am getting the cuda memory error.

I have 16 cpu cores and 4 gpus in the computer but I am only allowed to use gpus: 0 1 and 3.

My ideal scenario would be that say that I have 3 trials, My hope would be that when I call `tune.run` each of those 3 trials would start at the same time in the 3 gpus. But it’s just not working.

My `tune.run` looks like this

```auto

tune.run(
    trainer,
    name=name, 
    config=config,
    stop={'stop_flag': 0.95, 
            'training_iteration': 5,
            },
    local_dir=path_out_base, 
    checkpoint_at_end=False,
    resources_per_trial={'cpu':5, 'gpu': 1},
    num_samples=1)

```

---

<div class="post-metadata">

**Author:** ![kai](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/kai/32/3380_2.png) [@kai](https://discuss.ray.io/u/kai)\
**Post date:** [March 24, 2022, 11:31am UTC](https://discuss.ray.io/t/using-specific-gpus-in-a-shared-machine/5274/7 "2022-03-24T11:31:33Z")

</div>

Hi @lesolorzanov, that should work though (we use it all the time in our end to end testing). Just out of curiosity, are you using a grid search? Because `num_samples=1` means you’re only going to start 1 sample otherwise.

If that’s the case, it seems that it’s stil lscheduling on the wrong GPU. Can you confirm with nvidia-smi where the trials are scheduled exactly?
