Supercomputing Networking Research Education Ohio Supercomputer Center Site Map Staff Directory Support
Welder image

bcMPI - Tutorial

introduction

This tutorial demonstrates the process of creating and running a bcMPI application.

application

The value of pi is to be calculated by finding the area of a circle (or, 4 times the area of a quarter circle). Each node will approximate a portion of the area by summing up the area of many small rectangles. The final answer will be found by collectively summing the individual portions.

implementation

Contents of file matpi.m:

% matpi.m - calculate the digits of pi
%
% Adapted from the cpi.c example shipped with MPICH.
%

% Common initialization.
MPI_Init();
my_size = MPI_Comm_size(MPI_COMM_WORLD);
my_rank = MPI_Comm_rank(MPI_COMM_WORLD);

%
% First node obtains the run parameters.
% Here, values are simply hardcoded.
%
if (my_rank == 0)

   % Radius of the circle
   RADIUS = 1.23;

   % Number of rectangles per node
   N_INTERVALS = 42000;

end

%
% Run parameters are distributed from root to all.
%
if (my_rank == 0)
   [ r, n ] = MPI_Broadcast(0, MPI_COMM_WORLD, RADIUS, N_INTERVALS);
else
   [ r, n ] = MPI_Broadcast(0, MPI_COMM_WORLD);
end

%
% Calculate the pieces in parallel.
%
w = r / my_size;
h = w / n;
x0 = my_rank * w;
x1 = x0 + w - h;
x = linspace(x0, x1, n);
y = sqrt(r.^2 - x.^2);
area = 4 * h * sum(y);

%
% Sum for complete area.
%
global MPI_SUM;
total = MPI_Reduce(area, MPI_SUM, 0, MPI_COMM_WORLD);

%
% First node displays the results.
%
if (my_rank == 0)
   calc_pi = total / r^2;
   printf('Calculated value for pi:\n\t %.16f\n', calc_pi);
   printf('Correct value for pi:\n\t %.16f\n', pi);
   printf('Error (calculated - correct):\n\t%+.16f\n', calc_pi - pi);
end

% Common finalization.
MPI_Finalize();

%--%

      

batch system job script

To test the above program (matpi.m), user has to create a batch system job script. The generator tool is $INSTALL_DIR/bin/create_job_file.py. To generate a job file, this tool reads information from installation, queue and job specific configuration files and command line options.

Following configuration files set values for a pbs job script matpi.pbs, in an smp machine named "kodos" where processors per node is two, using "octave" as interpreter and "openmpi" as mpi library. Batch system queue name is "matlab" and the total number of processes requested for this job is four.

configuration files:

Default installation configuration file - $INSTALL_DIR/etc/ParaM.pjc. Make sure the following values are set in this file:

batch_shell   = sh
batch_system  = PBS
interpreter   = octave
mpi_library   = openmpi
       
      

Queue configuration file - queue.pjc. Copy the example file queue.pjc from $INSTALL_DIR/share/example. Set the following values for the variables.

pernode         = False
procs_pernode   = 2
processes       = 4
queue           = matlab
machine_name    = kodos
        
      

Job configuration file - job.pjc. Create job.pjc file. This file follows the same format as queue configuration file and contains values for a specific job.

#
# job.pjc
#
[DEFAULT]
job_name        = matpi
walltime        = 00:01:00
batch_script    = matpi.pbs
programs        = matpi.m
       
      

generating batch system job script:

Be sure to change INSTALL_DIR to match your installation directory.

% $INSTALL_DIR/bin/create_job_file.py -c queue.pjc -c job.pjc
         
      

Contents of generated file matpi.pbs:

#PBS -N z-matpi
#PBS -l nodes=2:ppn=2
#PBS -l walltime=00:01:00
#PBS -S /bin/sh
#PBS -j oe

INSTALL_DIR=${HOME}/ParaM-kodos-openmpi-p4-octmpi
. ${INSTALL_DIR}/bin/octave-environ.sh

MPIEXEC_FLAGS="-np 4"
mpiexec="${MPIEXEC_BIN} ${MPIEXEC_FLAGS}"
mpi_setup="${MPI_SETUP_BIN}"

cd /dir/of/prorgam/file

${mpi_setup} matpi octave OCTAVE_COMMAND=matpi
${mpiexec} ./matpi

#--#

      

run the application

Create the program file matpi.m by copying or typing the file contents listed under implementation. Be sure to change INSTALL_DIR to match your installation directory.

To submit the tests using PBS:

% qsub matpi.pbs
      

To submit the tests using LSF:

% bsub matpi.lsf
      

To run the tests without a scheduler:

% sh matpi.pbs
      

output

The interpreters will usually display a bunch of startup messages, somewhere near the end the application output should appear:

Calculated value for pi:
        3.1416045412737090
Correct value for pi:
        3.1415926535897931
Error (calculated - correct):
        +0.0000118876839159