Question: Modify the program to do 3 x 3 matrix multiplication: #include #include #include using namespace std; int A[3] [3] = {{1,2,5},{3,4,6},{6,7,8}}; int B[3] [3] =
Modify the program to do 3 x 3 matrix multiplication:
#include #include #include using namespace std; int A[3] [3] = {{1,2,5},{3,4,6},{6,7,8}}; int B[3] [3] = {{5,6},{7,8},{3,5,8}}; int C[3] [3]; struct Position{ int row; int col; }; void *CalculateElement(void *pos){ Position *p = (Position *)pos; C[p->row][p->col] = 0; for(int i = 0; i < 3; i++){ C[p->row][p->col] += A[p->row][i]*B[i][p->col]; } pthread_exit(NULL); } const int NUM_THREADS = 4; int main() { pthread_t threads[NUM_THREADS]; for(int i = 0; i < NUM_THREADS; i++){ Position *p = new Position; p->row = i/3; p->col = i%3; pthread_create(&threads[i], NULL, CalculateElement, (void *)p); } for(int i = 0; i < NUM_THREADS; i++){ pthread_join (threads[i], NULL); } cout << "The result matrix is: "; for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ cout << C[i][j] << "\t"; } cout << endl; } }