Opened 15 years ago
Last modified 14 years ago
#1173 closed defect (fixed)
Unroll for variables in algorithms
Reported by: | bjozac | Owned by: | bjozac |
---|---|---|---|
Priority: | critical | Milestone: | |
Component: | Version: | ||
Keywords: | Cc: | bjozac, adrpo |
Description
consider following model
model testDiag Real x; Integer offset; Real arr[3,3]; Real arr2[3,3]=fill(999,3,3); equation der(x) = x+time; offset = floor(x); algorithm arr[1,1]:=1.1;arr[1,2]:=1.2; arr[2,1]:=2.1;arr[2,2]:=2.2; arr[3,1]:=3.1;arr[3,2]:=3.2; for k in 1:max(size(arr2)) loop arr[k + integer(abs(offset)),k] := arr2[k,1]; end for; end testDiag; model algorithmFor testDiag dl; end algorithmFor;
This will generate the following flat code;
for k in {1,2,3} loop dl.arr[dl.k + integer(Real(abs(dl.offset))), dl.k] := dl.arr2[dl.k, 1]; end for;
but if your move this part into equation section
for k in 1:max(size(arr2)) loop arr[k + integer(abs(offset)),k] = arr2[k,1]; end for;
we get
dl.arr[1 + integer(Real(abs(dl.offset))), 1] = dl.arr2[1,1]; dl.arr[2 + integer(Real(abs(dl.offset))), 2] = dl.arr2[2,1]; dl.arr[3 + integer(Real(abs(dl.offset))), 3] = dl.arr2[3,1];
So in the back end we get a compile error due to that dl.k does not exists.
Change History (2)
comment:1 Changed 15 years ago by adrpo
comment:2 Changed 15 years ago by adrpo
We talked a bit about this at the meeting yesterday.
For the for iterators in algorithms we should have 2
constantness one for the actual iterator which should
"always" be SCode.VAR and one for the range which
can be SCode.CONST.
If the range is CONST then we can unroll the loop, otherwise
we can't. Example why iterators should be SCode.VAR:
for i in 1:10 loop for j in 1:i loop ... end for; end for;
In the case above if we set i to have SCode.CONST
then the range 1:i will be constant which it shouldn't be.
I already started looking into this so I will fix it myself.
This is a problem with Prefix.prefixExp, it does lookup of the iterator
and if it finds it in the environment [which of course it does in the
"$for loop scope$"] it will prefix it!
The iterators should never be prefixed, or should also be prefixed in
the for prefix.iterator in .... .