Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make use of Threads optional in a Julia function

Tags:

I have a function that optionally uses threads for its main loop, doing so when an argument usingthreads is true. At the moment, the code looks like this:

function dosomething(usingthreads::Bool)
    n = 1000
    if usingthreads
        Threads.@threads for i = 1:n
            #20 lines of code here
        end
    else
        for i = 1:n
            #same 20 lines of code repeated here
        end
    end
end

Less nasty than the above would be to put the "20 lines" in a separate function. Is there another way?

like image 462
Philip Swannell Avatar asked Oct 27 '19 14:10

Philip Swannell


1 Answers

You could use a macro that changes its behavior depending on the result of Threads.nthreads():

macro maybe_threaded(ex)
    if Threads.nthreads() == 1
        return esc(ex)
    else
        return esc(:(Threads.@threads $ex))
    end
end

Without threading, this macro will be a no-op:

julia> @macroexpand @maybe_threaded for i in 1:5
           print(i)
       end
:(for i = 1:5
      #= REPL[2]:2 =#
      print(i)
  end)

But when threading is enabled and e.g. JULIA_NUM_THREADS=4 it will expand to the threaded version:

julia>  @maybe_threaded for i in 1:5
           print(i)
       end
41325

Edit: Upon rereading the question, I realize this doesn't really answer it but it might be useful anyway.

like image 116
Kristoffer Carlsson Avatar answered Nov 14 '22 22:11

Kristoffer Carlsson