@@ -75,14 +75,11 @@ public void processInstruction( ExecutionContext ec ) {
7575 int blen = ConfigurationManager .getBlocksize ();
7676
7777 if (aggun .isRowAggregate () || aggun .isColAggregate ()) {
78- // intermediate state per aggregation index
79- HashMap <Long , MatrixBlock > aggs = new HashMap <>(); // partial aggregates
80- HashMap <Long , MatrixBlock > corrs = new HashMap <>(); // correction blocks
81- HashMap <Long , Integer > cnt = new HashMap <>(); // processed block count per agg idx
82-
8378 DataCharacteristics chars = ec .getDataCharacteristics (input1 .getName ());
8479 // number of blocks to process per aggregation idx (row or column dim)
85- long nBlocks = aggun .isRowAggregate ()? chars .getNumColBlocks () : chars .getNumRowBlocks ();
80+ long emitThreshold = aggun .isRowAggregate ()? chars .getNumColBlocks () : chars .getNumRowBlocks ();
81+ OOCMatrixBlockTracker aggTracker = new OOCMatrixBlockTracker (emitThreshold );
82+ HashMap <Long , MatrixBlock > corrs = new HashMap <>(); // correction blocks
8683
8784 LocalTaskQueue <IndexedMatrixValue > qOut = new LocalTaskQueue <>();
8885 ec .getMatrixObject (output ).setStreamHandle (qOut );
@@ -94,9 +91,8 @@ public void processInstruction( ExecutionContext ec ) {
9491 while ((tmp = q .dequeueTask ()) != LocalTaskQueue .NO_MORE_TASKS ) {
9592 long idx = aggun .isRowAggregate () ?
9693 tmp .getIndexes ().getRowIndex () : tmp .getIndexes ().getColumnIndex ();
97- if (aggs .containsKey (idx )) {
98- // update existing partial aggregate for this idx
99- MatrixBlock ret = aggs .get (idx );
94+ MatrixBlock ret = aggTracker .get (idx );
95+ if (ret != null ) {
10096 MatrixBlock corr = corrs .get (idx );
10197
10298 // aggregation
@@ -105,17 +101,18 @@ public void processInstruction( ExecutionContext ec ) {
105101 OperationsOnMatrixValues .incrementalAggregation (ret ,
106102 _aop .existsCorrection () ? corr : null , ltmp , _aop , true );
107103
108- aggs .replace (idx , ret );
109- corrs .replace (idx , corr );
110- cnt .replace (idx , cnt .get (idx ) + 1 );
104+ if (!aggTracker .putAndIncrementCount (idx , ret )){
105+ corrs .replace (idx , corr );
106+ continue ;
107+ }
111108 }
112109 else {
113110 // first block for this idx - init aggregate and correction
114111 // TODO avoid corr block for inplace incremental aggregation
115112 int rows = tmp .getValue ().getNumRows ();
116113 int cols = tmp .getValue ().getNumColumns ();
117114 int extra = _aop .correction .getNumRemovedRowsColumns ();
118- MatrixBlock ret = aggun .isRowAggregate ()? new MatrixBlock (rows , 1 + extra , false ) : new MatrixBlock (1 + extra , cols , false );
115+ ret = aggun .isRowAggregate ()? new MatrixBlock (rows , 1 + extra , false ) : new MatrixBlock (1 + extra , cols , false );
119116 MatrixBlock corr = aggun .isRowAggregate ()? new MatrixBlock (rows , 1 + extra , false ) : new MatrixBlock (1 + extra , cols , false );
120117
121118 // aggregation
@@ -124,25 +121,24 @@ public void processInstruction( ExecutionContext ec ) {
124121 OperationsOnMatrixValues .incrementalAggregation (ret ,
125122 _aop .existsCorrection () ? corr : null , ltmp , _aop , true );
126123
127- aggs .put (idx , ret );
128- corrs .put (idx , corr );
129- cnt .put (idx , 1 );
124+ if (emitThreshold > 1 ){
125+ aggTracker .putAndIncrementCount (idx , ret );
126+ corrs .put (idx , corr );
127+ continue ;
128+ }
130129 }
131130
132- if (cnt .get (idx ) == nBlocks ) {
133- // all input blocks for this idx processed - emit aggregated block
134- MatrixBlock ret = aggs .get (idx );
135- // drop correction row/col
136- ret .dropLastRowsOrColumns (_aop .correction );
137- MatrixIndexes midx = aggun .isRowAggregate ()? new MatrixIndexes (tmp .getIndexes ().getRowIndex (), 1 ) : new MatrixIndexes (1 , tmp .getIndexes ().getColumnIndex ());
138- IndexedMatrixValue tmpOut = new IndexedMatrixValue (midx , ret );
139-
140- qOut .enqueueTask (tmpOut );
141- // drop intermediate states
142- aggs .remove (idx );
143- corrs .remove (idx );
144- cnt .remove (idx );
145- }
131+ // all input blocks for this idx processed - emit aggregated block
132+ ret .dropLastRowsOrColumns (_aop .correction );
133+ MatrixIndexes midx = aggun .isRowAggregate () ?
134+ new MatrixIndexes (tmp .getIndexes ().getRowIndex (), 1 ) :
135+ new MatrixIndexes (1 , tmp .getIndexes ().getColumnIndex ());
136+ IndexedMatrixValue tmpOut = new IndexedMatrixValue (midx , ret );
137+
138+ qOut .enqueueTask (tmpOut );
139+ // drop intermediate states
140+ aggTracker .remove (idx );
141+ corrs .remove (idx );
146142 }
147143 qOut .closeInput ();
148144 }
0 commit comments