使用Fortran查找可用的显卡内存 [英] Find available graphics card memory using Fortran
问题描述
我使用GlobalMemoryStatusEX为了找出我的系统中的内存量。
有没有类似的方法来找到我的显卡的内存量?
这是我的一段代码:
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
我使用英特尔Visual Fortran 2010 Windows 7 x64。
谢谢!
I am using Intel Visual Fortran 2010 on Windows 7 x64. Thank you!
推荐答案
由于您使用CUDA标记标记此问题,我会提供CUDA答案。不知道是否真的对你的环境有意义。
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.
我没有测试这个在IVF,但它适用于gfortran和PGI fortran(linux)。您可以使用fortran iso_c_binding
模块在许多实现中直接从fortran代码中的CUDA运行时库库中调用例程。其中一个例程是 cudaMemGetInfo 。
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.
这是一个完全工作的例子,从gfortran(在linux)调用它:
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
$
在windows中,您需要有一个正确安装的CUDA环境(假设visual studio)。然后,您需要在该安装中找到 cudart.lib
,并链接。我不是100%确定这将在IVF中成功链接,因为我不知道它是否将链接类似的VS库链接的方式。
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.
这篇关于使用Fortran查找可用的显卡内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!