Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Note:I Want to use a POSIX Threads in the following code as mentioned in the Question *You will implement a multithreaded version of your work

Note:I Want to use a POSIX Threads in the following code as mentioned in the Question

*You will implement a multithreaded version of your work in phase 1 using POSIX Threads. *You will compute each number in the output matrix with a separate thread. *You will display the contents of the output matrix whenever a thread sets a value in the output matrix. Initially, all the values in the output matrix will be equal to -1.

CODE:

#include

using namespace std;

// multiplies corresponding elements of matrices a and b int correspondingMultiplication(vector> a, vector> b) { // both a and b should have same dimensions int result = 0; for(int row = 0; row < a.size(); ++row) { for(int col = 0; col < a[0].size(); ++col) { result += a[row][col] * b[row][col]; } } return result; }

// generates an overlapping region for matrix M2 in matrix M1 vector> overlappingMatrix(vector> m1, vector> m2, int row, int col) { int len1 = m1.size(), brd1 = m1[0].size(); int len2 = m2.size(), brd2 = m2[0].size(); vector> mat(len2, vector(brd2));

for(int i = 0; i < len2; ++i) { for(int j = 0; j < brd2; ++j) { if(row + i < len1 && col + j < brd1) mat[i][j] = m1[row + i][col + j]; else mat[i][j] = 0; } } return mat; }

int main() { // M1 and M2 given in the sample vector> m1 = {{17, 11, 17, 1, 14, 17, 16}, {19, 20, 3, 17, 4, 14, 16}, {3, 20, 9, 19, 15, 7, 4}, {19, 4, 19, 14, 1, 20, 10}, {13, 20, 16, 16, 6, 1, 9}, {2, 20, 20, 15, 1, 9, 13}, {6, 10, 14, 8, 2, 8, 15}}; vector> m2 = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};

int len1 = m1.size(), brd1 = m1[0].size(); int len2 = m2.size(), brd2 = m2[0].size(); vector> res(len1, vector(brd1));

for(int row = 0; row < len1; ++row) { for(int col = 0; col < brd1; ++col) { vector> tmp = overlappingMatrix(m1, m2, row, col); res[row][col] = correspondingMultiplication(tmp, m2); } } // verify the result with the sample output given cout << "Resultant Matrix : "; for(int row = 0; row < len1; ++row) { for(int col = 0; col < brd1; ++col) cout << res[row][col] << " "; cout << " "; }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions

Question

Persuading Your Audience Strategies for

Answered: 1 week ago