CUSP
Loading...
Searching...
No Matches
Matrix Algorithms

Algorithms for processing matrices in sparse and dense formats. More...

Functions

template<typename SourceType , typename DestinationType >
void cusp::convert (const SourceType &src, DestinationType &dst)
 Convert between matrix formats.
 
template<typename SourceType , typename DestinationType >
void cusp::copy (const SourceType &src, DestinationType &dst)
 Copy one array or matrix to another.
 
template<typename MatrixType1 , typename MatrixType2 , typename MatrixType3 , typename BinaryFunction >
void cusp::elementwise (const MatrixType1 &A, const MatrixType2 &B, MatrixType3 &C, BinaryFunction op)
 Perform transform operation on two matrices.
 
template<typename MatrixType1 , typename MatrixType2 , typename MatrixType3 >
void cusp::add (const MatrixType1 &A, const MatrixType2 &B, MatrixType3 &C)
 Compute the sum of two matrices.
 
template<typename MatrixType1 , typename MatrixType2 , typename MatrixType3 >
void cusp::subtract (const MatrixType1 &A, const MatrixType2 &B, MatrixType3 &C)
 Compute the difference of two matrices.
 
template<typename OffsetArray , typename IndexArray >
void cusp::offsets_to_indices (const OffsetArray &offsets, IndexArray &indices)
 Expand CSR row offsets to COO row indices.
 
template<typename IndexArray , typename OffsetArray >
void cusp::indices_to_offsets (const IndexArray &indices, OffsetArray &offsets)
 Compress COO row indices to CSR row offsets.
 
template<typename MatrixType , typename ArrayType >
void cusp::extract_diagonal (const MatrixType &A, ArrayType &output)
 Extract the main diagonal of a matrix.
 
template<typename ArrayType1 , typename ArrayType2 >
size_t cusp::count_diagonals (const size_t num_rows, const size_t num_cols, const ArrayType1 &row_indices, const ArrayType2 &column_indices)
 Count the number of occupied diagonals in the input matrix.
 
template<typename ArrayType >
size_t cusp::compute_max_entries_per_row (const ArrayType &row_offsets)
 Compute the maximum row length of a matrix.
 
template<typename ArrayType >
size_t cusp::compute_optimal_entries_per_row (const ArrayType &row_offsets, float relative_speed=3.0f, size_t breakeven_threshold=4096)
 Compute the optimal number of entries per row of HYB matrix.
 
template<typename LinearOperator , typename MatrixOrVector1 , typename MatrixOrVector2 >
void cusp::multiply (const LinearOperator &A, const MatrixOrVector1 &B, MatrixOrVector2 &C)
 Implements matrix-matrix and matrix-vector multiplication.
 
template<typename LinearOperator , typename MatrixOrVector1 , typename MatrixOrVector2 , typename UnaryFunction , typename BinaryFunction1 , typename BinaryFunction2 >
void cusp::multiply (const LinearOperator &A, const MatrixOrVector1 &B, MatrixOrVector2 &C, UnaryFunction initialize, BinaryFunction1 combine, BinaryFunction2 reduce)
 Implements matrix-vector multiplication with custom combine and reduce functionality.
 
template<typename LinearOperator , typename MatrixOrVector1 , typename MatrixOrVector2 , typename UnaryFunction , typename BinaryFunction1 , typename BinaryFunction2 >
void cusp::generalized_spgemm (const LinearOperator &A, const MatrixOrVector1 &B, MatrixOrVector2 &C, UnaryFunction initialize, BinaryFunction1 combine, BinaryFunction2 reduce)
 Implements generalized matrix-matrix multiplication.
 
template<typename LinearOperator , typename Vector1 , typename Vector2 , typename Vector3 , typename BinaryFunction1 , typename BinaryFunction2 >
void cusp::generalized_spmv (const LinearOperator &A, const Vector1 &x, const Vector2 &y, Vector3 &z, BinaryFunction1 combine, BinaryFunction2 reduce)
 Implements generalized matrix-vector multiplication.
 
template<typename ArrayType >
void cusp::counting_sort (ArrayType &keys, typename ArrayType::value_type min, typename ArrayType::value_type max)
 Use counting sort to order an array.
 
template<typename ArrayType1 , typename ArrayType2 >
void cusp::counting_sort_by_key (ArrayType1 &keys, ArrayType2 &vals, typename ArrayType1::value_type min, typename ArrayType1::value_type max)
 Use counting sort to order an array and permute an array of values.
 
template<typename ArrayType1 , typename ArrayType2 , typename ArrayType3 >
void cusp::sort_by_row (ArrayType1 &row_indices, ArrayType2 &column_indices, ArrayType3 &values, typename ArrayType1::value_type min_row=0, typename ArrayType1::value_type max_row=0)
 Sort matrix indices by row.
 
template<typename ArrayType1 , typename ArrayType2 , typename ArrayType3 >
void cusp::sort_by_row_and_column (ArrayType1 &row_indices, ArrayType2 &column_indices, ArrayType3 &values, typename ArrayType1::value_type min_row=0, typename ArrayType1::value_type max_row=0, typename ArrayType2::value_type min_col=0, typename ArrayType2::value_type max_col=0)
 Sort matrix indices by row and column.
 
template<typename MatrixType1 , typename MatrixType2 >
void cusp::transpose (const MatrixType1 &A, MatrixType2 &At)
 Transpose a matrix.
 
template<typename MatrixType >
bool cusp::is_valid_matrix (const MatrixType &matrix)
 Validate format of a given matrix.
 
template<typename MatrixType , typename OutputStream >
bool cusp::is_valid_matrix (const MatrixType &matrix, OutputStream &ostream)
 Validate format of a given matrix.
 
template<typename MatrixType >
void cusp::assert_is_valid_matrix (const MatrixType &matrix)
 Validate format of a given matrix and exit if invalid.
 
template<typename Array1 , typename Array2 >
void cusp::assert_same_dimensions (const Array1 &array1, const Array2 &array2)
 Assert dimensions of two arrays are equal. Throw invalid input exception if dimensions do not match.
 
template<typename Array1 , typename Array2 , typename Array3 >
void cusp::assert_same_dimensions (const Array1 &array1, const Array2 &array2, const Array3 &array3)
 Assert dimensions of two arrays are equal. Throw invalid input exception if dimensions do not match.
 
template<typename Array1 , typename Array2 , typename Array3 , typename Array4 >
void cusp::assert_same_dimensions (const Array1 &array1, const Array2 &array2, const Array3 &array3, const Array4 &array4)
 Assert dimensions of two arrays are equal. Throw invalid input exception if dimensions do not match.
 

Detailed Description

Algorithms for processing matrices in sparse and dense formats.

format

Function Documentation

◆ add()

template<typename MatrixType1 , typename MatrixType2 , typename MatrixType3 >
void cusp::add ( const MatrixType1 &  A,
const MatrixType2 &  B,
MatrixType3 &  C 
)

Compute the sum of two matrices.

Template Parameters
MatrixType1Type of first matrix
MatrixType2Type of second matrix
MatrixType3Type of output matrix
Parameters
Afirst input matrix
Bsecond input matrix
Coutput matrix
Example
The following code snippet demonstrates how to use add.
#include <cusp/array2d.h>
#include <cusp/print.h>
int main(void)
{
// initialize first 2x3 matrix
A(0,0) = 10; A(0,1) = 20; A(0,2) = 30;
A(1,0) = 40; A(1,1) = 50; A(1,2) = 60;
// print A
// initialize second 2x3 matrix
B(0,0) = 60; B(0,1) = 50; B(0,2) = 40;
B(1,0) = 30; B(1,1) = 20; B(1,2) = 10;
// print B
// compute the sum
cusp::add(A, B, C);
// print C
return 0;
}
2D array of elements that may reside in "host" or "device" memory space
The array2d class is a 2D vector container that may contain elements stored in "host" or "device" mem...
Definition array2d.h:94
Elementwise operations on matrices.
void print(const Printable &p)
print a textual representation of an object
void add(const MatrixType1 &A, const MatrixType2 &B, MatrixType3 &C)
Compute the sum of two matrices.
Print textual representation of an object.

◆ assert_is_valid_matrix()

template<typename MatrixType >
void cusp::assert_is_valid_matrix ( const MatrixType &  matrix)

Validate format of a given matrix and exit if invalid.

Template Parameters
MatrixTypematrix container
Parameters
matrixA matrix container (e.g. csr_matrix or coo_matrix)

◆ assert_same_dimensions() [1/3]

template<typename Array1 , typename Array2 >
void cusp::assert_same_dimensions ( const Array1 &  array1,
const Array2 &  array2 
)

Assert dimensions of two arrays are equal. Throw invalid input exception if dimensions do not match.

Template Parameters
Array1First array container type
Array2Second array container type
Parameters
array1First array used in comparison
array2Second array used in comparison

◆ assert_same_dimensions() [2/3]

