USC Center for High Performance Computing and Communications

Simple MPI Execution

Systems & Access :: Learning to Use :: Sun Computing Resource

Example of an MPI Program mpitest.c

/* program hello */
/* Adapted from mpihello.f by drs */

#include <mpi.h>
#include <stdio.h>

int main(int argc, char **argv)
{
  int *buf, i, rank, nints, len;
  char hostname[256];
  
  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  gethostname(hostname,255);
  printf("Hello world!  I am process number: %d on host %s\n", rank, hostname);
  MPI_Finalize();
  return 0;
}

Example of an mpitest.pbs

#!/bin/csh

#*** The "#PBS" lines must come before any non-blank non-comment lines ***
#PBS -k eo
#PBS -l walltime=2:00,nodes=1:ppn=1

# The following should contain your program and any arguments
set mycommand = "mpitest"

if ($?PBS_JOBID) then           #if this is a PBS job
source /usr/usc/mpich/default/setup.csh
  echo "Starting" $PBS_JOBID `date` 
  echo "Initiated on `hostname`"
  echo ""
  cd "$PBS_O_WORKDIR"           #connect to working directory of qsub
else 
  echo "This script must be run as a PBS job"
  exit 1
endif

if ($?PBS_NODEFILE) then
  #count the number of processors assigned by PBS
  set NP = `wc -l < $PBS_NODEFILE`
  echo Running on $NP processors: `cat $PBS_NODEFILE`

else
  echo "This job did not have the number of nodes specified with" 
  echo "the node= resource" 
  exit 1
endif

# run the program
  mpirun -np $NP -machinefile $PBS_NODEFILE $mycommand

echo "Done   " `date`
exit

Sample Run

To run a job, the first step is to compile the program:

almaak-06.usc.edu: mpicc -o mpitest mpitest.c

The second step is to submit the job to the PBS queue. The PBS will then report the assigned job number.

almaak-06.usc.edu: qsub mpitest.pbs
27748.almaak-06.usc.edu

The output files for the above run are created in the home directory and they can be viewed by listing the following files:

almaak-06.usc.edu: cd
almaak-06.usc.edu: ls -l mpitest.pbs.*
-rw-------   1 nirmal          0 Jul 14 19:03 mpitest.pbs.e27748
-rw-------   1 nirmal        797 Jul 14 19:03 mpitest.pbs.o27748
almaak-06.usc.edu: more mpitest.pbs.*
::::::::::::::
mpitest.pbs.e27748
::::::::::::::
::::::::::::::
mpitest.pbs.o27748
::::::::::::::
SunOS rcf-01.usc.edu 5.8 Generic_108528-25 sun4u sparc SUNW,Sun-Fire-15000
System type is Sun SUN4U with 262144 MB of memory.

Starting 27748.almaak-06.usc.edu Wed Jul 14 19:01:54 PDT 2004
Initiated on rcf101.usc.edu

Running on 1 processors: rcf101
Hello world!  I am process number: 0 on host rcf101.usc.edu
Done    Wed Jul 14 19:01:55 PDT 2004

  ITS Policies       Contact HPCC