windows下安装mpi4py库

来源:互联网 发布:lapse it 编辑:程序博客网 时间:2024/06/05 16:05

关于mpi以及mpi4py这里不多做介绍了,具体可以参阅百度百科,以及mpi4py主页。

测试程序

从https://bitbucket.org/mpi4py/mpi4py/downloads/下载了mpi4py-1.3.1.tar.gz,解压后使用/demo/helloworld.py作为测试程序。摘录如下:

#!/usr/bin/env python"""Parallel Hello World"""from mpi4py import MPIimport syssize = MPI.COMM_WORLD.Get_size()rank = MPI.COMM_WORLD.Get_rank()name = MPI.Get_processor_name()sys.stdout.write(    "Hello, World! I am process %d of %d on %s.\n"    % (rank, size, name))

初始安装方法(出错)

1,下载openMPI的windows版本安装包OpenMPI_v1.6.2-2_win64.exe,成功安装。
2,使用pip install mpi4py直接安装,安装过程没有报错。
3,cmd运行mpirun -n 4 python helloworld.py ,报错,错误信息大意为:没有可执行的文件。运行mpirun -n 4 python.exe helloworld.py ,报错,这里明确提示代码第一句from mpi4py import MPI 引用MPI出错。
4,打开文件pythomPath/Lib/site-packages/mpi4py,发现里面的文件和往常的python第三方库很不一样,找不见MPI.py,只有MPI.pyd和MPI.pxd。
5,怀疑是安装的mpi4py版本不对,下载源码mpi4py-1.3.1.tar.gz查看。源码里确实有MPI文件夹。遂尝试使用python setup.py install自行安装1.3.1版本。然而安装失败,报错信息太长,最后一行大意是没有mpi.h文件。

解决方法 

项百度找到网页Installing MPI and mpi4py on a Windows computer,然而连接失效了-_-||| 借助百度快照强行阅读。整理摘录如下:

Installing MPI and mpi4py on a Windows computer MPI NOTE:

This guide assumes that you are working with Python 2.7 and that you have Anaconda for Python 2.7 installed or that you are executing the console commands in a Python 2.7 environment.

First, you need something corresponding to mpich or openmpi. Microsoft has its own version which works fine on computers using Windows. It is found at Microsoft’s homepage. After downloading and installing, you will need to add the path in which you installed the MPI to your PATH variable. It is easiest done by searching for environment variables in the control panel, which will bring up the System Properties window. Clicking the Environment Variables button will bring up a list of variables. Edit the one named PATH and append the directory in which you installed mpiexec. It will most likely be C:\Program Files\Microsoft MPI\Bin however that may vary, so make sure that you append the right directory to your PATH. To check if it worked properly, open a Command Prompt and type PATH. If it worked correctly the directory will be shown at the end of the PATH variable. Also, you may enter mpiexec in the command prompt to get information on the program. mpi4py

Now, while you are in the Command Prompt, it is time to install a mpi4py package for python. It is done by entering
conda install --channel https://conda.anaconda.org/dhirschfeld mpi4py
in the console. Information on this package can be found here. The package will now be installed and you are most likely good to go. Test the MPI by entering
mpiexec -n 4 python C:\Users\YourName\Documents\Python\YourMPItest.py
in the command prompt, where the directory is (obviously) edited to where you have your Python programs. Examples of programs you can use to test are found at the page included att the FMNN25 homepage. Page 2

大概就是说:
1,要下一个Microsoft MPI,将其Bin文件夹所在路径添入环境变量path
2,使用conda install --channel https://conda.anaconda.org/dhirschfeld mpi4py 安装mpi4py
3,使用mpiexec -n 4 python C:\Users\YourName\Documents\Python\YourMPItest.py 测试一下安装是否成功

按着说明一步一步执行,最终cmd显示如下:

Hello, World! I am process 2 of 4 on user-PC.Hello, World! I am process 3 of 4 on user-PC.Hello, World! I am process 0 of 4 on user-PC.Hello, World! I am process 1 of 4 on user-PC.

终于是装上了。。。开心^_^

0 0