2

I am trying to do in parallel some stuff. Based on @time the performance is excellent, but I am actually waiting quite long in front of my computer. The code is something like below.

function max(n)    
    rand_n = SharedArray{Float64}(n, n)

    @distributed for i in 1:n
        @distributed for j in 1:n

            r = Random(Uniform(), 100)
            rand_n[i,j] = StatsBase.maximum(EV0)

        end
    end
    rand_n
end

@time max(1000)

0.000166 seconds (118 allocations: 18.203 KiB)

tick()
max(1000)
tock()

2.865833086s: 2 seconds, 865 milliseconds

So the actual time elapsed on the computer is much longer that what @time says.

3
  • my guess is that you're seeing compilation times Commented Aug 13, 2019 at 4:55
  • Thank you for your response! I don't think that's my question. The first time I run the function it takes about 0.015 seconds. This should be when Julia compiles the code for the first time. Then the second time I run the function, it takes less than 0.0002 seconds. But my question is that, physically I feel it takes much longer than that. It is not like in matlab that there isn't a too big difference between what the computer tells me and what I feel. Sometimes for Julia @time tells me that the function takes 1 second when I actually have to wait for more than 10 seconds. Commented Aug 13, 2019 at 5:10
  • I know btime will do this since it runs your code many times to get accurate timings, but a simple time call really shouldn't do this. That's really weird Commented Aug 13, 2019 at 5:43

1 Answer 1

1

You should read the documentation of @distributed (type ?@distributed at the prompt):

Note that without a reducer function, @distributed executes asynchronously, i.e. it spawns independent tasks on all available workers and returns immediately without waiting for completion. To wait for completion, prefix the call with @sync, like :

@sync @distributed for var = range
      body
end

Currently, you are just starting the calculation and bailing out, so you get the timing of starting the calculation without waiting for it to finish.

A couple of more things:

  • Please always provide a "minimal working example" so that other posters can just copy-paste your code, and it will run. So include using Distributed and other required packages. Define all variables, etc. What is EV0, what does Random mean here? etc. etc.
  • You're defining r but you're not using it. What's it for?
  • max is the name of a function in Base, it's probably not a good idea to overload that name like this.
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.