我有一个调用外部静态库中的 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/