【问题标题】:fortran: how to get the node name of a cluster [duplicate]fortran:如何获取集群的节点名 [重复]
【发布时间】:2016-05-02 15:43:06
【问题描述】:

我在一个装有 linux 系统的集群上运行一个 fortran 代码。当代码开始运行时,我希望它输出运行所在节点的一些基本信息,尤其是节点名称。如何在 fortran 中做到这一点。

【问题讨论】:

    标签: linux fortran


    【解决方案1】:

    如果您的代码与 MPI 并行化(这对于在集群上运行的代码来说很常见),那么只需调用 MPI_Get_processor_name() 即可。 如果没有,只需使用iso_c_binding 模块调用C 函数gethostname(),它再次执行此操作。

    编辑:这是一个关于如何使用iso_c_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
    

    【讨论】:

    • 嗨,吉尔斯。谢谢你的答案。我可以确认MPI_Get_processor_name() 有效。但是我无法让gethostname() 工作,你能给出一个代码示例吗?
    • 哦,天哪。这真的出乎我的意料。我认为它必须是几行......无论如何,正如我测试的那样,这行得通!我想我会坚持使用 MPI 方法,或者 RussF 的方法也很好。
    【解决方案2】:

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

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

    【讨论】:

    • 这行得通。在我的集群上,环境变量是HOSTNAME。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 2021-07-24
    • 1970-01-01
    相关资源
    最近更新 更多