template<typename Array1 , typename Array2 , typename Array3 >
void cusp::assert_same_dimensions ( const Array1 &  array1,
const Array2 &  array2,
const Array3 &  array3 
)

Assert dimensions of two arrays are equal. Throw invalid input exception if dimensions do not match.

Template Parameters
Array1First array container type
Array2Second array container type
Array3Third array container type
Parameters
array1First array used in comparison
array2Second array used in comparison
array3Third array used in comparison

◆ assert_same_dimensions() [3/3]

template<typename Array1 , typename Array2 , typename Array3 , typename Array4 >
void cusp::assert_same_dimensions ( const Array1 &  array1,
const Array2 &  array2,
const Array3 &  array3,
const Array4 &  array4 
)

Assert dimensions of two arrays are equal. Throw invalid input exception if dimensions do not match.

Template Parameters
Array1First array container type
Array2Second array container type
Array3Third array container type
Array4Fourth array container type
Parameters
array1First array used in comparison
array2Second array used in comparison
array3Third array used in comparison
array4Fourth array used in comparison

◆ compute_max_entries_per_row()

template<typename ArrayType >
size_t cusp::compute_max_entries_per_row ( const ArrayType &  row_offsets)

Compute the maximum row length of a matrix.

Template Parameters
ArrayTypeType of input row offsets
Parameters
row_offsetsrow offsets of input matrix
Returns
maximum row length
Example
#include <iostream>
int main()
{
// initialize 5x5 poisson matrix
// count the number of occupied diagonals of A
std::cout << compute_max_entries_per_row(A.row_offsets) << std::endl;
}
Compressed sparse row (CSR) representation a sparse matrix.
Definition csr_matrix.h:108
row_offsets_array_type row_offsets
Definition csr_matrix.h:150
Compressed Sparse Row matrix format.
Various matrix utility functions.
size_t compute_max_entries_per_row(const ArrayType &row_offsets)
Compute the maximum row length of a matrix.
Poisson matrix generators.

◆ compute_optimal_entries_per_row()

template<typename ArrayType >
size_t cusp::compute_optimal_entries_per_row ( const ArrayType &  row_offsets,
float  relative_speed = 3.0f,
size_t  breakeven_threshold = 4096 
)

Compute the optimal number of entries per row of HYB matrix.

Template Parameters
ArrayTypeType of input row offsets
Parameters
row_offsetsrow offsets of input matrix
relative_speedEstimated performance difference between ELL and COO row storage schemes
breakeven_thresholdThreshold value separating ELL and COO row classification
Returns
optimal number of columns to store in ELL matrix
Example
#include <iostream>
int main()
{
// initialize 5x5 poisson matrix
// count the number of occupied diagonals of A
std::cout << compute_optimal_entries_per_row(A.row_offsets) << std::endl;
}
size_t compute_optimal_entries_per_row(const ArrayType &row_offsets, float relative_speed=3.0f, size_t breakeven_threshold=4096)
Compute the optimal number of entries per row of HYB matrix.

◆ convert()

template<typename SourceType , typename DestinationType >
void cusp::convert ( const SourceType &  src,
DestinationType &  dst 
)

Convert between matrix formats.

Template Parameters
SourceTypeType of the input matrix to convert
DestinationTypeType of the output matrix to create
Parameters
srcInput matrix to convert
dstOutput matrix created by converting src to the specified format
Note
DestinationType will be resized as necessary
Example
#include <cusp/array2d.h>
#include <cusp/print.h>
// include cusp convert header file
#include <cusp/convert.h>
int main()
{
// create an empty sparse matrix structure
// create 2D Poisson problem
// create an empty array2d structure
// convert coo_matrix to array2d
// print the contents of B
}
Coordinate (COO) representation a sparse matrix.
Definition coo_matrix.h:117
Matrix format conversion.
Coordinate matrix format.
void convert(const SourceType &src, DestinationType &dst)
Convert between matrix formats.
See also
copy

◆ copy()

template<typename SourceType , typename DestinationType >
void cusp::copy ( const SourceType &  src,
DestinationType &  dst 
)

Copy one array or matrix to another.

