To start, write code like the following.
program main
print *, "hello world!"
stop
end
to implement the good old Hello World introductory program. Compile and run the code you have created. The code
should produce the string in the print statement. That is,
hello world!
Now, let's do a bit more with this example. We will need to add some code to the example to have each of the cores
on your computer write out the same statement. In addition, the way that the code is compiled will also change.
That we will do on the other side of the following code.
| program main
| integer id, nthrds
| integer omp_get_thread_num, omp_get_num_threads
|C$OMP PARALLEL PRIVATE(id)
| id = omp_get_thread_num()
| print *, 'hello world from thread', id
|C$OMP BARRIER
| if(id .eq. 0) then
| nthrds = omp_get_num_threads()
| print *, 'There are', nthrds, ' threads!'
| end if
|C$OMP END PARALLEL
| stop
| end
First, there are some things that need to be described in the code:
- The line on the left indicates how the code needs to be written in the column. In fortran, the comment
line must start as the first character of the line typed in. So, the pipe, "|", is like the edge of the
window and is not actually a character on your screen, just the border of the document.
- The string "C$OMP" tells the compiler that the line is a line associated with OpenMP. The first letter
must appear in the first column and indicates that when compiled without OpenMP extensions, will be ignored.
If the OpenMP library is available, the compiler will include the parameters contained in the comments. These
strings are the start of a "directive" to the compiler to do something.
- For the present time, (1) save the code above into a file named "hello.f", (2) compile the code above, using
% gfortran -fopenmp hello.f -o hello
and then run the compiled version of the code using the command
% ./hello.exe
Report the results of running the code.