If so, then continue to the next step. If not, then login as you did previously for Exercise 1.
workshare1
example codeThis example demonstrates use of the OpenMP loop work-sharing construct. Notice that it specifies dynamic scheduling of threads and assigns a specific number of iterations to be done by each thread.
setenv OMP_NUM_THREADS 4
icc -openmp omp_workshare1.c -o workshare1
workshare1 | sort
ifort -openmp omp_workshare1.f -o workshare1
workshare1 | sort
Review the output. Note that it is piped through the sort utility. This will make it easier to view how loop iterations were actually scheduled across the team of threads.
Run the program a couple more times and review the output. What do you see? Typically, dynamic scheduling is not deterministic. Everytime you run the program, different threads can run different chunks of work. It is even possible that a thread might not do any work because another thread is quicker and takes more work. In fact, it might be possible for one thread to do all of the work.
Edit the workshare1.*
source file and change the dynamic scheduling to static scheduling.
Recompile and run the modified program. Notice the difference in output compared to dynamic scheduling. Specifically, notice that thread 0 gets the first chunk, thread 1 the second chunk, and so on.
Run the program a couple more times. Does the output change? With static scheduling, the allocation of work is deterministic and should not change between runs, and every thread gets work to do.
This example performs a matrix multiple by distributing the iterations of the operation between available threads.
icc -openmp omp_mm.c -o matmult
matmult
ifort -openmp omp_mm.f -o matmult
matmult
Review the output. It shows which thread did each iteration and the final result matrix.
matmult | sort | grep Thread
Do the loop iterations match the SCHEDULE(STATIC,CHUNK) directive for the matrix multiple loop in the code?
This example demonstrates use of the OpenMP SECTIONS work-sharing construct Note how the PARALLEL region is divided into separate sections, each of which will be executed by one thread.
icc -openmp omp_workshare2.c -o workshare2
workshare2
ifort -openmp omp_workshare2.f -o workshare2
workshare2