0% found this document useful (0 votes)
4 views5 pages

Assignment 2

The document outlines the implementation of Parallel Bubble Sort and Merge Sort using OpenMP, highlighting the complexities and performance of both algorithms. It discusses the challenges of parallelizing bubble sort due to its inherent structure and provides a detailed explanation of the odd-even transposition sort. The conclusion emphasizes the benefits of parallelization while noting the importance of load balancing and communication overhead in achieving optimal performance.
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)
4 views5 pages

Assignment 2

The document outlines the implementation of Parallel Bubble Sort and Merge Sort using OpenMP, highlighting the complexities and performance of both algorithms. It discusses the challenges of parallelizing bubble sort due to its inherent structure and provides a detailed explanation of the odd-even transposition sort. The conclusion emphasizes the benefits of parallelization while noting the importance of load balancing and communication overhead in achieving optimal performance.
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
You are on page 1/ 5

Assignment 2

Problem Statement:

Write a program to implement Parallel Bubble Sort and Merge sort using OpenMP. Use existing
algorithms and measure the performance of sequential and parallel algorithms.

Theory:

Bubble Sort

1. The complexity of bubble sort is Θ(n2).

2. Bubble sort is difficult to parallelize since the algorithm has no concurrency.

3. A simple variant, though, uncovers the concurrency.

Odd-Even Transposition

Sorting n = 8 elements, using the odd-even transposition sort algorithm. During each phase, n = 8
elements are compared.

Odd-Even Transposition
Sequential odd-even transposition sort algorithm

1. After n phases of odd-even exchanges, the sequence is sorted.

2. Each phase of the algorithm (either odd or even) requires Θ(n) comparisons.

3. Serial complexity is Θ(n2).

Parallel Odd-Even Transposition

1. Consider the one item per processor case.

2. There are n iterations, in each iteration, each processor does one compare-exchange.

3. The parallel run time of this formulation is Θ(n).

4. This is cost optimal with respect to the base serial algorithm but not the optimal one.

Parallel formulation of odd-even transposition.

1. Consider a block of n/p elements per processor.

2. The first step is a local sort.


3. In each subsequent step, the compare exchange operation is replaced by the compare split
operation.

Algorithm:[Parallel Bubble Sort]

1. This program uses OpenMP to parallelize the bubble sort algorithm.

2. The #pragma omp parallel for directive tells the compiler to create a team of threads to
execute the for loop within the block in parallel.

3. Each thread will work on a different iteration of the loop, in this case on comparing and
swapping the elements of the array.

4. The bubbleSort function takes in an array, and it sorts it using the bubble sort algorithm. The
outer loop iterates from 0 to n-2 and the inner loop iterates from 0 to n-i-1, where i is the
index of the outer loop. The inner loop compares the current element with the next
element, and if the current element is greater than the next element, they are swapped.

5. The main function creates a sample array and calls the bubbleSort function to sort it. The
sorted array is then printed.

6. This is a skeleton code and it may not run as is and may need some modification to work
with specific inputs and requirements.

7. It is worth noting that bubble sort is not an efficient sorting algorithm, specially for large
inputs, and it may not scale well with more number of threads. Also parallelizing bubble sort
does not have a significant improvement in performance due to the nature of the algorithm
itself.

8. In this implementation, the bubble_sort_odd_even function takes in an array and sorts it


using the odd-even transposition algorithm. The outer while loop continues until the array is
sorted. Inside the loop, the #pragma omp parallel for directive creates a parallel region and
divides the loop iterations among the available threads. Each thread performs the swap
operation in parallel, improving the performance of the algorithm.

9. The two #pragma omp parallel for inside while loop, one for even indexes and one for odd
indexes, allows each thread to sort the even and odd indexed elements simultaneously and
prevent the dependency.

Parallel Merge Sort

Given a set of elements A = {a .. ~,~, ... ,an},

Aodd and Aeven are defined as the set of elements of A with odd and even indices, respectively.

For example, Aodd = {a .. a3,a" ... } and Aeven = { ~,a4,a6" .. } regarding a set of elements A = {a…an}'

Similarly, let a set of elements B = {b ….bn}. We can then define the merge operation as:

Merge(A,B) = {a.,b.,~,b2,a3,b3, ... ,an,bn} For example,

if A = {1,2,3,4} and B = {5,6,7,8}

then Merge( (1,2,3,4 },{5,6,7,8}) = {1,5,2,6,3,7,4,8}

Join(A,B) = (Merge(A,B), Odd-Even(A,B)}


Example: Suppose the set of elements S =

{2,3,6,1O,15,4,5,8} and we start with A =

{2,6,IO,15} and B = {3,4,5,8}, two sorted sets of elements. Then

Implementation:

1. Install mingw64 on your machine.


2. Install VS Code.
3. Install code runner and c++ extension in vs code.
4. Run c++ code.
5. Display result.

Compiling command

gcc -o bubble -fopenmp bubbleSort.cpp -lstdc++

To run file:

bubble
CONCLUSION

In conclusion, the implementation and performance evaluation of Parallel Bubble Sort and Merge
Sort using OpenMP have provided valuable insights into the effectiveness of parallelizing well-known
sorting algorithms. Parallelization offers advantages in terms of speedup, it also underscores the
need for careful consideration of load balancing and communication overhead.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy