Hello Steve,
  I'm not up to date on the new broadcast functionality, but I can say that 
the function norm does not heap allocate any memory, the heap allocation is 
coming from the x[p:p+2], which creates a new array of length 3 and copies 
the values from x into it.  There are a few solutions for creating 
sub-vectors that reference the existing array rather creating a new one. To 
do this without allocation, I use the `unsafe_view` from the ArrayViews 
package.  You can do `nr = norm(unsafe_view( x, p:p+2)).  The downside is 
there is no boundschecking with `unsafe_view`s, and you have to ensure a 
reference to the original array x exists somewhere (otherwise it might get 
garbage collected).  You can also use `view` from the same package, which 
does not have those downsides, but it does get heap allocated.  You can 
also use `sub` (renamed `view` in 0.5), which is part of Base, and works on 
any AbstractArray (ArrayViews only work on Arrays), but also gets heap 
allocated.

  Jared Crean

On Sunday, September 25, 2016 at 11:19:39 AM UTC-4, vav...@uwaterloo.ca 
wrote:
>
> My code has many operations on small matrices and vectors.  In Julia 0.4, 
> carrying these out with subvector operations causes a needless heap 
> allocation, so I've done them all with loops.  In other words, instead of 
>     nr = norm(x[p:p+2])
>
> I write
>
>     nr = 0.0
>     for j = p : p + 2
>         nr += x[j]^2
>     end
>     nr = sqrt(nr)
>
> I've also used the Einsum.jl macro package, which implicitly writes the 
> loops.
>
> My question is: do the new broadcast and generator-comprehension 
> operations available in 0.5 or 0.6 make it possible to accomplish these 
> small operations using one-line solutions that don't allocate heap memory? 
>
> Here are some other examples of operations for which I would like 
> efficient one-liners:
>     
>      t = dot(v[p:p+3], w[p:p+3]) 
>      c += A[p:p+2, q:q+2] * v[q:q+2]  # "gaxpy"; c is a length-3 vector
>
> Thanks,
> Steve Vavasis
>
>

Reply via email to