-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix-mult.c
92 lines (72 loc) · 1.67 KB
/
matrix-mult.c
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
/*
Simple example of a huge matrix multiplication
and speeding the process up as much as possible
by running the main tasks parallelly using threads
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define SIZE 1024
static double a[SIZE][SIZE];
static double b[SIZE][SIZE];
static double c[SIZE][SIZE];
void *initialize_row(void *arg) {
int row = *(int *)arg;
free(arg);
for (int j = 0; j < SIZE; j++) {
a[row][j] = 1.0;
b[row][j] = 1.0;
}
return NULL;
}
static void init_matrix() {
pthread_t threads[SIZE];
for (int i = 0; i < SIZE; i++) {
int *row = malloc(sizeof(int));
*row = i;
pthread_create(&threads[i], NULL, initialize_row, row);
}
for (int i = 0; i < SIZE; i++) {
pthread_join(threads[i], NULL);
}
}
static void
print_matrix(void)
{
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++)
printf(" %7.2f", c[i][j]);
printf("\n");
}
}
// using the pthread to make it run parallel
void *matmul_rows(void *arg) {
int row = *(int *)arg;
free(arg);
for (int j = 0; j < SIZE; j++) {
c[row][j] = 0.0;
for (int k = 0; k < SIZE; k++) {
c[row][j] += a[row][k] * b[k][j];
}
}
return NULL;
}
static void matmul_para() {
pthread_t threads[SIZE];
for (int i = 0; i < SIZE; i++) {
int *row = malloc(sizeof(int));
*row = i;
pthread_create(&threads[i], NULL, matmul_rows, row);
}
for (int i = 0; i < SIZE; i++) {
pthread_join(threads[i], NULL);
}
}
int
main(int argc, char **argv)
{
init_matrix();
matmul_para();
print_matrix();
}