]>
vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/blob - ParallelMatrixSearch.c
538af2481c9a069629d067ec0a02485210e5282d
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
){
31 printf("1st --> Allocate memory\n");
32 clock_t t_t1
; t_t1
= clock();
33 size_t rows_size
= m_rows
* sizeof(int*);
34 matrix
= malloc(rows_size
);
36 perror("malloc failled");
39 size_t data_size
= (size_t)m_rows
* m_cols
* sizeof(int);
41 if(posix_memalign((void**)&data
, 64, data_size
) != 0){
42 perror("not able to alocate memory");
46 t_t1
= clock() - t_t1
;
47 double t1_ttaken
= ((double)t_t1
)/CLOCKS_PER_SEC
;
48 printf(" %f sec\n",t1_ttaken
);
50 printf("2nd --> Populate the matrix\n");
51 clock_t t_t2
; t_t2
= clock();
52 for(int r
= 0; r
<m_rows
;r
++){
53 matrix
[r
] = data
+ r
* m_cols
;
55 for(int r
= 0; r
< m_rows
; r
++){
56 for(int c
= 0; c
< m_cols
; c
++){
57 matrix
[r
][c
] = r
*m_cols
+c
;
60 t_t2
= clock() - t_t2
;
61 double t2_ttaken
= ((double)t_t2
)/CLOCKS_PER_SEC
;
62 printf(" %f sec\n",t2_ttaken
);
64 printf("3rd --> Search matrix\n");
65 clock_t t_t3
; t_t3
= clock();
68 thread_args
*args
= malloc(sizeof(thread_args
));
70 args
->end
= abs(m_cols
/3);
71 pthread_create(&t1
,NULL
,¶llel_search_cols
,args
);
74 thread_args
*args2
= malloc(sizeof(thread_args
));
75 args2
->start
= abs(m_cols
/3);
76 args2
->end
= m_cols
-(m_cols
/3);
77 pthread_create(&t2
,NULL
,¶llel_search_cols
,args2
);
80 thread_args
*args3
= malloc(sizeof(thread_args
));
81 args3
->start
= m_cols
-abs((m_cols
/3));
83 pthread_create(&t3
,NULL
,¶llel_search_cols
,args3
);
85 pthread_join(t1
,NULL
);
86 pthread_join(t2
,NULL
);
87 pthread_join(t3
,NULL
);
89 t_t3
= clock() - t_t3
;
90 double t3_ttaken
= ((double)t_t3
)/CLOCKS_PER_SEC
;
91 printf(" %f sec\n",t3_ttaken
);
93 printf("Done\n The biggest value found is %d", hv
);