##### Sections
You are here: Home Cluster Job Example: C/C++/FORTRAN Code

# 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 Copyright (c) 1998-2001 University of Notre Dame.
c Copyright (c) 1994-1998 The Ohio State University.
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/bash
# 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

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 Copyright (c) 1998-2001 University of Notre Dame.
c Copyright (c) 1994-1998 The Ohio State University.
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
{"ft_lang_label":"__label__en","ft_lang_prob":0.74369687,"math_prob":0.73354936,"size":5487,"snap":"2019-35-2019-39","text_gpt3_token_len":1642,"char_repetition_ratio":0.08717855,"word_repetition_ratio":0.43116328,"special_character_ratio":0.3176599,"punctuation_ratio":0.14726028,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9773576,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-21T15:42:41Z\",\"WARC-Record-ID\":\"<urn:uuid:641f39b8-f57a-491e-a2e9-10007d9f867e>\",\"Content-Length\":\"20398\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1ee9b180-bec8-48c2-9ca7-e958ee89dc95>\",\"WARC-Concurrent-To\":\"<urn:uuid:41058b1d-fac0-40ff-a2b2-2ae7b03d491e>\",\"WARC-IP-Address\":\"149.149.254.5\",\"WARC-Target-URI\":\"https://www.cae.tntech.edu/help/parallel/cluster-job-example-c-c-fortran-code\",\"WARC-Payload-Digest\":\"sha1:L37S5HOGDT6JNRQO7SPP7HQG2VBSCGJN\",\"WARC-Block-Digest\":\"sha1:R33QUTJQIWQJ3J4JRAU2VAUISHDL5IVN\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514574532.44_warc_CC-MAIN-20190921145904-20190921171904-00051.warc.gz\"}"}