user3705273
user3705273

Reputation: 335

Running MPI programs with multiple processes with Code::Blocks

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();
}

The output is just

Hello world from process 0 of 1

I have multi-core CPU and I think there should be at least 4 processes running. So Output should be:

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

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.

Upvotes: 1

Views: 5118

Answers (1)

Praxeolitic
Praxeolitic

Reputation: 24039

It worked for me.

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

Note: "f 4" is not a copy paste error. When multiple processes are writing to stdout you should expect garbled or interspersed messages.

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

From that same link, it looks like the command on a Windows command line is:

mpiexec –n 10 MyMPIProject.exe

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

Setting the -mpi flag:

"Argument this must be define in Codeblocks menu in Project -> Set programs’ arguments?

-mpi <path to mpiexec> -n <number of processes>

example:

-mpi C:/Progra~1/MPICH2/bin/mpiexec -n 8 

"

Upvotes: 2

Related Questions