fortran:如何获取集群的节点名称 [英] fortran: how to get the node name of a cluster
问题描述
我在一个装有linux系统的集群上运行Fortran代码。当代码开始运行时,我希望它输出它正在运行的节点的一些基本信息,尤其是节点名称。如何在Fortran中实现它。
I am running a fortran code on a cluster equiped with linux system. When the code begin to run, I want it to output some basic information of the node where it is running, especially the node name. How to do it in fortran.
推荐答案
如果您的代码与MPI并行 - 这对于运行代码一个集群 - 然后只需调用 MPI_Get_processor_name()
即可。
如果没有,只需使用 iso_c_binding
模块调用C函数 gethostname()
,那么。
If your code is parallelised with MPI - which is kind of common for a code running on a cluster - then just call MPI_Get_processor_name()
that just does exactly this.
If not, just use the iso_c_binding
module to call the C function gethostname()
, which again just does that.
编辑:这里是一个如何调用 gethostname()
使用 iso_c_binding
模块。我绝对不是这方面的专家,所以它可能不是最有效的......
EDIT: here is an example on how to call gethostname()
with the iso_c_binding
module. I'm definitely not an expert with that so it might not be the most effective one ever...
module unistd
interface
integer( kind = C_INT ) function gethostname( hname, len ) bind( C, name = 'gethostname' )
use iso_c_binding
implicit none
character( kind = C_CHAR ) :: hname( * )
integer( kind = C_INT ), VALUE :: len
end function gethostname
end interface
end module unistd
program hostname
use iso_c_binding
use unistd
implicit none
integer( kind = C_INT ), parameter :: sl = 100
character( kind = C_CHAR ) :: hn( sl )
character( len = sl ) :: fn
character :: c
integer :: res, i, j
res = gethostname( hn, sl )
if ( res == 0 ) then
do i = 1, sl
c = hn( i )
if ( c == char( 0 ) ) exit
fn( i: i ) = c
end do
do j = i, sl
fn( j: j ) = ' '
end do
print *, "->", trim( fn ), "<-"
else
print *, "call to gethostname() didn't work..."
end if
end program hostname
这篇关于fortran:如何获取集群的节点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!