# Notes on use of OpenMP

GNU parallel is useful if you have $X$ .sh scripts that need to be run in parallel across multiple cores. If instead we want to run the components of a single (Fortran) script in parallel, we can use OpenMP.

Use is fairly straightforward - it is just important to define the variables used correctly. An example is presented below:

PROGRAM demo
INTEGER I,J,
REAL r,theta,phi

r=8
theta = 1.57
phi = 3.14

!$OMP PARALLEL DO PRIVATE(I,J,xstr,ystr,loadfile) SHARED(r,theta,phi) do I = -5,5 do J = -5,5 write(xstr, '(F8.2)') REAL(I) write(ystr, '(F8.2)') REAL(J) loadfile = '/path/to/some/file/file='//trim(adjustl(xstr))//'_y='//trim(adjustl(ystr))//'.txt' call some_subroutine(loadfile,r,theta,phi) enddo enddo !$OMP END PARALLEL DO

END


So all the stuff thats run in parallel is contained within the two !\$OMP statements. WE must also declare which variables are ‘private’ - i.e. used by only one strand - and which are ‘shared’. In the example case, the values of $r, \theta, \phi$ are the same for each strand. What is different is the load file and so the parameters related to the loadfile are declared as private.

Compilation is of the form,

gfortran -fopenmp script.f


The number of strands to run is set externally as,

export OMP_NUM_THREADS=8


for setting e.g. 8 threads running in parallel.

Calling nproc will confirm the number of threads that will be run, whilst nproc --all will show you the total number of cores available to you.

Categories:

Updated: