Question

In: Computer Science

Multiplication of Matrix is one of the tasks that takes time to compute. Note that the...

Multiplication of Matrix is one of the tasks that takes time to compute. Note that the time complexity of multiplying two square matrix is o(n^3) using the nested loops method, and Strassen algorithm improves it to O(n^2.8074). Multi-threading can be used to further enhance this.

Using the matrices below, write

(a) a serial program (the normal nested loops method) to perform the multiplication.

(b) a multithreaded program using pthreads to perform this computation.

(c) compare the execution times of (a) and (b) in a table.

Solutions

Expert Solution

Solution

#include<stdio.h>
#include<pthread.h>
#include<time.h>
#include<stdlib.h>
#define SIZE 12
#define FACTOR 4
struct xy {
int x,y;
};
int a[SIZE][SIZE],b[SIZE][SIZE],ans2[SIZE][SIZE];
void *calculate(void *arg) {
int i,j,k,t=0;
struct xy *c = arg;
int x=(c->x),y=(c->y);

for(j=0;j<(SIZE/FACTOR);j++) {
for(k=0;k<(SIZE/FACTOR);k++) {
t=0;
for(i=0;i<SIZE;i++) {
t+=a[x+j][i]*b[i][y+k];

}
ans2[x+j][y+k]=t;
}
}
}
void main() {
int ans1[SIZE][SIZE],i,j,t,k;
for(i=0;i<SIZE;i++) {
for(j=0;j<SIZE;j++) {
a[i][j]=1;
b[i][j]=1;
}
}
clock_t start,end;
double t1,t2;


// (a). Multiplication of matrix using normal sequential execution
start=clock();
for(i=0;i<SIZE;i++) {
for(j=0;j<SIZE;j++) {
t=0;
for(k=0;k<SIZE;k++) {
t+=a[i][k]*b[k][j];
}
ans1[i][j]=t;
}
}
end=clock();
// (b). Multiplication of matrix using multithreading
t1=(double)(end-start)/CLOCKS_PER_SEC;
start=clock();
int sub_size=(int)(SIZE/FACTOR);
pthread_t threads[FACTOR*FACTOR];
struct xy coords[FACTOR*FACTOR];
k=0;
for(i=0;i<FACTOR;i++) {
for(j=0;j<FACTOR;j++) {
coords[k].x=i*sub_size;
coords[k].y=j*sub_size;
k++;
}
}
for(i=0;i<FACTOR*FACTOR;i++) {
pthread_create(&threads[i],NULL,calculate,&coords[i]);
}
for(i=0;i<FACTOR*FACTOR;i++) {
pthread_join(threads[i],NULL);
}
end=clock();
t2=(double)(end-start)/CLOCKS_PER_SEC;
for(i=0;i<SIZE;i++) {
for(j=0;j<SIZE;j++) {
if(ans1[i][j]!=ans2[i][j]) {
printf("Error in Calculation\n");
return;
}
else {
// printf("%d ",ans1[i][j]);
}
}
printf("\n");
}
// (c). Comparision of execution time of both methods

printf("Normal Execution - %f s...\n",t1);
printf("Threaded Execution - %f s...\n",t2);
}


Related Solutions

Matrix multiplication with arrays In this question you will verify that the numpy.ndarray matrix multiplication operator,...
Matrix multiplication with arrays In this question you will verify that the numpy.ndarray matrix multiplication operator, @, does a proper matrix multiplcation. To do this, first write a function, mat_mult(a1, a2) that creates a new 2d array of zeros, and using nested for loops, fill in the elements of the matrix multiplication, and return this new array. Then, write a second function, matrix_diff(b, c) that takes two arrays as arguments then generates and returns the following ratio: sqrt((∑(??,?−??,?)^2)/(∑(??,?+??,?)^2)) This function...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the time it takes to run the program with different data sizes. Languages: Python Task: Create matrix multiplication Input: Size of square matrix.   Size should be    250, 500, 1000, 1500, 2000 Internals: Explicitly or implicitly allocate sufficient memory to hold three NxN floating point Matrices, using a random number generator -- populate two of the Matrices, Multiply the two matrices, putting the result into the...
Prove that GL(2, Z2) is a group with matrix multiplication
Prove that GL(2, Z2) is a group with matrix multiplication
Things to note The mean time it takes a man to run a mile is known...
Things to note The mean time it takes a man to run a mile is known to be 25 minutes. A running company has developed a shoe that claim provides faster times time. Scientists tested the new shoe on a sample of 25 individuals. For this sample, the mean mile time was 20 mins. The population standard deviation for the mile is known to be 7 minutes. A level of significance of .01 is to be used to test if...
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation...
Recall the Matrix-Multiplication Algorithm for determining all-pairs distances in a graph. Provide a linear-time recursive implementation of the function void print_path(Matrix D[], int n, int i, int j); that takes as input the array of matrices D[1], D[2], . . . , D[n − 1] that have been computed by the algorithm, and prints the optimal path from vertex i to vertex j. Hint: for convenience you may use the notation Dr ij to denote the value D[r][i, j], i.e....
Write a Matlab function for a matrix that takes in a matrix in echelon form and...
Write a Matlab function for a matrix that takes in a matrix in echelon form and will return the row canonical form. The function cannot use rref, or any other matlab built in functions.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Show that SE(3) forms a group under the operation of matrix multiplication.
Show that SE(3) forms a group under the operation of matrix multiplication.
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the...
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the straightforward algorithm) to multiply a pair of 2 by 2 matrices. Explain why it is not possible to further reduce this number, 7, to anything less than 4, in multiplying a pair of 2 by 2 matrices.
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the...
Strassen’s algorithm for matrix multiplication relies on using 7 multiplications (instead of 8 as in the straightforward algorithm) to multiply a pair of 2 by 2 matrices. Explain why it is not possible to further reduce this number, 7, to anything less than 4, in multiplying a pair of 2 by 2 matrices.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT