]> vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/blob - ParallelMatrixSearch.c
before parallel
[ue-pp-squarematrixparallelisation.git] / ParallelMatrixSearch.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 = m_rows;
8
9 _Atomic int hv = 0;
10 int **matrix;
11
12
13 int main(void){
14 printf("1st --> Allocate memory\n");
15 clock_t t_t1; t_t1 = clock();
16 size_t rows_size = m_rows * sizeof(int*);
17 matrix = malloc(rows_size);
18 if(matrix == NULL){
19 perror("malloc failled");
20 return 1;
21 }
22 size_t data_size = (size_t)m_rows * m_cols * sizeof(int);
23 int *data;
24 if(posix_memalign((void**)&data, 64, data_size) != 0){
25 perror("not able to alocate memory");
26 free(matrix);
27 return 1;
28 }
29 t_t1 = clock() - t_t1;
30 double t1_ttaken = ((double)t_t1)/CLOCKS_PER_SEC;
31 printf(" %f sec\n",t1_ttaken);
32
33 printf("2nd --> Populate the matrix\n");
34 clock_t t_t2; t_t2 = clock();
35 for(int r = 0; r<m_rows;r++){
36 matrix[r] = data + r * m_cols;
37 }
38 for(int r = 0; r < m_rows; r++){
39 for(int c = 0; c < m_cols; c++){
40 matrix[r][c] = r*m_cols+c;
41 }
42 }
43 t_t2 = clock() - t_t2;
44 double t2_ttaken = ((double)t_t2)/CLOCKS_PER_SEC;
45 printf(" %f sec\n",t2_ttaken);
46
47
48 printf("3rd --> Search matrix\n");
49 clock_t t_t3; t_t3 = clock();
50 for(int r = 0; r < m_rows; r++){
51 for(int c = 0; c < m_cols; c++){
52 if(matrix[r][c] > hv){
53 hv = matrix[r][c];
54 }
55 }
56 }
57 t_t3 = clock() - t_t3;
58 double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC;
59 printf(" %f sec\n",t3_ttaken);
60
61 printf("Done\n The biggest value found is %d", hv);
62 free(data);
63 free(matrix);
64 return 0;
65 }
66
67