Issue
This Content is from Stack Overflow. Question asked by Adam G
I have implemented a parallel merge sort, but when I go to compile it i get this error: ‘task’: requires ‘-openmp:llvm’ command line options
I’ve tried adding the flag in the command line options (Properties -> Debugging) but still doesn’t work. Is there anything I can do to fix this?
I’m using VS Studio 2022 and on Windows 11
My code:
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#include <time.h>
#include <memory.h>
//static const long Num_To_Sort = 1000000000;
static const long Num_To_Sort = 10000000;
void sort_s(int* arr);
void sort_p(int* arr);
void merge(int* array, int start, int end);
void mergeSort(int* array, int start, int end);
void mergeSortPara(int* array, int start, int end);
int main() {
clock_t start, end;
int* arr_s = new int[Num_To_Sort];
int* arr_p = new int[Num_To_Sort];
long chunk_size = Num_To_Sort / omp_get_max_threads();
srand(time(NULL));
#pragma omp parallel num_threads(omp_get_max_threads())
{
int p = omp_get_thread_num();
long chunk_start = p * chunk_size;
long chunk_end = chunk_start + chunk_size;
for (long i = chunk_start; i < chunk_end; i++) {
arr_s[i] = arr_p[i] = rand();
}
}
// Copy the array so that the sorting function can operate on it directly.
// Note that this doubles the memory usage.
// You may wish to test with slightly smaller arrays if you're running out of memory.
printf("Timing sequential...n");
start = clock();
sort_s(arr_s);
end = clock();
printf("Took %f secondsnn", (double)(end - start) / CLOCKS_PER_SEC);
printf("Timing parallel...n");
start = clock();
sort_p(arr_p);
end = clock();
printf("Took %f secondsnn", (double)(end - start) / CLOCKS_PER_SEC);
free(arr_s);
free(arr_p);
return 0;
}
// Sequential version of your sort
// If you're implementing the PSRS algorithm, you may ignore this section
void sort_s(int* arr) {
mergeSort(arr, 0, Num_To_Sort - 1);
}
// Parallel version of your sort
void sort_p(int* arr) {
mergeSortPara(arr, 0, Num_To_Sort - 1);
}
void merge(int* array, int start, int end)
{
int middle = (start + end) / 2;
int temp_index = 0;
/* create a temporary array */
//int* temp = malloc(sizeof(int) * (end - start + 1));
int* temp = new int[end - start + 1];
/* merge in sorted data from the 2 halves */
int left = start;
int right = middle + 1;
/* while both halves have data */
while ((left <= middle) && (right <= end)) {
/* if the left half value is less than right */
if (array[left] < array[right]) {
/* take from left */
temp[temp_index] = array[left];
temp_index++;
left++;
}
else {
/* take from right */
temp[temp_index] = array[right];
temp_index++;
right++;
}
}
/* add the remaining elements from the left half */
while (left <= middle) {
temp[temp_index] = array[left];
temp_index++;
left++;
}
/* add the remaining elements from the right half */
while (right <= end) {
temp[temp_index] = array[right];
temp_index++;
right++;
}
/* move from temp array to the original array */
int i;
for (i = start; i <= end; i++) {
array[i] = temp[i - start];
}
/* free the temporary array */
free(temp);
}
void mergeSortPara(int* array, int start, int end) {
if (start < end) {
int middle = (start + end) / 2;
/* sort both halves in parallel */
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task
mergeSort(array, start, middle);
mergeSort(array, middle + 1, end);
}
}
/* merge the two halves */
merge(array, start, end);
}
}
void mergeSort(int* array, int start, int end) {
if (start < end) {
int middle = (start + end) / 2;
/* sort left half */
mergeSort(array, start, middle);
/* sort right half */
mergeSort(array, middle + 1, end);
/* merge the two halves */
merge(array, start, end);
}
}
Solution
You should add -openmp:llvm
option to Properties/C/C++/Command line/Additional Options
.
However, I suggest you using the clang compiler in Visual Studio as it has OpenMP 5.0 support. -openmp:llvm
flag offers a partial OpenMP 3.1 support only.
This Question was asked in StackOverflow by Adam G and Answered by Laci It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.