c++ - 为什么我的 fortran 例程将不正确的值传递给我的 C++ 函数?

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

我有一个调用外部静态库中的 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 绑定(bind),没有(太多)问题。这里发生了什么?

编辑:这是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;
    }
}

解决了这个问题。

最佳答案

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

关于c++ - 为什么我的 fortran 例程将不正确的值传递给我的 C++ 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60456777/

相关文章:

cmake - CMake 找不到 Fortran 模块文件

function - 函数 ‘array’没有IMPLICIT类型

fortran - 在 Fortran 90 中使用高斯消元法计算 4 方程组中的浮点误差

数组结构的 MPI 数据类型

c++ - QFile和QTextStream帮助(用Usernames、passwords和names为用户写一个ID)

c++ - 虽然循环比我指定的要循环更多次

c++ - Microsoft Visual Studio 2010 无法编译、加载/创建文件或识别 iostream?

c - MPI_Send是否需要在对应的MPI_Recv之前调用

output - MPI pbs 输出文件在运行时可用吗?

c++ - 如何在 google mock for c 中自动生成 mock