Template Parameters
SourceTypeType of the input matrix to copy
DestinationTypeType of the output matrix
Parameters
srcInput matrix to copy
dstOutput matrix created by copying src to dst
Note
SourceType and DestinationType must have the same format
DestinationType will be resized as necessary
Example
#include <cusp/array1d.h>
#include <cusp/print.h>
// include cusp copy header file
#include <cusp/copy.h>
int main()
{
// Allocate a array of size 10
// Create a random array with 10 entries
// Copy random values from rand into a
cusp::copy(rand, a);
// print the contents of a
}
1D array of elements that may reside in "host" or "device" memory space
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition array1d.h:99
Specialized array1d_view wrapping cusp::random_iterator.
Definition array1d.h:664
Performs (deep) copy operations between containers and views.
void copy(const SourceType &src, DestinationType &dst)
Copy one array or matrix to another.
See also
convert

◆ count_diagonals()

template<typename ArrayType1 , typename ArrayType2 >
size_t cusp::count_diagonals ( const size_t  num_rows,
const size_t  num_cols,
const ArrayType1 &  row_indices,
const ArrayType2 &  column_indices 
)

Count the number of occupied diagonals in the input matrix.

Template Parameters
ArrayType1Type of input row indices
ArrayType2Type of input column indices
Parameters
num_rowsNumber of rows.
num_colsNumber of columns.
row_indicesrow indices of input matrix
column_indicescolumn indices of input matrix
Returns
number of occupied diagonals
Example
#include <iostream>
int main()
{
// initialize 5x5 poisson matrix
// count the number of occupied diagonals of A
std::cout << count_diagonals(A.num_rows,
A.num_cols,
A.column_indices) << std::endl;
}
column_indices_array_type column_indices
Definition coo_matrix.h:159
row_indices_array_type row_indices
Definition coo_matrix.h:155
size_t count_diagonals(const size_t num_rows, const size_t num_cols, const ArrayType1 &row_indices, const ArrayType2 &column_indices)
Count the number of occupied diagonals in the input matrix.

◆ counting_sort()

template<typename ArrayType >
void cusp::counting_sort ( ArrayType &  keys,
typename ArrayType::value_type  min,
typename ArrayType::value_type  max 
)

Use counting sort to order an array.

Template Parameters
ArrayTypeType of input array
Parameters
keysinput of keys to sort
minminimum key in input array
maxmaximum key in input array
Example
The following code snippet demonstrates how to use counting_sort.
#include <cusp/array1d.h>
#include <cusp/print.h>
#include <cusp/sort.h>
#include <thrust/transform.h>
struct mod
{
int operator()(int i)
{
// transform i to range [0,5)
return (i >= 0 ? i : -i) % 5;
}
};
int main(void)
{
// create random array with 10 elements
// initialize v to random array
// transform entries to interval [0,5)
thrust::transform(v.begin(), v.end(), v.begin(), mod());
// print array
// sort
// print the sorted array
return 0;
}
void counting_sort(ArrayType &keys, typename ArrayType::value_type min, typename ArrayType::value_type max)
Use counting sort to order an array.
Specialized sorting routines.

◆ counting_sort_by_key()

template<typename ArrayType1 , typename ArrayType2 >
void cusp::counting_sort_by_key ( ArrayType1 &  keys,
ArrayType2 &  vals,
typename ArrayType1::value_type  min,
typename ArrayType1::value_type  max 
)

Use counting sort to order an array and permute an array of values.

Template Parameters
ArrayType1Type of keys array
ArrayType2Type of values array
Parameters
keysinput array of keys to sort
valsinput array of values to permute
minminimum key in keys array
maxmaximum key in keys array
Example
The following code snippet demonstrates how to use counting_sort_by_key.
#include <cusp/array1d.h>
#include <cusp/print.h>
#include <cusp/sort.h>
#include <thrust/transform.h>
struct mod
{
int operator()(int i)
{
// transform i to range [0,5)
return (i >= 0 ? i : -i) % 5;
}
};
int main(void)
{
// create random array with 10 elements
// initialize v to random array
// transform entries to interval [0,5)
thrust::transform(v1.begin(), v1.end(), v1.begin(), mod());
// print array
// sort
// print the sorted array
return 0;
}
void counting_sort_by_key(ArrayType1 &keys, ArrayType2 &vals, typename ArrayType1::value_type min, typename ArrayType1::value_type max)
Use counting sort to order an array and permute an array of values.

◆ elementwise()

template<typename MatrixType1 , typename MatrixType2 , typename MatrixType3 , typename BinaryFunction >
void cusp::elementwise ( const MatrixType1 &  A,
const MatrixType2 &  B,
MatrixType3 &  C,
BinaryFunction  op 
)

Perform transform operation on two matrices.

