X-Git-Url: https://vgcfreebox.myrthtech.pt/gitweb/ue-pp-sortingalgorith.git/blobdiff_plain/8d38f9952f14d98d5bb76b323403f9fef9bfd8fc..08869ec7a7dd81f73c4e293d938cfefa57a32bf6:/parallel-sorting.c?ds=inline diff --git a/parallel-sorting.c b/parallel-sorting.c index 1b6aff4..109b116 100644 --- a/parallel-sorting.c +++ b/parallel-sorting.c @@ -2,8 +2,10 @@ #include #include #include +#include -// Global variables (simulating shared memory access for simplicity) + +// Global variables (simulating shared memory access) int* data = NULL; size_t data_size = 0; @@ -16,7 +18,7 @@ void merge(int low, int mid, int high) { 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)); @@ -57,7 +59,7 @@ void merge(int low, int mid, int high) { data[k++] = R[j++]; } - // --- Crucial Step: Manual Memory Cleanup --- + // Manual Memory Cleanup free(L); free(R); } @@ -69,8 +71,7 @@ void parallel_merge_sort(int low, int high) { 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 @@ -156,7 +157,7 @@ int write_file(const char* filename) { 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; @@ -179,12 +180,12 @@ int main(int argc, char *argv[]) { 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 @@ -193,22 +194,31 @@ int main(int argc, char *argv[]) { } printf("Successfully read %zu elements from %s\n", data_size, input_file); - // --- 4. Sort Data --- + // ------------------------------- WALL CLOCK TIME ------------------------------------ + 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); + // ------------------------------- END WALL CLOCK TIME --------------------------------- + + // determine actual wall clock time + double elapsed_time = (end_sort_time.tv_sec - start_sort_time.tv_sec); + elapsed_time += (end_sort_time.tv_nsec - start_sort_time.tv_nsec) / 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; }