]>
vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/blob - ParallelMatrixSearch.c
6 const int m_rows
= 40000;
7 const int m_cols
= m_rows
;
17 void* parallel_search_cols(void *arg
){
18 thread_args
*args
= (thread_args
*)arg
;
19 for(int r
= 0; r
< m_rows
; r
++){
20 for(int c
= args
->start
; c
< args
->end
; c
++){
21 if(matrix
[r
][c
] > hv
){
29 void run_parallel_search(int num_threads
) {
30 pthread_t
*threads
= malloc(num_threads
* sizeof(pthread_t
));
31 thread_args
*args
= malloc(num_threads
* sizeof(thread_args
));
33 if (threads
== NULL
|| args
== NULL
) {
34 perror("Failed to allocate memory for threads");
38 int chunk_size
= m_cols
/ num_threads
;
40 for (int i
= 0; i
< num_threads
; i
++) {
41 args
[i
].start
= i
* chunk_size
;
42 if (i
== num_threads
- 1) {
45 args
[i
].end
= (i
+ 1) * chunk_size
;
47 if (pthread_create(&threads
[i
], NULL
, ¶llel_search_cols
, &args
[i
]) != 0) {
48 perror("Failed to create thread");
53 for (int i
= 0; i
< num_threads
; i
++) {
54 pthread_join(threads
[i
], NULL
);
61 printf("1st --> Allocate memory\n");
62 clock_t t_t1
; t_t1
= clock();
63 size_t rows_size
= m_rows
* sizeof(int*);
64 matrix
= malloc(rows_size
);
66 perror("malloc failled");
69 size_t data_size
= (size_t)m_rows
* m_cols
* sizeof(int);
71 if(posix_memalign((void**)&data
, 64, data_size
) != 0){
72 perror("not able to alocate memory");
76 t_t1
= clock() - t_t1
;
77 double t1_ttaken
= ((double)t_t1
)/CLOCKS_PER_SEC
;
78 printf(" %f sec\n",t1_ttaken
);
80 printf("2nd --> Populate the matrix\n");
81 clock_t t_t2
; t_t2
= clock();
82 for(int r
= 0; r
<m_rows
;r
++){
83 matrix
[r
] = data
+ r
* m_cols
;
85 for(int r
= 0; r
< m_rows
; r
++){
86 for(int c
= 0; c
< m_cols
; c
++){
87 matrix
[r
][c
] = r
*m_cols
+c
;
90 t_t2
= clock() - t_t2
;
91 double t2_ttaken
= ((double)t_t2
)/CLOCKS_PER_SEC
;
92 printf(" %f sec\n",t2_ttaken
);
94 printf("3rd --> Search matrix\n");
95 clock_t t_t3
; t_t3
= clock();
96 int threads_to_use
= 200;
97 run_parallel_search(threads_to_use
);
98 t_t3
= clock() - t_t3
;
99 double t3_ttaken
= ((double)t_t3
)/CLOCKS_PER_SEC
;
100 printf(" %f sec\n",t3_ttaken
);
102 printf("Done\n The biggest value found is %d", hv
);