Template Parameters
MatrixType1Type of first matrix
MatrixType2Type of second matrix
MatrixType3Type of output matrix
BinaryFunctionType of binary transform to apply
Parameters
Afirst input matrix
Bsecond input matrix
Coutput matrix
opbinary transform to apply
Example
The following code snippet demonstrates how to use elementwise.
#include <cusp/array2d.h>
#include <cusp/print.h>
int main(void)
{
// initialize first 2x3 matrix
A(0,0) = 10; A(0,1) = 20; A(0,2) = 30;
A(1,0) = 40; A(1,1) = 50; A(1,2) = 60;
// print A
// initialize second 2x3 matrix
B(0,0) = 60; B(0,1) = 50; B(0,2) = 40;
B(1,0) = 30; B(1,1) = 20; B(1,2) = 10;
// print B
// compute the sum
cusp::elementwise(A, B, C, ::cuda::std::plus<int>());
// print C
return 0;
}
void elementwise(const MatrixType1 &A, const MatrixType2 &B, MatrixType3 &C, BinaryFunction op)
Perform transform operation on two matrices.

◆ extract_diagonal()

template<typename MatrixType , typename ArrayType >
void cusp::extract_diagonal ( const MatrixType &  A,
ArrayType &  output 
)

Extract the main diagonal of a matrix.

Template Parameters
MatrixTypeType of input matrix
ArrayTypeType of input diagonal array
Parameters
AThe input matrix
outputOn return contains the main diagonal of A with zeros inserted for missing entries.
Example
// include cusp array1d header file
#include <cusp/array1d.h>
#include <cusp/print.h>
int main()
{
// initialize 5x5 poisson matrix
// allocate array to hold diagonal entries
// extract diagonal of A
cusp::extract_diagonal(A, diagonal);
// print diagonal entries
cusp::print(diagonal);
}
void extract_diagonal(const MatrixType &A, ArrayType &output)
Extract the main diagonal of a matrix.

◆ generalized_spgemm()

template<typename LinearOperator , typename MatrixOrVector1 , typename MatrixOrVector2 , typename UnaryFunction , typename BinaryFunction1 , typename BinaryFunction2 >
void cusp::generalized_spgemm ( const LinearOperator &  A,
const MatrixOrVector1 &  B,
MatrixOrVector2 &  C,
UnaryFunction  initialize,
BinaryFunction1  combine,
BinaryFunction2  reduce 
)

Implements generalized matrix-matrix multiplication.

Overview

generalized multiply can be used with dense and sparse matrices, and user-defined linear_operator objects. This function compute nonzeros only for the entries present in the output matrix. Entries that are not specified in the output matrix are disregarded (annihilated). This specification significantly reduces the computational work corresponding to computing the sparsity of the output and performing unnecessary (combine, reduce) operations.

Template Parameters
LinearOperatorType of matrix
MatrixOrVector1Type of second matrix
MatrixOrVector2Type of output matrix
UnaryFunctionType of unary function to initialize RHS
BinaryFunction1Type of binary function to combine entries
BinaryFunction2Type of binary function to reduce entries
Parameters
Afirst input matrix
Bsecond input matrix
Coutput matrix
initializeunary function used to initialize the output
combinebinary function used to combine entries
reducebinary function used to reduce entries
Example

The following code snippet demonstrates how to use generalized_spgemm to compute a matrix-matrix product.

#include <cusp/multiply.h>
#include <cusp/print.h>
int main(void)
{
// define multiply functors
::cuda::std::identity identity;
::cuda::std::multiplies<float> combine;
::cuda::std::plus<float> reduce;
// initialize matrix
// allocate output matrices and initialize output nonzeros
// compute B = A * A
cusp::generalized_spgemm(A, A, B, zero, combine, reduce);
// compute C += A * A
cusp::generalized_spgemm(A, A, C, identity, combine, reduce);
// print output matrices
return 0;
}
Defines templated convenience functors analogous to what is found in thrust's functional.
void generalized_spgemm(const LinearOperator &A, const MatrixOrVector1 &B, MatrixOrVector2 &C, UnaryFunction initialize, BinaryFunction1 combine, BinaryFunction2 reduce)
Implements generalized matrix-matrix multiplication.
Matrix multiplication.
constant_functor is a function object returns a constant value, ignores the input.
Definition functional.h:435

◆ generalized_spmv()

template<typename LinearOperator , typename Vector1 , typename Vector2 , typename Vector3 , typename BinaryFunction1 , typename BinaryFunction2 >
void cusp::generalized_spmv ( const LinearOperator &  A,
const Vector1 &  x,
const Vector2 &  y,
Vector3 &  z,
BinaryFunction1  combine,
BinaryFunction2  reduce 
)

