在Python中运行批量同步并行模型(BSP)
作者:互联网
BSP并行编程模型有几个好处-程序员不必显式关心同步,死锁变得不可能,并且速度的推理比传统方法容易得多. SciPy中有一个BSPlib的Python接口:
import Scientific.BSP
我写了一个小程序来测试BSP.该程序是一个简单的随机实验,可以“计算”投掷n个骰子可得出k之和的概率:
from Scientific.BSP import ParSequence, ParFunction, ParRootFunction
from sys import argv
from random import randint
n = int(argv[1]) ; m = int(argv[2]) ; k = int(argv[3])
def sumWuerfe(ws): return len([w for w in ws if sum(w)==k])
glb_sumWuerfe= ParFunction(sumWuerfe)
def ausgabe(result): print float(result)/len(wuerfe)
glb_ausgabe = ParRootFunction(output)
wuerfe = [[randint(1,6) for _ in range(n)] for _ in range(m)]
glb_wuerfe = ParSequence(wuerfe)
# The parallel calc:
ergs = glb_sumWuerfe(glb_wuerfe)
# collecting the results in Processor 0:
ergsGesamt= results.reduce(lambda x,y:x+y, 0)
glb_output(ergsGesamt)
该程序运行良好,但是:它仅使用一个过程!
我的问题:有谁知道如何告诉此Pythonb-BSP-Script使用4(或8或16)个进程?我以为这个BSP实现应该使用MPI,但是通过mpiexe -n 4 randExp.py启动脚本不起作用.
解决方法:
很小的事情,但是科学Python!=您问题中的SciPy …
如果您下载ScientificPython源代码,则会看到README.BSP,README.MPI和README.BSPlib.不幸的是,在线页面上的信息并没有太多提及.
README.BSP非常明确地说明了使BSP真正并行运行所需的操作:
In order to use the module
Scientific.BSP using more than one
real processor, you must compile
either the BSPlib or the MPI
interface. See README.BSPlib and
README.MPI for installation details.
The BSPlib interface is probably more
efficient (I haven’t done extensive
tests yet), and allows the use of the
BSP toolset, on the other hand MPI is
more widely available and might thus
already be installed on your machine.
For serious use, you should probably
install both and make comparisons for
your own applications. Application
programs do not have to be modified to
switch between MPI and BSPlib, only
the method to run the program on a
multiprocessor machine must be
adapted.To execute a program in parallel mode,
use the mpipython or bsppython
executable. The manual for your MPI or
BSPlib installation will tell you how
to define the number of processors.
README.MPI会告诉您如何获得MPI支持:
Here is what you have to do to get MPI
support in Scientific Python:1) Build and install Scientific Python
as usual (i.e. “python setup.py
install” in most cases).2) Go to the directory Src/MPI.
3) Type “python compile.py”.
4) Move the resulting executable
“mpipython” to a directory on your
system’s execution path.
因此,您必须显式构建更多的BSP东西,才能利用真正的并行性.好消息是您不必更改程序.这样做的原因是,不同的系统安装了不同的并行库,并且位于这些库之上的库必须具有这样的配置/构建步骤,才能利用可用的一切.
标签:parallel-processing,python 来源: https://codeday.me/bug/20191208/2092286.html