Skip to content

Commit

Permalink
Array connection improvements.
Browse files Browse the repository at this point in the history
- Replace : subscripts with 1:size subscripts.
- Fill out connector crefs with : subscripts such that all dimensions
  are subscripted.
- Pad the maps for each connector so they all have the same dimensions.
  • Loading branch information
perost committed Oct 23, 2020
1 parent 292691b commit 9839b82
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
19 changes: 15 additions & 4 deletions OMCompiler/Compiler/NFFrontEnd/NFArrayConnections.mo
Expand Up @@ -308,10 +308,10 @@ protected
input output ComponentRef cref;
output list<Subscript> subs;
algorithm
cref := ComponentRef.fillSubscripts(cref);
cref := ComponentRef.replaceWholeSubscripts(cref);
subs := ComponentRef.subscriptsAllFlat(cref);
cref := ComponentRef.stripSubscriptsAll(cref);

// TODO: Replace any : in the subs with the correct dimension.
end separate;

function getConnectIntervals
Expand Down Expand Up @@ -346,7 +346,7 @@ protected
mi := Array.map(miv, make_lo_interval);
else
index := 1;
mi := arrayCreate(listLength(subs), SBInterval.newEmpty());
mi := arrayCopy(miv);

for s in subs loop
sub_exp := evalCrefs(Subscript.toExp(s));
Expand All @@ -365,6 +365,11 @@ protected

index := index + 1;
end for;

for i in listLength(subs)+1:arrayLength(mi) loop
aux_lo := SBInterval.lowerBound(miv[i]);
mi[index] := SBInterval.new(aux_lo, 1, aux_lo);
end for;
end if;

outMI := SBMultiInterval.fromArray(mi);
Expand Down Expand Up @@ -406,7 +411,7 @@ protected

mi := SBMultiInterval.fromArray(arrayCreate(Vector.size(vCount), SBInterval.new(vc, 1, vc)));
else
ints := arrayCreate(listLength(dims), SBInterval.newEmpty());
ints := arrayCreate(Vector.size(vCount), SBInterval.newEmpty());
new_vc := Vector.copy(vCount);
index := 1;

Expand All @@ -431,6 +436,11 @@ protected
index := index + 1;
end for;

for i in listLength(dims)+1:Vector.size(vCount) loop
vc := Vector.get(vCount, 1);
ints[i] := SBInterval.new(vc, 1, vc);
end for;

mi := SBMultiInterval.fromArray(ints);

if not SBMultiInterval.isEmpty(mi) then
Expand Down Expand Up @@ -899,6 +909,7 @@ protected

if Type.isArray(Expression.typeOf(outExp)) then
subs := list(Subscript.fromTypedExp(i) for i in indices);
subs := List.firstN(subs, Type.dimensionCount(Expression.typeOf(outExp)));
outExp := Expression.applySubscripts(subs, outExp);
end if;
end generateConnector;
Expand Down
63 changes: 63 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -44,6 +44,7 @@ protected
import Class = NFClass;
import List;
import Prefixes = NFPrefixes;
import MetaModelica.Dangerous.*;

import ComponentRef = NFComponentRef;

Expand Down Expand Up @@ -572,6 +573,68 @@ public
end match;
end foldSubscripts;

function fillSubscripts
"Fills in any unsubscripted dimensions in the cref with : subscripts."
input output ComponentRef cref;
algorithm
() := match cref
local
list<Dimension> dims;
Integer dim_count, sub_count;

case CREF()
algorithm
dims := Type.arrayDims(cref.ty);
dim_count := listLength(dims);
sub_count := listLength(cref.subscripts);

if sub_count < dim_count then
cref.subscripts := List.consN(dim_count - sub_count, Subscript.WHOLE(), cref.subscripts);
end if;

cref.restCref := fillSubscripts(cref.restCref);
then
();

else ();
end match;
end fillSubscripts;

function replaceWholeSubscripts
"Replaces any : subscripts with slice subscripts for the corresponding dimension."
input output ComponentRef cref;
algorithm
() := match cref
local
list<Dimension> dims;
list<Subscript> subs;

case CREF()
algorithm
if List.exist(cref.subscripts, Subscript.isWhole) then
dims := Type.arrayDims(cref.ty);
subs := {};

for s in cref.subscripts loop
if Subscript.isWhole(s) then
s := Subscript.fromDimension(listHead(dims));
end if;

subs := s :: subs;
dims := listRest(dims);
end for;

cref.subscripts := listReverseInPlace(subs);
end if;

cref.restCref := replaceWholeSubscripts(cref.restCref);
then
();

else ();
end match;
end replaceWholeSubscripts;

function compare
input ComponentRef cref1;
input ComponentRef cref2;
Expand Down
21 changes: 21 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFSubscript.mo
Expand Up @@ -727,6 +727,27 @@ public
end match;
end toDimension;

function fromDimension
"Returns a slice subscripts that covers the given dimension.
Will fail for untyped or unknown dimensions."
input Dimension dimension;
output Subscript subscript;
algorithm
subscript := match dimension
case Dimension.INTEGER()
then Subscript.SLICE(Expression.makeIntegerRange(1, 1, dimension.size));
case Dimension.BOOLEAN()
then Subscript.SLICE(Expression.makeRange(Expression.BOOLEAN(false), NONE(), Expression.BOOLEAN(true)));
case Dimension.ENUM()
then Subscript.SLICE(Expression.makeRange(
Expression.makeEnumLiteral(dimension.enumType, 1),
NONE(),
Expression.makeEnumLiteral(dimension.enumType, Type.enumSize(dimension.enumType))));
case Dimension.EXP()
then Subscript.SLICE(Expression.makeRange(Expression.INTEGER(1), NONE(), dimension.exp));
end match;
end fromDimension;

function scalarize
input Subscript subscript;
input Dimension dimension;
Expand Down

0 comments on commit 9839b82

Please sign in to comment.