You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用含MPI的Fortran并行程序报错:ORTE_ERROR_LOG在ess_env_module.c中未找到

Fixing MPI Communication Error Between mpi4py and f2py-compiled Fortran Code

Alright, let's break down why you're hitting that ORTE error when running your code in parallel, and how to fix it. The core issue here is a mismatch or misconfiguration in the MPI runtime environment between mpi4py and your f2py-compiled Fortran module.

Let's Walk Through the Fixes Step by Step

1. Fix the Invalid Library Path in Your f2py Compile Command

First off, your compile command has a typo in the library path: -L/usr/local/lib/lib is incorrect. The correct path should be -L/usr/local/lib (drop the extra trailing lib). This mistake was likely causing the linker to fail to find the proper MPI libraries, leading to runtime communication errors.

2. Ensure mpi4py and Fortran Use the Exact Same MPI Installation

mpi4py is built against your system's Open MPI 1.10.2, so you need to make sure the mpif90 you're using with f2py is from the same Open MPI installation.

To verify this:

  • Run this Python snippet to check mpi4py's MPI vendor:
    from mpi4py import MPI
    print(MPI.get_vendor())
    
  • Then run mpif90 --version in your terminal. Both should report Open MPI 1.10.2. If they don't, adjust your PATH to prioritize the correct mpif90.

3. Simplify Your f2py Compile Command

You don't need to manually specify --include-paths, -L, or -lmpi when using mpif90 as your Fortran compiler. mpif90 automatically handles including MPI headers and linking against MPI libraries. A clean, correct compile command is:

f2py -c --fcompiler=gnu95 --f90exec=mpif90 helloworld.f90 -m helloworld

This eliminates the risk of manually specifying wrong paths or libraries.

4. Add MPI Initialization Check to Fortran (Optional but Robust)

While mpi4py initializes the MPI environment for you, adding a check in your Fortran subroutine ensures compatibility across different scenarios. This isn't strictly necessary for your case, but it makes the code more robust:

subroutine sayhello(comm)
use mpi
implicit none
integer :: comm, rank, size, ierr, flag
! Check if MPI is already initialized
call MPI_Initialized(flag, ierr)
if (.not. flag) then
    call MPI_Init(ierr)
endif
call MPI_Comm_size(comm, size, ierr)
call MPI_Comm_rank(comm, rank, ierr)
print *, 'Hello, World! I am process ',rank,' of ',size,'.'
end subroutine sayhello

5. Verify Runtime Library Path

Make sure your LD_LIBRARY_PATH includes the Open MPI library directory before running the code. You can set this temporarily with:

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

Test the Fixed Setup

After making these changes:

  1. Recompile your Fortran module with the corrected command.
  2. Run the parallel command again:
    mpiexec -n 2 python driver_helloworld.py
    

You should now see the expected output from both processes:

Hello, World! I am process  0 of  2 .
 Hello, World! I am process  1 of  2 .

内容的提问来源于stack exchange,提问作者JGM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:51:14