2
votes

I am writing a macro in Julia that can split a certain function between many processors. I have the following:

 macro parallel_calc(N::Int,proc::Int=4)

      n=round(Int,N/proc);

      proc_sum = @parallel (+) for i=1:proc

          f(n)

     end

    return proc_sum / proc

 end 

The problem is I want to write the macro so that

@parallel_calc f(n)

And then it will be able to automatically split the job between my processors. Is there any way in Julia to write a macro that takes a function as its input? I've tried looking online, but I haven't found anything that could help. I've also tried using

@everywhere f(n)

After adding my processors, but it runs significantly slower than the macro defined above. I need to define a new macro that splits the computation load equally (or almost equally) between my different processors.

1

1 Answers

2
votes

Macros only get expressions as inputs and should only produce expressions. The expression produced by a macro is the code that is actually evaluated as if that was what had originally been written. It seems that you could accomplish what you're trying to write with a function that takes the function f as an argument, e.g.:

function parallel_calc(f::Function, N::Int, proc::Int=4)
    n = round(Int, N/proc)
    proc_sum = @parallel (+) for i = 1:proc
        f(n)
    end
    return proc_sum/proc
end

This is literally just what you wrote but as a normal function taking f as its first argument. You can then call this like so:

parallel_calc(100) do n
    # do something with `n`
end