Loading Documentation/Examples/Matrices/CMakeLists.txt +2 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ IF( BUILD_CUDA ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forRows_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forRows.out OUTPUT DenseMatrixExample_forRows.out ) ELSE() ADD_EXECUTABLE( DenseMatrixExample_setElement DenseMatrixExample_setElement.cpp ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElement > Loading Loading @@ -58,6 +59,7 @@ ELSE() ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forRows > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forRows.out OUTPUT DenseMatrixExample_forRows.out ) ENDIF() ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cpp ) Loading Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cpp +8 −1 Original line number Diff line number Diff line Loading @@ -32,6 +32,13 @@ void allRowsReduction() return TNL::abs( value ); }; /*** * Reduce lambda return maximum of given values. */ auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double { return TNL::max( a, b ); }; /*** * Keep lambda store the largest value in each row to the vector rowMax. */ Loading @@ -42,7 +49,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ matrix.allRowsReduction( fetch, std::plus<>{}, keep, std::numeric_limits< double >::lowest() ); matrix.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "Max. elements in rows are: " << rowMax << std::endl; } Loading Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cu 0 → 120000 +1 −0 Original line number Diff line number Diff line DenseMatrixExample_forAllRows.cpp No newline at end of file src/TNL/Matrices/DenseMatrix.h +116 −8 Original line number Diff line number Diff line Loading @@ -266,6 +266,8 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \include Matrices/DenseMatrixExample_getConstRow.cpp * \par Output * \include DenseMatrixExample_getConstRow.out * * See \ref DenseMatrixRowView. */ __cuda_callable__ const RowView getRow( const IndexType& rowIdx ) const; Loading @@ -281,6 +283,8 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \include Matrices/DenseMatrixExample_getRow.cpp * \par Output * \include DenseMatrixExample_getRow.out * * See \ref DenseMatrixRowView. */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); Loading Loading @@ -434,11 +438,14 @@ class DenseMatrix : public Matrix< Real, Device, Index > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for iteration over all matrix rows. * \brief Method for iteration over all matrix rows for constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, const RealType& value, bool compute )`. * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The column index repeats twice only for compatibility with sparse matrices. * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * * \param first is index is the first row to be processed. * \param last is index of the row after the last row to be processed. Loading @@ -452,20 +459,76 @@ class DenseMatrix : public Matrix< Real, Device, Index > template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, RealType& value, bool& compute )`. * The column index repeats twice only for compatibility with sparse matrices. * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * * \param first is index is the first row to be processed. * \param last is index of the row after the last row to be processed. * \param function is an instance of the lambda function to be called in each row. * * \par Example * \include Matrices/DenseMatrixExample_forRows.cpp * \par Output * \include DenseMatrixExample_forRows.out */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ); /** * \brief This method calls \e forRows for all matrix rows. * * See \ref DenseMatrix::forRows. * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > void forAllRows( Function& function ) const; /** * \brief This method calls \e forRows for all matrix rows. * * See \ref DenseMatrix::forRows. * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > void forAllRows( Function& function ); /** * \brief This method computes scalar product of given vector and one * row of the matrix. * * \tparam Vector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. * \param row is index of the row used for the scalar product. * \param vector is the input vector. * \return */ template< typename Vector > __cuda_callable__ typename Vector::RealType rowVectorProduct( const IndexType row, const Vector& vector ) const; /** * \brief Computes product of matrix and vector. * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. * \tparam OutVector is type of output vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. * * \param inVector is input vector. * \param outVector is output vector. */ template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, OutVector& outVector ) const; Loading Loading @@ -494,16 +557,16 @@ class DenseMatrix : public Matrix< Real, Device, Index > /** * \brief Assignment operator for exactly the same type of the dense matrix. * * @param matrix * @return * \param matrix is the right-hand side matrix. * \return reference to this matrix. */ DenseMatrix& operator=( const DenseMatrix& matrix ); /** * \brief Assignment operator for other dense matrices. * * @param matrix * @return * \param matrix is the right-hand side matrix. * \return reference to this matrix. */ template< typename RHSReal, typename RHSDevice, typename RHSIndex, bool RHSRowMajorOrder, typename RHSRealAllocator > Loading @@ -511,26 +574,64 @@ class DenseMatrix : public Matrix< Real, Device, Index > /** * \brief Assignment operator for other (sparse) types of matrices. * @param matrix * @return * * \param matrix is the right-hand side matrix. * \return reference to this matrix. */ template< typename RHSMatrix > DenseMatrix& operator=( const RHSMatrix& matrix ); /** * \brief Comparison operator with another dense matrix. * * \param matrix is the right-hand side matrix. * \return \e true if the RHS matrix is equal, \e false otherwise. */ template< typename Real_, typename Device_, typename Index_, typename RealAllocator_ > bool operator==( const DenseMatrix< Real_, Device_, Index_, RowMajorOrder >& matrix ) const; /** * \brief Comparison operator with another dense matrix. * * \param matrix is the right-hand side matrix. * \return \e false if the RHS matrix is equal, \e true otherwise. */ template< typename Real_, typename Device_, typename Index_, typename RealAllocator_ > bool operator!=( const DenseMatrix< Real_, Device_, Index_, RowMajorOrder >& matrix ) const; /** * \brief Method for saving the matrix to the file with given filename. * * \param fileName is name of the file. */ void save( const String& fileName ) const; /** * \brief Method for loading the matrix from the file with given filename. * * \param fileName is name of the file. */ void load( const String& fileName ); /** * \brief Method for saving the matrix to a file. * * \param fileName is name of the file. */ void save( File& file ) const; /** * \brief Method for loading the matrix from a file. * * \param fileName is name of the file. */ void load( File& file ); /** * \brief Method for printing the matrix to output stream. * * \param str is the output stream. */ void print( std::ostream& str ) const; protected: Loading @@ -544,6 +645,13 @@ class DenseMatrix : public Matrix< Real, Device, Index > ViewType view; }; /** * \brief Insertion operator for dense matrix and output stream. * * \param str is the output stream. * \param matrix is the dense matrix. * \return reference to the stream. */ template< typename Real, typename Device, typename Index, Loading src/TNL/Matrices/DenseMatrixRowView.h +60 −1 Original line number Diff line number Diff line Loading @@ -13,30 +13,89 @@ namespace TNL { namespace Matrices { /** * \brief RowView is a simple structure for accessing rows of dense matrix. * * \tparam SegmentView is a segment view of segments representing the matrix format. * \tparam ValuesView is a vector view storing the matrix elements values. * * See \ref DenseMatrix and \ref DenseMatrixView. * * \par Example * \include Matrices/DenseMatrixExample_getRow.cpp * \par Output * \include DenseMatrixExample_getRow.out */ template< typename SegmentView, typename ValuesView > class DenseMatrixRowView { public: /** * \brief The type of matrix elements. */ using RealType = typename ValuesView::RealType; /** * \brief The type used for matrix elements indexing. */ using IndexType = typename SegmentView::IndexType; /** * \brief Type representing matrix row format. */ using SegmentViewType = SegmentView; using IndexType = typename SegmentViewType::IndexType; /** * \brief Type of container view used for storing matrix elements values. */ using ValuesViewType = ValuesView; /** * \brief Constructor with \e segmentView and \e values * * \param segmentView instance of SegmentViewType representing matrix row. * \param values is a container view for storing the matrix elements values. */ __cuda_callable__ DenseMatrixRowView( const SegmentViewType& segmentView, const ValuesViewType& values ); /** * \brief Returns size of the matrix row, i.e. number of matrix elements in this row. * * \return Size of the matrix row. */ __cuda_callable__ IndexType getSize() const; /** * \brief Returns constants reference to an element with given column index. * * \param column is column index of the matrix element. * * \return constant reference to the matrix element. */ __cuda_callable__ const RealType& getElement( const IndexType column ) const; /** * \brief Returns non-constants reference to an element with given column index. * * \param column is a column index of the matrix element. * * \return non-constant reference to the matrix element. */ __cuda_callable__ RealType& getElement( const IndexType column ); /** * \brief Sets value of matrix element with given column index * . * \param column is a column index of the matrix element. * \param value is a value the matrix element will be set to. */ __cuda_callable__ void setElement( const IndexType column, const RealType& value ); Loading Loading
Documentation/Examples/Matrices/CMakeLists.txt +2 −0 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ IF( BUILD_CUDA ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forRows_cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forRows.out OUTPUT DenseMatrixExample_forRows.out ) ELSE() ADD_EXECUTABLE( DenseMatrixExample_setElement DenseMatrixExample_setElement.cpp ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElement > Loading Loading @@ -58,6 +59,7 @@ ELSE() ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forRows > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forRows.out OUTPUT DenseMatrixExample_forRows.out ) ENDIF() ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cpp ) Loading
Documentation/Examples/Matrices/DenseMatrixExample_allRowsReduction.cpp +8 −1 Original line number Diff line number Diff line Loading @@ -32,6 +32,13 @@ void allRowsReduction() return TNL::abs( value ); }; /*** * Reduce lambda return maximum of given values. */ auto reduce = [=] __cuda_callable__ ( double& a, const double& b ) -> double { return TNL::max( a, b ); }; /*** * Keep lambda store the largest value in each row to the vector rowMax. */ Loading @@ -42,7 +49,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ matrix.allRowsReduction( fetch, std::plus<>{}, keep, std::numeric_limits< double >::lowest() ); matrix.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "Max. elements in rows are: " << rowMax << std::endl; } Loading
Documentation/Examples/Matrices/DenseMatrixExample_forAllRows.cu 0 → 120000 +1 −0 Original line number Diff line number Diff line DenseMatrixExample_forAllRows.cpp No newline at end of file
src/TNL/Matrices/DenseMatrix.h +116 −8 Original line number Diff line number Diff line Loading @@ -266,6 +266,8 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \include Matrices/DenseMatrixExample_getConstRow.cpp * \par Output * \include DenseMatrixExample_getConstRow.out * * See \ref DenseMatrixRowView. */ __cuda_callable__ const RowView getRow( const IndexType& rowIdx ) const; Loading @@ -281,6 +283,8 @@ class DenseMatrix : public Matrix< Real, Device, Index > * \include Matrices/DenseMatrixExample_getRow.cpp * \par Output * \include DenseMatrixExample_getRow.out * * See \ref DenseMatrixRowView. */ __cuda_callable__ RowView getRow( const IndexType& rowIdx ); Loading Loading @@ -434,11 +438,14 @@ class DenseMatrix : public Matrix< Real, Device, Index > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for iteration over all matrix rows. * \brief Method for iteration over all matrix rows for constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, const RealType& value, bool compute )`. * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, const RealType& value, bool& compute )`. * The column index repeats twice only for compatibility with sparse matrices. * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * * \param first is index is the first row to be processed. * \param last is index of the row after the last row to be processed. Loading @@ -452,20 +459,76 @@ class DenseMatrix : public Matrix< Real, Device, Index > template< typename Function > void forRows( IndexType first, IndexType last, Function& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, RealType& value, bool& compute )`. * The column index repeats twice only for compatibility with sparse matrices. * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * * \param first is index is the first row to be processed. * \param last is index of the row after the last row to be processed. * \param function is an instance of the lambda function to be called in each row. * * \par Example * \include Matrices/DenseMatrixExample_forRows.cpp * \par Output * \include DenseMatrixExample_forRows.out */ template< typename Function > void forRows( IndexType first, IndexType last, Function& function ); /** * \brief This method calls \e forRows for all matrix rows. * * See \ref DenseMatrix::forRows. * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > void forAllRows( Function& function ) const; /** * \brief This method calls \e forRows for all matrix rows. * * See \ref DenseMatrix::forRows. * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > void forAllRows( Function& function ); /** * \brief This method computes scalar product of given vector and one * row of the matrix. * * \tparam Vector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. * \param row is index of the row used for the scalar product. * \param vector is the input vector. * \return */ template< typename Vector > __cuda_callable__ typename Vector::RealType rowVectorProduct( const IndexType row, const Vector& vector ) const; /** * \brief Computes product of matrix and vector. * * \tparam InVector is type of input vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. * \tparam OutVector is type of output vector. It can be \ref Vector, * \ref VectorView, \ref Array, \ref ArraView or similar container. * * \param inVector is input vector. * \param outVector is output vector. */ template< typename InVector, typename OutVector > void vectorProduct( const InVector& inVector, OutVector& outVector ) const; Loading Loading @@ -494,16 +557,16 @@ class DenseMatrix : public Matrix< Real, Device, Index > /** * \brief Assignment operator for exactly the same type of the dense matrix. * * @param matrix * @return * \param matrix is the right-hand side matrix. * \return reference to this matrix. */ DenseMatrix& operator=( const DenseMatrix& matrix ); /** * \brief Assignment operator for other dense matrices. * * @param matrix * @return * \param matrix is the right-hand side matrix. * \return reference to this matrix. */ template< typename RHSReal, typename RHSDevice, typename RHSIndex, bool RHSRowMajorOrder, typename RHSRealAllocator > Loading @@ -511,26 +574,64 @@ class DenseMatrix : public Matrix< Real, Device, Index > /** * \brief Assignment operator for other (sparse) types of matrices. * @param matrix * @return * * \param matrix is the right-hand side matrix. * \return reference to this matrix. */ template< typename RHSMatrix > DenseMatrix& operator=( const RHSMatrix& matrix ); /** * \brief Comparison operator with another dense matrix. * * \param matrix is the right-hand side matrix. * \return \e true if the RHS matrix is equal, \e false otherwise. */ template< typename Real_, typename Device_, typename Index_, typename RealAllocator_ > bool operator==( const DenseMatrix< Real_, Device_, Index_, RowMajorOrder >& matrix ) const; /** * \brief Comparison operator with another dense matrix. * * \param matrix is the right-hand side matrix. * \return \e false if the RHS matrix is equal, \e true otherwise. */ template< typename Real_, typename Device_, typename Index_, typename RealAllocator_ > bool operator!=( const DenseMatrix< Real_, Device_, Index_, RowMajorOrder >& matrix ) const; /** * \brief Method for saving the matrix to the file with given filename. * * \param fileName is name of the file. */ void save( const String& fileName ) const; /** * \brief Method for loading the matrix from the file with given filename. * * \param fileName is name of the file. */ void load( const String& fileName ); /** * \brief Method for saving the matrix to a file. * * \param fileName is name of the file. */ void save( File& file ) const; /** * \brief Method for loading the matrix from a file. * * \param fileName is name of the file. */ void load( File& file ); /** * \brief Method for printing the matrix to output stream. * * \param str is the output stream. */ void print( std::ostream& str ) const; protected: Loading @@ -544,6 +645,13 @@ class DenseMatrix : public Matrix< Real, Device, Index > ViewType view; }; /** * \brief Insertion operator for dense matrix and output stream. * * \param str is the output stream. * \param matrix is the dense matrix. * \return reference to the stream. */ template< typename Real, typename Device, typename Index, Loading
src/TNL/Matrices/DenseMatrixRowView.h +60 −1 Original line number Diff line number Diff line Loading @@ -13,30 +13,89 @@ namespace TNL { namespace Matrices { /** * \brief RowView is a simple structure for accessing rows of dense matrix. * * \tparam SegmentView is a segment view of segments representing the matrix format. * \tparam ValuesView is a vector view storing the matrix elements values. * * See \ref DenseMatrix and \ref DenseMatrixView. * * \par Example * \include Matrices/DenseMatrixExample_getRow.cpp * \par Output * \include DenseMatrixExample_getRow.out */ template< typename SegmentView, typename ValuesView > class DenseMatrixRowView { public: /** * \brief The type of matrix elements. */ using RealType = typename ValuesView::RealType; /** * \brief The type used for matrix elements indexing. */ using IndexType = typename SegmentView::IndexType; /** * \brief Type representing matrix row format. */ using SegmentViewType = SegmentView; using IndexType = typename SegmentViewType::IndexType; /** * \brief Type of container view used for storing matrix elements values. */ using ValuesViewType = ValuesView; /** * \brief Constructor with \e segmentView and \e values * * \param segmentView instance of SegmentViewType representing matrix row. * \param values is a container view for storing the matrix elements values. */ __cuda_callable__ DenseMatrixRowView( const SegmentViewType& segmentView, const ValuesViewType& values ); /** * \brief Returns size of the matrix row, i.e. number of matrix elements in this row. * * \return Size of the matrix row. */ __cuda_callable__ IndexType getSize() const; /** * \brief Returns constants reference to an element with given column index. * * \param column is column index of the matrix element. * * \return constant reference to the matrix element. */ __cuda_callable__ const RealType& getElement( const IndexType column ) const; /** * \brief Returns non-constants reference to an element with given column index. * * \param column is a column index of the matrix element. * * \return non-constant reference to the matrix element. */ __cuda_callable__ RealType& getElement( const IndexType column ); /** * \brief Sets value of matrix element with given column index * . * \param column is a column index of the matrix element. * \param value is a value the matrix element will be set to. */ __cuda_callable__ void setElement( const IndexType column, const RealType& value ); Loading