fortran_c_on_hcc.md 5.71 KB
Newer Older
Adam Caprez's avatar
Adam Caprez committed
1
2
3
4
5
6
7
8
9
10
+++
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).

11
#### Login to a HCC Cluster
Adam Caprez's avatar
Adam Caprez committed
12

Adam Caprez's avatar
Adam Caprez committed
13
14
Log in to a HCC cluster through PuTTY ([For Windows Users]({{< relref "/Quickstarts/connecting/for_windows_users">}})) or Terminal ([For Mac/Linux
Users]({{< relref "/Quickstarts/connecting/for_maclinux_users">}})) and make a subdirectory called `serial_dir` under the `$WORK` directory. 
Adam Caprez's avatar
Adam Caprez committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

{{< 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 %}}