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

PDC Lab Manual

The document outlines lab tasks for the Faculty of Information Technology at the University of Central Punjab, focusing on implementing sorting algorithms and matrix multiplication in both serial and parallel formats using C programming. It includes specific tasks such as Bubble Sort, Merge Sort with POSIX Threads, and matrix multiplication, along with Fibonacci sequence calculations using both recursive and iterative approaches. Each task emphasizes measuring execution time and comparing performance between serial and parallel implementations.

Uploaded by

Naila Nisar
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)
6 views5 pages

PDC Lab Manual

The document outlines lab tasks for the Faculty of Information Technology at the University of Central Punjab, focusing on implementing sorting algorithms and matrix multiplication in both serial and parallel formats using C programming. It includes specific tasks such as Bubble Sort, Merge Sort with POSIX Threads, and matrix multiplication, along with Fibonacci sequence calculations using both recursive and iterative approaches. Each task emphasizes measuring execution time and comparing performance between serial and parallel implementations.

Uploaded by

Naila Nisar
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

University of Central Punjab

(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)


FACULTY OF INFORMATION TECHNOLOGY

Parallel and Distributive Computing

Lab No 6

THREADS & POSIX Threads

Faculty of Information Technology

UCP, Lahore Pakistan


University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
FACULTY OF INFORMATION TECHNOLOGY

Task 1 – Sorting Algorithms - Serial Implementation (Bubble Sort)

Write a serial C program to implement the Bubble Sort algorithm. The program should take an array
of integers and sort it in ascending order. The program should also measure the time taken to sort an
array of size 1,000,000.

Hints:

 Bubble Sort compares adjacent elements and swaps them if they are in the wrong order. This
process is repeated until the array is sorted.
 The time complexity of Bubble Sort is O(n²), so for large arrays, the program will take a
significant amount of time.

Example:

 Input array: [5, 2, 9, 1, 5, 6]


 Output array: [1, 2, 5, 5, 6, 9]

Task 1 – Sorting Algorithms - Parallel Implementation (Merge Sort with Threading)

Write a parallel version of your sorting program using the Merge Sort algorithm and POSIX Threads
for parallelization. The program should measure the time required to sort an array of integers of size
1,000,000. Compare the speed-up between the serial and parallel versions.

Hints:

 Merge Sort is a divide-and-conquer algorithm with a time complexity of O(n log n).
 Use POSIX threads to divide the array into smaller subarrays and merge them in parallel.

Example:
University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
FACULTY OF INFORMATION TECHNOLOGY

 Input array: [5, 2, 9, 1, 5, 6]


 Output array: [1, 2, 5, 5, 6, 9]

Task 2 – Matrix Multiplication - Serial Implementation

Write a serial C program to multiply two square matrices of size n x n (where n is a user input) and
print the resulting matrix. The program should measure the time taken to perform the multiplication.

Hints:

 Matrix multiplication is done by taking the dot product of the rows of the first matrix and the
columns of the second matrix.
 The time complexity of matrix multiplication is O(n³), so for large matrices, the computation
can be time-consuming.

Example:

 Input matrices:

A = [1 2 3]
[4 5 6]
[7 8 9]

B = [9 8 7]
[6 5 4]
[3 2 1]

 Output matrix C = A * B:

C = [30 24 18]
[84 69 54]
[138 114 90]
University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
FACULTY OF INFORMATION TECHNOLOGY

Task 2 – Matrix Multiplication - Parallel Implementation (Using POSIX Threads)

Write a parallel C program to multiply two square matrices using POSIX Threads to distribute the
workload across multiple threads. Measure the time taken to multiply matrices of size n x n for large n
(e.g., n = 1,000) and compare the time with the serial implementation.

Hints:

 Matrix multiplication can be parallelized by dividing the rows of the first matrix among
multiple threads.
 Use POSIX threads to parallelize the outermost loop of the matrix multiplication.

Example:

 Input matrices (same as Task 5).


 Output matrix C = A * B (same as Task 5).

Task 3 – Fibonacci Series - Recursive Approach

Write a C program that calculates the Fibonacci sequence recursively. The program should prompt the
user to enter a number n, and then calculate the nth Fibonacci number. Measure the time taken to
compute the nth Fibonacci number for large values of n.

Hints:

 The Fibonacci sequence is defined as:

F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2)
University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
FACULTY OF INFORMATION TECHNOLOGY

 The time complexity of this recursive approach is exponential, so it can be very slow for large
values of n.

Example:

 Input: n = 10
 Output: F(10) = 55

Task 3 – Fibonacci Series - Iterative Approach

Write a C program that calculates the Fibonacci sequence iteratively. The program should prompt the
user to enter a number n and then calculate the nth Fibonacci number. Measure the time taken to
compute the nth Fibonacci number for large values of n.

Hints:

 The iterative approach uses a loop to calculate Fibonacci numbers, which has a time
complexity of O(n) compared to the recursive approach's exponential time complexity.

Example:

 Input: n = 10
 Output: F(10) = 55

You might also like