Implements generalized matrix-vector multiplication.

Overview

generalized multiply can be used with dense and sparse matrices, and user-defined linear_operator objects.

Template Parameters
LinearOperatorType of first matrix
Vector1Type of second input vector
Vector2Type of third input vector
Vector3Type of output vector
Parameters
Ainput matrix
xinput vector
yinput vector
zoutput vector
combinebinary function used to combine entries
reducebinary function used to reduce entries
Example

The following code snippet demonstrates how to use multiply to compute a matrix-vector product.

#include <cusp/array1d.h>
#include <cusp/array2d.h>
#include <cusp/multiply.h>
#include <cusp/print.h>
int main(void)
{
// define multiply functors
::cuda::std::multiplies<float> combine;
::cuda::std::plus<float> reduce;
// initialize matrix
A(0,0) = 10; A(0,1) = 20;
A(1,0) = 40; A(1,1) = 50;
// initialize input vector
x[0] = 1;
x[1] = 2;
// initial RHS filled with 2's
// allocate output vector
// compute z = y + (A * x)
cusp::generalized_spmv(A, x, y, z, combine, reduce);
// print z
return 0;
}
Specialized array1d_view wrapping thrust::constant_iterator.
Definition array1d.h:616
void generalized_spmv(const LinearOperator &A, const Vector1 &x, const Vector2 &y, Vector3 &z, BinaryFunction1 combine, BinaryFunction2 reduce)
Implements generalized matrix-vector multiplication.

◆ indices_to_offsets()

template<typename IndexArray , typename OffsetArray >
void cusp::indices_to_offsets ( const IndexArray &  indices,
OffsetArray &  offsets 
)

Compress COO row indices to CSR row offsets.

Template Parameters
IndexArrayType of input row indices
OffsetTypeType of input row offsets
Parameters
indicesThe input row indices
offsetsThe output row offsets
Example
// include cusp array1d header file
#include <cusp/array1d.h>
#include <cusp/print.h>
int main()
{
// Allocate a array of size 5
// Set the elements to [0, 0, 1, 1, 1]
a[0] = 0; a[1] = 0; a[2] = 1; a[3] = 1; a[4] = 1;
// Allocate a seceond array equal to the last entry in
// a (number of rows) + 2 (zero entry + last entry)
// compute the expanded entries
// print [0, 2, 5]
}
void indices_to_offsets(const IndexArray &indices, OffsetArray &offsets)
Compress COO row indices to CSR row offsets.

◆ is_valid_matrix() [1/2]

template<typename MatrixType >
bool cusp::is_valid_matrix ( const MatrixType &  matrix)

Validate format of a given matrix.

Template Parameters
MatrixTypematrix container
Parameters
matrixA matrix container (e.g. csr_matrix or coo_matrix)
Returns
true if format is valid otherwise false

◆ is_valid_matrix() [2/2]

template<typename MatrixType , typename OutputStream >
bool cusp::is_valid_matrix ( const MatrixType &  matrix,
OutputStream &  ostream 
)

Validate format of a given matrix.

Template Parameters
MatrixTypematrix container
OutputStreamstream type
Parameters
matrixA matrix container (e.g. csr_matrix or coo_matrix)
ostreamStream to which the validate stream should print
Returns
true if format is valid otherwise false

◆ multiply() [1/2]

template<typename LinearOperator , typename MatrixOrVector1 , typename MatrixOrVector2 >
void cusp::multiply ( const LinearOperator &  A,
const MatrixOrVector1 &  B,
MatrixOrVector2 &  C 
)

Implements matrix-matrix and matrix-vector multiplication.

Overview

multiply can be used with dense matrices, sparse matrices, and user-defined linear_operator objects.

Template Parameters
LinearOperatorType of first matrix
MatrixOrVector1Type of second matrix or vector
MatrixOrVector2Type of output matrix or vector
Parameters
Ainput matrix
Binput matrix or vector
Coutput matrix or vector
Example

The following code snippet demonstrates how to use multiply to compute a matrix-vector product.

#include <cusp/array1d.h>
#include <cusp/array2d.h>
#include <cusp/multiply.h>
#include <cusp/print.h>
int main(void)
{
// initialize matrix
A(0,0) = 10; A(0,1) = 20;
A(1,0) = 40; A(1,1) = 50;
// initialize input vector
x[0] = 1;
x[1] = 2;
// allocate output vector
// compute y = A * x
cusp::multiply(A, x, y);
// print y
return 0;
}
void multiply(const LinearOperator &A, const MatrixOrVector1 &B, MatrixOrVector2 &C)
Implements matrix-matrix and matrix-vector multiplication.

