-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMatrixAddition.c
More file actions
86 lines (66 loc) · 1.86 KB
/
MatrixAddition.c
File metadata and controls
86 lines (66 loc) · 1.86 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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
int M,K;
int A[100][100],B[100][100],C[100][100];
struct v
{
int i; /* row */
int j; /* column */
};
void *runner(void *param); /* the thread */
int main(int argc, char *argv[])
{
printf("enter the numbers of rows: ");
scanf("%d",&M);
printf("enter the numbers of columns: ");
scanf("%d",&K);
int i,j;
printf("enter the first matrix:\n");
for (i = 0; i < M; i++){
for (j = 0; j < K; j++){
scanf("%d",&A[i][j]);
}}
printf("enter the second matrix:\n");
for (i = 0; i < M; i++){
for (j = 0; j < K; j++){
scanf("%d",&B[i][j]);
}}
for (i = 0; i < M; i++)
{
for (j = 0; j < K; 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);
}
}
//Print out the resulting matrix
printf("The Addition of two matrix is:\n");
for (i = 0; i < M; i++)
{
for (j = 0; j < K; j++)
{
printf("%d ", C[i][j]);
}
printf("\n");
}
}
//The thread will begin control in this function
void *runner(void *param)
{
struct v *data = param; // the structure that holds our data
C[data->i][data->j]= A[data->i][data->j] + B[data->i][data->j];
//Exit the thread
pthread_exit(0);
}