Question

Consider the product of two matrices A and B of dimensions 4 x 4 as shown below. 0 1 2 3 11 2 3 4 | 0 1 2 3 I 1 0 2 46X2 3 4

Matrix A 0 2 4 6 Matrix B 0 1 2 3 1 2 3 4

Thread 0 starting. Thread 0 calculating element C [0, 01-0 Thread 1 starting. Thread 2 starting. Thread 0 calculating element

Consider the product of two matrices A and B of dimensions 4 x 4 as shown below. 0 1 2 3 11 2 3 4 | 0 1 2 3 I 1 0 2 46X2 3 4 5 I 0369 0 0 0 0 I I 14 20 26 32 I - 28 40 52 64 I I 42 60 78 96 I 3 456 1. Write a program to find the product of two matrices. The program should display the two input matrices and the output product matrix. Modify the program by using four threads to perform the matrix product concurrently. Decide on how to distribute the task over the four threads. Each thread will display each element of the product matrix immediately after it is calculated in order to observe the concurrency between the threads. The expected output of your program should look somehow similar to the sample display below. 2.
Matrix A 0 2 4 6 Matrix B 0 1 2 3 1 2 3 4
Thread 0 starting. Thread 0 calculating element C [0, 01-0 Thread 1 starting. Thread 2 starting. Thread 0 calculating element C[0, 11 0 Thread 1 calculating element C[1, 0]-14 Thread 3 starting. Thread 2 calculating element C[2, 0]-28 Thread 0 calculating element C[0, 21-0 Thread 1 calculating element C[1, 1]-20. Thread 3 calculating element C[3, 01-42. Thread 2 calculating element C[2, 1]-40. Thread 0 calculating element C[0, 3]-0. Thread 1 calculating element C[l, 2]-26 Thread 3 calculating element C[3, 1]-60 Thread 2 calculating element C[2, 2]-52 Thread 1 calculating element C[1, 31-3.2 Thread 3 calculating element C[3, 21-78. Thread 2 calculating element C[2, 31-64. Thread 3 calculating element C[3, 3]-96. Product of A and B 14 20 26 32 28 40 52 64 42 60 78 96
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since you have not mentioned the language of your preference, I am providing the code in C++.

CODE

// C++ program to multiply

// two square matrices.

#include <iostream>

using namespace std;

#define N 4

// This function multiplies

// mat1[][] and mat2[][], and

// stores the result in res[][]

void multiply(int mat1[][N],

      int mat2[][N],

      int res[][N])

{

  int i, j, k;

  for (i = 0; i < N; i++)

  {

    for (j = 0; j < N; j++)

    {

      res[i][j] = 0;

      for (k = 0; k < N; k++)

        res[i][j] += mat1[i][k] *

              mat2[k][j];

    }

  }

}

// Driver Code

int main()

{

  int i, j;

  int res[N][N]; // To store result

  int mat1[N][N] = {{0, 0, 0, 0},

          {0, 1, 2, 3},

          {0, 2, 4, 6},

          {0, 3, 6, 9}};

  int mat2[N][N] = {{0, 1, 2, 3},

          {1, 2, 3, 4},

          {2, 3, 4, 5},

          {3, 4, 5, 6}};

cout << "Matrix A is \n";

  for (i = 0; i < N; i++)

  {

    for (j = 0; j < N; j++)

    cout << mat1[i][j] << " ";

    cout << "\n";

  }

cout << "\nMatrix B is \n";

  for (i = 0; i < N; i++)

  {

    for (j = 0; j < N; j++)

    cout << mat2[i][j] << " ";

    cout << "\n";

  }

  multiply(mat1, mat2, res);

  cout << "\nResult matrix is \n";

  for (i = 0; i < N; i++)

  {

    for (j = 0; j < N; j++)

    cout << res[i][j] << " ";

    cout << "\n";

  }

  return 0;

}

clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE_700/final Matrix A is 012 3 0 246 0369 Matrix B is 0123 1 2 3 4 2 3 4 5 3

2)

#include <bits/stdc++.h>

using namespace std;

// maximum size of matrix

#define MAX 4

// maximum number of threads

#define MAX_THREAD 4

int matA[MAX][MAX] = {{0, 0, 0, 0},

          {0, 1, 2, 3},

          {0, 2, 4, 6},

          {0, 3, 6, 9}};

int matB[MAX][MAX] = {{0, 1, 2, 3},

          {1, 2, 3, 4},

          {2, 3, 4, 5},

          {3, 4, 5, 6}};

int matC[MAX][MAX];

int step_i = 0;

void* multi(void* arg)

{

int core = step_i++;

// Each thread computes 1/4th of matrix multiplication

for (int i = core * MAX / 4; i < (core + 1) * MAX / 4; i++)

for (int j = 0; j < MAX; j++)

for (int k = 0; k < MAX; k++)

matC[i][j] += matA[i][k] * matB[k][j];

}

// Driver Code

int main()

{

  int i, j;

  

// Displaying matA

cout << endl

<< "Matrix A" << endl;

for (int i = 0; i < MAX; i++) {

for (int j = 0; j < MAX; j++)

cout << matA[i][j] << " ";

cout << endl;

}

// Displaying matB

cout << endl

<< "Matrix B" << endl;

for (int i = 0; i < MAX; i++) {

for (int j = 0; j < MAX; j++)

cout << matB[i][j] << " ";

cout << endl;

}

// declaring four threads

pthread_t threads[MAX_THREAD];

// Creating four threads, each evaluating its own part

for (int i = 0; i < MAX_THREAD; i++) {

int* p;

pthread_create(&threads[i], NULL, multi, (void*)(p));

}

// joining and waiting for all threads to complete

for (int i = 0; i < MAX_THREAD; i++)

pthread_join(threads[i], NULL);

// Displaying the result matrix

cout << endl

<< "Multiplication of A and B" << endl;

for (int i = 0; i < MAX; i++) {

for (int j = 0; j < MAX; j++)

cout << matC[i][j] << " ";

cout << endl;

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Consider the product of two matrices A and B of dimensions 4 x 4 as shown below. 0 1 2 3 11 2 3 4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT