既然你用 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 库链接的方式链接。