As long as you're using the return value from qr(), qr is always allocating
the memory for "a" inside itself.  You need to use qrfact!() to avoid some
allocation:

julia> A = randn(4,4)
4x4 Array{Float64,2}:
  1.10997    1.27173   -0.997429   -0.916055
  1.50834    0.691077   0.0358451  -0.200697
 -0.522316   1.27148    1.32542     0.776502
  0.622128  -0.567629   0.331535    0.829938


julia> qrfact!(A)
QRCompactWY{Float64}(4x4 Array{Float64,2}:
 -2.04132   -0.703816   0.753963   0.592149
  0.478643   1.88106    0.416794  -0.19707
 -0.165747  -0.74871   -1.45624   -1.26801
  0.19742    0.448426   0.166636   0.416953,4x4
Triangular{Float64,Array{Float64,2},:U,false}:
 1.54375  -1.21153  -1.18989  0.0
 0.0       1.1353    1.489    0.0
 0.0       0.0       1.94597  0.0
 0.0       0.0       0.0      0.0)

julia> A
4x4 Array{Float64,2}:
 -2.04132   -0.703816   0.753963   0.592149
  0.478643   1.88106    0.416794  -0.19707
 -0.165747  -0.74871   -1.45624   -1.26801
  0.19742    0.448426   0.166636   0.416953

So you can see that A has been clobbered, because we're reusing that memory.
-E

On Tue, Oct 7, 2014 at 4:04 PM, Ryan Young <[email protected]> wrote:

> Thanks for the reply.
>
> I think this still suffers from the same issue of allocating tmp every
> iteration through the loop. I only need to store the output of qr() if
> certain criteria are met. Then I do as you recommended and push it into a
> array. Consider the following
>
> function test_qr3(iters)
>     tmp=rand(3,3)
>     a=Array{Array{Float64,2},1}
>     for i=1:iters
>         a=qr(tmp)
>     end
> end
>
> julia> @time test_qr3(100000)
> elapsed time: 0.474589094 seconds (147200224 bytes allocated)
>
> julia> @time test_qr3(200000)
> elapsed time: 0.90662408 seconds (294400224 bytes allocated)
>
>
> While this is a trivial example, there should be a way to make it so that
> 'a' is not re-alloacted each time through the loop. Or maybe the allocation
> is happening within qr()?
>
> Thanks
>
>
>
>
> On Tuesday, October 7, 2014 3:29:01 PM UTC-7, Max Suster wrote:
>>
>>
>> You could try this.
>>
>> a= Array{Float64,2}[]
>> b= Array{Float64,2}[]
>>
>> function test_qr(a,b)
>>
>>   for i=1:100000
>>     tmp = qr(rand(3,3))
>>     push!(a,tmp[1])
>>     push!(b,tmp[2])
>>   end
>>
>> end
>>
>> julia> size(a,1)
>> 100000
>>
>> julia> size(b,1)
>> 100000
>>
>>
>> On Tuesday, October 7, 2014 10:17:07 PM UTC+2, Ryan Young wrote:
>>>
>>> Hello,
>>>
>>> I am working on optimizing a code that looks at 3D deformation fields.
>>> At one point I have to take the QR decompositions at many points. My
>>> problem is that regardless of how I  pre-allocate my variables to store the
>>> results of the QR, they are allocated each time through the loop. I tried
>>> to use the qrfact! method but found it suffered from the same problem.  Any
>>> help in getting around this would be great.  A basic function that has the
>>> same problem is below.
>>>
>>> function test_qr()
>>>     tmp=rand(3,3)
>>>     a=Array(Float64,2)
>>>     b=Array(Float64,2)
>>>
>>>     for i=1:100000
>>>         a,b=qr(tmp)
>>>     end
>>>
>>> end
>>>
>>

Reply via email to