+++ title = "Fortran/C on HCC" description = "How to compile and run Fortran/C program on HCC machines" weight = "50" +++ This quick start demonstrates how to implement a Fortran/C program on HCC supercomputers. The sample codes and submit scripts can be downloaded from [serial_dir.zip](/attachments/serial_dir.zip). #### Login to a HCC Cluster Connect to a HCC cluster]({{< relref "../../connecting/" >}}) and make a subdirectory called `serial_dir` under your `$WORK` directory. {{< highlight bash >}} $ cd $WORK $ mkdir serial_dir {{< /highlight >}} In the subdirectory `serial_dir`, save all the relevant Fortran/C codes. Here we include two demo programs, `demo_f_serial.f90` and `demo_c_serial.c`, that compute the sum from 1 to 20. {{%expand "demo_f_serial.f90" %}} {{< highlight bash >}} Program demo_f_serial implicit none integer, parameter :: N = 20 real*8 w integer i common/sol/ x real*8 x real*8, dimension(N) :: y do i = 1,N w = i*1d0 call proc(w) y(i) = x write(6,*) 'i,x = ', i, y(i) enddo write(6,*) 'sum(y) =',sum(y) Stop End Program Subroutine proc(w) real*8, intent(in) :: w common/sol/ x real*8 x x = w Return End Subroutine {{< /highlight >}} {{% /expand %}} {{%expand "demo_c_serial.c" %}} {{< highlight c >}} //demo_c_serial #include <stdio.h> double proc(double w){ double x; x = w; return x; } int main(int argc, char* argv[]){ int N=20; double w; int i; double x; double y[N]; double sum; for (i = 1; i <= N; i++){ w = i*1e0; x = proc(w); y[i-1] = x; printf("i,x= %d %lf\n", i, y[i-1]) ; } sum = 0e0; for (i = 1; i<= N; i++){ sum = sum + y[i-1]; } printf("sum(y)= %lf\n", sum); return 0; } {{< /highlight >}} {{% /expand %}} --- #### Compiling the Code The compiling of a Fortran/C++ code to executable is usually done behind the scene in a Graphical User Interface (GUI) environment, such as Microsoft Visual Studio. In a HCC cluster, the compiling is done explicitly by first loading a choice compiler and then executing the corresponding compiling command. Here we will use the GNU Complier Collection, `gcc`, for demonstration. Other available compilers such as `intel` or `pgi` can be looked up using the command line `module avail`. Before compiling the code, make sure there is no dependency on any numerical library in the code. If invoking a numerical library is necessary, contact a HCC specialist ({{< icon name="envelope" >}}[hcc-support@unl.edu](mailto:hcc-support@unl.edu)) to discuss implementation options. {{< highlight bash >}} $ module load compiler/gcc/8.2 $ gfortran demo_f_serial.f90 -o demo_f_serial.x $ gcc demo_c_serial.c -o demo_c_serial.x {{< /highlight >}} The above commends load the `gcc` complier and use the compiling commands `gfortran` or `gcc` to compile the codes to`.x` files (executables). #### Creating a Submit Script Create a submit script to request one core (default) and 1-min run time on the supercomputer. The name of the main program enters at the last line. {{% panel header="`submit_f.serial`"%}} {{< highlight bash >}} #!/bin/sh #SBATCH --mem-per-cpu=1024 #SBATCH --time=00:01:00 #SBATCH --job-name=Fortran #SBATCH --error=Fortran.%J.err #SBATCH --output=Fortran.%J.out module load compiler/gcc/4.9 ./demo_f_serial.x {{< /highlight >}} {{% /panel %}} {{% panel header="`submit_c.serial`"%}} {{< highlight bash >}} #!/bin/sh #SBATCH --mem-per-cpu=1024 #SBATCH --time=00:01:00 #SBATCH --job-name=C #SBATCH --error=C.%J.err #SBATCH --output=C.%J.out module load compiler/gcc/4.9 ./demo_c_serial.x {{< /highlight >}} {{% /panel %}} #### Submit the Job The job can be submitted through the command `sbatch`. The job status can be monitored by entering `squeue` with the `-u` option. {{< highlight bash >}} $ sbatch submit_f.serial $ sbatch submit_c.serial $ squeue -u <username> {{< /highlight >}} Replace `<username>` with your HCC username. #### Sample Output The sum from 1 to 20 is computed and printed to the `.out` file (see below). {{%expand "Fortran.out" %}} {{< highlight batchfile>}} i,x = 1 1.0000000000000000 i,x = 2 2.0000000000000000 i,x = 3 3.0000000000000000 i,x = 4 4.0000000000000000 i,x = 5 5.0000000000000000 i,x = 6 6.0000000000000000 i,x = 7 7.0000000000000000 i,x = 8 8.0000000000000000 i,x = 9 9.0000000000000000 i,x = 10 10.000000000000000 i,x = 11 11.000000000000000 i,x = 12 12.000000000000000 i,x = 13 13.000000000000000 i,x = 14 14.000000000000000 i,x = 15 15.000000000000000 i,x = 16 16.000000000000000 i,x = 17 17.000000000000000 i,x = 18 18.000000000000000 i,x = 19 19.000000000000000 i,x = 20 20.000000000000000 sum(y) = 210.00000000000000 {{< /highlight >}} {{% /expand %}} {{%expand "C.out" %}} {{< highlight batchfile>}} i,x= 1 1.000000 i,x= 2 2.000000 i,x= 3 3.000000 i,x= 4 4.000000 i,x= 5 5.000000 i,x= 6 6.000000 i,x= 7 7.000000 i,x= 8 8.000000 i,x= 9 9.000000 i,x= 10 10.000000 i,x= 11 11.000000 i,x= 12 12.000000 i,x= 13 13.000000 i,x= 14 14.000000 i,x= 15 15.000000 i,x= 16 16.000000 i,x= 17 17.000000 i,x= 18 18.000000 i,x= 19 19.000000 i,x= 20 20.000000 sum(y)= 210.000000 {{< /highlight >}} {{% /expand %}}