Commit 4464a9dd authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Implemented getTransposition for SparseMatrix and SparseSandboxMatrix

parent 37aab443
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -1054,11 +1054,12 @@ public:
   void addMatrix( const SparseMatrix< Real2, Segments, Device, Index2 >& matrix,
                   const RealType& matrixMultiplicator = 1.0,
                   const RealType& thisMatrixMultiplicator = 1.0 );
   */

   template< typename Real2, typename Index2 >
   void getTransposition( const SparseMatrix< Real2, Segments, Device, Index2 >& matrix,
   void
   getTransposition( const SparseSandboxMatrix< Real2, Device, Index2, MatrixType >& matrix,
                     const RealType& matrixMultiplicator = 1.0 );
    */

   template< typename Vector1, typename Vector2 >
   bool
+57 −12
Original line number Diff line number Diff line
@@ -521,7 +521,8 @@ SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAlloca
   this->sequentialForRows( 0, this->getRows(), function );
}

/*template< typename Real,
/*
template< typename Real,
          template< typename, typename, typename > class Segments,
          typename Device,
          typename Index,
@@ -532,23 +533,67 @@ IndexAllocator2 > void SparseSandboxMatrix< Real, Device, Index, MatrixType, Rea
SparseSandboxMatrix< Real2, Segments2, Device, Index2, RealAllocator2, IndexAllocator2 >& matrix, const RealType&
matrixMultiplicator, const RealType& thisMatrixMultiplicator )
{

}
*/

template< typename Real,
          template< typename, typename, typename > class Segments,
          typename Device,
          typename Index,
          typename RealAllocator,
          typename IndexAllocator >
template< typename Real, typename Device, typename Index, typename MatrixType, typename RealAllocator, typename IndexAllocator >
template< typename Real2, typename Index2 >
void
SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::
getTransposition( const SparseSandboxMatrix< Real2, Device, Index2 >& matrix,
SparseSandboxMatrix< Real, Device, Index, MatrixType, RealAllocator, IndexAllocator >::getTransposition(
   const SparseSandboxMatrix< Real2, Device, Index2, MatrixType >& matrix,
   const RealType& matrixMultiplicator )
{

}*/
   // set transposed dimensions
   setDimensions( matrix.getColumns(), matrix.getRows() );

   // stage 1: compute row capacities for the transposition
   RowsCapacitiesType capacities;
   capacities.resize( this->getRows(), 0 );
   auto capacities_view = capacities.getView();
   using MatrixRowView = typename SparseSandboxMatrix< Real2, Device, Index2, MatrixType >::ConstRowView;
   matrix.forAllRows(
      [ = ] __cuda_callable__( const MatrixRowView& row ) mutable
      {
         for( IndexType c = 0; c < row.getSize(); c++ ) {
            // row index of the transpose = column index of the input
            const IndexType& transRowIdx = row.getColumnIndex( c );
            if( transRowIdx < 0 )
               continue;
            // increment the capacity for the row in the transpose
            Algorithms::AtomicOperations< DeviceType >::add( capacities_view[ row.getColumnIndex( c ) ], IndexType( 1 ) );
         }
      } );

   // set the row capacities
   setRowCapacities( capacities );
   capacities.reset();

   // index of the first unwritten element per row
   RowsCapacitiesType offsets;
   offsets.resize( this->getRows(), 0 );
   auto offsets_view = offsets.getView();

   // stage 2: copy and transpose the data
   auto trans_view = getView();
   matrix.forAllRows(
      [ = ] __cuda_callable__( const MatrixRowView& row ) mutable
      {
         // row index of the input = column index of the transpose
         const IndexType& rowIdx = row.getRowIndex();
         for( IndexType c = 0; c < row.getSize(); c++ ) {
            // row index of the transpose = column index of the input
            const IndexType& transRowIdx = row.getColumnIndex( c );
            if( transRowIdx < 0 )
               continue;
            // local index in the row of the transpose
            const IndexType transLocalIdx =
               Algorithms::AtomicOperations< DeviceType >::add( offsets_view[ transRowIdx ], IndexType( 1 ) );
            // get the row in the transposed matrix and set the value
            auto transRow = trans_view.getRow( transRowIdx );
            transRow.setElement( transLocalIdx, rowIdx, row.getValue( c ) * matrixMultiplicator );
         }
      } );
}

