You are on page 1of 10

Lab Assignment 2

NAME: BALAGA SAI KIRAN


REG No.: 20BCE0023
SLOT: L13+L14
COURSE: PARALLEL AND DISTRIBUTED COMPUTING
DATE:10/03/2023
FACULTY: Dr. NARAYAN PRASANTH
Question1:

Program:
#include <iostream>

#include <cstdlib>

#include <ctime>

#include <omp.h>

using namespace std;

struct Node {

int value;

Node* left;

Node* right;

};

// Function to create a new node with the given value

Node* createNode(int value) {

Node* node = new Node;

node->value = value;

node->left = NULL;

node->right = NULL;

return node;

// Function to insert a node into the binary tree

void insertNode(Node*& root, int value) {

if (root == NULL) {

root = createNode(value);

else if (value < root->value) {

insertNode(root->left, value);
}

else {

insertNode(root->right, value);

// Function to traverse the binary tree in inorder recursively

void inorderTraversal(Node* node) {

if (node == NULL) {

return;

inorderTraversal(node->left);

//cout << node->value << " ";

inorderTraversal(node->right);

// Function to traverse the binary tree in inorder using OpenMP task parallelism

void parallelInorderTraversal(Node* node) {

if(node->left!=NULL){

#pragma omp task shared(node) // create a new tas

parallelInorderTraversal(node->left); // traverse left subtree

//cout << node->value << " "; // print node value

if(node->left!=NULL){

#pragma omp task shared(node) // create a new task

parallelInorderTraversal(node->right); // traverse right subtree

#pragma omp taskwait // wait for tasks to complete

int main() {

Node* root = NULL;

// Insert 1000000 random values into the binary tree


srand(time(NULL));

for (int i = 0; i < 1000000; i++) {

insertNode(root, rand() % 1000000);

// Traverse the binary tree in inorder using OpenMP task parallelism and measure time taken

double start = omp_get_wtime();

#pragma omp parallel

#pragma omp single

parallelInorderTraversal(root);

double end = omp_get_wtime();

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.

You might also like