Search code examples
shellgpucondanvidiapid

How to check which conda environment is running on Nvidia GPU


I want to know who is using which GPUs.

For example.

After nvidia-smi

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      6260      C   python                           1203MiB |
|    0   N/A  N/A     19842      C   python                           7735MiB |
|    1   N/A  N/A       594      C   python                           1703MiB |
|    1   N/A  N/A      6260      C   python                           1371MiB |
|    1   N/A  N/A     19842      C   python                           5343MiB |

Here I want know which conda environments are using for each PIDs.
(Because for our team, we use different conda envs)

What I want is

PID : 6260 = env1  
PID : 19842 = env2   
...

Thank you.


Solution

  • Conda environment activation is only a state of a shell session. Short of recovering that state from the parent process, the next best thing might be inspecting the process path. For example, I can check some like:

    $ top
    #   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    #     1 root      20   0    2500    584    520 S   0.0   0.0   0:00.19 tini
    #     8 root      20   0    6000   3740   3136 S   0.0   0.0   0:00.00 bash
    #    18 root      20   0    2616    596    528 S   0.0   0.0   0:00.02 sh
    #    25 root      20   0   11592   8936   5756 T   0.0   0.1   0:00.01 python
    #    28 root      20   0    6000   3960   3312 S   0.0   0.0   0:00.16 bash
    #    99 root      20   0    6000   2144   1516 S   0.0   0.0   0:00.00 bash
    #   100 root      20   0    6000   2332   1700 S   0.0   0.0   0:00.00 bash
    #   103 root      20   0   23200  19900   7352 S   0.0   0.1   0:00.10 conda
    #   104 root      20   0    5868   3572   3236 S   0.0   0.0   0:00.00 bash
    #   116 root      20   0   10776   7228   4744 S   0.0   0.0   0:00.00 python
    # 10743 root      20   0    7860   3604   3096 R   0.0   0.0   0:00.00 top     
    
    $ readlink -f /proc/25/exe
    /opt/conda/bin/python3.9
    
    $ readlink -f /proc/116/exe
    /opt/conda/envs/foo/bin/python3.10
    

    which are the base and foo environments, respectively.