Question

Consider the product of two matrices A and B of dimensions 4 x 4 as shown below 100 00 I 1 01 23 I 1 0 2 46 X 0369 1 01 2 3 I

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

1)

#include <iostream>

using namespace std;

#define N 4

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];

}

}

}

int main()

{

int i, j;

int res[N][N];

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;

}

2)

#include <bits/stdc++.h>

using namespace std;

#define MAX 4

#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++;

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];

}

int main()

{

int i, j;

  

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;

}

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;

}

pthread_t threads[MAX_THREAD];

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

int* p;

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

}

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

pthread_join(threads[i], NULL);

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...
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