Could you help me to solve this assignment, please? 1 3. Description of programm
ID: 3888695 • Letter: C
Question
Could you help me to solve this assignment, please?
13. Description of programming
Write a C/C++ language program to implement multithreaded matrix multiplication using Pthreads.
C i, j = Kn=1 A i, n * B n, j
• Create a separate worker thread to compute each row of the result matrix, instead of a thread for each element.
• Do not initialize the contents of the A and B matrices statically. The A and B matrices will be initialized by reading data from an input file (see notes below).
• Be able to process multiple sets of input matrices by continuing to read data in the specified format (described below). If any other character than an integer including negative number is specified, output an error message and terminate the program with a non-zero return code.
• Each set of data processed must be labeled to indicate which set of data is being output (e.g.,Matrix A,Matrix B, etc).
• Maximum size of Matrices can be 50000x50000
2. Input
The input file will be an ASCII file containing numbers that define the dimensions and contents of the A and B matrices for which a matrix product is to be computed. The first line of the input file will contain two numbers specifying the dimensions of the A matrix (M x K). Following that will be M lines, each with K numbers, representing the elements of matrix A. Next will be a line with two numbers specifying the dimensions of the B matrix (K x N). And following that will be K lines, each with N numbers, representing the elements of matrix B. For example, suppose that matrix A has 4 rows and 3 columns and matrix B has 3 rows and 4 columns, as follows:
The lines in the input file would be as follows:
4 3
2 1 3
0 -1 -2
5 1 -1
4 5 8
3 4
3 2 1 0
0 4 2 -1
-1 3 2 4
2. Output
Program should print the contents of matrix A and matrix B. It should then print lines showing the thread ID numbers for the worker threads it creates. Then it should print the contents of the result matrix C and total execution time. The output from program for the above input should look similar to the following:
Matrix A: 2 1 3
0 -1 -2
5 1 -1
4 5 8
Matrix B: 3 2 1 0
0 4 2 -1
-1 3 2 4
Created worker thread 15823744 for row 0 Created worker thread 26375040 for row 1 Created worker thread 35823744 for row 2 Created worker thread 46375040 for row 3
Matrix C = A x B: 3 17 10 11
2 -10 -6 -7
16 11 5 -5
4 52 30 27
Total execution time using 5 threads is 0.001 ms.
Thank you
Explanation / Answer
#include <iostream>
#include <stdio.h>
#include <pthread.h>
using namespace std;
const int DIM = 720;
const int num_of_thr = 4;
int matrix_A[DIM][DIM];
int matrix_B[DIM][DIM];
int c[DIM][DIM];
struct v
{
int i;
int j;
};
//worker thread
void* matrix_multi(void* data)
{
for(int i = 0; i < DIM; i++)
{
for(int j = 0; j < DIM; j++)
{
c[i][j] = 0;
for(int k = 0; k < DIM; k++)
{
c[i][j] += matrix_A[i][k] * matrix_B[k][j];
}
}
}
pthread_exit(0);
}
int main()
{
pthread_t thr_id[DIM][DIM];
pthread_attr_t thr_attr;
pthread_attr_init(&thr_attr);
for(int i = 0; i < DIM; i++)
{
for(int j = 0; j < DIM; j++)
{
matrix_A[i][j]= i + j;
matrix_B[i][j] = i + 3;
}
}
for(int i = 0; i < num_of_thr/2; i++)
{
for(int j = 0; j < num_of_thr/2; j++)
{
struct v *data = (struct v *) malloc(sizeof(struct v));
data->i = i;
data->j = j;
pthread_create(&thr_id[i][j],NULL,matrix_multi, &data);
}
}
for(int i = 0; i < num_of_thr/2; i++)
{
for(int j = 0; j < num_of_thr/2; j++)
{
pthread_join(thr_id[i][j],NULL);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.