#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 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));
data[k++] = R[j++];
}
- // --- Crucial Step: Manual Memory Cleanup ---
+ // Manual Memory Cleanup
free(L);
free(R);
}
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
}
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 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);
- 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);