0% found this document useful (0 votes)
145 views

MPI Using Java PDF

The document discusses MPI (Message Passing Interface) and MPJ (MPI for Java). MPI is an API for communication between nodes in a distributed memory parallel system. MPJ specifies how MPI can be implemented in Java. It describes how to set up the MPJ environment, key classes like Comm that represent communicators, and basic send/receive communication as well as collective communication functions. Examples are provided of how to use MPJ to sum arrays of numbers across processors using scatter/gather operations.

Uploaded by

Swapnil Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

MPI Using Java PDF

The document discusses MPI (Message Passing Interface) and MPJ (MPI for Java). MPI is an API for communication between nodes in a distributed memory parallel system. MPJ specifies how MPI can be implemented in Java. It describes how to set up the MPJ environment, key classes like Comm that represent communicators, and basic send/receive communication as well as collective communication functions. Examples are provided of how to use MPJ to sum arrays of numbers across processors using scatter/gather operations.

Uploaded by

Swapnil Shinde
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

MPI using Java

Assignment no. 02
Computer Lab IX
MPI: The Message Passing Interface

● MPI is the HPC Message Passing Interface standardized in the early 1990s
by the MPI Forum.
● It is an API for communication between nodes of a distributed memory
parallel computer (typically, now, a workstation cluster).
● The original standard defines bindings to C and Fortran (later C++).
● MPI is a technology from the HPC world, which various people have worked
on importing into Java.
MPI: The Message Passing Interface

an API for sending and receiving messages.

a general platform for Single Program Multiple Data (SPMD) parallel


computing on distributed memory architectures.

MPJ

○ An API specification by the “Message-passing Working Group” of the Java Grande Forum.
Published 2000.
MPJ - Setup Environment

program is implemented by a public static main() method of some class


defined by the programmer

All the classes belong to the package mpi

The MPI class has a few static methods for administrative things, and
numerous static fields holding global constants
MPJ - Setup Environment

You should forward the arguments parameter of the main() method to the
Init() method

Call MPI.Finalize() to shut down MPI before the main() method Terminates.

Failing to do this may cause your executable to not terminate properly.


The Comm class

The Comm class represents an MPI communicator, which makes it probably


the most important class in the API.

All communication operations ultimately go through instances of the Comm


class.

In MPI a communicator defines two things:

1. it defines a group of processes—the participants in some kind of parallel


task or subtask, and
2. it defines a communication context.
Communication Context

The idea of a communication context is that the same group of processes


might be involved in more than one kind of “ongoing activity”

You don’t want these distinct “activities” to interfere with one another.

So you give each activity a different communication context. You do this by


giving them different instances of the Comm class.

Messages sent on one communicator can never be received on another.


Rank & Size

A process group in MPI is defined as a fixed set of processes, which never


changes in the lifetime of the group.

The number of processes in the group associated with a communicator can


be found by the Size() method of the Comm class. It returns an int.

Each process in a group has a unique rank within the group, an int value
between 0 and Size() – 1. This value is returned by the Rank() method.
The World Communicator

In MPI, the “initial” communicator is a communicator spanning all the


processes in which the SPMD program was started.

In mpi-Java, this communicator is accessed as a static field of the MPI


class: MPI.COMM_WORLD
Simple send and receive

the basic point-to-point communication methods are members of the Comm


class.

Send and receive members of Comm:

void Send(Object buf, int offset, int count, Datatype type, int dst, int tag) ;

Status Recv(Object buf, int offset, int count, Datatype type, int src, int tag) ;

The arguments buf, offset, count, type describe the data buffer—the storage
of the data that is sent or received.
Simple send and receive

dst is the rank of the destination process relative to this communicator.

Similarly in Recv(), src is the rank of the source process.

An arbitrarily chosen tag value can be used in Recv() to select between


several incoming messages: the call will wait until a message sent with a
matching tag value arrives.

The Recv() method returns a Status value,


Communication Buffers

Most of the communication operations take a sequence of parameters like


Object buf, int offset, int count, Datatype type

In the actual arguments passed to these methods, buf must be an array (or a
run-time exception will occur).

Declaring Object buf allows any kind of array in one signature.

offset is the element in the buf array where message starts.

count is the number of items to send. type describes the type of these items.
ANY_SOURCE and ANY_TAG

A recv() operation can explicitly specify which process within the communicator group
it wants to accept a message from, through the src parameter.

It can also explicitly specify what message tag the message should have been sent
with, through the tag parameter.

The recv() operation will block until a message meeting both these criteria arrives.

If you want the recv() operation to accept a message from any source, or with any tag,
you may specify the values MPI.ANY_SOURCE or MPI.ANY_TAG for the respective
arguments.
Status values

The recv() method returns an instance of the Status class.

This object provides access to several useful pieces about the message that
arrived.

Below we assume the Status object is saved to a variable called status:

int field status.source holds the rank of the process that sent the message
(particularly useful if the message was received with MPI.ANY_SOURCE).

int field status.tag holds the message tag specified by the sender of the
message (particularly useful if the message was received with MPI.ANY_TAG).
Status values

Below we assume the Status object is saved to a variable called status:

int method status.Get_count(type) returns number of items received in the


message.

int method status.Get_elements(type) returns number of basic elements


received in the message.
MPIException

Nearly all methods of mpiJava are declared to throw the MPIException.

This is supposed to report the error status of a failed method, closely following
the failure modes documented in the MPI standard.
Collective Communications

A popular feature of MPI is its family of collective communication operations. Of course


these all have mpiJava bindings.

All processes in the communicator group must engage in a collective operation “at the
same time”

– i.e. all processes must invoke collective operations in the same sequence, all passing
consistent arguments.

Can argue that collective communications (and other similar collective functions built on
them) are a defining feature of the SPMD programming model.
Example Collective Operations

void Gather(Object sendbuf, int sendoffset, int sendcount, Datatype sendtype,

Object recvbuf, int recvoffset, int recvcount, Datatype recvtype, int root)

» Each process sends the contents of its send buffer to the root process.

void Scatter(Object sendbuf, int sendoffset, int sendcount, Datatype sendtype, Object
recvbuf, int recvoffset, int recvcount, Datatype recvtype, int root)

» Inverse of the operation Gather.


Supporting MPI.OBJECT

mpiJava has supported MPI.OBJECT as one of its basic message types.

This is very natural in Java, and it allows various things to be done conveniently.

Besides sending arrays of true objects, it allows you to send Java


multidimensional arrays directly.

Unfortunately this comes with substantial performance overheads because


Java object serialization is very slow, by the standards of HPC.
MPJ Implementation Examples

● Write the program to sum 100 numbers using Send and Recv

● Write the program to square p numbers using Scatter and Gather.

● Initially data is [1,2,9,64,625]. Scatter this data to number of processors and

gather the output as [2,3,10,65,626]


MPJ Implementation Examples

● Consider the addition of 100 numbers on a distributed memory 4-processor


computer.A parallel algorithm to sum 100 numbers proceeds in four stages:
○ 1. distribute 100 numbers evenly among the 4 processors;
○ 2. Every processor sums 25 numbers;
○ 3. Collect the 4 sums to the manager node; and
○ 4. Add the 4 sums and print the result.
○ Scattering an array of 100 number over 4 processors and gathering the partial sums at the
4 processors to the root
Thanks!
Sample MPI based assignments
are available at :

www.thelearningsolutions.com

You might also like