Skip to content

How to use OpenMP the right way? #245

Answered by jrfondren
stefanos82 asked this question in Q&A
Discussion options

You must be logged in to vote

Each iteration of your loop depends on the previous iteration's values, so I don't see how it can be parallelized. Maybe the error is OpenMP's way of complaining about that.

Here's a recursive fibonacci and a use of it that's speed up with OpenMP. The usage is the same as in examples/condots.nelua:

local function fib(n: integer)
  if n < 2 then return n end
  return fib(n - 1) + fib(n - 2)
end

local results: [1000]uinteger
## if pragmas.withmp then
  ## cflags '-fopenmp'
  ## cemit '#pragma omp parallel for schedule(dynamic)'
## end
for i=0, #results-1 do
  results[i] = fib(30)
end
print(results[0])

comparing them:

$ nelua -o fib1 -M fibopenmp.nelua
$ nelua -o fib2 -P withmp -M fibopenmp…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@stefanos82
Comment options

Answer selected by stefanos82
Comment options

You must be logged in to vote
1 reply
@stefanos82
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants