#include <stdlib.h>
#include <string.h>
#include <omp.h>
+#include <time.h>
+
// Global variables (simulating shared memory access for simplicity)
int* data = NULL;
int main(int argc, char *argv[]) {
- // --- 1. Handle Command Line Arguments ---
+ // Handle Command Line Arguments
int thread_count = -1;
char *input_file = NULL;
char *output_file = NULL;
return EXIT_FAILURE;
}
- // --- 2. Setup OpenMP Threads ---
+ // Setup OpenMP Threads ---
omp_set_num_threads(thread_count);
printf("--- Starting Sort Process ---\n");
printf("Threads allocated by OpenMP: %d\n", omp_get_max_threads());
- // --- 3. Read Data ---
+ // Read Data
if (!read_file(input_file)) {
fprintf(stderr, "Failed to read data.\n");
// Clean up data memory before exit
}
printf("Successfully read %zu elements from %s\n", data_size, input_file);
- // --- 4. Sort Data ---
+ // Parallel Sort Data
+ struct timespec start_sort_time;
+ clock_gettime(CLOCK_MONOTONIC, &start_sort_time);
if (data_size > 0) {
// Execute the parallel sort
parallel_merge_sort(0, data_size - 1);
printf("Sorting completed successfully.\n");
}
+ struct timespec end_sort_time;
+ clock_gettime(CLOCK_MONOTONIC, &end_sort_time);
+
+ // Write Results
+ 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;
- // --- 5. Write Results ---
if (!write_file(output_file)) {
fprintf(stderr, "Failed to write output data.\n");
free(data);
return EXIT_FAILURE;
}
printf("Sorted data written successfully to %s\n", output_file);
+ printf("Total Elapsed Wall-Clock Time: %lf seconds\n", elapsed_time);
- // --- 6. Cleanup ---
+ // Cleanup
free(data);
return EXIT_SUCCESS;
}