python mpi4py,如何从串行python脚本使用mpi4py应用程序

I tried to make a library based on mpi4py, but I want to use it in serial python code.

$ python serial_source.py

but inside serial_source.py exists some function called parallel_bar

from foo import parallel_bar

# Can I to make this with mpi4py like a common python source code?

result = parallel_bar(num_proc = 5)

The motivation for this question is about finding the right way to use mpi4py to optimize programs in python which were not necessarily designed to be run completely in parallel.

解决方案

This is indeed possible and is in the documentation of mpi4py in the section Dynamic Process Management. What you need is the so called Spawn functionality which is not available with MSMPI (in case you are working with Windows) see also Spawn not implemented in MSMPI.

Example

The first file provides a kind of wrapper to your function to hide all the MPI stuff, which I guess is your intention. Internally it calls the "actual" script containing your parallel code in 4 newly spawned processes.

Finally, you can open a python terminal and call:

from my_prog import parallel_fun

parallel_fun()

# Hi from 0/4

# Hi from 3/4

# Hi from 1/4

# Hi from 2/4

# We got the magic number 6

my_prog.py

import sys

import numpy as np

from mpi4py import MPI

def parallel_fun():

comm = MPI.COMM_SELF.Spawn(

sys.executable,

args = ['child.py'],

maxprocs=4)

N = np.array(0, dtype='i')

comm.Reduce(None, [N, MPI.INT], op=MPI.SUM, root=MPI.ROOT)

print(f'We got the magic number {N}')

Here the child file with the parallel code:

child.py

from mpi4py import MPI

import numpy as np

comm = MPI.Comm.Get_parent()

print(f'Hi from {comm.Get_rank()}/{comm.Get_size()}')

N = np.array(comm.Get_rank(), dtype='i')

comm.Reduce([N, MPI.INT], None, op=MPI.SUM, root=0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值