Timing an OpenMP run using Fortran

What to expect:

  • How you can time a run in Fortran by calling cpu_time
  • How you can time a run in Fortran (or C) using OpenMP using omp_get_wtime()


I am using the Intel Fortran Compiler, ifort, called Intel Fortran Composer XE 2011 for Linux. It’s version 11.1. You can find the compiler here, as part of Intel’s Non-Commercial Software Downloads. You can check the version of your ifort by supplying the command

$ ifort -v

The ifort compiler has OpenMP capability built in. OpenMP has a built-in ability to time the run that you are executing. One way that we can time the run natively in Fortran will also be shown.

At the beginning of my code, it looks like the following:

Example Code

program goodtimes

c$     use omp_lib
       use your_modules

       implicit none

       double precision :: fstart, fend
       [Declare other variables]
       double precision :: ostart,oend

c      Fortran timing
       call cpu_time (fstart)

c      OpenMP timing
c$     ostart = omp_get_wtime()

c      Start of your meaningful code

c      Middle of your meaningful code

c      End of your meaningful code

c      End Fortran timing
       call cpu_time (fend)

c      End OpenMP timing
c$     oend = omp_get_wtime()

       write(*,*) 'Fortran CPU time elapsed', fend-fstart
c$     write(*,*) 'OpenMP Walltime elapsed', oend-ostart

end program

There are a few things to mention in the loosely-written code above. I wrote it a little Fortran 77-esque, where I started writing in the 7th column, and the OpenMP pragma is ‘c$’, ‘!$’, or ‘*$’. I used ‘c$’ above. In later versions of Fortran, use ‘!$’.

Notice that you must use the ‘omp_lib’ module in order to access the built-in ‘omp_get_wtime’. Otherwise, you will get an error. I strongly recommend making your start and end variables double precision. It doesn’t matter how you specify them as double precision, and I don’t necessarily recommend the way I did it above, but I just want to make it clear that you will have a better time with a double precision specification.

Note that cpu_time yields information about the CPU time (how long the CPU was working on your problem) and omp_get_wtime yields the wall clock time, such as the time that would have elapsed if you were timing the run from beginning to end with a very precise clock. I had a few runs for my application that showed the CPU operating at about 90% efficiency (where wall time is 100% of the total time). I recommend reading this post I did about profiling your code, so you can see which regions of your code are time consuming, and you can direct your OpenMP use in those regions.

Remember to include the ‘-openmp’ flag when compiling, and specifying the environment variable ‘OMP_NUM_THREADS’. I typically modify the ~/.bashrc file with a value and then source ~/.bashrc.

The rest of the code is self-explanatory. Read up on Fortran (or C) and OpenMP tutorials and other documentation for any additional information, or feel free to ask questions below. The C techniques are very similar and straightforward.


2 thoughts on “Timing an OpenMP run using Fortran

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s