165869e8 |
1 | #include "matrix.h" |
2 | #include <stdlib.h> |
3 | #include <pthread.h> |
4 | |
5 | struct point { |
6 | |
7 | int r; |
8 | int c; |
9 | } pt; |
10 | |
11 | int gResult[MATRIX_SIZE][MATRIX_SIZE]; |
12 | |
13 | void mult_entry(void *ptr) { |
14 | |
15 | struct point *deref_pt = (struct point*) ptr; |
16 | |
17 | gResult[deref_pt->r][deref_pt->c] = matrix_multiply(deref_pt->r, deref_pt->c); |
18 | } |
19 | |
20 | int main(int argc, char* argv[]) |
21 | { |
22 | int row, col; |
23 | int left[MATRIX_SIZE][MATRIX_SIZE]; |
24 | int right[MATRIX_SIZE][MATRIX_SIZE]; |
25 | pthread_t threads[100]; |
26 | |
27 | for(row = 0; row < MATRIX_SIZE; ++row) |
28 | { |
29 | for(col = 0; col < MATRIX_SIZE; ++col) |
30 | { |
31 | left[row][col] = (rand() % 2) + 1; |
32 | right[row][col] = (rand() % 3) + 1; |
33 | } |
34 | } |
35 | |
36 | // this causes setleft and setright to operate in parallel |
37 | { |
38 | pthread_t init_thread; |
39 | pthread_create(&init_thread, NULL, |
40 | ({ |
41 | void* setleft(void* arg) { set_left(left); return NULL; } |
42 | setleft; |
43 | }), NULL); |
44 | |
45 | set_right(right); |
46 | pthread_join(init_thread, NULL); |
47 | } |
48 | |
49 | /* change this loop to execute on 100 different threads (each thread does 1 row) */ |
50 | for(row = 0; row < MATRIX_SIZE; ++row) |
51 | { |
52 | for(col = 0; col < MATRIX_SIZE; ++col) |
53 | { |
54 | |
55 | struct point curPoint = { row, col }; |
56 | |
57 | pthread_create(&threads[(row * 10) + col], NULL, mult_entry, (void *) &curPoint); |
58 | } |
59 | } |
60 | /* make the above line run in parallel */ |
61 | |
62 | print_matrix(gResult); |
63 | return EXIT_SUCCESS; |
64 | } |
65 | |
66 | |