Exercises

Overview

Teaching: 0 min
Exercises: 60 min
Questions
Objectives

Exercise 1:

  1. Write a Fortran program that writes “Hello OpenMP”. Compile and execute.

  2. Convert the previous Fortran program in multithreaded with a paralell directive

  3. Load the omp_lib module and print also the thread number using the function omp_get_thread_num() and omp_get_num_threads()

  4. Store the values for the number of threads and thread ID on variables before printing

  5. What is the difference bettween making those variables public or private.

Exercise 2:

  1. Write a Fortran code that computes the integral of a function f(x)=log(pix)sin(pi*x)**2 + x in the range [1,10]

  2. Use rectangles to approximate the integral and use an OpenMP parallel loop. (*) An alternative could be using trapezoids instead of triangles.

  3. Compare the performance of the serial code compared with the parallel version.

  4. Use the function omp_get_wtime() to get the time spent on the loop. The function returns a real and can be used before and after the loop.

Exercise 3:

Use the following product to approximate pi

  1. Use the reduction for a product to an approximation of pi

  2. How many terms are needed to get 4 good digits?

Key Points