Professional Documents
Culture Documents
Program:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <omp.h>
struct Node {
int value;
Node* left;
Node* right;
};
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
if (root == NULL) {
root = createNode(value);
insertNode(root->left, value);
}
else {
insertNode(root->right, value);
if (node == NULL) {
return;
inorderTraversal(node->left);
inorderTraversal(node->right);
// Function to traverse the binary tree in inorder using OpenMP task parallelism
if(node->left!=NULL){
if(node->left!=NULL){
int main() {
// Traverse the binary tree in inorder using OpenMP task parallelism and measure time taken
parallelInorderTraversal(root);
cout << "\nParallel traversal time: " << end - start << " seconds\n";
// Traverse the binary tree in inorder recursively and measure time taken
start = omp_get_wtime();
inorderTraversal(root);
end = omp_get_wtime();
cout << "\nSerial traversal time: " << end - start << " seconds\n";
return 0;
}
Output:
Question2:
Program1:
Output1:
Concept1:
In OpenMP, the threadprivate directive is used to declare global or
static variables as thread-local variables. When a variable is declared
as thread-local using the threadprivate directive, each thread has its
own copy of the variable, and changes made to the variable by one
thread do not affect the value of the variable in other threads.
Program2:
Output2:
Concept2:
In OpenMP, the lastprivate directive is used to ensure that the final
value of a variable is preserved outside a parallel loop. When a
variable is declared as lastprivate, its final value after the loop is
assigned to a private copy of the variable, and that private copy is
then assigned to the original variable outside the loop.
Program3:
Output3:
Concept3:
In OpenMP, the firstprivate directive is used to initialize private
copies of variables with their values from outside the parallel region.
This ensures that each thread has its own copy of the variable,
initialized with the value from the master thread or the main
program.