为什么我无法在合并排序程序中使用 OpenMP 任务?

问题描述 投票:0回答:2

我已经实现了并行合并排序,但是当我去编译它时,我收到此错误:'task':需要'-openmp:llvm'命令行选项

我尝试在命令行选项(属性 -> 调试)中添加标志,但仍然不起作用。我能做些什么来解决这个问题吗?

我在 Windows 11 上使用 VS Studio 2022

我的代码:

#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 seconds\n\n", (double)(end - start) / CLOCKS_PER_SEC);

    printf("Timing parallel...\n");
    start = clock();

    sort_p(arr_p);

    end = clock();
    printf("Took %f seconds\n\n", (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);
    }
}
c++ parallel-processing openmp llvm visual-studio-2022
2个回答
0
投票

您应该将

-openmp:llvm
选项添加到
Properties/C/C++/Command line/Additional Options

但是,我建议您在 Visual Studio 中使用 clang 编译器,因为它支持 OpenMP 5.0。

-openmp:llvm
标志仅提供部分 OpenMP 3.1 支持


0
投票

如果 /openmp:llvm 和 /openmp:experemental 对您没有帮助,请将平台从 x64 更改为 x86。

© www.soinside.com 2019 - 2024. All rights reserved.