Lab 4. Matrix Multiplication
Task 0. Get Prepared.
- Create the folder lab04 and task 1.
- Download the file matrix.c and understand what the program functions are for.
Task 1. Matrix Multiplication: Scatter the
elements of a and broadcast b.
- Start from the files matrix.c and understand what the program functions are for.
- If processor 0 then
o Initialise the matrices a and b.
- Scatter the matrix a and broadcast b on processors.
o Use &a[0][0] as pointer for where the elements of a are located.
o If a has n*n elements then n*n/size elements are to be scattered.
- Compute the product local_c=local_a*b
o local_ a has n/size by n elements.
- Gather the matrices local_c back on root.
- Write a few elements of c.
Test the
performances of this programme.
- Find out what it the largest squared matrix you can allocate.
- Test how the execution times reduce when size increases.
- If no reduction is observed, try to explain why?
- Replace c[i][j]=c[i][j]+a[i][k]*b[k][j] with c[i][j]=c[i][j]+a[i][k]*b[j][k] and evaluate the execution times.
- If a time reduction is observed, try to explain why?
Task 2. Matrix Multiplication: Scatter the rows of
a and broadcast b.
- Start from the program matrix.c and save it as matrix_row.c.
- If processor 0 then
o Initialise the matrices a and b.
- Declare and commit the datatype row
- Scatter a and broadcast b on processors.
o Use &a[0][0] as pointer for where the elements of a are located.
o If a has n rows then n/size row elements are to be scattered.
- Compute the product local_c=local_a*b
o local_ a has n/size rows by n elements.
- Gather the rows of the matrices local_c back on root.
- Write a few elements of c.
The solutions of this lab are included task1 and task 2.