1. Goal of this programming assignment The primary goal of this assignment is to
ID: 3889205 • Letter: 1
Question
1. Goal of this programming assignment The primary goal of this assignment is to understand and gain some familiarity with pthread libraries in Linix Environment.
2. Requirements (1) Programming language: You have to use either C or C++ to develop your program.
3. Description of programming Write a C/C++ language program to implement multithreaded matrix multiplication using Pthreads.
- 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 10000x10000
3.1 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
3.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
Explanation / Answer
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define M 3
#define K 2
#define N 3
#define NUM_THREADS 10
int A [M][K] = { {1,4}, {2,5}, {3,6} };
int B [K][N] = { {8,7,6}, {5,4,3} };
int C [M][N];
struct v {
int i; /* row */
int j; /* column */
};
void *runner(void *param); /* the thread */
int main(int argc, char *argv[]) {
int i,j, count = 0;
for(i = 0; i < M; i++) {
for(j = 0; j < N; j++) {
//Assign a row and column for each thread
struct v *data = (struct v *) malloc(sizeof(struct v));
data->i = i;
data->j = j;
/* Now create the thread passing it data as a parameter */
pthread_t tid; //Thread ID
pthread_attr_t attr; //Set of thread attributes
//Get the default attributes
pthread_attr_init(&attr);
//Create the thread
pthread_create(&tid,&attr,runner,data);
//Make sure the parent waits for all thread to complete
pthread_join(tid, NULL);
count++;
}
}
//Print out the resulting matrix
for(i = 0; i < M; i++) {
for(j = 0; j < N; j++) {
printf("%d ", C[i][j]);
}
printf(" ");
}
}
//The thread will begin control in this function
void *runner(void *param) {
struct v *data = param; // the structure that holds our data
int n, sum = 0; //the counter and sum
//Row multiplied by column
for(n = 0; n< K; n++){
sum += A[data->i][n] * B[n][data->j];
}
//assign the sum to its coordinate
C[data->i][data->j] = sum;
//Exit the thread
pthread_exit(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.