Shared Memory Programming Flashcards
pragma omp parallel
Use the parallel directive to start multiple threads.
pragma omp critical
Protect sections using critical to ensure only one thread executes a block at a time.
pragma omp atomic
For operations that can be performed atomically to improve efficiency.
omp_lock_t lock;
omp_init_lock(&lock);
#pragma omp parallel
{
omp_set_lock(&lock);
// Critical section
omp_unset_lock(&lock);
}
omp_destroy_lock(&lock);
Manage access to critical sections more explicitly with locks.
pragma omp parallel for
Divide loop iterations among threads, avoiding loop-carried dependencies.
pragma omp parallel for private(x) shared(y)
Explicitly declare variable scope to avoid unintended data sharing.
pragma omp parallel for reduction(+:sum)
Parallelize reduction operations efficiently.
pragma omp barrier
Synchronize all threads at a certain point.
A structured parallel region
- One point of entry: Execution can only start at the beginning of the block.
- One point of exit: Execution leaves the block at one specific point, ensuring a clear and predictable flow of control.
- Limited branching: The only allowed branching out of the structured block is through an
exit() call or reaching the end of the block.
pragma omp parallel default(private)
By default, variables declared before a parallel block have shared scope among all threads, unless specified otherwise. However, you can change this default behavior using the default clause.
pragma omp parallel for schedule(static, 1)
Distributes the iterations in a round-robin fashion among the threads.
pragma omp parallel for schedule(dynamic, 3)
Iterations are divided into chunks. When a thread finishes its chunk, it requests another, continuing until all iterations are completed.
pragma omp parallel for schedule(guided, 2)
Similar to dynamic, but the chunk size decreases as threads complete their tasks. This can lead to more efficient utilization of threads towards the end of the task list.
pragma omp parallel for schedule(runtime)
export OMP_SCHEDULE=”dynamic,4”
The scheduling strategy is determined at runtime based on the OMP_SCHEDULE environment
variable. This allows flexibility as the same code can use different scheduling by just changing an environment variable.
pragma omp task
Tasks are created within a single execution block to ensure that only one thread is involved in task creation, avoiding unnecessary duplication of work.