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