[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread Christoph Ortner
also look at `StaticArrays` and `reinterpret`

E.g.,
R = reinterpret(SVector{3,Float64}, r, m)

I use this a lot and find it very convenient.


[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread Niccolo' Antonello
Thank you so much for all your help! ;)

On Tuesday, October 4, 2016 at 4:12:43 PM UTC+2, Greg Plowman wrote:
>
> Not sure what the best way is, especially on Julia 0.5 where maybe some 
> sort of view.
>
> But you could always use an Array of Arrays if you don't really need a 
> true multi-dimensional array elsewhere:
>
> d = randn(3,n)
> d2 = [ d[:,i]::Vector{Float64} for i=1:n ]
> r = randn(3,m)
> r2 = [ r[:,i]::Vector{Float64} for i=1:m ]
>
> function nested_loop!(Y::Array{Complex{Float64}},X::Array{Complex{Float64
> }},
>   d::Vector{Vector{Float64}},r::Vector{Vector{Float64
> }},n::Int64,m::Int64,f::Int64)
> for i_1 = 1:f
> for i_3 = 1:n
> k = 2*π*i_1*d[i_3]
> for i_2 = 1:m
> A = exp(im*dot(k,r[i_2]))
> Y[i_1,i_2] += X[i_1,i_3] * A
> end
> end
> end
> end
>
>  nested_loop!(Y,X,d2,r2,n,m,f)
>
> On V0.4 this has less allocations and runs slightly faster (maybe 2-3x)
>
>
> On Tuesday, October 4, 2016 at 11:45:53 PM UTC+11, Niccolo' Antonello 
> wrote:
>
>> what is the best way to slice an array without allocating? I thank you!
>>
>> On Tuesday, October 4, 2016 at 1:20:29 PM UTC+2, Kristoffer Carlsson 
>> wrote:
>>>
>>> The slicing of "r" will also make a new array and thus allocate. 
>>
>>

[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread 'Greg Plowman' via julia-users
Not sure what the best way is, especially on Julia 0.5 where maybe some 
sort of view.

But you could always use an Array of Arrays if you don't really need a true 
multi-dimensional array elsewhere:

d = randn(3,n)
d2 = [ d[:,i]::Vector{Float64} for i=1:n ]
r = randn(3,m)
r2 = [ r[:,i]::Vector{Float64} for i=1:m ]

function nested_loop!(Y::Array{Complex{Float64}},X::Array{Complex{Float64}},
  d::Vector{Vector{Float64}},r::Vector{Vector{Float64}},
n::Int64,m::Int64,f::Int64)
for i_1 = 1:f
for i_3 = 1:n
k = 2*π*i_1*d[i_3]
for i_2 = 1:m
A = exp(im*dot(k,r[i_2]))
Y[i_1,i_2] += X[i_1,i_3] * A
end
end
end
end

 nested_loop!(Y,X,d2,r2,n,m,f)

On V0.4 this has less allocations and runs slightly faster (maybe 2-3x)


On Tuesday, October 4, 2016 at 11:45:53 PM UTC+11, Niccolo' Antonello wrote:

> what is the best way to slice an array without allocating? I thank you!
>
> On Tuesday, October 4, 2016 at 1:20:29 PM UTC+2, Kristoffer Carlsson wrote:
>>
>> The slicing of "r" will also make a new array and thus allocate. 
>
>

[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread Andrew
You can do view(r, :, i_2) in 0.5. I think slice does the same thing in 0.4.

Also as a general point, you would have realized you hadn't defined r in 
the function if you had wrapped your entire program in a function, like
function test()
(your code)
end
test()
This would throw an error with your original code since r would not be in 
scope in nested_loop!().

I occasionally have accidentally used outside global variables in my 
functions like you did, and this can lead to bizarre and difficult to find 
bugs.

On Tuesday, October 4, 2016 at 8:45:53 AM UTC-4, Niccolo' Antonello wrote:
>
> what is the best way to slice an array without allocating? I thank you!
>
> On Tuesday, October 4, 2016 at 1:20:29 PM UTC+2, Kristoffer Carlsson wrote:
>>
>> The slicing of "r" will also make a new array and thus allocate. 
>
>

[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread Niccolo' Antonello
what is the best way to slice an array without allocating? I thank you!

On Tuesday, October 4, 2016 at 1:20:29 PM UTC+2, Kristoffer Carlsson wrote:
>
> The slicing of "r" will also make a new array and thus allocate. 



[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread Niccolo' Antonello
Thank you so much both of you!

On Tuesday, October 4, 2016 at 1:20:29 PM UTC+2, Kristoffer Carlsson wrote:
>
> The slicing of "r" will also make a new array and thus allocate. 



[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread Kristoffer Carlsson
The slicing of "r" will also make a new array and thus allocate. 

[julia-users] Re: memory allocation in nested loops

2016-10-04 Thread 'Greg Plowman' via julia-users
You haven't passed in r as argument to function.
Try nested_loop!(Y,X,d,r,n,m,f)


On Tuesday, October 4, 2016 at 8:05:41 PM UTC+11, Niccolo' Antonello wrote:

> Hi all,
>
> I'm trying to make this code to go faster:
>
> n,m = 100,25
> f = 100
> d = randn(3,n)
> r = randn(3,m)
>
> Y = zeros(Complex{Float64},f,m)
> X = ones(f,n)+im*ones(f,n)
>
> function 
> nested_loop!(Y::Array{Complex{Float64}},X::Array{Complex{Float64}},
>   d::Array{Float64,2},n::Int64,m::Int64,f::Int64)
> for i_1 = 1:f
> for i_3 = 1:n
> k = 2*π*i_1*d[:,i_3]
> for i_2 = 1:m
> A = exp(im*dot(k,r[:,i_2]))
> Y[i_1,i_2] += X[i_1,i_3] * A
> end
> end
> end
> end
>
> @time nested_loop!(Y,X,d,n,m,f)
> Profile.clear_malloc_data()
> Y = zeros(Complex{Float64},f,m)
> @time nested_loop!(Y,X,d,n,m,f)
>
> I see that it is allocating a lot of memory. 
> So I also tried to put k and A on Arrays and preallocate that, but this 
> didn't remove the allocations. 
> Could you please tell me what could be possibly done to make this code 
> faster?
> I thank you! 
>
>