Python调用使用MPI的(Fortran)库

4 投票
1 回答
577 浏览
提问于 2025-05-01 14:13

我想加载并调用一个使用MPI的库。我想象每个进程(rank)都会加载自己版本的库,然后这些库之间会相互通信。我不想在调用库的代码中处理任何通信或MPI的相关操作。无论我加载的是使用MPI的库还是使用OpenMP的库,Python代码都应该保持不变。我在用C语言动态加载并调用这个库时成功了,但在Python中却失败了,出现了以下错误:

mca: base: component_find: 无法打开 /usr/lib/openmpi/lib/openmpi/mca_paffinity_hwloc: 可能是缺少符号,或者是为不同版本的Open MPI编译的?(已忽略)

[..]

看起来opal_init因为某种原因失败了;

[..]

opal_shmem_base_select失败 --> 返回值 -1,而不是OPAL_SUCCESS ompi_mpi_init: orte_init失败 --> 返回“错误” (-1),而不是“成功” (0)

[..]

我想知道我需要做什么才能让它在Python中正常工作。是不是需要重新编译Python和OpenMPI?

下面是一个例子:

testMPI.py

#!/usr/bin/env python
from ctypes import *
# Loading library
raw = cdll.LoadLibrary('./libtest.so.1.0')
print "hello world "
raw.test()

test.f90

subroutine test() bind(c,name='test')
    use MPI
    implicit none
    integer :: nprocs =-1 !< total number of process 
    integer :: rank=0    !< rank in comm world
    integer :: ierr =-1  !< 
    call MPI_init(ierr)
    call MPI_comm_size(MPI_comm_world, nprocs, ierr)
    call MPI_comm_rank(MPI_comm_world, rank, ierr)
    write(*,*)"hello world from ",rank," of ",nprocs
    call MPI_finalize(ierr)
end subroutine

Makefile

FC=mpif90.openmpi
FFLAGS=-free -fPIC -g -Wall 
all: obj test
test:
    mpirun.openmpi -n 4 ./testMPI.py
obj:
    $(FC) $(FFLAGS) -c test.f90
    $(FC) $(FFLAGS) -shared -Wl,-soname,libtest.so.1 -o libtest.so.1.0 test.o
clean:
    rm *.o libtest*
暂无标签

1 个回答

1

我之前也遇到过类似的问题,这里有一个解决办法:

在编译openmpi的时候,运行配置命令时,使用以下选项:

./configure --disable-dlopen

希望这个方法对你有用!

撰写回答