Newer
Older
Tomáš Oberhuber
committed
/***************************************************************************
Tomáš Oberhuber
committed
-------------------
begin : Dec 18, 2013
copyright : (C) 2013 by Tomas Oberhuber
Tomáš Oberhuber
committed
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
Tomáš Oberhuber
committed
Tomáš Oberhuber
committed
#include <TNL/Assert.h>
template< typename Real,
typename Device,
typename Index >
template< typename Real,
typename Device,
typename Index >
Jakub Klinkovský
committed
void Matrix< Real, Device, Index >::setDimensions( const IndexType rows,
const IndexType columns )
Jakub Klinkovský
committed
std::cerr << " rows = " << rows << " columns = " << columns );
this->rows = rows;
this->columns = columns;
}
template< typename Real,
typename Device,
typename Index >
void Matrix< Real, Device, Index >::getCompressedRowLengths( Containers::Vector< IndexType, DeviceType, IndexType >& rowLengths ) const
{
rowLengths.setSize( this->getRows() );
for( IndexType row = 0; row < this->getRows(); row++ )
rowLengths.setElement( row, this->getRowLength( row ) );
template< typename Real,
typename Device,
typename Index >
template< typename Real2,
typename Device2,
typename Index2 >
Jakub Klinkovský
committed
void Matrix< Real, Device, Index >::setLike( const Matrix< Real2, Device2, Index2 >& matrix )
Jakub Klinkovský
committed
setDimensions( matrix.getRows(), matrix.getColumns() );
}
template< typename Real,
typename Device,
typename Index >
Index Matrix< Real, Device, Index >::getRows() const
{
return this->rows;
}
template< typename Real,
typename Device,
typename Index >
Index Matrix< Real, Device, Index >::getColumns() const
{
return this->columns;
}
template< typename Real,
typename Device,
typename Index >
{
this->rows = 0;
this->columns = 0;
}
template< typename Real,
typename Device,
typename Index >
template< typename MatrixT >
bool Matrix< Real, Device, Index >::copyFrom( const MatrixT& matrix,
const CompressedRowLengthsVector& rowLengths )
/*tnlStaticTNL_ASSERT( DeviceType::DeviceType == Devices::HostDevice, );
tnlStaticTNL_ASSERT( DeviceType::DeviceType == Matrix:DeviceType::DeviceType, );*/
Jakub Klinkovský
committed
this->setCompressedRowLengths( rowLengths );
Containers::Vector< RealType, Devices::Host, IndexType > values;
Containers::Vector< IndexType, Devices::Host, IndexType > columns;
Jakub Klinkovský
committed
values.setSize( this->getColumns() );
columns.setSize( this->getColumns() );
for( IndexType row = 0; row < this->getRows(); row++ )
{
// TODO: fix this
//matrix.getRow( row, columns.getData(), values.getData() );
this->setRow( row, columns.getData(), values.getData(), rowLengths.getElement( row ) );
}
template< typename Real,
typename Device,
typename Index >
Matrix< Real, Device, Index >& Matrix< Real, Device, Index >::operator = ( const Matrix< RealType, DeviceType, IndexType >& m )
Containers::Vector< IndexType, DeviceType, IndexType > rowLengths;
m.getCompressedRowLengths( rowLengths );
this->setCompressedRowLengths( rowLengths );
Containers::Vector< RealType, DeviceType, IndexType > rowValues;
Containers::Vector< IndexType, DeviceType, IndexType > rowColumns;
const IndexType maxRowLength = rowLengths.max();
rowValues.setSize( maxRowLength );
rowColumns.setSize( maxRowLength );
for( IndexType row = 0; row < this->getRows(); row++ )
{
rowColumns.getData(),
rowValues.getData() );
this->setRow( row,
rowColumns.getData(),
rowValues.getData(),
template< typename Real,
typename Device,
typename Index >
template< typename MatrixT >
bool Matrix< Real, Device, Index >::operator == ( const MatrixT& matrix ) const
{
if( this->getRows() != matrix.getRows() ||
this->getColumns() != matrix.getColumns() )
return false;
for( IndexType row = 0; row < this->getRows(); row++ )
for( IndexType column = 0; column < this->getColumns(); column++ )
if( this->getElement( row, column ) != matrix.getElement( row, column ) )
return false;
return true;
}
template< typename Real,
typename Device,
typename Index >
template< typename MatrixT >
bool Matrix< Real, Device, Index >::operator != ( const MatrixT& matrix ) const
template< typename Real,
typename Device,
typename Index >
bool Matrix< Real, Device, Index >::save( File& file ) const
if( ! Object::save( file ) ||
! file.write( &this->columns ) ||
return false;
return true;
}
template< typename Real,
typename Device,
typename Index >
bool Matrix< Real, Device, Index >::load( File& file )
if( ! Object::load( file ) ||
! file.read( &this->columns ) ||
! this->values.load( file ) )
template< typename Real,
typename Device,
typename Index >
void Matrix< Real, Device, Index >::print( std::ostream& str ) const
#ifdef HAVE_CUDA
template< typename Matrix,
typename InVector,
typename OutVector >
__global__ void MatrixVectorProductCudaKernel( const Matrix* matrix,
const InVector* inVector,
OutVector* outVector,
static_assert( std::is_same< typename Matrix::DeviceType, Devices::Cuda >::value, "" );
const typename Matrix::IndexType rowIdx = ( gridIdx * Devices::Cuda::getMaxGridSize() + blockIdx.x ) * blockDim.x + threadIdx.x;
if( rowIdx < matrix->getRows() )
( *outVector )[ rowIdx ] = matrix->rowVectorProduct( rowIdx, *inVector );
}
#endif
template< typename Matrix,
typename InVector,
typename OutVector >
void MatrixVectorProductCuda( const Matrix& matrix,
const InVector& inVector,
OutVector& outVector )
typedef typename Matrix::IndexType IndexType;
Matrix* kernel_this = Devices::Cuda::passToDevice( matrix );
InVector* kernel_inVector = Devices::Cuda::passToDevice( inVector );
OutVector* kernel_outVector = Devices::Cuda::passToDevice( outVector );
dim3 cudaBlockSize( 256 ), cudaGridSize( Devices::Cuda::getMaxGridSize() );
const IndexType cudaBlocks = roundUpDivision( matrix.getRows(), cudaBlockSize.x );
const IndexType cudaGrids = roundUpDivision( cudaBlocks, Devices::Cuda::getMaxGridSize() );
for( IndexType gridIdx = 0; gridIdx < cudaGrids; gridIdx++ )
{
if( gridIdx == cudaGrids - 1 )
cudaGridSize.x = cudaBlocks % Devices::Cuda::getMaxGridSize();
MatrixVectorProductCudaKernel<<< cudaGridSize, cudaBlockSize >>>
( kernel_this,
kernel_inVector,
kernel_outVector,
gridIdx );
Devices::Cuda::freeFromDevice( kernel_this );
Devices::Cuda::freeFromDevice( kernel_inVector );
Devices::Cuda::freeFromDevice( kernel_outVector );
checkCudaDevice;
#endif
}