Re: [julia-users] Re: Tips for optimizing this short code snippet

2016-06-19 Thread Kristoffer Carlsson
If you get the new ProfileView it should work. A new version was recently 
tagged.

On Sunday, June 19, 2016 at 6:59:11 PM UTC+2, Marius Millea wrote:
>
> Ah, that makes sense. So I tried with the latest 0.5 nightly and I go from 
> ~3ms to ~1ms, a nice improvement! (different than what Andrew reported 
> above, so perhaps something changed over the last few nights tho) 
> Unfortunately ProfileView is giving me an error on 0.5, but from printing 
> the profile data I can at least confirm jl_apply_generic is no longer being 
> called. 
>
> On Sun, Jun 19, 2016 at 6:06 PM, Giuseppe Ragusa  > wrote:
>
>> As Eric pointed out, with Julia 0.4.x functions passed as arguments are 
>> not optimized as their type is difficult to infer. That's why the profiler 
>> shows jl_generic_function being the bottleneck. Try it with 0.5 and things 
>> could get dramatically faster.
>
>
>

Re: [julia-users] Re: Tips for optimizing this short code snippet

2016-06-19 Thread Marius Millea
Ah, that makes sense. So I tried with the latest 0.5 nightly and I go from
~3ms to ~1ms, a nice improvement! (different than what Andrew reported
above, so perhaps something changed over the last few nights tho)
Unfortunately ProfileView is giving me an error on 0.5, but from printing
the profile data I can at least confirm jl_apply_generic is no longer being
called.

On Sun, Jun 19, 2016 at 6:06 PM, Giuseppe Ragusa 
wrote:

> As Eric pointed out, with Julia 0.4.x functions passed as arguments are
> not optimized as their type is difficult to infer. That's why the profiler
> shows jl_generic_function being the bottleneck. Try it with 0.5 and things
> could get dramatically faster.


[julia-users] Re: Tips for optimizing this short code snippet

2016-06-19 Thread Giuseppe Ragusa
As Eric pointed out, with Julia 0.4.x functions passed as arguments are not 
optimized as their type is difficult to infer. That's why the profiler shows 
jl_generic_function being the bottleneck. Try it with 0.5 and things could get 
dramatically faster.

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-19 Thread Marius Millea


Actually I suppose normalizing by calls to integrand does answer your point 
about implementations*, *just about algorithm. Its true, when I look at the 
ProfileView (attached) it does seem like most of the time is actually spent 
inside quadgk. In fact, most of it is inside the jl_apply_generic function. 
I don't know enough about Julia to know what that function does. Could that 
be a sign there's something non-optimal going on? (To profile this I am 
doing @profile for _=1:1; test.f(1.); end to get enough samples, is 
that correct?) 


Marius




On Sunday, June 19, 2016 at 4:41:47 PM UTC+2, Marius Millea wrote:
>
> They *are* different algorithms, but when I was comparing speeds with the 
> other codes, I compared it in terms of time per number of calls of the 
> inner integrand function. So basically I'm testing the speed of the 
> integrand functions themselves, as well as the speed of the integration 
> library code, as well as any function call overhead type thing. With this 
> metric, the Julia code was close, but it was the slowest (although of 
> course far more succinct and easy to read). 
>
>
> On Saturday, June 18, 2016 at 7:46:35 PM UTC+2, Gabriel Gellner wrote:
>>
>> What integration library are you using with Cython/Fortran? Is it using 
>> the same algorithm as quadgk? Your code seems so simple I imagine this is 
>> just comparing the quadrature implementations :)
>>
>> On Saturday, June 18, 2016 at 5:53:57 AM UTC-7, Marius Millea wrote:
>>>
>>> Hi all, I'm sort of just starting out with Julia, I'm trying to get 
>>> gauge of how fast I can make some code of which I have Cython and Fortran 
>>> versions to see if I should continue down the path of converting more or my 
>>> stuff to Julia (which in general I'd very much like to, if I can get it 
>>> fast enough). I thought maybe I'd post the code in question here to see if 
>>> I could get any tips. I've stripped down the original thing to what I think 
>>> are the important parts, a nested integration with an inner function 
>>> closure and some global variables. 
>>>
>>> module test
>>>
>>> const a = 1.
>>>
>>> function f(x)
>>> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
>>> end
>>>
>>> function g(y)
>>> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
>>> quadgk(integrand,0,Inf)[1]   # <=== inner integral
>>> end
>>>
>>> end
>>>
>>>
>>> > @timeit test.f(1.)
>>> 100 loops, best of 3: 3.10 ms per loop
>>>
>>>
>>>
>>>
>>> Does anyone have any tips that squeezes a little more out of this code? 
>>> I have run ProfileView on it, and although I'm not sure I fully understand 
>>> how to read its output, I think it's saying the majority of runtime is 
>>> spent in quadgk itself. So perhaps I should look into using a different 
>>> integration library? 
>>>
>>> Thanks for any help. 
>>>
>>>

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-19 Thread Marius Millea
They *are* different algorithms, but when I was comparing speeds with the 
other codes, I compared it in terms of time per number of calls of the 
inner integrand function. So basically I'm testing the speed of the 
integrand functions themselves, as well as the speed of the integration 
library code, as well as any function call overhead type thing. With this 
metric, the Julia code was close, but it was the slowest (although of 
course far more succinct and easy to read). 


On Saturday, June 18, 2016 at 7:46:35 PM UTC+2, Gabriel Gellner wrote:
>
> What integration library are you using with Cython/Fortran? Is it using 
> the same algorithm as quadgk? Your code seems so simple I imagine this is 
> just comparing the quadrature implementations :)
>
> On Saturday, June 18, 2016 at 5:53:57 AM UTC-7, Marius Millea wrote:
>>
>> Hi all, I'm sort of just starting out with Julia, I'm trying to get gauge 
>> of how fast I can make some code of which I have Cython and Fortran 
>> versions to see if I should continue down the path of converting more or my 
>> stuff to Julia (which in general I'd very much like to, if I can get it 
>> fast enough). I thought maybe I'd post the code in question here to see if 
>> I could get any tips. I've stripped down the original thing to what I think 
>> are the important parts, a nested integration with an inner function 
>> closure and some global variables. 
>>
>> module test
>>
>> const a = 1.
>>
>> function f(x)
>> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
>> end
>>
>> function g(y)
>> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
>> quadgk(integrand,0,Inf)[1]   # <=== inner integral
>> end
>>
>> end
>>
>>
>> > @timeit test.f(1.)
>> 100 loops, best of 3: 3.10 ms per loop
>>
>>
>>
>>
>> Does anyone have any tips that squeezes a little more out of this code? I 
>> have run ProfileView on it, and although I'm not sure I fully understand 
>> how to read its output, I think it's saying the majority of runtime is 
>> spent in quadgk itself. So perhaps I should look into using a different 
>> integration library? 
>>
>> Thanks for any help. 
>>
>>

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-19 Thread Marius Millea
They *are* different algorithms, but when I was comparing speeds with the 
other codes, I compared it in terms of time per number of calls of the 
inner integrand function. So basically I'm testing the speed of the 
integrand functions themselves, as well as the speed of the integration 
library code, as well as any function call overhead type thing. With this 
metric, the Julia code was close, but it was the slowest (although of 
course far more succinct and easy to read). 

Marius




