0

I have this sequential code:

int fib(int n) {
int x, y;

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

And this parallel code:

int fib(int n) {
int x, y;

if (n < 2)
return n;

#pragma omp task shared(x)
x = fib(n-1);

#pragma omp task shared(y)
y = fib(n-2);

#pragma omp taskwait
return x + y;
}

The openmp parallel code is slower than serial. I use tdm-gcc 7.4. I have no other program open at Fibonacci runtime. What's wrong?

alk
  • 69,737
  • 10
  • 105
  • 255
glo
  • 75
  • 6
  • 7
    You're just not giving the parallel version enough work to do, and the overhead of creating all those tasks has to be paid for (in time). – High Performance Mark Nov 12 '19 at 13:48
  • 2
    What input value are you using? What timings do you get? – Max Langhof Nov 12 '19 at 14:27
  • 2
    Your "sequential code" is not sequential *at all*, it's recursive. – Marco Bonelli Nov 12 '19 at 15:08
  • @MarcoBonelli The *sequential* code is certainly sequential as opposed to parallel or concurrent. The fact that it is recursive is orthogonal and true for both variants. – Zulan Nov 12 '19 at 16:42
  • @Zulan sequential and parallel are *not* opposites and have nothing in common. Sequential code can be run in parallel and non-sequential code can be run in parallel too. Those are two different concepts. That said, OP's code is certainly *not* sequential. – Marco Bonelli Nov 12 '19 at 16:45
  • You haven't added any parallelism! (There is no #pragma omp parallel in your "parallel" version). You have expressed that there is parallelism, but never created any parallel threads. Even with that it's not clear that this will give speedup for any sane size, though, since thread creation and task creation cost... – Jim Cownie Nov 13 '19 at 09:19

0 Answers0