template< typename Real, typename Device, typename Index, typename MatrixType, typename RealAllocator, typename IndexAllocator >
template< typename Vector1, typename Vector2 >
+5 −4
Original line number Diff line number Diff line
@@ -1088,12 +1088,13 @@ public:
   void addMatrix( const SparseMatrix< Real2, Segments, Device, Index2 >& matrix,
                   const RealType& matrixMultiplicator = 1.0,
                   const RealType& thisMatrixMultiplicator = 1.0 );

   template< typename Real2, typename Index2 >
   void getTransposition( const SparseMatrix< Real2, Segments, Device, Index2 >& matrix,
                          const RealType& matrixMultiplicator = 1.0 );
    */

   template< typename Real2, typename Index2, template< typename, typename, typename > class Segments2 >
   void
   getTransposition( const SparseMatrix< Real2, Device, Index2, MatrixType, Segments2 >& matrix,
                     const ComputeRealType& matrixMultiplicator = 1.0 );

   /**
    * \brief Copy-assignment of exactly the same matrix type.
    *
+61 −8
Original line number Diff line number Diff line
@@ -870,7 +870,8 @@ SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAlloca
   this->sequentialForRows( (IndexType) 0, this->getRows(), function );
}

/*template< typename Real,
/*
template< typename Real,
          template< typename, typename, typename > class Segments,
          typename Device,
          typename Index,
@@ -882,23 +883,75 @@ addMatrix( const SparseMatrix< Real2, Segments2, Device, Index2, RealAllocator2,
           const RealType& matrixMultiplicator,
           const RealType& thisMatrixMultiplicator )
{

}
*/

template< typename Real,
          template< typename, typename, typename > class Segments,
          typename Device,
          typename Index,
          typename MatrixType,
          template< typename, typename, typename >
          class Segments,
          typename ComputeReal,
          typename RealAllocator,
          typename IndexAllocator >
template< typename Real2, typename Index2 >
template< typename Real2, typename Index2, template< typename, typename, typename > class Segments2 >
void
SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::
getTransposition( const SparseMatrix< Real2, Device, Index2 >& matrix,
                  const RealType& matrixMultiplicator )
{

}*/
SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >::getTransposition(
   const SparseMatrix< Real2, Device, Index2, MatrixType, Segments2 >& matrix,
   const ComputeRealType& matrixMultiplicator )
{
   // set transposed dimensions
   setDimensions( matrix.getColumns(), matrix.getRows() );

   // stage 1: compute row capacities for the transposition
   RowsCapacitiesType capacities;
   capacities.resize( this->getRows(), 0 );
   auto capacities_view = capacities.getView();
   using MatrixRowView = typename SparseMatrix< Real2, Device, Index2, MatrixType, Segments2 >::ConstRowView;
   matrix.forAllRows(
      [ = ] __cuda_callable__( const MatrixRowView& row ) mutable
      {
         for( IndexType c = 0; c < row.getSize(); c++ ) {
            // row index of the transpose = column index of the input
            const IndexType& transRowIdx = row.getColumnIndex( c );
            if( transRowIdx == row.getPaddingIndex() )
               continue;
            // increment the capacity for the row in the transpose
            Algorithms::AtomicOperations< DeviceType >::add( capacities_view[ transRowIdx ], IndexType( 1 ) );
         }
      } );

   // set the row capacities
   setRowCapacities( capacities );
   capacities.reset();

   // index of the first unwritten element per row
   RowsCapacitiesType offsets;
   offsets.resize( this->getRows(), 0 );
   auto offsets_view = offsets.getView();

   // stage 2: copy and transpose the data
   auto trans_view = getView();
   matrix.forAllRows(
      [ = ] __cuda_callable__( const MatrixRowView& row ) mutable
      {
         // row index of the input = column index of the transpose
         const IndexType& rowIdx = row.getRowIndex();
         for( IndexType c = 0; c < row.getSize(); c++ ) {
            // row index of the transpose = column index of the input
            const IndexType& transRowIdx = row.getColumnIndex( c );
            if( transRowIdx == row.getPaddingIndex() )
               continue;
            // local index in the row of the transpose
            const IndexType transLocalIdx =
               Algorithms::AtomicOperations< DeviceType >::add( offsets_view[ transRowIdx ], IndexType( 1 ) );
            // get the row in the transposed matrix and set the value
            auto transRow = trans_view.getRow( transRowIdx );
            transRow.setElement( transLocalIdx, rowIdx, row.getValue( c ) * matrixMultiplicator );
         }
      } );
}

// copy assignment
template< typename Real,
+0 −29
Original line number Diff line number Diff line
@@ -820,35 +820,6 @@ SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >::
   this->sequentialForRows( (IndexType) 0, this->getRows(), function );
}

/*template< typename Real,
          template< typename, typename > class SegmentsView,
          typename Device,
          typename Index,
          typename RealAllocator,
          typename IndexAllocator >
template< typename Real2, template< typename, typename > class Segments2, typename Index2, typename RealAllocator2, typename
IndexAllocator2 > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: addMatrix( const
SparseMatrixView< Real2, Segments2, Device, Index2, RealAllocator2, IndexAllocator2 >& matrix, const RealType&
matrixMultiplicator, const RealType& thisMatrixMultiplicator )
{

}

template< typename Real,
          template< typename, typename > class SegmentsView,
          typename Device,
          typename Index,
          typename RealAllocator,
          typename IndexAllocator >
template< typename Real2, typename Index2 >
void
SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >::
getTransposition( const SparseMatrixView< Real2, Device, Index2 >& matrix,
                  const RealType& matrixMultiplicator )
{

}*/

template< typename Real,
          typename Device,
          typename Index,
Loading