使用Code :: Blocks运行具有多个进程的MPI程序 [英] Running MPI programs with multiple processes with Code::Blocks
问题描述
我是MPI的新手,正在尝试运行"hello world"程序.这是我的程序
I am new to MPI and trying to run 'hello world' program. Here is my program
#include <iostream>
#include <mpi.h>
using namespace std;
int main(int argc, char ** argv)
{
int mynode, totalnodes;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &totalnodes);
MPI_Comm_rank(MPI_COMM_WORLD, &mynode);
cout << "Hello world from process " << mynode;
cout << " of " << totalnodes << endl;
MPI_Finalize();
}
输出是
第0步(共1步)中的Hello world
Hello world from process 0 of 1
我有多核CPU,我认为至少应运行4个进程.因此输出应为:
I have multi-core CPU and I think there should be at least 4 processes running. So Output should be:
第0步(共4步)中的Hello world您好,来自第1步,共4步的世界第2步(共4步)的Hello worldHello world from 4 of 3
Hello world from process 0 of 4 Hello world from process 1 of 4 Hello world from process 2 of 4 Hello world from process 3 of 4
或类似的东西.谁能评论我在程序或编译命令中错过了什么?顺便说一句,我在Windows,MSMPI,Code :: Blocks IDE上的gcc编译器上运行.谢谢.
or something similar. Can anyone comment what did I miss in the program or compile command? By the way I am running on Windows, MSMPI, gcc compiler on Code::Blocks IDE. Thanks.
推荐答案
它对我有用.
我将粘贴的代码复制到mpi_app.cpp中.mpicxx由MPI实现者提供的编译器包装脚本,负责处理include和lib.mpirun是用于启动mpi程序的包装脚本,该脚本也由MPI实现者提供.我正在使用的MPI实现是mpich2.
I copy pasted your code into mpi_app.cpp. mpicxx a compiler wrapper script provided by MPI implementers that takes care of includes and libs. mpirun is a wrapper script for launching mpi programs that is also provided by MPI implementers. The MPI implementation I'm using is mpich2.
$ mpicxx -O0 -o mpi_app mpi_app.cpp
$ mpirun -n 4 ./mpi_app
Hello world from procHello world from process 2 Hello world from process Hello world from process 0 oess 1 of 4
of 4
3 of 4
f 4
注意:"f 4"不是复制粘贴错误.当多个进程写入标准输出时,您应该期望消息乱码或散布.
Note: "f 4" is not a copy paste error. When multiple processes are writing to stdout you should expect garbled or interspersed messages.
听起来好像您正在编译它,但是,如果没有,似乎必须在Windows上手动添加include和lib:
It sounds like you are getting it to compile, but if not, it looks like on Windows you have to manually add the include and lib: http://blogs.msdn.com/b/risman/archive/2009/01/04/ms-mpi-with-visual-studio-2008.aspx
在同一链接中,Windows命令行上的命令看起来像是:
From that same link, it looks like the command on a Windows command line is:
mpiexec –n 10 MyMPIProject.exe
要使其在code :: blocks中运行,您需要像上面一样告诉code :: blocks运行命令.在下面链接的博客文章中,代码看起来像:: blocks使用"cb_console_runner.exe"来运行编译的程序.这篇博客文章对该程序进行了修改,可以接受-mpi标志,告诉它mpiexec的位置.
To get this to run within code::blocks you need to tell code::blocks to run the command just like above. From the blog post linked below, it looks like code::blocks uses a 'cb_console_runner.exe' to run compiled programs. This blog post has a modified version of that program that will accept a -mpi flag telling it where mpiexec is.
http://www.blog.kubiak.co.uk/post/44
设置-mpi标志:
参数必须在项目"->设置程序的参数"的代码块"菜单中定义吗?
"Argument this must be define in Codeblocks menu in Project -> Set programs’ arguments?
-mpi <path to mpiexec> -n <number of processes>
示例:
-mpi C:/Progra~1/MPICH2/bin/mpiexec -n 8
"
这篇关于使用Code :: Blocks运行具有多个进程的MPI程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!