[Rtk-users] Clearing cuda variables in python

krah nils.krah at creatis.insa-lyon.fr
Fri Jun 16 14:58:28 CEST 2023


      
Just to add to Simon's answer:
  
​Objects are deleted by python's garbage collection mechanism once there are no references left to the object. (keyword: reference counting).
  
​The
  
​del variable
  
​statement Simon mention deletes the reference variable, but the memory is freed only when python runs a garbage collection. That might immediately afterwards, or slightly later (scheduled). Usually, you do not need to worry about that and deleting the reference as Simon said is sufficient. If for some reason you need to be absolutely sure that the object is garbage collected immediately, you can trigger a garbage collection by
  
​
  
​import gc
  
​gc.collect()
  
  
Again, that should not be necessary, but it is useful to bear in mind how the memory is actually handled in python.
  
​
  
​Cheers,
  
​Nils ​
  
​
  
On Jun 16 2023, at 9:10 am, Simon Rit  <simon.rit at creatis.insa-lyon.fr>  wrote:
  
>   
>   
> Hi,
>   
>   
> If you have a Python variable holding memory, you can simply call
>   
>   
> del variable_name
>   
> If you want to clear the GPU memory for an image, you can access the CPU buffer pointer to have it moved to the computer RAM. Examples below.
>   
> Simon
>   
>
>   
> >>>  import os
>   
> >>>  os.system('nvidia-smi')
>   
> Fri Jun 16 08:49:38 2023            
>   
> +-----------------------------------------------------------------------------+
>   
> | NVIDIA-SMI 510.39.01        Driver Version: 510.39.01        CUDA Version: 11.6         |
>   
> |-------------------------------+----------------------+----------------------+
>   
> | GPU    Name                Persistence-M| Bus-Id                Disp.A | Volatile Uncorr. ECC |
>   
> | Fan    Temp    Perf    Pwr:Usage/Cap|                 Memory-Usage | GPU-Util    Compute M. |
>   
> |                                                             |                                            |                             MIG M. |
>   
> |===============================+======================+======================|
>   
> |     0    Quadro P2000                Off    | 00000000:01:00.0 Off |                                    N/A |
>   
> | N/A     52C        P8        N/A /    N/A |            0MiB /    4096MiB |            0%            Default |
>   
> |                                                             |                                            |                                    N/A |
>   
> +-------------------------------+----------------------+----------------------+
>   
>                                                                                                                                                               
>   
> +-----------------------------------------------------------------------------+
>   
> | Processes:                                                                                                                                    |
>   
> |    GPU     GI     CI                PID     Type     Process name                                    GPU Memory |
>   
> |                ID     ID                                                                                                     Usage            |
>   
> |=============================================================================|
>   
> |    No running processes found                                                                                                 |
>   
> +-----------------------------------------------------------------------------+
>   
> 0
>   
> >>>  import itk
>   
> >>>  from itk import RTK as rtk
>   
> >>>  img=itk.CudaImage[itk.F, 3].New()
>   
> >>>  img.SetRegions([500]*3)
>   
> >>>  img.Allocate()
>   
> >>>  os.system('nvidia-smi')
>   
> Fri Jun 16 08:51:28 2023            
>   
> +-----------------------------------------------------------------------------+
>   
> | NVIDIA-SMI 510.39.01        Driver Version: 510.39.01        CUDA Version: 11.6         |
>   
> |-------------------------------+----------------------+----------------------+
>   
> | GPU    Name                Persistence-M| Bus-Id                Disp.A | Volatile Uncorr. ECC |
>   
> | Fan    Temp    Perf    Pwr:Usage/Cap|                 Memory-Usage | GPU-Util    Compute M. |
>   
> |                                                             |                                            |                             MIG M. |
>   
> |===============================+======================+======================|
>   
> |     0    Quadro P2000                Off    | 00000000:01:00.0 Off |                                    N/A |
>   
> | N/A     58C        P8        N/A /    N/A |         45MiB /    4096MiB |            0%            Default |
>   
> |                                                             |                                            |                                    N/A |
>   
> +-------------------------------+----------------------+----------------------+
>   
>                                                                                                                                                               
>   
> +-----------------------------------------------------------------------------+
>   
> | Processes:                                                                                                                                    |
>   
> |    GPU     GI     CI                PID     Type     Process name                                    GPU Memory |
>   
> |                ID     ID                                                                                                     Usage            |
>   
> |=============================================================================|
>   
> |        0     N/A    N/A         10890            C     python                                                         43MiB |
>   
> +-----------------------------------------------------------------------------+
>   
> 0
>   
> >>>  img.GetCudaDataManager().GetGPUBufferPointer()
>   
> <Swig Object of type 'void *' at 0x7f20d3327f60>
>   
> >>>  os.system('nvidia-smi')
>   
> Fri Jun 16 08:52:05 2023            
>   
> +-----------------------------------------------------------------------------+
>   
> | NVIDIA-SMI 510.39.01        Driver Version: 510.39.01        CUDA Version: 11.6         |
>   
> |-------------------------------+----------------------+----------------------+
>   
> | GPU    Name                Persistence-M| Bus-Id                Disp.A | Volatile Uncorr. ECC |
>   
> | Fan    Temp    Perf    Pwr:Usage/Cap|                 Memory-Usage | GPU-Util    Compute M. |
>   
> |                                                             |                                            |                             MIG M. |
>   
> |===============================+======================+======================|
>   
> |     0    Quadro P2000                Off    | 00000000:01:00.0 Off |                                    N/A |
>   
> | N/A     54C        P0        N/A /    N/A |        525MiB /    4096MiB |            0%            Default |
>   
> |                                                             |                                            |                                    N/A |
>   
> +-------------------------------+----------------------+----------------------+
>   
>                                                                                                                                                               
>   
> +-----------------------------------------------------------------------------+
>   
> | Processes:                                                                                                                                    |
>   
> |    GPU     GI     CI                PID     Type     Process name                                    GPU Memory |
>   
> |                ID     ID                                                                                                     Usage            |
>   
> |=============================================================================|
>   
> |        0     N/A    N/A         10890            C     python                                                        523MiB |
>   
> +-----------------------------------------------------------------------------+
>   
> 0
>   
> >>>  del img
>   
> >>>  os.system('nvidia-smi')
>   
> Fri Jun 16 08:52:21 2023            
>   
> +-----------------------------------------------------------------------------+
>   
> | NVIDIA-SMI 510.39.01        Driver Version: 510.39.01        CUDA Version: 11.6         |
>   
> |-------------------------------+----------------------+----------------------+
>   
> | GPU    Name                Persistence-M| Bus-Id                Disp.A | Volatile Uncorr. ECC |
>   
> | Fan    Temp    Perf    Pwr:Usage/Cap|                 Memory-Usage | GPU-Util    Compute M. |
>   
> |                                                             |                                            |                             MIG M. |
>   
> |===============================+======================+======================|
>   
> |     0    Quadro P2000                Off    | 00000000:01:00.0 Off |                                    N/A |
>   
> | N/A     56C        P0        N/A /    N/A |            2MiB /    4096MiB |            0%            Default |
>   
> |                                                             |                                            |                                    N/A |
>   
> +-------------------------------+----------------------+----------------------+
>   
>                                                                                                                                                               
>   
> +-----------------------------------------------------------------------------+
>   
> | Processes:                                                                                                                                    |
>   
> |    GPU     GI     CI                PID     Type     Process name                                    GPU Memory |
>   
> |                ID     ID                                                                                                     Usage            |
>   
> |=============================================================================|
>   
> |    No running processes found                                                                                                 |
>   
> +-----------------------------------------------------------------------------+
>   
> 0
>   
> >>>  img=itk.CudaImage[itk.F, 3].New()
>   
> >>>  img.SetRegions([500]*3)
>   
> >>>  img.GetCudaDataManager().GetGPUBufferPointer()
>   
> <Swig Object of type 'void *' at 0x7f20b2c30870>
>   
> >>>  os.system('nvidia-smi')
>   
> Fri Jun 16 08:54:10 2023            
>   
> +-----------------------------------------------------------------------------+
>   
> | NVIDIA-SMI 510.39.01        Driver Version: 510.39.01        CUDA Version: 11.6         |
>   
> |-------------------------------+----------------------+----------------------+
>   
> | GPU    Name                Persistence-M| Bus-Id                Disp.A | Volatile Uncorr. ECC |
>   
> | Fan    Temp    Perf    Pwr:Usage/Cap|                 Memory-Usage | GPU-Util    Compute M. |
>   
> |                                                             |                                            |                             MIG M. |
>   
> |===============================+======================+======================|
>   
> |     0    Quadro P2000                Off    | 00000000:01:00.0 Off |                                    N/A |
>   
> | N/A     58C        P0        N/A /    N/A |        525MiB /    4096MiB |            0%            Default |
>   
> |                                                             |                                            |                                    N/A |
>   
> +-------------------------------+----------------------+----------------------+
>   
>                                                                                                                                                               
>   
> +-----------------------------------------------------------------------------+
>   
> | Processes:                                                                                                                                    |
>   
> |    GPU     GI     CI                PID     Type     Process name                                    GPU Memory |
>   
> |                ID     ID                                                                                                     Usage            |
>   
> |=============================================================================|
>   
> |        0     N/A    N/A         10890            C     python                                                        523MiB |
>   
> +-----------------------------------------------------------------------------+
>   
> 0
>   
> >>>  img.GetCudaDataManager().GetCPUBufferPointer()
>   
> >>>  os.system('nvidia-smi')
>   
> Fri Jun 16 08:54:20 2023            
>   
> +-----------------------------------------------------------------------------+
>   
> | NVIDIA-SMI 510.39.01        Driver Version: 510.39.01        CUDA Version: 11.6         |
>   
> |-------------------------------+----------------------+----------------------+
>   
> | GPU    Name                Persistence-M| Bus-Id                Disp.A | Volatile Uncorr. ECC |
>   
> | Fan    Temp    Perf    Pwr:Usage/Cap|                 Memory-Usage | GPU-Util    Compute M. |
>   
> |                                                             |                                            |                             MIG M. |
>   
> |===============================+======================+======================|
>   
> |     0    Quadro P2000                Off    | 00000000:01:00.0 Off |                                    N/A |
>   
> | N/A     58C        P0        N/A /    N/A |         47MiB /    4096MiB |            0%            Default |
>   
> |                                                             |                                            |                                    N/A |
>   
> +-------------------------------+----------------------+----------------------+
>   
>                                                                                                                                                               
>   
> +-----------------------------------------------------------------------------+
>   
> | Processes:                                                                                                                                    |
>   
> |    GPU     GI     CI                PID     Type     Process name                                    GPU Memory |
>   
> |                ID     ID                                                                                                     Usage            |
>   
> |=============================================================================|
>   
> |        0     N/A    N/A         10890            C     python                                                         45MiB |
>   
> +-----------------------------------------------------------------------------+
>   
> 0
>   
>   
>   
>   
>   
> On Thu, Jun 15, 2023 at 5:33 PM Rahman, Obaid  <rahmano at ornl.gov (mailto:rahmano at ornl.gov)>  wrote:
>   
>   
> >   
> >   
> > Hi,
> >   
> >
> >   
> > I am using ink-rtk-cuda116 with  python.
> >   
> > I have too many cuda variables (images and filters).
> >   
> > I would like to clear some of these variables.
> >   
> >
> >   
> > I am getting the following error:
> >   
> > ITK ERROR: CUDA ERROR: out of memory
> >   
> >
> >   
> > Does anyone know how to clear Cuda variables in python?
> >   
> > Thanks.
> >   
> >
> >   
> > Best,
> >   
> > Obaidullah Rahman
> >   
> >   
> > Oak Ridge National Laboratory, TN, United States
> >   
> >   
> > _______________________________________________
> >   
> > Rtk-users mailing list
> >   
> > rtk-users at openrtk.org (mailto:rtk-users at openrtk.org)
> >   
> >  https://www.creatis.insa-lyon.fr/mailman/listinfo/rtk-users
> >   
>   
>   
>   
> _______________________________________________
>   
> Rtk-users mailing list
>   
> rtk-users at openrtk.org
>   
> https://www.creatis.insa-lyon.fr/mailman/listinfo/rtk-users
>   
>   
     
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.creatis.insa-lyon.fr/pipermail/rtk-users/attachments/20230616/fa5b188a/attachment-0001.htm>


More information about the Rtk-users mailing list