]>
vgcfreebox.myrthtech.pt Git - ue-pp-sortingalgorith.git/blob - parallel-sorting.c
1b6aff4050b373d1d187c1d35fda8d0583914762
6 // Global variables (simulating shared memory access for simplicity)
11 * @brief Merges two sorted sub-arrays (data[low...mid] and data[mid+1...high]).
12 * NOTE: This function must manually manage temporary memory allocation and deallocation.
14 void merge(int low
, int mid
, int high
) {
15 // Calculate sizes of the two halves
16 int n1
= mid
- low
+ 1;
19 // --- Manual Memory Allocation for Temporary Arrays ---
20 // We must allocate space for L and R.
21 int* L
= (int*)malloc(n1
* sizeof(int));
22 int* R
= (int*)malloc(n2
* sizeof(int));
24 if (L
== NULL
|| R
== NULL
) {
25 perror("Failed to allocate temporary merge arrays");
29 // Copy data from the main array into the temporary arrays
30 for (int i
= 0; i
< n1
; i
++) {
33 for (int j
= 0; j
< n2
; j
++) {
34 R
[j
] = data
[mid
+ 1 + j
];
37 // Standard sequential merging logic
38 int i
= 0; // Index for L
39 int j
= 0; // Index for R
40 int k
= low
; // Index for the main data array
42 while (i
< n1
&& j
< n2
) {
50 // Copy remaining elements of L
55 // Copy remaining elements of R
60 // --- Crucial Step: Manual Memory Cleanup ---
66 * @brief Recursive function to perform parallel Merge Sort using OpenMP.
68 void parallel_merge_sort(int low
, int high
) {
70 int mid
= low
+ (high
- low
) / 2;
72 // Use OpenMP sections to parallelize the recursive calls
73 // The OpenMP runtime handles the threads assignment and synchronization.
74 #pragma omp parallel sections
78 // Recurse on the left half
79 parallel_merge_sort(low
, mid
);
83 // Recurse on the right half
84 parallel_merge_sort(mid
+ 1, high
);
87 // Implicit barrier ensures merge only happens when both halves are sorted.
88 // Combine the two sorted halves
89 merge(low
, mid
, high
);
95 * @brief Reads all integers from the specified input file.
96 * @return 1 on success, 0 on failure.
98 int read_file(const char* filename
) {
99 FILE* infile
= fopen(filename
, "r");
100 if (infile
== NULL
) {
101 perror("Error opening input file");
105 // Initial allocation (guessing a reasonable maximum size)
106 size_t capacity
= 1024;
107 data
= (int*)malloc(capacity
* sizeof(int));
109 perror("Memory allocation failed for data array");
116 while (fscanf(infile
, "%d", &num
) == 1) {
117 // Check if realloc is necessary
118 if (count
== capacity
- 1) {
120 int* new_data
= (int*)realloc(data
, capacity
* sizeof(int));
121 if (new_data
== NULL
) {
122 perror("Failed to reallocate memory for data array");
138 * @brief Writes the sorted elements to the specified output file.
139 * @return 1 on success, 0 on failure.
141 int write_file(const char* filename
) {
142 FILE* outfile
= fopen(filename
, "w");
143 if (outfile
== NULL
) {
144 perror("Error opening output file");
148 // Write the data sequentially
149 for (size_t i
= 0; i
< data_size
; ++i
) {
150 fprintf(outfile
, "%d\n", data
[i
]);
158 int main(int argc
, char *argv
[]) {
159 // --- 1. Handle Command Line Arguments ---
160 int thread_count
= -1;
161 char *input_file
= NULL
;
162 char *output_file
= NULL
;
164 for (int i
= 1; i
< argc
; ++i
) {
165 if (strcmp(argv
[i
], "-t") == 0 && i
+ 1 < argc
) {
166 // Get thread count from the next argument
167 thread_count
= atoi(argv
[i
+1]);
168 i
++; // Skip the argument after -t
169 } else if (input_file
== NULL
) {
170 input_file
= argv
[i
];
171 } else if (output_file
== NULL
) {
172 output_file
= argv
[i
];
176 // Check for mandatory arguments and thread count
177 if (thread_count
<= 0 || input_file
== NULL
|| output_file
== NULL
) {
178 fprintf(stderr
, "Usage: %s <input_file> <output_file> [-t <thread_count>]\n", argv
[0]);
182 // --- 2. Setup OpenMP Threads ---
183 omp_set_num_threads(thread_count
);
184 printf("--- Starting Sort Process ---\n");
185 printf("Threads allocated by OpenMP: %d\n", omp_get_max_threads());
187 // --- 3. Read Data ---
188 if (!read_file(input_file
)) {
189 fprintf(stderr
, "Failed to read data.\n");
190 // Clean up data memory before exit
194 printf("Successfully read %zu elements from %s\n", data_size
, input_file
);
196 // --- 4. Sort Data ---
198 // Execute the parallel sort
199 parallel_merge_sort(0, data_size
- 1);
200 printf("Sorting completed successfully.\n");
203 // --- 5. Write Results ---
204 if (!write_file(output_file
)) {
205 fprintf(stderr
, "Failed to write output data.\n");
209 printf("Sorted data written successfully to %s\n", output_file
);
211 // --- 6. Cleanup ---