Cluster Job Example: C/C++/FORTRAN Code
C/C++/FORTRAN (Traditional non-parallel code)
Programs compiled with the GNU compiler tools (gcc, g++, g77) will only run on a single CPU by default. Programs compiled with the Portland Group's compilers (pgcc, pgCC, pgf77, pgf90, pgf95, and pghpf) can run on multiple processors if the -mautopar flag was used during the compilation and the -np flag is used when running the executable, but only if those processors are all on a single multi-CPU system (see the Unix compiler documentation). C, C++, and FORTRAN programs would have to be modified to use parallel libraries such as MPI or OpenMP before they can spread across multiple systems. Therefore, use either "#PBS -l nodes=1:ppn=1" for a single-CPU job, "#PBS -l nodes=1:ppn=2" for a dual-CPU job, or "#PBS -l nodes=1:ppn=4" for a 4-CPU job.
Sample program file (fpi-serial.f)
c Copyright (c) 2001-2003 The Trustees of Indiana University.
c All rights reserved.
c Copyright (c) 1998-2001 University of Notre Dame.
c All rights reserved.
c Copyright (c) 1994-1998 The Ohio State University.
c All rights reserved.
c
c This file is part of the LAM/MPI software package. For license
c information, see the LICENSE file in the top level directory of the
c LAM/MPI source distribution.
c
c $Id: fpi.f,v 1.4 2003/03/25 20:13:27 jsquyres Exp $
c
c Portions taken from the MPICH distribution example fpi.f.
c
c Example program to calculate the value of pi by integrating f(x) =
c 4 / (1 + x^2).
c
c Compile me with 'g77 -o fpi-serial fpi-serial.f'
program main
double precision PI25DT
parameter (PI25DT = 3.141592653589793238462643d0)
double precision mypi, pi, h, sum, x, f, a
integer*4 num_iters
integer rank, size, i, rc
character junk
c Loop until finished
num_iters = 200000
c Calculate the interval size
h = 1.0d0 / num_iters
sum = 0.0d0
do 10 i = 1, num_iters
x = h * (dble(i) - 0.5d0)
sum = sum + 4.d0 / (1.d0 + x * x)
10 continue
mypi = h * sum
c All finished
write(6, 97) num_iters, mypi, abs(mypi - PI25DT)
97 format(i20, ' points: pi is approximately: ', F18.16,
+ ' error is: ', F18.16)
stop
end
Sample qsub command file (fpi-serial.sh)
#!/bin/sh # Calculate pi by integrating f(x)=4/(1+x^2) # Request 1 node with 1 free CPU core, since this code uses no parallel features # by default. If Portland compilers are used with the -mautopar flag, change the # ppn value to something between 2 and 8, and adjust the last line of this job file # accordingly. #PBS -l nodes=1:ppn=1 # Reserve 24 hours on selected cores #PBS -l walltime=24:00:00 # Give the job a descriptive name for emails (name must start with a letter) #PBS -N Finding_pi_serial # Send mail to address given below when the job begins, ends normally, or aborts #PBS -m bea #PBS -M myusername@tntech.edu cd $PBS_O_WORKDIR # Single-CPU version ./fpi-serial # Example 4-CPU version. Only works if code was compiled with Portland compilers # and if you set the ppn value above to 4 -- commented out by default. # ./fpi-serial -np 4
C/C++/FORTRAN (MPI-compatible parallel code)
If you've gone to the effort of converting your code into a parallel version using the MPI libraries, you can take advantage of multiple processors on several systems. Therefore, you won't be limited to a single node's capacity.
Sample program file (fpi.f)
c Copyright (c) 2001-2003 The Trustees of Indiana University.
c All rights reserved.
c Copyright (c) 1998-2001 University of Notre Dame.
c All rights reserved.
c Copyright (c) 1994-1998 The Ohio State University.
c All rights reserved.
c
c This file is part of the LAM/MPI software package. For license
c information, see the LICENSE file in the top level directory of the
c LAM/MPI source distribution.
c
c $Id: fpi.f,v 1.4 2003/03/25 20:13:27 jsquyres Exp $
c
c Portions taken from the MPICH distribution example fpi.f.
c
c Example program to calculate the value of pi by integrating f(x) =
c 4 / (1 + x^2).
c
c Compile me with 'mpif77 -o fpi fpi.f'
program main
include 'mpif.h'
double precision PI25DT
parameter (PI25DT = 3.141592653589793238462643d0)
double precision mypi, pi, h, sum, x, f, a
integer num_iters, rank, size, i, rc
c Function to integrate
f(a) = 4.d0 / (1.d0 + a * a)
c Normal MPI startup
call MPI_INIT(ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr)
print *, "Process ", rank, " of ", size, " is alive"
c Loop until finished
num_iters = 100000
do 20 iter = 2, num_iters
c Calculate the interval size
h = 1.0d0 / iter
sum = 0.0d0
do 10 i = rank + 1, iter, size
x = h * (dble(i) - 0.5d0)
sum = sum + f(x)
10 continue
mypi = h * sum
c Collect all the partial sums
call MPI_REDUCE(mypi, pi, 1, MPI_DOUBLE_PRECISION, MPI_SUM, 0,
$ MPI_COMM_WORLD, ierr)
20 continue
c All finished
c Node 0 prints the answer.
if (rank .eq. 0) then
write(6, 97) iter, pi, abs(pi - PI25DT)
97 format(i3, ' points: pi is approximately: ', F18.16,
+ ' error is: ', F18.16)
endif
call MPI_FINALIZE(rc)
stop
end
Sample qsub command file (fpi.sh)
#!/bin/sh # Calculate pi by integrating f(x)=4/(1+x^2) # Request up to 4 nodes with at least 1 free CPU core, since this code uses parallel features. #PBS -l nodes=4:ppn=1 # Reserve 24 hours on selected cores #PBS -l walltime=24:00:00 # Give the job a descriptive name for emails (name must start with a letter) #PBS -N Finding_pi_parallel # Send mail to address given below when the job begins, ends normally, or aborts #PBS -m bea #PBS -M myusername@tntech.edu cd $PBS_O_WORKDIR mpiexec -np 4 ./fpi
