为什么并行合并排序在单线程中运行得更快?

时间:2019-10-27 04:44:25

标签: c gcc parallel-processing openmp mergesort

我在openMP中具有以下并行合并排序算法。 OpenMP默认情况下会创建8个线程,但是比顺序运行要慢得多。但是当我指定omp_set_num_threads(1)时(即将线程数设置为1),时间比顺序的要快。有什么合理的解释吗?

void para_merge_sort(int para_arr[], int low, int high)
{
    if (low < high)
    {
        int med = low + (high-low)/2;
        #pragma omp task firstprivate(para_arr, low, med)
        para_merge_sort (para_arr, low, med);
        #pragma omp task firstprivate(para_arr, med, high)
        para_merge_sort (para_arr, med+1, high);
        #pragma omp taskwait
        merge (para_arr, low, med, high);
    }
}

void merge_sort()
{
    int seq_arr[MAX], para_arr[MAX];
    omp_set_num_threads(1);

    for (int i=0; i<MAX; ++i)
        seq_arr[i] = para_arr[i] = rand()%100;

    double start_time = omp_get_wtime();
    seq_merge_sort(seq_arr, 0, MAX-1);
    double comp_time = omp_get_wtime() - start_time;
    printf ("Sequential merge sort : %f\n", comp_time);

    start_time = omp_get_wtime();
    #pragma omp parallel
    {
        #pragma omp single
        para_merge_sort(para_arr, 0, MAX-1);
    }
    comp_time = omp_get_wtime() - start_time;
    printf ("Parallel merge sort : %f\n", comp_time);

    /*for (int i=0; i<MAX; ++i)
        printf ("%d\n", para_arr[i]);*/
}

顺序合并排序:0.004533

并行合并排序:0.007102

当omp_set_num_threads(1)时:

顺序合并排序:0.005124

并行合并排序:0.002424

0 个答案:

没有答案