OpenMP中的并行合并排序

OpenMP中的并行合并排序

本文介绍了OpenMP中的并行合并排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在这篇论文.这是代码:

void mergesort_parallel_omp (int a[], int size, int temp[], int threads)
{
    if ( threads == 1)       { mergesort_serial(a, size, temp); }
    else if (threads > 1)
    {
         #pragma omp parallel sections
         {
          #pragma omp section
             mergesort_parallel_omp(a, size/2, temp, threads/2);
          #pragma omp section
             mergesort_parallel_omp(a + size/2, size - size/2, temp + size/2, threads - threads/2);
         }
merge(a, size, temp);
    } // threads > 1
}

我在多核上运行它.发生的是,在树的叶子处,有两个线程并行运行.他们完成工作后,将启动其他2个线程,依此类推.即使我们为所有叶节点提供了免费内核.

I run it on a multicore. What happens is that at the leafs of the tree, 2 threads run in parallel. After they finished their work 2 other threads start and so on. Even if we have free cores for all the leaf nodes.

我认为原因是此OpenMP代码未在并行区域内创建并行区域.我说得对吗?

I think the reason is this OpenMP code does not create parallel regions inside parallel regions. Am I correct?

推荐答案

我认为原因是OpenMP无法在并行区域内创建并行区域"

"I think the reason is that OpenMP cannot create parallel regions inside parallel regions"

您可以具有平行区域的平行区域.

You can have parallel region of parallel region.

为了正确运行代码,您需要调用omp_set_nested(1)omp_set_num_threads(2).

In order to run your code correctly, you need to call omp_set_nested(1) and omp_set_num_threads(2).

这篇关于OpenMP中的并行合并排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:42