MPJ Guide

From Storrs HPC Wiki
Jump to: navigation, search

MPJ (aka Java MPI)

MPJ v 0.38 is installed. You need to copy it to your home directory so that the logfiles are writable by you. In order to use MPJ, the first step is.

$ cp -rp /apps/mpj/0.38 /home/$USER/.mpj/

Now you want to load the MPJ module every time you log in with:

$ module initadd mpj/0.38

After doing this, you can either log out and log back in again or simply load the module manually (first time only)

$ module load mpj/0.38

A simple example is Foo.java:

import java.net.*;
import java.io.*;

public class Foo {
  public static String getHostName() {
    String hostname;
    try {
      InetAddress addr = InetAddress.getLocalHost();
      byte[] ipAddr = addr.getAddress();
      hostname = addr.getHostName();
    } catch (UnknownHostException e) {
      hostname="undef";
    }
    return(hostname);
  }

  public static void main(String args[]) throws Exception {
     System.out.println("hi the hostname is "+getHostName());
  }
}

Compile your program with:

$ javac -cp .:$MPJ_HOME/lib/mpj.jar Foo.java

Next, you create a submission script. The following is an example file! You should name it slurm.mpj. This code assumes your working directory is /home/$USER/mpj/ if it does not exist you should create it with: mkdir ~/mpj You will also need to update your email address (line 12), and update the java class name (line 29) which gets run (our example uses Foo).

#!/bin/bash
#the name of your job on the SLURM system
#SBATCH -J MPJ_test
# the partition that you will use, the example here use the partiton called "Westmere"
# please use sinfo command to check the available queues
#SBATCH -p Westmere
# the system output and error message output, %J will show as your jobID
#SBATCH -o %J.out
#SBATCH -e %J.err
# the number of processors that you will use (in this example a 36)
#SBATCH -n 36
# when job finish get email notification
#SBATCH --mail-user=yourEmail@domain.com
#SBATCH --mail-type=END 
############ enter your working directory, change to the location of where the executable (compiled program) is ###
work_dir="/home/$USER/mpj"
cd $work_dir
############ create machines file, DO NOT modify this section unless you really know ####
srun hostname|sort -u >machines
echo $SLURM_JOB_NODELIST
echo $SLURM_NTASKS
mpjboot machines
############  Replace Foo with the name of your class that has main() method ###
mpjrun.sh -np $SLURM_NTASKS -dev niodev Foo
############ exit the mpd ring and clean off the nodes ###################
mpjhalt machines
`rm mpd.procs`
`rm machines`

Then, to submit your job just type:

$ sbatch slurm.mpj

Here is a HelloWorld program that utilizes some functions of MPI:

import mpi.*;
public class HelloWorld {
     public static void main(String args[]) throws Exception {
          MPI.Init(args);
          int me = MPI.COMM_WORLD.Rank();
          int size = MPI.COMM_WORLD.Size();
          System.out.println("Hi from <"+me+">");
          MPI.Finalize();
     }
}