]> vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/commitdiff
3 threads and performance decreases
authorVGoncalo <vitor.goncalo.costa@gmail.com>
Fri, 13 Mar 2026 00:57:28 +0000 (00:57 +0000)
committerVGoncalo <vitor.goncalo.costa@gmail.com>
Fri, 13 Mar 2026 00:57:28 +0000 (00:57 +0000)
MatrixParallelPrograming.pdf [new file with mode: 0644]
ParallelMatrixSearch.c

diff --git a/MatrixParallelPrograming.pdf b/MatrixParallelPrograming.pdf
new file mode 100644 (file)
index 0000000..0a33a6f
Binary files /dev/null and b/MatrixParallelPrograming.pdf differ
index 2a3c49964e929ae079199b5346206172058aaf9b..538af2481c9a069629d067ec0a02485210e5282d 100644 (file)
@@ -9,6 +9,23 @@ const int m_cols = m_rows;
 _Atomic int hv = 0;
 int **matrix;
 
 _Atomic int hv = 0;
 int **matrix;
 
+typedef struct{
+  int start;
+  int end;
+} thread_args;
+
+void* parallel_search_cols(void *arg){
+  thread_args *args = (thread_args *)arg;
+  for(int r = 0; r < m_rows; r++){
+    for(int c = args->start; c < args->end; c++){
+      if(matrix[r][c] > hv){
+        hv = matrix[r][c];
+      }
+    }
+  }
+  free(args);
+  return NULL;
+}
 
 int main(void){
   printf("1st --> Allocate memory\n");
 
 int main(void){
   printf("1st --> Allocate memory\n");
@@ -43,17 +60,32 @@ int main(void){
   t_t2 = clock() - t_t2;
   double t2_ttaken = ((double)t_t2)/CLOCKS_PER_SEC; 
   printf(" %f sec\n",t2_ttaken);  
   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();
   
   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];
-      }
-    }
-  }
+  
+  pthread_t t1;
+  thread_args *args = malloc(sizeof(thread_args));
+  args->start = 0;
+  args->end = abs(m_cols/3);
+  pthread_create(&t1,NULL,&parallel_search_cols,args);
+  
+  pthread_t t2;
+  thread_args *args2 = malloc(sizeof(thread_args));
+  args2->start = abs(m_cols/3);
+  args2->end = m_cols-(m_cols/3);
+  pthread_create(&t2,NULL,&parallel_search_cols,args2);  
+  
+  pthread_t t3;
+  thread_args *args3 = malloc(sizeof(thread_args));
+  args3->start = m_cols-abs((m_cols/3));
+  args3->end = m_cols;
+  pthread_create(&t3,NULL,&parallel_search_cols,args3);
+
+  pthread_join(t1,NULL);
+  pthread_join(t2,NULL);
+  pthread_join(t3,NULL);
+
   t_t3 = clock() - t_t3;
   double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC;
   printf(" %f sec\n",t3_ttaken);
   t_t3 = clock() - t_t3;
   double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC;
   printf(" %f sec\n",t3_ttaken);
@@ -63,5 +95,3 @@ int main(void){
   free(matrix);
   return 0;
 }
   free(matrix);
   return 0;
 }
-
-