On Saturday, June 18, 2016 at 7:46:35 PM UTC+2, Gabriel Gellner wrote:
>
> What integration library are you using with Cython/Fortran? Is it using 
> the same algorithm as quadgk? Your code seems so simple I imagine this is 
> just comparing the quadrature implementations :)
>
> On Saturday, June 18, 2016 at 5:53:57 AM UTC-7, Marius Millea wrote:
>>
>> Hi all, I'm sort of just starting out with Julia, I'm trying to get gauge 
>> of how fast I can make some code of which I have Cython and Fortran 
>> versions to see if I should continue down the path of converting more or my 
>> stuff to Julia (which in general I'd very much like to, if I can get it 
>> fast enough). I thought maybe I'd post the code in question here to see if 
>> I could get any tips. I've stripped down the original thing to what I think 
>> are the important parts, a nested integration with an inner function 
>> closure and some global variables. 
>>
>> module test
>>
>> const a = 1.
>>
>> function f(x)
>> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
>> end
>>
>> function g(y)
>> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
>> quadgk(integrand,0,Inf)[1]   # <=== inner integral
>> end
>>
>> end
>>
>>
>> > @timeit test.f(1.)
>> 100 loops, best of 3: 3.10 ms per loop
>>
>>
>>
>>
>> Does anyone have any tips that squeezes a little more out of this code? I 
>> have run ProfileView on it, and although I'm not sure I fully understand 
>> how to read its output, I think it's saying the majority of runtime is 
>> spent in quadgk itself. So perhaps I should look into using a different 
>> integration library? 
>>
>> Thanks for any help. 
>>
>>

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-18 Thread Gabriel Gellner
What integration library are you using with Cython/Fortran? Is it using the 
same algorithm as quadgk? Your code seems so simple I imagine this is just 
comparing the quadrature implementations :)

On Saturday, June 18, 2016 at 5:53:57 AM UTC-7, Marius Millea wrote:
>
> Hi all, I'm sort of just starting out with Julia, I'm trying to get gauge 
> of how fast I can make some code of which I have Cython and Fortran 
> versions to see if I should continue down the path of converting more or my 
> stuff to Julia (which in general I'd very much like to, if I can get it 
> fast enough). I thought maybe I'd post the code in question here to see if 
> I could get any tips. I've stripped down the original thing to what I think 
> are the important parts, a nested integration with an inner function 
> closure and some global variables. 
>
> module test
>
> const a = 1.
>
> function f(x)
> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
> end
>
> function g(y)
> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
> quadgk(integrand,0,Inf)[1]   # <=== inner integral
> end
>
> end
>
>
> > @timeit test.f(1.)
> 100 loops, best of 3: 3.10 ms per loop
>
>
>
>
> Does anyone have any tips that squeezes a little more out of this code? I 
> have run ProfileView on it, and although I'm not sure I fully understand 
> how to read its output, I think it's saying the majority of runtime is 
> spent in quadgk itself. So perhaps I should look into using a different 
> integration library? 
>
> Thanks for any help. 
>
>

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-18 Thread Andrew
I don't think the anonymous y -> 1/g(y) and the nested function invg(y) = 
1//g(y) are any different in terms of performance. They both form closures 
over local variables and they should both be faster under 0.5.

I did run your code under 0.5dev and it was slower, but I think they're 
still fixing a variety of performance issues. I don't know much about it 
though.  

On Saturday, June 18, 2016 at 10:25:11 AM UTC-4, Marius Millea wrote:
>
> Ahh sorry, forget the 2x slower thing, I had accidentally changed 
> something else. Both the anonymous y->1/g(y) and invg(y) give essentially 
> the exact same run time. 
>
> There are a number of 1's and 0's, but AFAICT they shouldn't cause any 
> type instabilities, if the input variable y or x is a Float64, the output 
> should always be Float64 also. In any case I did check switching them to 1. 
> and 0.'s but that also has no effect. 
>
> Marius
>
>
>
>
> On Saturday, June 18, 2016 at 4:08:59 PM UTC+2, Eric Forgy wrote:
>>
>> Try code_warntype. I'm guessing you have some type instabilities, e.g. I 
>> see some 1's and 0's, where it might be better to use 1.0 and 0.0. Not sure 
>> :)
>>
>> On Saturday, June 18, 2016 at 9:48:29 PM UTC+8, Marius Millea wrote:
>>>
>>> Thanks, yea, I had read that too and at some point checked if it 
>>> mattered and it didn't seem to which wasn't entirely surprising since its 
>>> on the outer loop. 
>>>
>>> But I just checked again given your comment and on Julia 0.4.5 it seems 
>>> to actually be 2x slower if I switch it to this:
>>>
>>> function f(x)
>>> invg(y) = 1/g(y)
>>> quadgk(invg,0,x)[1]  # <=== outer integral
>>> end
>>>
>>> Odd...
>>>
>>>
>>> On Saturday, June 18, 2016 at 3:41:37 PM UTC+2, Eric Forgy wrote:

 Which version of Julia are you using? One thing that stands out is the 
 anonymous function y->1/g(y) being passed as an argument to quadgk. I'm 
 not 
 an expert, but I've heard this is slow in v0.4 and below, but should be 
 fast in v0.5. Just a though.

 On Saturday, June 18, 2016 at 8:53:57 PM UTC+8, Marius Millea wrote:
