Find available graphics card memory using Fortran

2020-03-25 10:37发布

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 piece of my code :

use kernel32
use ifwinty 
implicit none
type(T_MEMORYSTATUSEX) :: status
integer(8) :: RetVal
status%dwLength = sizeof(status)
RetVal =  GlobalMemoryStatusEX(status)
write(*,*) 'Memory Available =',status%ullAvailPhys

I am using Intel Visual Fortran 2010 on Windows 7 x64. Thank you!

1条回答
够拽才男人
2楼-- · 2020-03-25 11:43

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.

查看更多
登录 后发表回答