Find available graphics card memory using Fortran

前端 未结 1 606
孤城傲影
孤城傲影 2021-01-13 20:29

I am using GlobalMemoryStatusEX in order to find out the amount of memory in my system. Is there a similar way to find the amount of memory on my graphics card? Here is a pi

相关标签:
1条回答
  • 2021-01-13 21:02

    Since you tagged this question with the CUDA tag, I'll offer a CUDA answer. Not sure if it really makes sense given your environment.

    I haven't tested this on IVF, but it works on gfortran and PGI fortran (linux). You can use the fortran iso_c_binding module available in many implementations to directly call routines from the CUDA runtime API library in fortran code. One of those routines is cudaMemGetInfo.

    Here's a fully worked example of calling it from gfortran (on linux):

    $ cat cuda_mem.f90
    !=======================================================================================================================
    !Interface to cuda C subroutines
    !=======================================================================================================================
    module cuda_rt
    
      use iso_c_binding
    
      interface
         !
         integer (c_int) function cudaMemGetInfo(fre, tot) bind(C, name="cudaMemGetInfo")
           use iso_c_binding
           implicit none
           type(c_ptr),value :: fre
           type(c_ptr),value :: tot
         end function cudaMemGetInfo
         !
      end interface
    
    end module cuda_rt
    
    
    
    !=======================================================================================================================
    program main
    !=======================================================================================================================
    
      use iso_c_binding
    
      use cuda_rt
    
      type(c_ptr) :: cpfre, cptot
      integer*8, target   :: freemem, totmem
      integer*4   :: stat
      freemem = 0
      totmem  = 0
      cpfre = c_loc(freemem)
      cptot = c_loc(totmem)
      stat = cudaMemGetInfo(cpfre, cptot)
      if (stat .ne. 0 ) then
          write (*,*)
          write (*, '(A, I2)') " CUDA error: ", stat
          write (*,*)
          stop
      end if
    
      write (*, '(A, I10)') "  free: ", freemem
      write (*, '(A, I10)') " total: ", totmem
      write (*,*)
    
    end program main
    
    $ gfortran -O3 cuda_mem.f90 -L/usr/local/cuda/lib64 -lcudart -o cuda_mem
    $ ./cuda_mem
      free: 2755256320
     total: 2817982464
    
    $
    

    In windows, you would need to have a properly installed CUDA environment, (which presumes visual studio). You would then need to locate the cudart.lib in that install, and link against that. I'm not 100% sure this would link successfully in IVF, since I don't know if it would link similarly to the way VS libraries link.

    0 讨论(0)
提交回复
热议问题