>
> Hi all, I'm sort of just starting out with Julia, I'm trying to get 
> gauge of how fast I can make some code of which I have Cython and Fortran 
> versions to see if I should continue down the path of converting more or 
> my 
> stuff to Julia (which in general I'd very much like to, if I can get it 
> fast enough). I thought maybe I'd post the code in question here to see 
> if 
> I could get any tips. I've stripped down the original thing to what I 
> think 
> are the important parts, a nested integration with an inner function 
> closure and some global variables. 
>
> module test
>
> const a = 1.
>
> function f(x)
> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
> end
>
> function g(y)
> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
> quadgk(integrand,0,Inf)[1]   # <=== inner integral
> end
>
> end
>
>
> > @timeit test.f(1.)
> 100 loops, best of 3: 3.10 ms per loop
>
>
>
>
> Does anyone have any tips that squeezes a little more out of this 
> code? I have run ProfileView on it, and although I'm not sure I fully 
> understand how to read its output, I think it's saying the majority of 
> runtime is spent in quadgk itself. So perhaps I should look into using a 
> different integration library? 
>
> Thanks for any help. 
>
>

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-18 Thread Eric Forgy
I'd be curious to see if any magic happens if you try to run it with v0.5 :)

Although not anonymous, you're still passing functions as arguments and I 
think that might be faster in v0.5, but I'm not sure.


[julia-users] Re: Tips for optimizing this short code snippet

2016-06-18 Thread Marius Millea
Ahh sorry, forget the 2x slower thing, I had accidentally changed something 
else. Both the anonymous y->1/g(y) and invg(y) give essentially the exact 
same run time. 

There are a number of 1's and 0's, but AFAICT they shouldn't cause any type 
instabilities, if the input variable y or x is a Float64, the output should 
always be Float64 also. In any case I did check switching them to 1. and 
0.'s but that also has no effect. 

Marius




