]>
vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/blob - ImprovedParallelMatricSearch.c
6 const int m_rows
= 40000;
7 const int m_cols
= 40000;
8 const int threads_to_use
= 8;
18 void* parallel_search_cols(void *arg
){
19 thread_args
*args
= (thread_args
*)arg
;
21 for(int r
= 0; r
< m_rows
; r
++){
22 for(int c
= args
->start
; c
< args
->end
; c
++){
23 if(matrix
[r
][c
] > local_hv
){
24 local_hv
= matrix
[r
][c
];
35 void run_parallel_search(int num_threads
) {
36 pthread_t
*threads
= malloc(num_threads
* sizeof(pthread_t
));
37 thread_args
*args
= malloc(num_threads
* sizeof(thread_args
));
39 if (threads
== NULL
|| args
== NULL
) {
40 perror("Failed to allocate memory for threads");
44 int chunk_size
= m_cols
/ num_threads
;
46 for (int i
= 0; i
< num_threads
; i
++) {
47 args
[i
].start
= i
* chunk_size
;
48 if (i
== num_threads
- 1) {
51 args
[i
].end
= (i
+ 1) * chunk_size
;
53 if (pthread_create(&threads
[i
], NULL
, ¶llel_search_cols
, &args
[i
]) != 0) {
54 perror("Failed to create thread");
59 for (int i
= 0; i
< num_threads
; i
++) {
60 pthread_join(threads
[i
], NULL
);
66 void init_parallel_matrix(int num_threads
){
67 for(int r
= 0; r
< m_rows
; r
++){
68 for(int c
= 0; c
< m_cols
; c
++){
69 matrix
[r
][c
] = r
*m_cols
+c
;
75 printf("1st --> Allocate memory\n");
76 clock_t t_t1
; t_t1
= clock();
77 size_t rows_size
= m_rows
* sizeof(int*);
78 matrix
= malloc(rows_size
);
80 perror("malloc failled");
83 size_t data_size
= (size_t)m_rows
* m_cols
* sizeof(int);
85 if(posix_memalign((void**)&data
, 64, data_size
) != 0){
86 perror("not able to alocate memory");
90 t_t1
= clock() - t_t1
;
91 double t1_ttaken
= ((double)t_t1
)/CLOCKS_PER_SEC
;
92 printf(" %f sec\n",t1_ttaken
);
94 printf("2nd --> Populate the matrix\n");
95 clock_t t_t2
; t_t2
= clock();
96 for(int r
= 0; r
<m_rows
;r
++){
97 matrix
[r
] = data
+ r
* m_cols
;
99 init_parallel_matrix(threads_to_use
);
100 t_t2
= clock() - t_t2
;
101 double t2_ttaken
= ((double)t_t2
)/CLOCKS_PER_SEC
;
102 printf(" %f sec\n",t2_ttaken
);
104 printf("3rd --> Search matrix\n");
105 clock_t t_t3
; t_t3
= clock();
106 run_parallel_search(threads_to_use
);
107 t_t3
= clock() - t_t3
;
108 double t3_ttaken
= ((double)t_t3
)/CLOCKS_PER_SEC
;
109 printf(" %f sec\n",t3_ttaken
);
111 printf("Done\n The biggest value found is %d \n", hv
);