Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux fortran:如何获取集群的节点名_Linux_Fortran - Fatal编程技术网

Linux fortran:如何获取集群的节点名

Linux fortran:如何获取集群的节点名,linux,fortran,Linux,Fortran,我在装有linux系统的集群上运行fortran代码。当代码开始运行时,我希望它输出它正在运行的节点的一些基本信息,特别是节点名称。如何在fortran中实现这一点。如果您的代码与MPI并行(这对于集群上运行的代码来说是很常见的),那么只需调用MPI\u Get\u processor\u name()即可。 如果没有,只需使用iso_c_binding模块调用c函数gethostname(),该函数也会这样做 编辑:下面是一个如何使用iso\u c\u绑定模块调用gethostname()的示

我在装有linux系统的集群上运行fortran代码。当代码开始运行时,我希望它输出它正在运行的节点的一些基本信息,特别是节点名称。如何在fortran中实现这一点。

如果您的代码与MPI并行(这对于集群上运行的代码来说是很常见的),那么只需调用
MPI\u Get\u processor\u name()
即可。 如果没有,只需使用
iso_c_binding
模块调用c函数
gethostname()
,该函数也会这样做

编辑:下面是一个如何使用
iso\u c\u绑定
模块调用
gethostname()
的示例。我绝对不是这方面的专家,所以这可能不是有史以来最有效的一个

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
模块unistd
界面
整数(kind=C_INT)函数gethostname(hname,len)bind(C,name='gethostname')
使用iso_c_绑定
隐式无
字符(种类=字符)::hname(*)
整数(种类=C_INT),值::len
结束函数gethostname
端接口
端模块unistd
程序主机名
使用iso_c_绑定
使用unistd
隐式无
整数(种类=C_INT),参数::sl=100
字符(种类=字符)::hn(sl)
字符(len=sl)::fn
字符::c
整数::res,i,j
res=gethostname(hn,sl)
如果(res==0),则
i=1,sl吗
c=hn(i)
如果(c==char(0))退出
fn(i:i)=c
结束
do j=i,sl
fn(j:j)=”
结束

print*,“->”,trim(fn),“如果您的代码与MPI并行-这对于集群上运行的代码来说是很常见的-那么只需调用
MPI\u Get\u processor\u name()
就可以了。 如果没有,只需使用
iso_c_binding
模块调用c函数
gethostname()
,该函数也会这样做

EDIT:下面是一个如何使用
iso\u c\u binding
模块调用
gethostname()
的示例。我肯定不是这方面的专家,因此它可能不是有史以来最有效的一个

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
模块unistd
界面
整数(kind=C_INT)函数gethostname(hname,len)bind(C,name='gethostname')
使用iso_c_绑定
隐式无
字符(种类=字符)::hname(*)
整数(种类=C_INT),值::len
结束函数gethostname
端接口
端模块unistd
程序主机名
使用iso_c_绑定
使用unistd
隐式无
整数(种类=C_INT),参数::sl=100
字符(种类=字符)::hn(sl)
字符(len=sl)::fn
字符::c
整数::res,i,j
res=gethostname(hn,sl)
如果(res==0),则
i=1,sl吗
c=hn(i)
如果(c==char(0))退出
fn(i:i)=c
结束
do j=i,sl
fn(j:j)=”
结束

print*,“->”,trim(fn),“如果您想要的信息包含在环境变量中,那么简单的方法就是通过调用
get_environment_variable
获取其值。对于主机名

program gethost
character*32 hostname
call get_environment_variable('HOST',hostname)
write(*,*) 'My gracious host is ',trim(hostname)
end program gethost

如果您想要的信息包含在环境变量中,那么简单的方法就是通过调用
get\u environment\u variable
获取其值。对于主机名

program gethost
character*32 hostname
call get_environment_variable('HOST',hostname)
write(*,*) 'My gracious host is ',trim(hostname)
end program gethost

嗨,吉尔。谢谢你的回答。我可以确认
MPI\u Get\u processor\u name()
工作正常。但是我无法让
gethostname()
工作,你能给我举个代码示例吗?哦,我的天啊。这真是出乎我的意料。我想一定是几行。。。无论如何,正如我测试的那样,这是有效的!我想我会坚持MPI方法,或者说俄罗斯科学基金会的方法也很好。嗨,吉尔。谢谢你的回答。我可以确认
MPI\u Get\u processor\u name()
工作正常。但是我无法让
gethostname()
工作,你能给我举个代码示例吗?哦,我的天啊。这真是出乎我的意料。我想一定是几行。。。无论如何,正如我测试的那样,这是有效的!我认为我应该坚持MPI方法,或者RussF的方法也很好。这很有效。在我的集群上,环境变量是
HOSTNAME
。非常感谢你的作品。在我的集群上,环境变量是
HOSTNAME
。多谢各位