On Saturday, June 18, 2016 at 4:08:59 PM UTC+2, Eric Forgy wrote:
>
> Try code_warntype. I'm guessing you have some type instabilities, e.g. I 
> see some 1's and 0's, where it might be better to use 1.0 and 0.0. Not sure 
> :)
>
> On Saturday, June 18, 2016 at 9:48:29 PM UTC+8, Marius Millea wrote:
>>
>> Thanks, yea, I had read that too and at some point checked if it mattered 
>> and it didn't seem to which wasn't entirely surprising since its on the 
>> outer loop. 
>>
>> But I just checked again given your comment and on Julia 0.4.5 it seems 
>> to actually be 2x slower if I switch it to this:
>>
>> function f(x)
>> invg(y) = 1/g(y)
>> quadgk(invg,0,x)[1]  # <=== outer integral
>> end
>>
>> Odd...
>>
>>
>> On Saturday, June 18, 2016 at 3:41:37 PM UTC+2, Eric Forgy wrote:
>>>
>>> Which version of Julia are you using? One thing that stands out is the 
>>> anonymous function y->1/g(y) being passed as an argument to quadgk. I'm not 
>>> an expert, but I've heard this is slow in v0.4 and below, but should be 
>>> fast in v0.5. Just a though.
>>>
>>> On Saturday, June 18, 2016 at 8:53:57 PM UTC+8, Marius Millea wrote:

 Hi all, I'm sort of just starting out with Julia, I'm trying to get 
 gauge of how fast I can make some code of which I have Cython and Fortran 
 versions to see if I should continue down the path of converting more or 
 my 
 stuff to Julia (which in general I'd very much like to, if I can get it 
 fast enough). I thought maybe I'd post the code in question here to see if 
 I could get any tips. I've stripped down the original thing to what I 
 think 
 are the important parts, a nested integration with an inner function 
 closure and some global variables. 

 module test

 const a = 1.

 function f(x)
 quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
 end

 function g(y)
 integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
 quadgk(integrand,0,Inf)[1]   # <=== inner integral
 end

 end


 > @timeit test.f(1.)
 100 loops, best of 3: 3.10 ms per loop




 Does anyone have any tips that squeezes a little more out of this code? 
 I have run ProfileView on it, and although I'm not sure I fully understand 
 how to read its output, I think it's saying the majority of runtime is 
 spent in quadgk itself. So perhaps I should look into using a different 
 integration library? 

 Thanks for any help. 



[julia-users] Re: Tips for optimizing this short code snippet

2016-06-18 Thread Eric Forgy
Try code_warntype. I'm guessing you have some type instabilities, e.g. I 
see some 1's and 0's, where it might be better to use 1.0 and 0.0. Not sure 
:)

On Saturday, June 18, 2016 at 9:48:29 PM UTC+8, Marius Millea wrote:
>
> Thanks, yea, I had read that too and at some point checked if it mattered 
> and it didn't seem to which wasn't entirely surprising since its on the 
> outer loop. 
>
> But I just checked again given your comment and on Julia 0.4.5 it seems to 
> actually be 2x slower if I switch it to this:
>
> function f(x)
> invg(y) = 1/g(y)
> quadgk(invg,0,x)[1]  # <=== outer integral
> end
>
> Odd...
>
>
> On Saturday, June 18, 2016 at 3:41:37 PM UTC+2, Eric Forgy wrote:
>>
>> Which version of Julia are you using? One thing that stands out is the 
>> anonymous function y->1/g(y) being passed as an argument to quadgk. I'm not 
>> an expert, but I've heard this is slow in v0.4 and below, but should be 
>> fast in v0.5. Just a though.
>>
>> On Saturday, June 18, 2016 at 8:53:57 PM UTC+8, Marius Millea wrote:
>>>
>>> Hi all, I'm sort of just starting out with Julia, I'm trying to get 
>>> gauge of how fast I can make some code of which I have Cython and Fortran 
>>> versions to see if I should continue down the path of converting more or my 
>>> stuff to Julia (which in general I'd very much like to, if I can get it 
>>> fast enough). I thought maybe I'd post the code in question here to see if 
>>> I could get any tips. I've stripped down the original thing to what I think 
>>> are the important parts, a nested integration with an inner function 
>>> closure and some global variables. 
>>>
>>> module test
>>>
>>> const a = 1.
>>>
>>> function f(x)
>>> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
>>> end
>>>
>>> function g(y)
>>> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
>>> quadgk(integrand,0,Inf)[1]   # <=== inner integral
>>> end
>>>
>>> end
>>>
>>>
>>> > @timeit test.f(1.)
>>> 100 loops, best of 3: 3.10 ms per loop
>>>
>>>
>>>
>>>
>>> Does anyone have any tips that squeezes a little more out of this code? 
>>> I have run ProfileView on it, and although I'm not sure I fully understand 
>>> how to read its output, I think it's saying the majority of runtime is 
>>> spent in quadgk itself. So perhaps I should look into using a different 
>>> integration library? 
>>>
>>> Thanks for any help. 
>>>
>>>

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-18 Thread Marius Millea
Thanks, yea, I had read that too and at some point checked if it mattered 
and it didn't seem to which wasn't entirely surprising since its on the 
outer loop. 

