Upcoming SlideShare
×

# Matrix Multiplication(An example of concurrent programming)

44,149 views

Published on

its deals with how parallel programming can be achieved. its just an example of parallel programming.

Published in: Technology, Education
19 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• check 8th slide ......

Are you sure you want to  Yes  No
• Am interested in listing all possible sequence

Are you sure you want to  Yes  No
• yaa.....first register account....

Are you sure you want to  Yes  No

Are you sure you want to  Yes  No
Views
Total views
44,149
On SlideShare
0
From Embeds
0
Number of Embeds
222
Actions
Shares
0
7
4
Likes
19
Embeds 0
No embeds

No notes for slide

### Matrix Multiplication(An example of concurrent programming)

1. 1. Abstract <ul><li>Problem definition: </li></ul><ul><li>Parallel computing is a form of computing which many instructions carried out on the principle that large problems can almost always be divided into smaller ones, which may be carried out concurrently (&quot;in parallel&quot;). </li></ul><ul><li>Principles of Parallel Algorithm Design </li></ul><ul><li>Identifying concurrent tasks </li></ul><ul><li>Mapping tasks onto multiple processes </li></ul><ul><li>Distributing input, output, intermediate data </li></ul><ul><li>Managing access to shared data </li></ul><ul><li>Synchronizing processors </li></ul><ul><li>In this section we are going to see how to multiply two matrixes by using parallel programming in a unique and efficient way. </li></ul><ul><li>There are many different way to carry out matrix multiplication by using parallel computing. </li></ul>
2. 2. <ul><li>In this section we are going to see how to multiply two matrixes by using parallel programming in a unique and efficient way. </li></ul><ul><li>There are many different way to carry out matrix multiplication by using parallel computing. </li></ul><ul><li>Previous approach: </li></ul><ul><li>By creating thread for each computation, Cannon’s algorithm, strassen’s multiplication . </li></ul><ul><li>But we all know that strassen’s multiplication is quite difficult to implement and it is more restrictive also. </li></ul><ul><li>Proposed Approach: </li></ul><ul><li>There are two different approaches to compute matrix multiplication: </li></ul><ul><li>By dividing matrix in the as many no. of process as required and solving all of them concurrently. This algorithm works well for all the value of size n. </li></ul><ul><li>By dividing the matrix in terms of matrix of small size using recursion and solving the terminating condition of recursion by using strassen’s multiplication. This algorithm also works well for all the value of size n. </li></ul><ul><li>  </li></ul>
3. 3. General introduction Basic Matrix Multiplication Suppose we want to multiply two matrices of size N x N : for example A x B = C . C 11 = a 11 b 11 + a 12 b 21 C 12 = a 11 b 12 + a 12 b 22 C 21 = a 21 b 11 + a 22 b 21 C 22 = a 21 b 12 + a 22 b 22 2x2 matrix multiplication can be accomplished in 8 multiplication. (2 log 2 8 =2 3 )
4. 4. Time analysis Strassens’s Matrix Multiplication P 1 = (A 11 + A 22 )(B 11 +B 22 ) P 2 = (A 21 + A 22 ) * B 11 P 3 = A 11 * (B 12 - B 22 ) P 4 = A 22 * (B 21 - B 11 ) P 5 = (A 11 + A 12 ) * B 22 P 6 = (A 21 - A 11 ) * (B 11 + B 12 ) P 7 = (A 12 - A 22 ) * (B 21 + B 22 )
5. 5. C 11 = P 1 + P 4 - P 5 + P 7 C 12 = P 3 + P 5 C 21 = P 2 + P 4 C 22 = P 1 + P 3 - P 2 + P 6 Time analysis
6. 6. Proposed approach 1: <ul><li>Matrix multiplication by using recursion to divide the problem in terms of sub problems and solving the terminating condition of recursion by using Strassens’s Matrix Multiplication. </li></ul><ul><li>Steps involved in this approach are: </li></ul><ul><li>Step 1. Divide the matrix in 4 different parts by using divide and concur technique. </li></ul><ul><li>Step 2 . Multiply each part by using recursion . </li></ul><ul><li>Step 3 . S olve the terminating condition of the recursion by using Strassens’s Matrix Multiplication. </li></ul><ul><li>Divide-and-Conquer </li></ul><ul><li>Divide-and conquer is a general algorithm design paradigm: </li></ul><ul><ul><li>Divide: divide the input data S in two or more disjoint subsets S 1 , S 2 , … </li></ul></ul><ul><ul><li>Recur: solve the subproblems recursively </li></ul></ul><ul><ul><li>Conquer: combine the solutions for S 1 , S 2 , …, into a solution for S </li></ul></ul><ul><li>The base case for the recursion are subproblems of constant size </li></ul><ul><li>Analysis can be done using recurrence equations </li></ul>
7. 7. ALGORITHM Matmul(A[n]n],B[n][n],C[n][n]) //The algorithm implements matrix multiplication by dividing matrix in to sub matrix A0,A1,A2,A3…. B0,B1,B2,B3…. And multiplying each of them recursively. //Input: Two matrixes A and B of dimension n //Output: A matrix C of dimension n if (n==2) Multiply the two input matrix by using Strassens’s Matrix else Divide the matrix in the sub matrix of dimension n/2 and solve recursively. <ul><li>Divide matrices into sub-matrices: A 0 , A 1 , A 2 etc </li></ul><ul><li>Use blocked matrix multiply equations </li></ul><ul><li>Recursively multiply sub-matrices </li></ul>
8. 8. * Consider Multiplication of 4x4 matrix: A11 A12 A13 A14 A21 A22 A23 A24 A31 A32 A33 A34 A41 A42 A43 A44 B11 B12 B13 B14 B21 B22 B23 B24 B31 B32 B33 B34 B41 B42 B43 B44 C11 C12 C13 C14 C21 C22 C23 C24 C31 C32 C33 C34 C41 C42 C43 C44
9. 9.  = The sub matrix of size 2x2 can be computed as follows: Now c11 ,c12, c21,c22 can be computed as follows: C 11 = P 1 + P 4 - P 5 + P 7 C 12 = P 3 + P 5 C 21 = P 2 + P 4 C 22 = P 1 + P 3 - P 2 + P 6 P 1 = (A 11 + A 22 )(B 11 +B 22 ) P 2 = (A 21 + A 22 ) * B 11 P 3 = A 11 * (B 12 - B 22 ) P 4 = A 22 * (B 21 - B 11 ) P 5 = (A 11 + A 12 ) * B 22 P 6 = (A 21 - A 11 ) * (B 11 + B 12 ) P 7 = (A 12 - A 22 ) * (B 21 + B 22 ) A11 A12 A21 A22 B11 B12 B21 B22 C11 C12 C21 C22
10. 10. In similar way we can calculate all the multiplications. And same steps we can perform on any dimension of matrix. Results : Input Size(n) No. of multiplication In sequential algo. No. of multiplication In the implemented algo. No. of multiplication In Strassen’s 2 8 7 7 4 64 56 49 8 512 448 343 16 4096 3584 2401 . . . . . . . . p p 3 (7/8)*p 3 n 2.807
11. 11. Applying above algorithm when size is not in the power of 2 Steps required is as follows Step 1) Make the size to nearest power of 2 Step 2) Make the value of all extra rows to be zero Step 3) Make the value of all extra columns to be zero Step 4) Then apply above algorithm Suppose the dimension of given matrix is 3 then Nearest of 3 is 4 which is power of 2 Hence matrix becomes A11 A12 A13 0 A21 A22 A23 0 A31 A32 A33 0 0 0 0 0
12. 12. Efficiency calculation: Time analysis : If the value of dimension is not in the power of 2 then first make it to the nearest power of 2 then start proceeding…… T(2)=7 ( from base condition of strassen’s multiplication) T(N)= (7/8)*N 3 Hence total no. of multiplication required is = (7 * n 3 )/8 Which is less then (n 3 ).
13. 13. Proposed Approach 2: Matrix multiplication by creating process for each computation is as follows: A matrix can be divided in to number of parts and each part can be multiply by creating a separate process. A new process can be created by using fork () system call. Different steps involved in this approach are Step 1 : Partitioning a) Divide matrices into rows b) Each primitive task has corresponding rows of three matrices Step 2 : Communication a) Each task must eventually see every row of B b) Organize tasks into a ring Step 3: Agglomeration and mapping a) Fixed number of tasks, each requiring same amount of computation b) Regular communication among tasks c) Strategy: Assign each process a contiguous group of rows
14. 14. Examples: Let in the first case total no. of process to be created is equal to total no. of rows in the matrix i.e. total no. of process = n where n is the dimension of matrix. Task1 Task2 Task3 Task4 A11 A12 A13 A14 A21 A22 A23 A24 A31 A32 A33 A34 A41 A42 A43 A44