◆ multiply() [2/2]

template<typename LinearOperator , typename MatrixOrVector1 , typename MatrixOrVector2 , typename UnaryFunction , typename BinaryFunction1 , typename BinaryFunction2 >
void cusp::multiply ( const LinearOperator &  A,
const MatrixOrVector1 &  B,
MatrixOrVector2 &  C,
UnaryFunction  initialize,
BinaryFunction1  combine,
BinaryFunction2  reduce 
)

Implements matrix-vector multiplication with custom combine and reduce functionality.

Overview

multiply can be used with dense matrices, sparse matrices, and user-defined linear_operator objects.

Template Parameters
LinearOperatorType of matrix
MatrixOrVector1Type of second vector
MatrixOrVector2Type of output vector
UnaryFunctionType of unary function to initialize RHS
BinaryFunction1Type of binary function to combine entries
BinaryFunction2Type of binary function to reduce entries
Parameters
Ainput matrix
Binput vector
Coutput vector
initializeunary function used to initialize the output
combinebinary function used to combine entries
reducebinary function used to reduce entries
Example

The following code snippet demonstrates how to use multiply to compute a matrix-vector product.

#include <cusp/array1d.h>
#include <cusp/array2d.h>
#include <cusp/multiply.h>
#include <cusp/print.h>
int main(void)
{
// define multiply functors
::cuda::std::multiplies<float> combine;
::cuda::std::plus<float> reduce;
// initialize matrix
A(0,0) = 10; A(0,1) = 20;
A(1,0) = 40; A(1,1) = 50;
// initialize input vector
x[0] = 1;
x[1] = 2;
// allocate output vector
// compute y = A * x
cusp::multiply(A, x, y, initialize, combine, reduce);
// print y
return 0;
}

◆ offsets_to_indices()

template<typename OffsetArray , typename IndexArray >
void cusp::offsets_to_indices ( const OffsetArray &  offsets,
IndexArray &  indices 
)

Expand CSR row offsets to COO row indices.

Template Parameters
OffsetTypeType of input row offsets
IndexArrayType of input row indices
Parameters
offsetsThe input row offsets
indicesThe output row indices
Example
// include cusp array1d header file
#include <cusp/array1d.h>
int main()
{
// Allocate a array of size 3
// Set the elements to [0, 2, 5]
a[0] = 0; a[1] = 2; a[2] = 5;
// Allocate a seceond array equal to the total number
// of expanded entries from vector a (last entry in the vector)
// compute the expanded entries
// print [0, 0, 1, 1, 1]
}
void offsets_to_indices(const OffsetArray &offsets, IndexArray &indices)
Expand CSR row offsets to COO row indices.

◆ sort_by_row()

template<typename ArrayType1 , typename ArrayType2 , typename ArrayType3 >
void cusp::sort_by_row ( ArrayType1 &  row_indices,
ArrayType2 &  column_indices,
ArrayType3 &  values,
typename ArrayType1::value_type  min_row = 0,
typename ArrayType1::value_type  max_row = 0 
)

Sort matrix indices by row.

Template Parameters
ArrayType1Type of input matrix row indices
ArrayType2Type of input matrix column indices
ArrayType3Type of input matrix values
Parameters
row_indicesinput matrix row indices
column_indicesinput matrix column indices
valuesinput matrix values
min_rowminimum row index
max_rowmaximum row index
Example
The following code snippet demonstrates how to use sort_by_row.
#include <cusp/print.h>
#include <cusp/sort.h>
int main(void)
{
// allocate storage for (4,3) matrix with 6 nonzeros
// initialize matrix entries on host
A.row_indices[0] = 3; A.column_indices[0] = 0; A.values[0] = 10;
A.row_indices[1] = 3; A.column_indices[1] = 2; A.values[1] = 20;
A.row_indices[2] = 2; A.column_indices[2] = 0; A.values[2] = 30;
A.row_indices[3] = 2; A.column_indices[3] = 2; A.values[3] = 40;
A.row_indices[4] = 0; A.column_indices[4] = 1; A.values[4] = 50;
A.row_indices[5] = 0; A.column_indices[5] = 2; A.values[5] = 60;
// sort A by row
cusp::sort_by_row(A.row_indices, A.column_indices, A.values);
// print the sorted matrix
return 0;
}
void sort_by_row(ArrayType1 &row_indices, ArrayType2 &column_indices, ArrayType3 &values, typename ArrayType1::value_type min_row=0, typename ArrayType1::value_type max_row=0)
Sort matrix indices by row.

