]> vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/blob - ImprovedParallelMatricSearch.c
0df0ba1a6021e876f7e99027d1b109f04f4ffbcd
[ue-pp-squarematrixparallelisation.git] / ImprovedParallelMatricSearch.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4 #include <time.h>
5
6 const int m_rows = 40000;
7 const int m_cols = 40000;
8 const int threads_to_use = 8;
9
10 _Atomic int hv = 0;
11 int **matrix;
12
13 typedef struct{
14 int start;
15 int end;
16 } thread_args;
17
18 void* parallel_search_cols(void *arg){
19 thread_args *args = (thread_args *)arg;
20 int local_hv = 0;
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];
25 }
26 }
27 }
28
29 if(local_hv > hv){
30 hv = local_hv;
31 }
32 return NULL;
33 }
34
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));
38
39 if (threads == NULL || args == NULL) {
40 perror("Failed to allocate memory for threads");
41 exit(1);
42 }
43
44 int chunk_size = m_cols / num_threads;
45
46 for (int i = 0; i < num_threads; i++) {
47 args[i].start = i * chunk_size;
48 if (i == num_threads - 1) {
49 args[i].end = m_cols;
50 } else {
51 args[i].end = (i + 1) * chunk_size;
52 }
53 if (pthread_create(&threads[i], NULL, &parallel_search_cols, &args[i]) != 0) {
54 perror("Failed to create thread");
55 exit(1);
56 }
57 }
58
59 for (int i = 0; i < num_threads; i++) {
60 pthread_join(threads[i], NULL);
61 }
62 free(threads);
63 free(args);
64 }
65
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;
70 }
71 }
72 }
73
74 int main(void){
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);
79 if(matrix == NULL){
80 perror("malloc failled");
81 return 1;
82 }
83 size_t data_size = (size_t)m_rows * m_cols * sizeof(int);
84 int *data;
85 if(posix_memalign((void**)&data, 64, data_size) != 0){
86 perror("not able to alocate memory");
87 free(matrix);
88 return 1;
89 }
90 t_t1 = clock() - t_t1;
91 double t1_ttaken = ((double)t_t1)/CLOCKS_PER_SEC;
92 printf(" %f sec\n",t1_ttaken);
93
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;
98 }
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);
103
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);
110
111 printf("Done\n The biggest value found is %d \n", hv);
112 free(data);
113 free(matrix);
114 return 0;
115 }