Repository navigation
Expand file tree
/
Copy pathmultiply.c
More file actions
69 lines (50 loc) · 1.59 KB
/
Copy pathmultiply.c
File metadata and controls
69 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define M 3 // Rows in Matrix A
#define N 2 // Columns in Matrix A and Rows in Matrix B
#define P 3 // Columns in matrix B
int A[M][N] = { {1, 2}, {3, 4}, {5, 6} }; // Matrix A
int B[N][P] = { {7, 8, 9}, {10, 11, 12} }; // Matrix B
int result[M][P]; // Resultant Matrix
typedef struct {
int row;
int col;
} ThreadData;
void* multiply(void* arg){
ThreadData* data = (ThreadData*)arg;
int row = data->row;
int col = data->col;
result[row][col] = 0;
for(int k=0; k<N ;k++){
result[row][col] += A[row][k] * B[k][col]; // Perform multiplication and addition
}
pthread_exit(NULL); // Exit the thread
}
int main()
{
pthread_t resultant[M][P];
ThreadData thread_data[M][P]; // array to hold data for each thread
for(int i=0; i< M;i++){
for(int j=0; j<P;j++){
thread_data[i][j].row = i;
thread_data[i][j].col = j;
pthread_create(&resultant[i][j], NULL, multiply, (void*)&thread_data[i][j]);
}
}
// Wait for all threads to complete
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
pthread_join(resultant[i][j], NULL); // Join thread
}
}
// Print the resultant matrix C
printf("Resultant Matrix C:\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}