3838import java .util .stream .Collectors ;
3939import java .util .stream .IntStream ;
4040
41- import org .apache .commons .lang3 .ArrayUtils ;
4241import org .apache .commons .lang3 .NotImplementedException ;
4342import org .apache .commons .lang3 .concurrent .ConcurrentUtils ;
4443import org .apache .commons .logging .Log ;
5655import org .apache .sysds .runtime .compress .CompressedMatrixBlock ;
5756import org .apache .sysds .runtime .compress .DMLCompressionException ;
5857import org .apache .sysds .runtime .compress .lib .CLALibAggTernaryOp ;
59- import org .apache .sysds .runtime .compress .lib .CLALibCBind ;
6058import org .apache .sysds .runtime .compress .lib .CLALibMerge ;
6159import org .apache .sysds .runtime .compress .lib .CLALibTernaryOp ;
6260import org .apache .sysds .runtime .controlprogram .caching .CacheBlock ;
@@ -891,7 +889,7 @@ public void appendToSparse( MatrixBlock that, int rowoffset, int coloffset ) {
891889 appendToSparse (that , rowoffset , coloffset , true );
892890 }
893891
894- private void appendToSparse ( MatrixBlock that , int rowoffset , int coloffset , boolean deep )
892+ protected void appendToSparse ( MatrixBlock that , int rowoffset , int coloffset , boolean deep )
895893 {
896894 if ( that ==null || that .isEmptyBlock (false ) )
897895 return ; //nothing to append
@@ -3656,7 +3654,7 @@ public final MatrixBlock append(MatrixBlock that, MatrixBlock ret ) {
36563654 }
36573655
36583656 /**
3659- * Append that list of matrixblocks to this .
3657+ * Append that list of matrixblocks together .
36603658 *
36613659 * @param that That list.
36623660 * @param ret The output block
@@ -3681,38 +3679,10 @@ public static MatrixBlock append(List<MatrixBlock> that, MatrixBlock ret, boolea
36813679 * @param cbind if binding on columns or rows
36823680 * @return the ret MatrixBlock object with the appended result
36833681 */
3684- public final MatrixBlock append ( MatrixBlock that , MatrixBlock ret , boolean cbind ) {
3682+ public final MatrixBlock append (MatrixBlock that , MatrixBlock ret , boolean cbind ) {
36853683 return append (new MatrixBlock []{that }, ret , cbind );
36863684 }
36873685
3688- private final long calculateCombinedNNz (MatrixBlock [] that ){
3689- long nnz = nonZeros ;
3690- for (MatrixBlock b : that )
3691- nnz += b .nonZeros ;
3692- return nnz ;
3693- }
3694-
3695- private final int combinedRows (MatrixBlock [] that ){
3696- int r = rlen ;
3697- for (MatrixBlock b : that )
3698- r += b .rlen ;
3699- return r ;
3700- }
3701-
3702- private final int combinedCols (MatrixBlock [] that ){
3703- int c = clen ;
3704- for (MatrixBlock b : that )
3705- c += b .clen ;
3706- return c ;
3707- }
3708-
3709- private final int computeNNzRow (MatrixBlock [] that , int row ) {
3710- int lnnz = (int ) this .recomputeNonZeros (row , row , 0 , this .clen - 1 );
3711- for (MatrixBlock b : that )
3712- lnnz += b .recomputeNonZeros (row , row , 0 , b .clen - 1 );
3713- return lnnz ;
3714- }
3715-
37163686 /**
37173687 * Append that list of matrixes to this matrix.
37183688 *
@@ -3724,119 +3694,7 @@ private final int computeNNzRow(MatrixBlock[] that, int row) {
37243694 * @return the ret MatrixBlock object with the appended result
37253695 */
37263696 public MatrixBlock append (MatrixBlock [] that , MatrixBlock result , boolean cbind ) {
3727- checkDimensionsForAppend (that , cbind );
3728-
3729- for (int k = 0 ; k < that .length ; k ++)
3730- if ( that [k ] instanceof CompressedMatrixBlock ){
3731- if (that .length == 1 && cbind )
3732- return CLALibCBind .cbind (this , that [0 ], 1 );
3733- that [k ] = CompressedMatrixBlock .getUncompressed (that [k ], "Append N" );
3734- }
3735-
3736- final int m = cbind ? rlen : combinedRows (that );
3737- final int n = cbind ? combinedCols (that ) : clen ;
3738- final long nnz = calculateCombinedNNz (that );
3739-
3740- boolean shallowCopy = (nonZeros == nnz );
3741- boolean sp = evalSparseFormatInMemory (m , n , nnz );
3742-
3743- //init result matrix
3744- if ( result == null )
3745- result = new MatrixBlock (m , n , sp , nnz );
3746- else
3747- result .reset (m , n , sp , nnz );
3748-
3749- //core append operation
3750- //copy left and right input into output
3751- if ( !result .sparse && nnz !=0 ) //DENSE
3752- {
3753- if ( cbind ) {
3754- DenseBlock resd = result .allocateBlock ().getDenseBlock ();
3755- MatrixBlock [] in = ArrayUtils .addAll (new MatrixBlock []{this }, that );
3756-
3757- for ( int i =0 ; i <m ; i ++ ) {
3758- for ( int k =0 , off =0 ; k <in .length ; off +=in [k ].clen , k ++ ) {
3759- if ( in [k ].isEmptyBlock (false ) )
3760- continue ;
3761- if ( in [k ].sparse ) {
3762- SparseBlock src = in [k ].sparseBlock ;
3763- if ( src .isEmpty (i ) )
3764- continue ;
3765- int srcpos = src .pos (i );
3766- int srclen = src .size (i );
3767- int [] srcix = src .indexes (i );
3768- double [] srcval = src .values (i );
3769- double [] resval = resd .values (i );
3770- int resix = resd .pos (i , off );
3771- for (int j =srcpos ; j <srcpos +srclen ; j ++)
3772- resval [resix +srcix [j ]] = srcval [j ];
3773- }
3774- else {
3775- DenseBlock src = in [k ].getDenseBlock ();
3776- double [] srcval = src .values (i );
3777- double [] resval = resd .values (i );
3778- System .arraycopy (srcval , src .pos (i ),
3779- resval , resd .pos (i , off ), in [k ].clen );
3780- }
3781- }
3782- }
3783- }
3784- else { //rbind
3785- result .copy (0 , rlen -1 , 0 , n -1 , this , false );
3786- for (int i =0 , off =rlen ; i <that .length ; i ++) {
3787- result .copy (off , off +that [i ].rlen -1 , 0 , n -1 , that [i ], false );
3788- off += that [i ].rlen ;
3789- }
3790- }
3791- }
3792- //SPARSE
3793- else if (nnz != 0 ) {
3794- //adjust sparse rows if required
3795- result .allocateSparseRowsBlock ();
3796- //allocate sparse rows once for cbind
3797- if ( cbind && nnz > rlen && !shallowCopy && result .getSparseBlock () instanceof SparseBlockMCSR ) {
3798- final SparseBlock sblock = result .getSparseBlock ();
3799- // for each row calculate how many non zeros are pressent.
3800- for ( int i =0 ; i <result .rlen ; i ++ )
3801- sblock .allocate (i , computeNNzRow (that , i ));
3802-
3803- }
3804-
3805- //core append operation
3806- // we can always append this directly to offset 0.0 in both cbind and rbind.
3807- result .appendToSparse (this , 0 , 0 , !shallowCopy );
3808- if ( cbind ) {
3809- for (int i =0 , off =clen ; i <that .length ; i ++) {
3810- result .appendToSparse (that [i ], 0 , off );
3811- off += that [i ].clen ;
3812- }
3813- }
3814- else { //rbind
3815- for (int i =0 , off =rlen ; i <that .length ; i ++) {
3816- result .appendToSparse (that [i ], off , 0 );
3817- off += that [i ].rlen ;
3818- }
3819- }
3820- }
3821-
3822- //update meta data
3823- result .nonZeros = nnz ;
3824- return result ;
3825- }
3826-
3827- public void checkDimensionsForAppend (MatrixBlock [] in , boolean cbind ) {
3828- if (cbind ) {
3829- for (int i = 0 ; i < in .length ; i ++)
3830- if (in [i ].rlen != rlen )
3831- throw new DMLRuntimeException (
3832- "Invalid nRow dimension for append cbind: was " + in [i ].rlen + " should be: " + rlen );
3833- }
3834- else {
3835- for (int i = 0 ; i < in .length ; i ++)
3836- if (in [i ].clen != clen )
3837- throw new DMLRuntimeException (
3838- "Invalid nCol dimension for append rbind: was " + in [i ].clen + " should be: " + clen );
3839- }
3697+ return LibMatrixAppend .append (this , that , result , cbind );
38403698 }
38413699
38423700 public static MatrixBlock naryOperations (Operator op , MatrixBlock [] matrices , ScalarObject [] scalars , MatrixBlock ret ) {
0 commit comments