Assignment No. 2 PDC 21L-1786

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 6

National University

of Comput e r & Eme r ging Scie nce s-La hor e

CS3006– Parallel a n d Distributed C omputing

Assignment 2
Deadline: Friday 15th, March 2024
______________________________________________________________________________

Q uest i on N o. 1:
Suppose an array of size N is to be mapped on P processes numbered a s [ p0,
p1,......, p P-1] using an array-block-distribution scheme, then answer the following
questions:

a) How many tasks/blocks will be


there? b) What will be the size of
each block?
c) If block number starts from zero then, to which process number the i(th)
block will be assigned?
d) Suppose array indexing starts from zero, what will be the starting index of the
block assigned to i(th) process?
e) Suppose array indexing starts from zero, what will be the end index of the
block assigned to i(th) process?

Solution:

a) Number of tasks/blocks:
The array of size N is to be distributed among P processes. Each process
will receive a block of the array. Therefore, the number of tasks/blocks
will be equal to the number of processes, i.e., P.

b) Size of each block:


To calculate the size of each block, divide the total number of elements in
the array by the number of processes:
Size of each block = N / P

c) Process assignment of each block:


If block numbering starts from zero, then the i(th) block will be assigned
to process number i. So, the i(th) block will be assigned to process p_i.

d) Starting index of the block assigned to i(th)


process:
If the array indexing starts from zero, and each block has the same size,
then the starting index of the block assigned to the i(th) process can be
calculated as:
Starting index = i * (N / P)

e) End index of the block assigned to i(th)


process:
The end index of the block assigned to the i(th) process can be calculated
as the starting index of the next process minus 1. So:
End index = (i + 1) * (N / P) - 1

Q uest i on N o. 2
Write a multithreaded program using ‘Posix threads’ to perform matrix operations a s
instructed below. You will perform task decomposition a s well a s data decomposition. Your
program should provide the following functionality. You must submit your code file on
Google Classroom. Your assignment will be evaluated against it.

1. Take two matrices of size (m1 x n1) where ‘m1’ and ‘n1’ values are taken a s input
from the user through the command line. Initialize the matrix by some random values or
user input.
2. Then perform multiplication a s described below
i. These tasks should be performed using data decomposition by assigning each
row to a new thread.
ii. Mutual exclusion should be used to modify shared variables
SOLUTION:-
(code given seprate file)

Q uest i on N o. 3

National University
of Comput e r & Eme r ging Scie nce s-La hor e
CS3006– Parallel a n d Distributed C omputing

For the task graph given above, determine:


 Critical path
 Critical path length
 Maximum possible speedup assuming large number of processes are
available
 Minimum number of processes needed to obtain the maximum possible
speedup
 Maximum speed up if number of processes are limited to 4

SOLUTION

(a) 7 → 10 → 12 → 8 → 15
(b) 52
(c) SPEEDUP=1/(F+(1-F)/P)

P=NO OF PROCESSORDS=∞
SPEEDUP=1/(F+(1-F)/∞)
SPEEDUP=1/F
(d)
F=P/N=10/12=5/6
P=10 (SINCE FROM GRAPH WE SEE 10 TASKS ARE EXECUTED
PARALLEL)
SPEEDUP=1/(F+(1-F)/P)
S=1/((5/6)+(1-5/6/)/10)
S=1.1x
MINIMUM PROCESSES CAN BE 2

(E)
F=P/N=4/12=1/3
SPEEDUP=1/(F+(1-F)/P)
SPEEDUP=1/((1/3)+(1-1/3)/4)
SPEEDUP=2

Q uest i on N o. 4: RECURSIVE DECOMPOSITION

Write a multithreaded program using ‘Posix threads’ to perform matrix


operations a s instructed below.

You will perform task decomposition. Your program should provide the following
functionality. You can paste your code in this document. Teaching assistant can ask any
student/group to run the code, or he can conduct a viva to check the authenticity.

National University
of Comput e r & Eme r ging Scie nce s-La hor e
CS3006– Parallel a n d Distributed C omputing

1. Take a matrix of size (m x n) where ‘m’ and ‘n’ values are taken a s input
from the user. Initialize the matrix by some random values or user input.

2. Then take your student ID for the following.


• if the last digit of selected student ID is (0, 1 or 2) then create number of
threads equal to number of columns ‘n’ and each thread will sort a column
using Quick Sort in Ascending order.

• if the last digit of student ID is (3, 4 or 5) then create number of threads equal
to number of columns ‘n’ and each thread will sort a column using Quick Sort
in Descending order.

• if the last digit of student ID is (6 or 7) then create number of threads equal to


number of rows m’ and each thread will sort a row using Quick Sort in
Ascending order.

• if the last digit of student ID is (8 or 9) then create number of threads equal to


number of rows 'm and each thread will sort a row using Quick Sort In
Descending order.

3. While the above can be easily completed using serial processing the requirement
of this the assignment is to implement Quick Sort using Recursive decomposition.
Each subtask will need to be done by a new thread.

4. At the end each thread should add all values of the column/row that is being
sorted and the resultant number will be added to Matrix addition which is the sum of all
values of the matrix.

Let’s take the scenario of (4x4) matrix for first batch of students.

3 5 32 80

23 11 9 15

20 7 14 26
4 17 2 7
3 5 2 7

4 7 9 15

20 11 14 26

23 17 32 80

(Input Matrix) (Output Matrix)

National University
of Comput e r & Eme r ging Scie nce s-La hor e

CS3006– Parallel a n d Distributed C omputing

Matrix Addition = 275

NOTE: Kindly submit your assignment on Google Classroom. Each file contains your
roll number. Submit your assignment in Zip form with proper naming/roll no.

SOLUTION:-
(code given seprate file)

You might also like