]> vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/blobdiff - ParallelMatrixSearch.c
performance analysis
[ue-pp-squarematrixparallelisation.git] / ParallelMatrixSearch.c
index 538af2481c9a069629d067ec0a02485210e5282d..949f0733912add777175d2b1db7dd9beade5d337 100644 (file)
@@ -23,10 +23,40 @@ void* parallel_search_cols(void *arg){
       }
     }
   }
-  free(args);
   return NULL;
 }
 
+void run_parallel_search(int num_threads) {
+    pthread_t *threads = malloc(num_threads * sizeof(pthread_t));
+    thread_args *args = malloc(num_threads * sizeof(thread_args));
+
+    if (threads == NULL || args == NULL) {
+        perror("Failed to allocate memory for threads");
+        exit(1);
+    }
+
+    int chunk_size = m_cols / num_threads;
+
+    for (int i = 0; i < num_threads; i++) {
+        args[i].start = i * chunk_size;
+        if (i == num_threads - 1) {
+            args[i].end = m_cols;
+        } else {
+            args[i].end = (i + 1) * chunk_size;
+        }
+        if (pthread_create(&threads[i], NULL, &parallel_search_cols, &args[i]) != 0) {
+            perror("Failed to create thread");
+            exit(1);
+        }
+    }
+
+    for (int i = 0; i < num_threads; i++) {
+        pthread_join(threads[i], NULL);
+    }
+    free(threads);
+    free(args);
+}
+
 int main(void){
   printf("1st --> Allocate memory\n");
   clock_t t_t1; t_t1 = clock();
@@ -63,29 +93,8 @@ int main(void){
   
   printf("3rd --> Search matrix\n");
   clock_t t_t3; t_t3 = clock();
-  
-  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);
-
+  int threads_to_use = 800;
+  run_parallel_search(threads_to_use);
   t_t3 = clock() - t_t3;
   double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC;
   printf(" %f sec\n",t3_ttaken);