-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixOperation.c
More file actions
124 lines (104 loc) · 3.01 KB
/
Copy pathmatrixOperation.c
File metadata and controls
124 lines (104 loc) · 3.01 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define M 3 // Rows in Matrix A
#define N 3 // 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}, {7, 8, 9} }; // Matrix A
int B[N][P] = { {10, 11, 12}, {13, 14, 15} , {16, 17, 18}}; // Matrix B
int result[M][P]; // Resultant Matrix
int add_result[M][P]; // Addition Result
int sub_result[M][P]; // Subtraction Result
typedef struct {
int row;
int col;
} ThreadData;
// Matrix multiplication
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];
}
pthread_exit(NULL);
}
// Matrix addition
void* add(void* arg) {
ThreadData* data = (ThreadData*)arg;
int row = data->row;
int col = data->col;
add_result[row][col] = A[row][col] + B[row][col];
pthread_exit(NULL);
}
// Matrix subtraction
void* subtract(void* arg) {
ThreadData* data = (ThreadData*)arg;
int row = data->row;
int col = data->col;
sub_result[row][col] = A[row][col] - B[row][col];
pthread_exit(NULL);
}
int main() {
pthread_t threads[M][P];
ThreadData thread_data[M][P];
// Perform matrix multiplication
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(&threads[i][j], NULL, multiply, (void*)&thread_data[i][j]);
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
pthread_join(threads[i][j], NULL);
}
}
// Perform matrix addition
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
pthread_create(&threads[i][j], NULL, add, (void*)&thread_data[i][j]);
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
pthread_join(threads[i][j], NULL);
}
}
// Perform matrix subtraction
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
pthread_create(&threads[i][j], NULL, subtract, (void*)&thread_data[i][j]);
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
pthread_join(threads[i][j], NULL);
}
}
// Print the resultant matrices
printf("Resultant Matrix (Multiplication):\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
printf("\nResultant Matrix (Addition):\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
printf("%d ", add_result[i][j]);
}
printf("\n");
}
printf("\nResultant Matrix (Subtraction):\n");
for (int i = 0; i < M; i++) {
for (int j = 0; j < P; j++) {
printf("%d ", sub_result[i][j]);
}
printf("\n");
}
return 0;
}