]> vgcfreebox.myrthtech.pt Git - ue-pp-squarematrixparallelisation.git/blobdiff - ParallelMatrixSearch.c
even better performance on columns split - 800 threads
[ue-pp-squarematrixparallelisation.git] / ParallelMatrixSearch.c
index f56fa2e0a0f9cb39d1bb1db09a98ee78b8b17bb5..949f0733912add777175d2b1db7dd9beade5d337 100644 (file)
@@ -23,13 +23,10 @@ void* parallel_search_cols(void *arg){
       }
     }
   }
       }
     }
   }
-  free(args);
   return NULL;
 }
 
   return NULL;
 }
 
-// Function to dynamically create threads and divide columns
 void run_parallel_search(int num_threads) {
 void run_parallel_search(int num_threads) {
-    // 1. Dynamically allocate arrays for threads and their arguments
     pthread_t *threads = malloc(num_threads * sizeof(pthread_t));
     thread_args *args = malloc(num_threads * sizeof(thread_args));
 
     pthread_t *threads = malloc(num_threads * sizeof(pthread_t));
     thread_args *args = malloc(num_threads * sizeof(thread_args));
 
@@ -38,41 +35,26 @@ void run_parallel_search(int num_threads) {
         exit(1);
     }
 
         exit(1);
     }
 
-    // 2. Calculate the base number of columns each thread will process
     int chunk_size = m_cols / num_threads;
 
     int chunk_size = m_cols / num_threads;
 
-    // 3. Create the threads in a loop
     for (int i = 0; i < num_threads; i++) {
         args[i].start = i * chunk_size;
     for (int i = 0; i < num_threads; i++) {
         args[i].start = i * chunk_size;
-        
-        // If it is the last thread, let it process all remaining columns
         if (i == num_threads - 1) {
             args[i].end = m_cols;
         } else {
             args[i].end = (i + 1) * chunk_size;
         }
         if (i == num_threads - 1) {
             args[i].end = m_cols;
         } else {
             args[i].end = (i + 1) * chunk_size;
         }
-
-        // Pass the address of the specific struct for this thread
         if (pthread_create(&threads[i], NULL, &parallel_search_cols, &args[i]) != 0) {
             perror("Failed to create thread");
             exit(1);
         }
     }
 
         if (pthread_create(&threads[i], NULL, &parallel_search_cols, &args[i]) != 0) {
             perror("Failed to create thread");
             exit(1);
         }
     }
 
-    // 4. Wait for all threads to finish
     for (int i = 0; i < num_threads; i++) {
         pthread_join(threads[i], NULL);
     }
     for (int i = 0; i < num_threads; i++) {
         pthread_join(threads[i], NULL);
     }
-
-    // 5. Clean up the dynamically allocated arrays
     free(threads);
     free(threads);
-    // Note: Do not free(args) here IF your thread function (parallel_search_cols) 
-    // is already calling free(arg) internally! 
-    // Since your original thread function calls free(args), calling it here 
-    // would cause a "double free" crash. 
-    // However, since we allocated it as one big array, it is better to remove 
-    // free(args) from your thread function and do it here:
-    free(args); 
+    free(args);
 }
 
 int main(void){
 }
 
 int main(void){
@@ -111,7 +93,7 @@ int main(void){
   
   printf("3rd --> Search matrix\n");
   clock_t t_t3; t_t3 = clock();
   
   printf("3rd --> Search matrix\n");
   clock_t t_t3; t_t3 = clock();
-  int threads_to_use = 4;
+  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;
   run_parallel_search(threads_to_use);
   t_t3 = clock() - t_t3;
   double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC;