Example 1Consider the following example of a parallel program that sums the elements of an array:
Download this program. Then, compile and run it a few times. On GL, you need to use the -fopenmp flag to compile:
What happens when you run this program a few times? Does it give the same answers? (Hint: no.) The reason for this is that the shared variable sum is being updated by different threads and sum of the updates might get lost.
This is a common situation. OpenMP uses the reduction clause to solve this problem.