27 #ifndef _H_GRB_MATRIX_FACTORY 28 #define _H_GRB_MATRIX_FACTORY 154 friend class matrices< void, mode, backend >;
170 static size_t getP() {
179 static size_t getPID() {
193 static size_t compute_diag_length(
194 const size_t m,
const size_t n,
197 constexpr
const long zero = static_cast< long >( 0 );
198 const size_t k_abs = static_cast< size_t >(
199 (k < zero) ? -k : k );
201 if( k < zero && k_abs >= m ) {
203 }
else if( k > zero && k_abs >= n ) {
206 return std::min( std::min( m, n ), k < zero
244 template<
class IteratorV >
246 const size_t m,
const size_t n,
const long k,
247 const IteratorV V_iter,
const IteratorV V_end
250 std::cout <<
"createIdentity_generic called with m = " << m <<
", n = " 251 << n <<
", k = " << k <<
" (non-void variant)\n";
254 constexpr
const long s_zero = static_cast< long >( 0 );
255 constexpr
const size_t u_zero = static_cast< size_t >( 0 );
256 const size_t diag_length = compute_diag_length( m, n, k );
261 std::cout <<
"Computed diag_length = " << diag_length <<
"\n";
268 const RIT k_i_incr = static_cast< RIT >(
269 (k < s_zero) ? std::abs( k ) : u_zero );
270 const CIT k_j_incr = static_cast< CIT >(
271 (k < s_zero) ? u_zero : std::abs( k ) );
278 const size_t s = getPID();
279 const size_t P = getP();
280 assert( static_cast< size_t >(std::distance( V_iter, V_end )) ==
281 static_cast< size_t >(std::distance( I.
begin( s, P ), I.
end( s, P ) )) );
282 assert( static_cast< size_t >(std::distance( V_iter, V_end )) ==
283 static_cast< size_t >(std::distance( J.
begin( s, P ), J.
end( s, P ) )) );
293 throw std::runtime_error(
294 "Error: createIdentity_generic failed: rc = " +
grb::toString( rc )
312 const size_t m,
const size_t n
355 template<
class ValueIterator >
357 const size_t m,
const size_t n,
358 const ValueIterator values,
const ValueIterator valEnd,
359 const long k = static_cast< long >( 0 )
363 "matrices<>::diag is currently only supported with sequential I/O" );
366 typename std::iterator_traits< ValueIterator >::value_type,
369 "grb::factory::diag called with value types that are not convertible to the " 370 "matrix nonzero type." 374 if( m == 0 || n == 0 ) {
375 return empty( n, n );
379 return createIdentity_generic( m, n, k, values, valEnd );
405 const D value = static_cast< D >( 1 ),
406 const long k = static_cast< long >( 0 )
409 if( m == 0 || n == 0 ) {
410 return empty( m, n );
416 const size_t diag_length = compute_diag_length( m, n, k );
421 const size_t s = getPID();
422 const size_t P = getP();
423 return createIdentity_generic( m, n, k, V.
cbegin( s, P ), V.
cend( s, P ) );
468 const D identity_value = ring.template getOne< D >();
469 return eye( n, n, identity_value, 0 );
489 const size_t m,
const size_t n,
492 if( m == 0 || n == 0 ) {
493 return empty( m, n );
496 const size_t nz = m * n;
498 throw std::runtime_error(
"Requested dense matrix overflows in number of " 502 const size_t s = getPID();
503 const size_t P = getP();
515 grb::utils::containers::InterleavedIteratorsVector<
518 for(
size_t i = 0; i < m; ++i ) {
523 const auto entryInd2colInd = [&m] (
const CIT k) -> CIT {
527 J_raw.
cbegin( s, P ), J_raw.
cend( s, P ), entryInd2colInd );
529 J_raw.
cend( s, P ), J_raw.
cend( s, P ), entryInd2colInd );
532 const size_t local_nz = std::distance( I.
begin( s, P ), I.
end( s, P ) );
534 const size_t Isz = local_nz;
535 const size_t Jsz = std::distance( J_begin, J_end );
536 assert( Isz == Jsz );
540 const size_t Vsz = std::distance( V.
begin(), V.
end() );
541 assert( Isz == Vsz );
555 throw std::runtime_error(
556 "Error: factory::full<void> failed: rc = " +
grb::toString( rc )
576 const size_t m,
const size_t n,
const D value
578 return full( m, n, value );
619 if( n == 0 || m == 0 ) {
620 return empty( m, n );
624 return zeros( m, n, ring );
627 const size_t nz = m * n;
629 throw std::runtime_error(
"Requested dense matrix overflows in number of " 633 const auto addMon = ring.getAdditiveMonoid();
634 const D zero = ring.template getZero< D >();
637 rc = rc ? rc :
grb::foldl( matrix, A, addMon );
640 throw std::runtime_error(
"Could not promote input matrix to a dense one: " 672 const size_t m,
const size_t n,
675 const D zero = ring.template getZero< D >();
676 return full( m, n, static_cast< D >( zero ) );
704 const size_t m,
const size_t n,
707 const D one = ring.template getOne< D >();
708 return full( m, n, static_cast< D >( one ) );
721 typename RIT,
typename CIT,
typename NIT
723 class matrices< void, mode, backend, RIT, CIT, NIT > {
746 const size_t m,
const size_t n,
const long k
749 constexpr
const long s_zero = static_cast< long >( 0 );
750 constexpr
const size_t u_zero = static_cast< size_t >( 0 );
751 const size_t diag_length = BaseType::compute_diag_length( m, n, k );
753 const RIT k_i_incr = static_cast< RIT >(
754 (k < s_zero) ? std::abs( k ) : u_zero );
755 const CIT k_j_incr = static_cast< CIT >(
756 (k < s_zero) ? u_zero : std::abs( k ) );
759 const size_t s = BaseType::getPID();
760 const size_t P = BaseType::getP();
768 throw std::runtime_error(
769 "Error: createIdentity_generic<void> failed: rc = " +
grb::toString( rc )
787 const size_t m,
const size_t n
812 const size_t m,
const size_t n,
813 const long k = static_cast< long >( 0 )
816 if( m == 0 || n == 0 ) {
817 return empty( n, n );
821 return createIdentity_generic( m, n, k );
846 const long k = static_cast< long >( 0 )
849 if( m == 0 || n == 0 ) {
850 return empty( m, n );
854 return createIdentity_generic( m, n, k );
886 return eye( n, n, 0 );
907 if( m == 0 || n == 0 ) {
908 return empty( m, n );
911 const size_t nz = m * n;
913 throw std::runtime_error(
"Requested dense matrix overflows in number of " 917 const size_t s = BaseType::getPID();
918 const size_t P = BaseType::getP();
929 grb::utils::containers::InterleavedIteratorsVector<
932 for(
size_t i = 0; i < m; ++i ) {
937 const auto nonzeroInd2colInd = [&m] (
const size_t k) ->
size_t {
941 J_raw.
cbegin( s, P ), J_raw.
cend( s, P ), nonzeroInd2colInd );
943 J_raw.
cend( s, P ), J_raw.
cend( s, P ), nonzeroInd2colInd );
945 assert( std::distance( I.
begin( s, P ), I.
end( s, P ) ) ==
946 std::distance( J_begin, J_end ) );
958 throw std::runtime_error(
959 "Error: factory::full<void> failed: rc = " +
grb::toString( rc )
1020 return full( m, n );
1041 return full( m, n );
1048 #endif // end _H_GRB_MATRIX_FACTORY static MatrixType diag(const size_t m, const size_t n, const ValueIterator values, const ValueIterator valEnd, const long k=static_cast< long >(0))
Builds a diagonal matrix with the given values.
Definition: matrix_factory.hpp:356
The namespace for ALP/GraphBLAS algorithms.
Definition: graphblas.hpp:482
static MatrixType eye(const size_t m, const size_t n, const D value=static_cast< D >(1), const long k=static_cast< long >(0))
Builds a diagonal matrix.
Definition: matrix_factory.hpp:402
const_iterator cbegin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1319
RC set(Vector< DataType, backend, Coords > &x, const T val, const Phase &phase=EXECUTE, const typename std::enable_if< !grb::is_object< DataType >::value &&!grb::is_object< T >::value, void >::type *const =nullptr) noexcept
Sets all elements of a vector to the given value.
Definition: io.hpp:858
unsigned int RowIndexType
What data type should be used to store row indices.
Definition: base/config.hpp:436
Standard identity for numerical addition.
Definition: identities.hpp:57
Defines an iterator that adapts the values returned by a sub-iterator according to some user-defined ...
An ALP/GraphBLAS matrix.
Definition: matrix.hpp:72
static size_t nprocs() noexcept
Definition: spmd.hpp:56
RC
Return codes of ALP primitives.
Definition: rc.hpp:47
static MatrixType dense(const MatrixType &A)
Builds a dense pattern matrix from a given matrix.
Definition: matrix_factory.hpp:997
Standard identity for numerical multiplication.
Definition: identities.hpp:79
static MatrixType identity(const size_t n)
Builds an identity pattern matrix.
Definition: matrix_factory.hpp:883
const_iterator cbegin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1136
iterator begin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1273
static MatrixType empty(const size_t m, const size_t n)
Builds an empty matrix, without any values.
Definition: matrix_factory.hpp:311
static MatrixType empty(const size_t m, const size_t n)
Builds an empty matrix, without any values.
Definition: matrix_factory.hpp:786
Sequential mode IO.
Definition: iomode.hpp:75
static MatrixType zeros(const size_t m, const size_t n)
Builds a matrix filled with zeros.
Definition: matrix_factory.hpp:1019
iterator end(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1307
iterator begin(const size_t s=0, const size_t P=1) const
Returns a const-iterator at start position to this container.
Definition: regular.hpp:1092
static MatrixType eye(const size_t n)
Builds a diagonal pattern matrix.
Definition: matrix_factory.hpp:866
static Adapter< SubIterT > make_adapter_iterator(const SubIterT start, const SubIterT end, const std::function< typename SubIterT::value_type(const typename SubIterT::value_type) > func)
Creates an adapter of a given iterator.
Definition: adapter.hpp:349
size_t nnz(const Vector< DataType, backend, Coords > &x) noexcept
Request the number of nonzeroes in a given vector.
Definition: io.hpp:479
This operator multiplies the two input parameters and writes the result to the output variable.
Definition: ops.hpp:208
static MatrixType ones(const size_t m, const size_t n, const Semiring &ring=Semiring())
Builds a matrix filled with ones.
Definition: matrix_factory.hpp:703
size_t nrows(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the row size of a given matrix.
Definition: io.hpp:286
unsigned int ColIndexType
What data type should be used to store column indices.
Definition: base/config.hpp:449
A set of iterators that mimic containers with regular structure.
iterator end(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1125
RC buildMatrixUnique(Matrix< InputType, implementation, RIT, CIT, NIT > &A, fwd_iterator1 I, const fwd_iterator1 I_end, fwd_iterator2 J, const fwd_iterator2 J_end, fwd_iterator3 V, const fwd_iterator3 V_end, const IOMode mode)
Assigns nonzeroes to the matrix from a coordinate format.
Definition: io.hpp:1340
size_t ncols(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the column size of a given matrix.
Definition: io.hpp:339
static MatrixType identity(const size_t n, const Semiring &ring=Semiring())
Builds an identity matrix.
Definition: matrix_factory.hpp:464
RC foldl(IOType &x, const Vector< InputType, backend, Coords > &y, const Vector< MaskType, backend, Coords > &mask, const Monoid &monoid=Monoid(), const typename std::enable_if< !grb::is_object< IOType >::value &&!grb::is_object< InputType >::value &&!grb::is_object< MaskType >::value &&grb::is_monoid< Monoid >::value, void >::type *const =nullptr)
Reduces, or folds, a vector into a scalar.
Definition: blas1.hpp:3840
static size_t pid() noexcept
Definition: spmd.hpp:61
A (dense) vector of a given size that holds the same constant value at each entry.
Definition: regular.hpp:1026
Factories for creating ALP/GraphBLAS matrices with standard patterns such as identity matrices.
Definition: matrix_factory.hpp:152
Backend
A collection of all backends.
Definition: backends.hpp:49
static MatrixType full(const size_t m, const size_t n)
Build a dense pattern matrix.
Definition: matrix_factory.hpp:906
static MatrixType dense(const size_t m, const size_t n, const D value)
Builds a dense matrix filled with a given value.
Definition: matrix_factory.hpp:575
MatrixType diag(const size_t m, const size_t n, const long k=static_cast< long >(0))
Builds a diagonal pattern matrix.
Definition: matrix_factory.hpp:811
This operator takes the sum of the two input parameters and writes it to the output variable.
Definition: ops.hpp:175
IOMode
The GraphBLAS input and output functionalities can either be used in a sequential or parallel fashion...
Definition: iomode.hpp:67
static MatrixType eye(const size_t m, const size_t n, const long k=static_cast< long >(0))
Builds a diagonal pattern matrix.
Definition: matrix_factory.hpp:843
static MatrixType dense(const MatrixType &A, const Semiring &ring=Semiring())
Builds a dense matrix from a given matrix.
Definition: matrix_factory.hpp:613
const_iterator cend(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1330
static MatrixType full(const size_t m, const size_t n, const D value)
Builds a dense matrix filled with a given value.
Definition: matrix_factory.hpp:488
The main header to include in order to use the ALP/GraphBLAS API.
A container that contains a sequence of numbers with a given stride, and optionally a given number of...
Definition: regular.hpp:1163
Parallel mode IO.
Definition: iomode.hpp:92
iterator const_iterator
The const-iterator type.
Definition: regular.hpp:1194
const_iterator cend(const size_t s=0, const size_t P=1) const
Returns a const-iterator at end position to this container.
Definition: regular.hpp:1146
size_t NonzeroIndexType
What data type should be used to refer to an array containing nonzeroes.
Definition: base/config.hpp:462
Indicates the primitive has executed successfully.
Definition: rc.hpp:54
A generalised semiring.
Definition: semiring.hpp:190
static MatrixType eye(const size_t n)
Builds a diagonal matrix.
Definition: matrix_factory.hpp:435
static MatrixType ones(const size_t m, const size_t n)
Builds a dense pattern matrix filled with ones.
Definition: matrix_factory.hpp:1040
static MatrixType dense(const size_t m, const size_t n)
Builds a dense pattern matrix.
Definition: matrix_factory.hpp:979
static MatrixType zeros(const size_t m, const size_t n, const Semiring &ring=Semiring())
Builds a matrix filled with zeros.
Definition: matrix_factory.hpp:671
std::string toString(const RC code)