MPC++ MTTL Getting Started


This document describes, The following example assumes a user environment setting described in Getting Started in the SCore User's Guide.


Porting the "Hello World" program to MPC++

Here is how the simplest but most famous C program, "Hello, World" is converted to an MPC++ program.

#include <stdio.h>
main(int argc, char **argv) {
    printf("hello, world\n");
    exit(0);
}

Firstly, you have to add the include file, mpcxx.h. Then the mpcxx_initialize() function should be called at the beginning of the main() function. Essentially this is all you have to do.

#include <stdio.h>
#include <mpcxx.h>
main(int argc, char **argv) {
    mpcxx_initialize(argc, argv);
    printf("hello, world\n");
    exit(0);
}

Compilation

The compilation of an MPC++ program is as easy as a normal C/C++ compilation. Let us assume the MPC++ program converted above is saved as hello.cc. Because the MPC++ compiler only accepts source files suffixed with .cc, filename hello.c cannot be compiled.

$ mpc++ hello.cc
compiling  hello.cc
$

Just as a normal C/C++ compiler, the MPC++ compiler produces an executable file, named a.out. The MPC++ compiler accepts most of the compiler options that the C/C++ compiler accepts.

Running a program on a cluster

At first, you may invoke msgb to find some free hosts in the cluster, as follows:

$ msgb -group pcc &

Then invoke the scout program with the group name which specifies cluster hosts on which to run your program. The group name is defined in the scorehosts.db(5) database.

$ scout -g pcc
SCOUT: Spawn done.
SCOUT: session started
$
A new shell process is now created as a child of the scout program. If msgb is already running, some or all hosts is msgb window turn red, while scout invoking remote processes. Note that the host which the scout program is invoked doesn't have to be one of the hosts of your cluster.

Eventually you can invoke the program:

$ scrun ./a.out
SCORE: connected (jid=100)
<0:0> SCORE: Single host, single process ready.
hello, world
$
You should get the same result as a normal C/C++ program output.

Again, if msgb is already running, some or all hosts is msgb window turn red, while executing the program on your cluster. The msgb window let you know who is executing a program on your cluster, and hosts are locked not to execute multiple programs on the hosts simultaneously (use SCore-D multi-user mode to run multiple programs on same hosts).

In above example, the program is executed on one host (node). To execute the program on multple nodes, please specify -nodes=N as an option of scrun, where N is the number of nodes you want to run the program.

$ scrun -nodes=2 ./a.out
SCORE: connected (jid=100)
<0:0> SCORE: 2 hosts, single process/host ready.
hello, world
$
The result is exactly the same as the run on one host. What is wrong ? In this case, the mpcxx_initialize() function of MPC++ runtime library only returns on the host which is assigned as node number 0, and other nodes are idle. To get parallel hello program execution, you have to change the MPC++ initialization function from to mpcxx_spmd_initialize().
#include <stdio.h>
#include <mpcxx.h>
main(int argc, char **argv) {
    mpcxx_spmd_initialize(argc, argv);
    printf("hello, world\n");
    exit( 0 );
}

Do not forget to compile the modified file. Let's try again:

$ scrun -nodes=2 ./a.out
SCORE: connected (jid=100)
<0:0> SCORE: 2 hosts, single process/host ready.
hello, world
hello, world
$

Now you get two answers, because the mpcxx_spmd_initialize() function retunrs on each host. If you specify four nodes, you will get four answers. To make sure, the hello program is now slightly modified:

#include <stdio.h>
#include <mpcxx.h>
main(int argc, char **argv) {
    mpcxx_spmd_initialize(argc, argv);
    printf("hello, world (from node %d)\n", myNode);
    exit( 0 );
}

The variable myNode is defined in the MPC++ runtime library to contain the node number. The integer variable numNode indicates the number of nodes allocated for the parallel execution. Note that the value of numNode can be different from the number of nodes specified on the command line. Therefore any MPC++ program should not assume that numNode is assigned to some specific value. If your program can only run with some specific number of nodes, check the value of numNode at the very beginning, but after calling mpcxx_(spmd_)initialize, of your program.

Now let us try to run the modified program with four nodes.

$ scrun -nodes=4 ./a.out
SCORE: connected (jid=100)
<0:0> SCORE: 4 hosts, single process/host ready.
hello, world (from node 0)
hello, world (from node 2)
hello, world (from node 1)
hello, world (from node 3)
$

Now you get four answers, exact one answer from each node. Note the sequence of the answers. Since the body of main() function is executed on each node in parallel, there is no way to predict which one is the first, and so on. This is the nature of parallel computation.

Finishing up the session

To finish this scout session, please exit the shell.

$ exit
SCOUT: session done
$
You will find the following examples under the /opt/score/example/mttl-ult directory:

ainvoke.ccMeasure the round trip time of remote thread invocation
burst.ccMeasure network bandwidth between nodes

See also

SCore Cluster Software System Reference Guide


PCCC logo PC Cluster Consotium

CREDIT
This document is a part of the SCore cluster system software developed at PC Cluster Consortium, Japan. Copyright (C) 2003 PC Cluster Consortium.