Advanced OpenMP Worksheet Solutions
with Sleeping Barber Problem
Below are the correct answers and explanations for the advanced OpenMP worksheet
questions.
Question 1: Advanced Synchronization
Correct Answer: C) #pragma omp lock
Explanation: For thread-safe file writes, OpenMP locks (omp_set_lock,
omp_unset_lock) provide fine-grained control with less overhead than critical sections
for I/O operations. atomic is unsuitable for complex operations like file writes, and barrier
only synchronizes execution, not resource access.
Question 2: Task Parallelism
Correct Answer: B) #pragma omp task
Explanation: The #pragma omp task directive is designed for dynamic task parallelism,
ideal for recursive algorithms like Fibonacci, as it allows recursive calls to be executed as
independent tasks by available threads.
Question 3: Memory Consistency
Correct Answer: B) #pragma omp flush
Explanation: The #pragma omp flush directive ensures memory consistency by forcing
threads to update their view of shared variables, making the latest value visible to all threads
after a write.
Question 4: Sleeping Barber Problem in OpenMP
Correct Answer: B) #pragma omp critical for the waiting room queue and
omp_set_lock() for barber status
Explanation: In the Sleeping Barber problem, the waiting room queue (shared resource)
requires thread-safe access, managed by #pragma omp critical. The barber’s status
(awake or sleeping) can be controlled using OpenMP locks (omp_set_lock) to signal
availability, ensuring proper synchronization between the barber and customer threads.
Question 5: Load Balancing
Correct Answer: C) schedule(guided, 4)
Explanation: The guided scheduling strategy dynamically adjusts chunk sizes, starting
large and decreasing, balancing irregular workloads while keeping scheduling overhead
lower than dynamic, 1. A chunk size of 4 reduces overhead compared to 1.
Question 6: Thread Affinity
Correct Answer: B) Use OMP_PROC_BIND=spread to bind threads to specific cores.
Explanation: Setting OMP_PROC_BIND=spread ensures threads are pinned to specific
cores, reducing migration overhead and improving cache locality, thus enhancing
performance on multi-core systems.
Question 7: Reduction with Custom Operations
Correct Answer: A) Use #pragma omp critical to update a shared max variable.
Explanation: Without reduction(max:result), a critical section safely updates a
shared max variable by comparing and updating the maximum value. atomic is less
efficient for comparisons, and serial merging is slower.
Question 8: Scalability Limits
Correct Answer: B) Cache contention due to threads sharing the same cache lines.
Explanation: When threads exceed physical cores, they compete for cache, causing
contention and poor scalability. This is more likely than variable scoping issues or
scheduling, as OpenMP typically manages these well.
Note: Use this solutions sheet to check your answers and understand the reasoning behind
each correct option.