]> vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/commitdiff
before parallel
authorVGoncalo <vitor.goncalo.costa@gmail.com>
Thu, 12 Mar 2026 22:32:21 +0000 (22:32 +0000)
committerVGoncalo <vitor.goncalo.costa@gmail.com>
Thu, 12 Mar 2026 22:32:21 +0000 (22:32 +0000)
BetterSequencialMatrixSearch.c [new file with mode: 0644]
ParallelMatrixSearch.c [new file with mode: 0644]
SequencialMatrixSearch.c [new file with mode: 0644]

diff --git a/BetterSequencialMatrixSearch.c b/BetterSequencialMatrixSearch.c
new file mode 100644 (file)
index 0000000..0f38c7e
--- /dev/null
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+const int m_rows = 40000;
+const int m_cols = m_rows;
+
+int hv = 0;
+int **matrix;
+
+
+int main(void){
+  printf("1st --> Allocate aligned memory\n");
+  clock_t t_t1; t_t1 = clock();
+  size_t rows_size = m_rows * sizeof(int*);
+  matrix = malloc(rows_size);
+  if(matrix == NULL){
+    perror("malloc failled");
+    return 1;
+  }
+  size_t data_size = (size_t)m_rows * m_cols * sizeof(int);
+  int *data;
+  if(posix_memalign((void**)&data, 64, data_size) != 0){
+    perror("not able to alocate memory");
+    free(matrix);
+    return 1;
+  }
+  t_t1 = clock() - t_t1;
+  double t1_ttaken = ((double)t_t1)/CLOCKS_PER_SEC;
+  printf(" %f sec \n",t1_ttaken); 
+
+  printf("2nd --> Populate the matrix\n");
+  clock_t t_t2; t_t2 = clock();
+  for(int r = 0; r<m_rows;r++){
+    matrix[r] = data + r * m_cols;
+  }
+  for(int r = 0; r < m_rows; r++){  
+    for(int c = 0; c < m_cols; c++){
+      matrix[r][c] = r*m_cols+c;
+    }
+  }
+  t_t2 = clock() - t_t2;
+  double t2_ttaken = ((double)t_t2)/CLOCKS_PER_SEC;
+  printf(" %f sec\n ",t2_ttaken);
+
+  printf("3rd --> Search matrix \n");
+  clock_t t_t3; t_t3 = clock();
+  for(int r = 0; r < m_rows; r++){
+    for(int c = 0; c < m_cols; c++){
+      if(matrix[r][c] > hv){
+        hv = matrix[r][c];
+      }
+    }
+  }
+  t_t3 = clock() - t_t3;
+  double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC;
+  printf(" %f sec\n",t3_ttaken);
+
+  printf("Done\n The biggest value found is  %d", hv); 
+  free(matrix);
+  free(data);
+  return 0;
+}
+
+
diff --git a/ParallelMatrixSearch.c b/ParallelMatrixSearch.c
new file mode 100644 (file)
index 0000000..2a3c499
--- /dev/null
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <time.h>
+
+const int m_rows = 40000;
+const int m_cols = m_rows;
+
+_Atomic int hv = 0;
+int **matrix;
+
+
+int main(void){
+  printf("1st --> Allocate memory\n");
+  clock_t t_t1; t_t1 = clock();
+  size_t rows_size = m_rows * sizeof(int*);
+  matrix = malloc(rows_size);
+  if(matrix == NULL){
+    perror("malloc failled");
+    return 1;
+  }
+  size_t data_size = (size_t)m_rows * m_cols * sizeof(int);
+  int *data;
+  if(posix_memalign((void**)&data, 64, data_size) != 0){
+    perror("not able to alocate memory");
+    free(matrix);
+    return 1;
+  }
+  t_t1 = clock() - t_t1;
+  double t1_ttaken = ((double)t_t1)/CLOCKS_PER_SEC;
+  printf(" %f sec\n",t1_ttaken);
+
+  printf("2nd --> Populate the matrix\n");
+  clock_t t_t2; t_t2 = clock();
+  for(int r = 0; r<m_rows;r++){
+    matrix[r] = data + r * m_cols;
+  }
+  for(int r = 0; r < m_rows; r++){  
+    for(int c = 0; c < m_cols; c++){
+      matrix[r][c] = r*m_cols+c;
+    }
+  }
+  t_t2 = clock() - t_t2;
+  double t2_ttaken = ((double)t_t2)/CLOCKS_PER_SEC; 
+  printf(" %f sec\n",t2_ttaken);  
+
+  
+  printf("3rd --> Search matrix\n");
+  clock_t t_t3; t_t3 = clock();
+  for(int r = 0; r < m_rows; r++){
+    for(int c = 0; c < m_cols; c++){
+      if(matrix[r][c] > hv){
+        hv = matrix[r][c];
+      }
+    }
+  }
+  t_t3 = clock() - t_t3;
+  double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC;
+  printf(" %f sec\n",t3_ttaken);
+  printf("Done\n The biggest value found is  %d", hv); 
+  free(data);
+  free(matrix);
+  return 0;
+}
+
+
diff --git a/SequencialMatrixSearch.c b/SequencialMatrixSearch.c
new file mode 100644 (file)
index 0000000..f0870a3
--- /dev/null
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <pthread.h>
+
+const int m_rows = 40000;
+const int m_cols = m_rows;
+int hv = 0;
+int matrix[m_rows][m_cols];
+
+
+int main(void){
+  printf("Populate the matrix\n");
+  for(int r = 0; r < m_rows; r++){  
+    for(int c = 0; c < m_cols; c++){
+      matrix[r][c] = r*m_cols+c; 
+    }
+  }
+
+  printf("Search matrix \n");
+  for(int rr = 0; rr < m_rows; rr++){
+    for(int cc = 0; cc < m_cols; cc++){
+      if(matrix[rr][cc] > hv){
+        hv = matrix[rr][cc];
+      }
+    }
+  }
+  printf("Done\n The biggest value found is  %d", hv); 
+  return 0;
+}
+
+