◆ sort_by_row_and_column()

template<typename ArrayType1 , typename ArrayType2 , typename ArrayType3 >
void cusp::sort_by_row_and_column ( ArrayType1 &  row_indices,
ArrayType2 &  column_indices,
ArrayType3 &  values,
typename ArrayType1::value_type  min_row = 0,
typename ArrayType1::value_type  max_row = 0,
typename ArrayType2::value_type  min_col = 0,
typename ArrayType2::value_type  max_col = 0 
)

Sort matrix indices by row and column.

Template Parameters
ArrayType1Type of input matrix row indices
ArrayType2Type of input matrix column indices
ArrayType3Type of input matrix values
Parameters
row_indicesinput matrix row indices
column_indicesinput matrix column indices
valuesinput matrix values
min_rowminimum row index
max_rowmaximum row index
min_colminimum column index
max_colmaximum column index
Example
The following code snippet demonstrates how to use sort_by_row_and_column.
#include <cusp/print.h>
#include <cusp/sort.h>
int main(void)
{
// allocate storage for (4,3) matrix with 6 nonzeros
// initialize matrix entries on host
A.row_indices[0] = 3; A.column_indices[0] = 2; A.values[0] = 10;
A.row_indices[1] = 3; A.column_indices[1] = 0; A.values[1] = 20;
A.row_indices[2] = 2; A.column_indices[2] = 0; A.values[2] = 30;
A.row_indices[3] = 2; A.column_indices[3] = 2; A.values[3] = 40;
A.row_indices[4] = 0; A.column_indices[4] = 2; A.values[4] = 50;
A.row_indices[5] = 0; A.column_indices[5] = 1; A.values[5] = 60;
// sort A by row
cusp::sort_by_row_and_column(A.row_indices, A.column_indices, A.values);
// print the sorted matrix
return 0;
}
void sort_by_row_and_column(ArrayType1 &row_indices, ArrayType2 &column_indices, ArrayType3 &values, typename ArrayType1::value_type min_row=0, typename ArrayType1::value_type max_row=0, typename ArrayType2::value_type min_col=0, typename ArrayType2::value_type max_col=0)
Sort matrix indices by row and column.

◆ subtract()

template<typename MatrixType1 , typename MatrixType2 , typename MatrixType3 >
void cusp::subtract ( const MatrixType1 &  A,
const MatrixType2 &  B,
MatrixType3 &  C 
)

Compute the difference of two matrices.

Template Parameters
MatrixType1Type of first matrix
MatrixType2Type of second matrix
MatrixType3Type of output matrix
Parameters
Afirst input matrix
Bsecond input matrix
Coutput matrix
Example
The following code snippet demonstrates how to use subtract.
#include <cusp/array2d.h>
#include <cusp/print.h>
int main(void)
{
// initialize first 2x3 matrix
A(0,0) = 10; A(0,1) = 20; A(0,2) = 30;
A(1,0) = 40; A(1,1) = 50; A(1,2) = 60;
// print A
// initialize second 2x3 matrix
B(0,0) = 60; B(0,1) = 50; B(0,2) = 40;
B(1,0) = 30; B(1,1) = 20; B(1,2) = 10;
// print B
// compute the subtract
cusp::subtract(A, B, C);
// print C
return 0;
}
void subtract(const MatrixType1 &A, const MatrixType2 &B, MatrixType3 &C)
Compute the difference of two matrices.

◆ transpose()

template<typename MatrixType1 , typename MatrixType2 >
void cusp::transpose ( const MatrixType1 &  A,
MatrixType2 &  At 
)

Transpose a matrix.

Template Parameters
MatrixType1Type of input matrix to transpose
MatrixType2Type of output matrix
Parameters
Ainput matrix
Atoutput matrix (transpose of A)
Example
The following code snippet demonstrates how to use transpose.
#include <cusp/transpose.h>
#include <cusp/array2d.h>
#include <cusp/print.h>
int main(void)
{
// initialize a 2x3 matrix
A(0,0) = 10; A(0,1) = 20; A(0,2) = 30;
A(1,0) = 40; A(1,1) = 50; A(1,2) = 60;
// print A
// compute the transpose
// print A^T
return 0;
}
void transpose(const MatrixType1 &A, MatrixType2 &At)
Transpose a matrix.
Matrix transpose.