fix this code: #include <iostream> #include <vector> #include <algorithm> #include…
- User
fix this code: #include #include #include #include #include #include <sys/time.h> #include <omp.h> #include
using namespace std;
using std::vector;
template // Function prototypes for the sorting algorithms void merge(vector &arr, int low, int mid, int high); void parallelMergeSort(vector &arr, int low, int high); void parallelQuickSort(vector &arr, int left, int right); void parallelCountingSort(vector &arr, int maxValue); void runParallelSort(void (*sortFunc)(vector &, int, int), vector &data, const vector &thread_counts); void runParallelCountingSort(vector &data, const vector &thread_counts, int maxValue); double measureTime(std::function<void(vector &, int, int)> sortFunc, vector &data, int numThreads);
// Function to measure execution time double measureTime(void (*sortFunc)(vector &, int, int), vector &data, int numThreads);
int main() { int choice; const int MAX_VALUE = 1000; // Range: 0 to 1000
const int N = 10000000; // Hardcoded value for 10^9 cout << "Choose sorting algorithm:" << endl; cout << "1 - Parallel Merge Sort" << endl; cout << "2 - Parallel Quick Sort" << endl; cout << "3 - Parallel Counting Sort" << endl; cout << "Selection: "; cin >> choice; vector<int> data(N), temp; srand(static_cast<unsigned>(time(0))); generate(data.begin(), data.end(), [&] { return rand() % (MAX_VALUE + 1); }); vector<int> thread_counts = {1, 2, 4, 6, 8, 10, 12, 14, 16}; double totalTime, avgTime; switch (choice) { case 1: cout << "Running Parallel Merge Sort:" << endl; runParallelSort(parallelMergeSort, data, thread_counts); break; case 2: cout << "Running Parallel Quick Sort:" << endl; runParallelSort(parallelQuickSort, data, thread_counts); break; case 3: cout << "Running Parallel Counting Sort (for MAX_VALUE = " << MAX_VALUE << "):" << endl; runParallelCountingSort(data, thread_counts, MAX_VALUE); break; default: cerr << "Invalid choice." << endl; return 1; } return 0;}
void merge(vector &arr, int low, int mid, int high) { int n1 = mid - low + 1; int n2 = high - mid; vector L(n1), R(n2);
for (int i = 0; i < n1; i++) L[i] = arr[low + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = low; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; }}
void parallelMergeSort(vector &arr, int low, int high) { int mid; if (low < high) { mid = low + (high - low) / 2; #pragma omp parallel sections { #pragma omp section parallelMergeSort(arr, low, mid); #pragma omp section parallelMergeSort(arr, mid + 1, high); } merge(arr, low, mid, high); } }
void parallelQuickSort(vector &arr, int left, int right) { if (left < right) { int pivot = arr[left + (right - left) / 2]; int i = left, j = right; while (i <= j) { while(arr[i] < pivot) i++; while(arr[j] > pivot) j--; if(i <= j) { swap(arr[i], arr[j]); i++; j--; } } #pragma omp parallel sections { #pragma omp section { parallelQuickSort(arr, left, j); } #pragma omp section { parallelQuickSort(arr, i, right); } } } }
- Assistant
- User
give only the code but completely
- Assistant
- User
- Assistant
There's a syntax error in your code. In the
mainfunction, where you're generating random numbers, the lambda function syntax is incorrect. Here's the line with the error:generate(data.begin(), data.end(), & { return rand() % (MAX_VALUE + 1); });It should be:
generate(data.begin(), data.end(), [&]() { return rand() % (MAX_VALUE + 1); });Note that the ampersand
&comes just after the open brackets to signify the start of a lambda expression, not before the curly braces{.Apart from that, there might be potential issues with uncontrolled parallelism in
parallelMergeSortandparallelQuickSortdue to recursive calls with OpenMP parallel sections which can create too many threads. This could be mitigated with a conditional that limits parallelism at deeper recursion levels, or by using OpenMP tasks with a threshold for creating new tasks. - User
- Assistant