【问题标题】:Find available graphics card memory using Fortran使用 Fortran 查找可用的显卡内存
【发布时间】:2014-12-20 09:51:32
【问题描述】:

我正在使用 GlobalMemoryStatusEX 来找出我系统中的内存量。 有没有类似的方法来查找我的显卡上的内存量? 这是我的一段代码:

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

我在 Windows 7 x64 上使用 Intel Visual Fortran 2010。 谢谢!

【问题讨论】:

    标签: memory graphics cuda fortran intel


    【解决方案1】:

    既然你用 CUDA 标签标记了这个问题,我将提供一个 CUDA 答案。不确定考虑到您的环境是否真的有意义。

    我没有在 IVF 上测试过这个,但它适用于 gfortran 和 PGI fortran (linux)。您可以使用许多实现中可用的 fortran iso_c_binding 模块直接从 fortran 代码中的 CUDA 运行时 API 库调用例程。其中一个例程是cudaMemGetInfo

    这是一个从 gfortran 调用它的完整示例(在 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
    
    $
    

    在 Windows 中,您需要正确安装 CUDA 环境(假定为 Visual Studio)。然后,您需要在该安装中找到cudart.lib,并与之链接。我不能 100% 确定这会在 IVF 中成功链接,因为我不知道它是否会以类似于 VS 库链接的方式链接。

    【讨论】:

    • 非常感谢您的回答!它在我的体外受精环境中效果很好!
    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2010-11-16
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多