【问题标题】:Why is my fortran routine passing an incorrect value to my C++ function?为什么我的 fortran 例程将不正确的值传递给我的 C++ 函数?
【发布时间】:2020-06-12 20:32:51
【问题描述】:

我有一个调用外部静态库中的 C++ 函数的 fortran 例程。我有一个整数:

integer (c_int) :: processor_wall_point_count

这只是被传递给一个函数:

print*, processor_wall_point_count         ! gives 112
call fci_wmm_allocate_domain(processor_wall_point_count, wall_model_ray_point_count)

C++ 函数:

void fci_wmm_allocate_domain(int* _processor_wall_point_count, int* _ray_point_count)
{
    std::cout << *_processor_wall_point_count << std::endl; // gives 70
}

主代码有一个 MPI 环境,当运行在 10 个处理器上时,

print*, process_id, processor_wall_point_count !process id, variable
call MPI_Barrier()
call fci_wmm_allocate_domain(processor_wall_point_count, wall_model_ray_point_count)

在 C++ 中:

void fci_wmm_allocate_domain(int* _processor_wall_point_count, int* _ray_point_count)
{
    std::cout << process_id << ", " <<*_processor_wall_point_count << std::endl;
    MPI_Barrier(MPI_COMM_WORLD);
}

我得到以下信息:

       8          32
       9           0
       0          16
       2          48
       6           0
       1           0
       3          16
       5           0
       7           0
       4           0
2, 48
8, 32
0, 10
3, 16
5, 0
9, 0
6, 0
1, 0
7, 0
4, 0

即,除了处理器 0 之外,所有值都正确传递。我之前使用过 C 绑定,没有(太多)问题。这是怎么回事?

编辑:这里是 fortran 界面:

interface
    subroutine fci_wmm_allocate_domain(point_count_F, ray_points_F) bind (c)
        use iso_c_binding
        integer (c_int), intent(in)    :: point_count_F, ray_points_F
    end subroutine fci_wmm_allocate_domain
end interface

问题: 我不确定我是如何错过这个的,但我只在处理器 0 上调用了以下函数:

void print_node_info(void)
{
    if (is_node_root_process && verbose)
    {
        std::cout << "[I] Node " << node_name << " (0x";
        std::cout << std::hex << processor_node_id;
        std::cout << ") has root process " << current_node_root_process_id << std::endl;
    }
}

改成

void print_node_info(void)
{
    if (is_node_root_process && verbose)
    {
        std::cout << "[I] Node " << node_name << " (0x";
        std::cout << std::hex << processor_node_id << std::dec;
        std::cout << ") has root process " << current_node_root_process_id << std::endl;
    }
}

修复了问题。

【问题讨论】:

  • @francescalus 哎呀,我的意思是包括那个。立即编辑!

标签: c++ fortran mpi fortran-iso-c-binding


【解决方案1】:

有趣的是 112 = 0x70 和 16 = 0x10。会不会是某个被遗忘的 C++ 流操纵器 (std::hex)?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多