]> vgcfreebox.myrthtech.pt Git - ue-pp-sortingalgorith.git/blobdiff - parallel-sorting.c
grooming repo
[ue-pp-sortingalgorith.git] / parallel-sorting.c
index 0ce2eb2ffb8d3060debd0739a81d02d9dca3be72..98200b4d51a2def0b69672a90f3040e56a4d7092 100644 (file)
@@ -5,7 +5,7 @@
 #include <time.h>
 
 
 #include <time.h>
 
 
-// Global variables (simulating shared memory access for simplicity)
+// Global variables (simulating shared memory access)
 int* data = NULL;
 size_t data_size = 0;
 
 int* data = NULL;
 size_t data_size = 0;
 
@@ -18,7 +18,7 @@ void merge(int low, int mid, int high) {
     int n1 = mid - low + 1;
     int n2 = high - mid;
 
     int n1 = mid - low + 1;
     int n2 = high - mid;
 
-    // --- Manual Memory Allocation for Temporary Arrays ---
+    // Manual Memory Allocation for Temporary Arrays
     // We must allocate space for L and R.
     int* L = (int*)malloc(n1 * sizeof(int));
     int* R = (int*)malloc(n2 * sizeof(int));
     // We must allocate space for L and R.
     int* L = (int*)malloc(n1 * sizeof(int));
     int* R = (int*)malloc(n2 * sizeof(int));
@@ -59,7 +59,7 @@ void merge(int low, int mid, int high) {
         data[k++] = R[j++];
     }
 
         data[k++] = R[j++];
     }
 
-    // --- Crucial Step: Manual Memory Cleanup ---
+    // Manual Memory Cleanup
     free(L);
     free(R);
 }
     free(L);
     free(R);
 }
@@ -71,8 +71,7 @@ void parallel_merge_sort(int low, int high) {
     if (low < high) {
         int mid = low + (high - low) / 2;
 
     if (low < high) {
         int mid = low + (high - low) / 2;
 
-        // Use OpenMP sections to parallelize the recursive calls
-        // The OpenMP runtime handles the threads assignment and synchronization.
+        // Use OpenMP sections to parallelize the recursive calls, runtime handles the threads assignment and synchronization.
         #pragma omp parallel sections
         {
             #pragma omp section
         #pragma omp parallel sections
         {
             #pragma omp section
@@ -195,7 +194,7 @@ int main(int argc, char *argv[]) {
     }
     printf("Successfully read %zu elements from %s\n", data_size, input_file);
 
     }
     printf("Successfully read %zu elements from %s\n", data_size, input_file);
 
-    // Parallel Sort Data
+    // ------------------------------- WALL CLOCK TIME ------------------------------------
     struct timespec start_sort_time;
     clock_gettime(CLOCK_MONOTONIC, &start_sort_time);
     if (data_size > 0) {
     struct timespec start_sort_time;
     clock_gettime(CLOCK_MONOTONIC, &start_sort_time);
     if (data_size > 0) {
@@ -205,11 +204,13 @@ int main(int argc, char *argv[]) {
     }
     struct timespec end_sort_time;
     clock_gettime(CLOCK_MONOTONIC, &end_sort_time);
     }
     struct timespec end_sort_time;
     clock_gettime(CLOCK_MONOTONIC, &end_sort_time);
-
-    // Write Results
+    
+    // determine actual wall clock time
     double elapsed_time = (end_sort_time.tv_sec - start_sort_time.tv_sec);
     double elapsed_time = (end_sort_time.tv_sec - start_sort_time.tv_sec);
-    elapsed_time += (end_sort_time.tv_sec - start_sort_time.tv_sec) / 1e9;
+    elapsed_time += (end_sort_time.tv_nsec - start_sort_time.tv_nsec) / 1e9;
+    // ------------------------------- END WALL CLOCK TIME ---------------------------------
 
 
+    
     if (!write_file(output_file)) {
         fprintf(stderr, "Failed to write output data.\n");
         free(data);
     if (!write_file(output_file)) {
         fprintf(stderr, "Failed to write output data.\n");
         free(data);