0% found this document useful (0 votes)
7 views2 pages

Reduction Lab

The document explains the reduction clause in OpenMP, which is used for parallel reduction operations on variables to prevent race conditions during aggregate operations. It details the process of how threads independently operate on private copies of a variable and then combine results at the end of the parallel region. Additionally, it provides syntax, operators for various operations, and example code for summation and finding the maximum value.

Uploaded by

Yousra Awan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Reduction Lab

The document explains the reduction clause in OpenMP, which is used for parallel reduction operations on variables to prevent race conditions during aggregate operations. It details the process of how threads independently operate on private copies of a variable and then combine results at the end of the parallel region. Additionally, it provides syntax, operators for various operations, and example code for summation and finding the maximum value.

Uploaded by

Yousra Awan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Reduction

The reduction clause in OpenMP is used to perform a parallel reduction operation on a variable.
It helps avoid race conditions while performing aggregate operations like summation,
multiplication, finding minimum or maximum, etc., across threads.

How It Works:
1. Each thread has its own private copy of the variable.
2. The threads perform the operation independently on their local copies.
3. At the end of the parallel region, OpenMP combines the results from all threads using the
specified operator.

Syntax
operator: Specifies the reduction operation (e.g., +, *, max, min, &, |, etc.).
variable: The shared variable being reduced across threads.

#pragma omp parallel for reduction(operator : variable)

Operators:
Operator Operation Example
+ Addition Sum
* Multiplication Product
- Subtraction (since Difference
OpenMP 5.0)
max Maximum value Max of elements
min Minimum value Min of elements
& Bitwise AND Bitwise operations
` ` Bitwise OR
^ Bitwise XOR Bitwise operations

Activity 1: Summation
#include <iostream>
#include <omp.h>

int main() {
int sum = 0;

#pragma omp parallel for reduction(+ : sum)


for (int i = 0; i < 10; i++) {
sum += i;
}

std::cout << "Final sum: " << sum << '\n'; // Output: 45
return 0;
}

Activity 2: Finding Maximum Number


#include <iostream>
#include <omp.h>

int main() {
int sum = 0;

#pragma omp parallel for reduction(+ : sum)


for (int i = 0; i < 10; i++) {
sum += i;
}

std::cout << "Final sum: " << sum << '\n'; // Output: 45
return 0;
}

Activity 3: Sum of First 100 Natural Numbers Using reduction


#include <iostream>
#include <omp.h>

int main() {
int maxVal = INT_MIN;
int arr[] = {1, 4, 2, 9, 7, 5, 3, 8, 6, 0};

#pragma omp parallel for reduction(max : maxVal)


for (int i = 0; i < 10; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}

std::cout << "Maximum value: " << maxVal << '\n'; // Output: 9
return 0;
}

You might also like