Skip to content

Commit

Permalink
counting for preallocation in sum was broken with dependent iterates
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Jun 10, 2013
1 parent 478f6df commit 8ede799
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ function parseCurly(x::Expr, aff::Symbol, constantCoef)
len = gensym()
# precompute the number of elements to add
# this is unncessary if we're just summing constants
preblock = quote $len = 1 end
for level in length(x.args):-1:3
push!(preblock.args,:($len *= length($(x.args[level].args[2]))))
preblock = :($len += length($(x.args[length(x.args)].args[2])))
for level in (length(x.args)-1):-1:3
preblock = Expr(:for, x.args[level],preblock)
end
push!(preblock.args,:(
preblock = :($len = 1; $preblock;
sizehint($aff.vars,length($aff.vars)+$len);
sizehint($aff.coeffs,length($aff.coeffs)+$len)))
sizehint($aff.coeffs,length($aff.coeffs)+$len))
code = :($preblock;$code)
end

Expand Down

2 comments on commit 8ede799

@IainNZ
Copy link
Collaborator

@IainNZ IainNZ commented on 8ede799 Jun 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? Can we write a unit test for it?

@mlubin
Copy link
Member Author

@mlubin mlubin commented on 8ede799 Jun 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is with sum{ a[i][j]*x[i][j], i = 1:N, j = 1:foobar[i] }.
The previous code was trying to do len = length(1:N)*length(1:foobar[i]) which doesn't make sense. I changed this to

for i in 1:N
    len += length(1:foobar[i])
end

Please sign in to comment.