2

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 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.