But I just checked again given your comment and on Julia 0.4.5 it seems to 
actually be 2x slower if I switch it to this:

function f(x)
invg(y) = 1/g(y)
quadgk(invg,0,x)[1]  # <=== outer integral
end

Odd...


On Saturday, June 18, 2016 at 3:41:37 PM UTC+2, Eric Forgy wrote:
>
> Which version of Julia are you using? One thing that stands out is the 
> anonymous function y->1/g(y) being passed as an argument to quadgk. I'm not 
> an expert, but I've heard this is slow in v0.4 and below, but should be 
> fast in v0.5. Just a though.
>
> On Saturday, June 18, 2016 at 8:53:57 PM UTC+8, Marius Millea wrote:
>>
>> Hi all, I'm sort of just starting out with Julia, I'm trying to get gauge 
>> of how fast I can make some code of which I have Cython and Fortran 
>> versions to see if I should continue down the path of converting more or my 
>> stuff to Julia (which in general I'd very much like to, if I can get it 
>> fast enough). I thought maybe I'd post the code in question here to see if 
>> I could get any tips. I've stripped down the original thing to what I think 
>> are the important parts, a nested integration with an inner function 
>> closure and some global variables. 
>>
>> module test
>>
>> const a = 1.
>>
>> function f(x)
>> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
>> end
>>
>> function g(y)
>> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
>> quadgk(integrand,0,Inf)[1]   # <=== inner integral
>> end
>>
>> end
>>
>>
>> > @timeit test.f(1.)
>> 100 loops, best of 3: 3.10 ms per loop
>>
>>
>>
>>
>> Does anyone have any tips that squeezes a little more out of this code? I 
>> have run ProfileView on it, and although I'm not sure I fully understand 
>> how to read its output, I think it's saying the majority of runtime is 
>> spent in quadgk itself. So perhaps I should look into using a different 
>> integration library? 
>>
>> Thanks for any help. 
>>
>>

[julia-users] Re: Tips for optimizing this short code snippet

2016-06-18 Thread Eric Forgy
Which version of Julia are you using? One thing that stands out is the 
anonymous function y->1/g(y) being passed as an argument to quadgk. I'm not 
an expert, but I've heard this is slow in v0.4 and below, but should be 
fast in v0.5. Just a though.

On Saturday, June 18, 2016 at 8:53:57 PM UTC+8, Marius Millea wrote:
>
> Hi all, I'm sort of just starting out with Julia, I'm trying to get gauge 
> of how fast I can make some code of which I have Cython and Fortran 
> versions to see if I should continue down the path of converting more or my 
> stuff to Julia (which in general I'd very much like to, if I can get it 
> fast enough). I thought maybe I'd post the code in question here to see if 
> I could get any tips. I've stripped down the original thing to what I think 
> are the important parts, a nested integration with an inner function 
> closure and some global variables. 
>
> module test
>
> const a = 1.
>
> function f(x)
> quadgk(y->1/g(y),0,x)[1]  # <=== outer integral
> end
>
> function g(y)
> integrand(x) = x^2*sqrt(x^2*y^2+a)/(exp(sqrt(x^2+y^2))+a)
> quadgk(integrand,0,Inf)[1]   # <=== inner integral
> end
>
> end
>
>
> > @timeit test.f(1.)
> 100 loops, best of 3: 3.10 ms per loop
>
>
>
>
> Does anyone have any tips that squeezes a little more out of this code? I 
> have run ProfileView on it, and although I'm not sure I fully understand 
> how to read its output, I think it's saying the majority of runtime is 
> spent in quadgk itself. So perhaps I should look into using a different 
> integration library? 
>
> Thanks for any help. 
>
>