]>
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
= 4;
18 void* parallel_populate_matrix(void *arg
){
19 thread_args
*args
= (thread_args
*)arg
;
20 for(int r
= 0; r
< m_rows
; r
++){
21 for(int c
= args
->start
; c
< args
->end
;c
++){
22 matrix
[r
][c
] = r
*m_cols
+c
;
28 void* parallel_search_cols(void *arg
){
29 thread_args
*args
= (thread_args
*)arg
;
31 for(int r
= 0; r
< m_rows
; r
++){
32 for(int c
= args
->start
; c
< args
->end
; c
++){
33 if(matrix
[r
][c
] > local_hv
){
34 local_hv
= matrix
[r
][c
];
45 void run_parallel_search(int num_threads
) {
46 pthread_t
*threads
= malloc(num_threads
* sizeof(pthread_t
));
47 thread_args
*args
= malloc(num_threads
* sizeof(thread_args
));
49 if (threads
== NULL
|| args
== NULL
) {
50 perror("Failed to allocate memory for threads");
54 int chunk_size
= m_cols
/ num_threads
;
56 for (int i
= 0; i
< num_threads
; i
++) {
57 args
[i
].start
= i
* chunk_size
;
58 if (i
== num_threads
- 1) {
61 args
[i
].end
= (i
+ 1) * chunk_size
;
63 if (pthread_create(&threads
[i
], NULL
, ¶llel_search_cols
, &args
[i
]) != 0) {
64 perror("Failed to create thread");
69 for (int i
= 0; i
< num_threads
; i
++) {
70 pthread_join(threads
[i
], NULL
);
76 void init_parallel_matrix(int *data
,int num_threads
){
77 pthread_t
*threads
= malloc(num_threads
* sizeof(pthread_t
));
78 thread_args
*args
= malloc(num_threads
* sizeof(thread_args
));
79 if (threads
== NULL
|| args
== NULL
) {
80 perror("Failed to allocate memory for threads");
84 for(int r
= 0; r
<m_rows
;r
++){
85 matrix
[r
] = data
+ r
* m_cols
;
88 int chunk_size
= m_cols
/ num_threads
;
89 for (int i
= 0; i
< num_threads
; i
++) {
90 args
[i
].start
= i
* chunk_size
;
91 if (i
== num_threads
- 1) {
94 args
[i
].end
= (i
+ 1) * chunk_size
;
96 if (pthread_create(&threads
[i
], NULL
, ¶llel_populate_matrix
, &args
[i
]) != 0) {
97 perror("Failed to create thread");
102 for (int i
= 0; i
< num_threads
; i
++) {
103 pthread_join(threads
[i
], NULL
);
111 printf("1st --> Allocate memory\n");
112 clock_t t_t1
; t_t1
= clock();
113 size_t rows_size
= m_rows
* sizeof(int*);
114 matrix
= malloc(rows_size
);
116 perror("malloc failled");
119 size_t data_size
= (size_t)m_rows
* m_cols
* sizeof(int);
121 if(posix_memalign((void**)&data
, 64, data_size
) != 0){
122 perror("not able to alocate memory");
126 t_t1
= clock() - t_t1
;
127 double t1_ttaken
= ((double)t_t1
)/CLOCKS_PER_SEC
;
128 printf(" %f sec\n",t1_ttaken
);
130 printf("2nd --> Populate the matrix\n");
131 clock_t t_t2
; t_t2
= clock();
132 init_parallel_matrix(data
, threads_to_use
);
133 t_t2
= clock() - t_t2
;
134 double t2_ttaken
= ((double)t_t2
)/CLOCKS_PER_SEC
;
135 printf(" %f sec\n",t2_ttaken
);
137 printf("3rd --> Search matrix\n");
138 clock_t t_t3
; t_t3
= clock();
139 run_parallel_search(threads_to_use
);
140 t_t3
= clock() - t_t3
;
141 double t3_ttaken
= ((double)t_t3
)/CLOCKS_PER_SEC
;
142 printf(" %f sec\n",t3_ttaken
);
144 printf("Done\n The biggest value found is %d \n", hv
);