首页 > 解决方案 > Multithreaded algorithms work much slower

问题描述

I have tried with OpenMP and Cilk Plus. The result is the same, multithreading works slower. I don't know what I'm doing wrong. I did what the guy did in this tutorial

His code works better in parallel, while the situation in mine is like this:

PARALLEL: Fibonacci number #42 is 267914296
Calculated in 33.026 seconds using 8 workers

SERIAL: Fibonacci number #42 is 267914296
Calculated in 2.110 seconds using 8 workers

I exactly copied the source code of the tutorial.

I also tried it with OpenMP, the same thing happens there too. I check the usage of CPU cores during the execution. They all work, it is fine.

I tried to change the number of workers with this command:

export CILK_NWORKERS=4

It appears as the number of workers increases, the algorithm runs slower. But sometimes it doesn't. I implemented Cilk codes on both C and C++. No difference.

This is the sequential Fibonacci function:

int fib_s(int n)
{
    if (n < 2)
        return n;
    int x = fib_s(n-1);
    int y = fib_s(n-2);

    return x + y;
}

This is the parallel Fibonacci function:

int fib(int n)
{
    if (n < 2)
        return n;
    int x = cilk_spawn fib(n-1);
    int y = fib(n-2);
    cilk_sync;
    return x + y;
}

And I calculate running time like this in main() function:

clock_t start = clock();
int result = fib(n);
clock_t end = clock();
double duration = (double)(end - start) / CLOCKS_PER_SEC;

Can anyone help me?

标签: c++multithreadingperformanceparallel-processingcilk-plus

解决方案


您问题的正确答案取决于硬件。许多因素通常会影响代码的性能:可以应用不同的软件策略来加速执行。然而,它们中的一些比另一些更有效,这取决于 (1) 所选的特定应用程序和 (2) 所选的特定硬件平台。我想建议对您的应用程序进行概要分析。

在这里,您可以找到软件分析的一般介绍,而在这里,您可以找到可以帮助您完成此任务的软件工具列表。

链接和此其他链接中,您可以找到用于分析 OpenMP 应用程序的信息(您的问题的情况)。

了解和理解幕后发生的事情始终是一个好习惯。这将允许您定位著名的 tris 应用程序/代码/硬件的瓶颈。


推荐阅读