67#ifndef __ADS_interop_graphblas_GraphBLAS
68 #define __ADS_interop_graphblas_GraphBLAS
79#if __has_include( <suitesparse/GraphBLAS.h> )
80 #include <suitesparse/GraphBLAS.h>
81#elif __has_include( <GraphBLAS.h> )
84 #error "the interoperation with GraphBLAS needs the header of GraphBLAS"
110static_assert( GxB_IMPLEMENTATION_MAJOR >= 10 ,
111 "the views need the containers of GraphBLAS 10" );
118inline void check( GrB_Info r ,
const char * what )
120 if( ( r != GrB_SUCCESS ) && ( r != GrB_NO_VALUE ) )
121 throw( std::runtime_error( std::string(
"ADS::graphblas: " ) + what +
122 " returned " + std::to_string(
int( r ) ) ) );
128[[nodiscard]] GrB_Type
type(
void )
130 if constexpr( std::is_same_v< T , bool > )
132 else if constexpr( std::is_floating_point_v< T > ) {
133 static_assert( (
sizeof( T ) == 4 ) || (
sizeof( T ) == 8 ) ,
134 "a floating type of 32 or 64 bits" );
135 if constexpr(
sizeof( T ) == 4 )
141 static_assert( std::is_integral_v< T > ,
"a type of numbers" );
142 constexpr bool s = std::is_signed_v< T >;
143 if constexpr(
sizeof( T ) == 1 )
144 return( s ? GrB_INT8 : GrB_UINT8 );
145 else if constexpr(
sizeof( T ) == 2 )
146 return( s ? GrB_INT16 : GrB_UINT16 );
147 else if constexpr(
sizeof( T ) == 4 )
148 return( s ? GrB_INT32 : GrB_UINT32 );
150 static_assert(
sizeof( T ) == 8 ,
"an integer of 8 to 64 bits" );
151 return( s ? GrB_INT64 : GrB_UINT64 );
158template< std::
integral I >
161 static_assert( (
sizeof( I ) == 4 ) || (
sizeof( I ) == 8 ) ,
162 "the indices of GraphBLAS have 32 or 64 bits" );
163 if constexpr(
sizeof( I ) == 4 )
164 return( GrB_UINT32 );
166 return( GrB_UINT64 );
181 explicit Matrix( GrB_Matrix A ) : f_A( A ) { }
187 Matrix(
Matrix && o ) noexcept : f_A( std::exchange( o.f_A ,
nullptr ) ) { }
193 f_A = std::exchange( o.f_A ,
nullptr );
201 [[nodiscard]] GrB_Matrix
get(
void )
const {
return( f_A ); }
204 operator GrB_Matrix(
void )
const {
return( f_A ); }
209 GrB_Matrix_free( &f_A );
214 GrB_Matrix f_A =
nullptr;
227inline void loadReadOnly( GrB_Vector v ,
const void * x , GrB_Type t ,
228 std::size_t k , std::size_t s ,
const void * z )
230 void * p =
const_cast< void *
>( ( k == 0 ) ? z : x );
231 const std::size_t c = ( k == 0 ) ? 1 : k;
232 check( GxB_Vector_load( v , &p , t , std::uint64_t( k ) ,
233 std::uint64_t( c * s ) ,
234 GxB_IS_READONLY ,
nullptr ) ,
241template<
class P ,
class I ,
class T >
242[[nodiscard]]
Matrix csrView( std::size_t m , std::size_t n , std::size_t nnz ,
243 const P * p ,
const I * i ,
const T * x ,
246 static constexpr P zp = 0;
247 static constexpr I zi = 0;
248 static constexpr T zx = T();
249 GrB_Matrix A =
nullptr;
250 check( GrB_Matrix_new( &A ,
type< T >() , GrB_Index( m ) , GrB_Index( n ) ) ,
253 GxB_Container C =
nullptr;
254 check( GxB_Container_new( &C ) ,
"GxB_Container_new" );
256 C->nrows = std::uint64_t( m );
257 C->ncols = std::uint64_t( n );
258 C->nrows_nonempty = -1;
259 C->ncols_nonempty = -1;
260 C->nvals = std::uint64_t( nnz );
261 C->format = GxB_SPARSE;
262 C->orientation = GrB_ROWMAJOR;
265 loadReadOnly( C->p , p ,
indexType< P >() , m + 1 ,
sizeof( P ) , &zp );
266 loadReadOnly( C->i , i ,
indexType< I >() , nnz ,
sizeof( I ) , &zi );
267 loadReadOnly( C->x , x ,
type< T >() , iso ? 1 : nnz ,
sizeof( T ) , &zx );
268 check( GxB_load_Matrix_from_Container( A , C ,
nullptr ) ,
269 "GxB_load_Matrix_from_Container" );
272 GxB_Container_free( &C );
275 GxB_Container_free( &C );
281inline constexpr bool kSortedRows =
false;
283template<
class W , ArcLayout L >
284inline constexpr bool kSortedRows< StaticGraph< W , L > > =
true;
289[[nodiscard]]
bool increasingRows(
const G & g )
291 const int * o = g.offsets();
292 const int * h = g.heads();
293 for(
int u = 0 ; u < g.n() ; ++u )
294 for(
int k = o[ u ] + 1 ; k < o[ u + 1 ] ; ++k )
295 if( h[ k - 1 ] >= h[ k ] )
303concept ArcRows =
requires(
const G & g )
314 requires( detail::ArcRows< G > )
324template< CsrArrays G >
327 if constexpr( ! detail::kSortedRows< G > )
328 if( ! detail::increasingRows( g ) )
330 const std::size_t n = std::size_t( g.n() ) , a = std::size_t( g.arcs() );
331 if constexpr( G::weighted )
332 return( detail::csrView( n , n , a , g.offsets() , g.heads() ,
336 static constexpr bool one =
true;
337 return( detail::csrView( n , n , a , g.offsets() , g.heads() , &one ,
344template<
class T ,
class I ,
class P >
347 return( detail::csrView( A.rows() , A.cols() , A.nonzeros() , A.rowStart() ,
348 A.columns() , A.values() ,
false ) );
360#define ADS_GRB_TYPED( F, T, ... ) \
362 if constexpr( std::is_same_v< T, bool > ) \
363 return( F##_BOOL( __VA_ARGS__ ) ); \
364 else if constexpr( std::is_floating_point_v< T > ) { \
365 if constexpr( sizeof( T ) == 4 ) \
366 return( F##_FP32( __VA_ARGS__ ) ); \
368 return( F##_FP64( __VA_ARGS__ ) ); \
370 else if constexpr( std::is_signed_v< T > ) { \
371 if constexpr( sizeof( T ) == 1 ) \
372 return( F##_INT8( __VA_ARGS__ ) ); \
373 else if constexpr( sizeof( T ) == 2 ) \
374 return( F##_INT16( __VA_ARGS__ ) ); \
375 else if constexpr( sizeof( T ) == 4 ) \
376 return( F##_INT32( __VA_ARGS__ ) ); \
378 return( F##_INT64( __VA_ARGS__ ) ); \
381 if constexpr( sizeof( T ) == 1 ) \
382 return( F##_UINT8( __VA_ARGS__ ) ); \
383 else if constexpr( sizeof( T ) == 2 ) \
384 return( F##_UINT16( __VA_ARGS__ ) ); \
385 else if constexpr( sizeof( T ) == 4 ) \
386 return( F##_UINT32( __VA_ARGS__ ) ); \
388 return( F##_UINT64( __VA_ARGS__ ) ); \
393template< std::
size_t S ,
bool Signed >
396struct IntOf< 1 , true >
398 using type = std::int8_t;
401struct IntOf< 2 , true >
403 using type = std::int16_t;
406struct IntOf< 4 , true >
408 using type = std::int32_t;
411struct IntOf< 8 , true >
413 using type = std::int64_t;
416struct IntOf< 1 , false >
418 using type = std::uint8_t;
421struct IntOf< 2 , false >
423 using type = std::uint16_t;
426struct IntOf< 4 , false >
428 using type = std::uint32_t;
431struct IntOf< 8 , false >
433 using type = std::uint64_t;
439using Number =
typename std::conditional_t<
440 std::is_same_v< T , bool > || std::is_floating_point_v< T > ,
441 std::type_identity< T > ,
442 IntOf<
sizeof( T ) , std::is_signed_v< T > > >
::type;
446[[nodiscard]] GrB_Info build( GrB_Matrix A ,
const GrB_Index * R ,
447 const GrB_Index * C ,
const T * X , GrB_Index k )
449 const auto * x =
reinterpret_cast< const Number< T > *
>( X );
450 return(
ADS_GRB_TYPED( GrB_Matrix_build , T , A , R , C , x , k ,
nullptr ) );
456[[nodiscard]] GrB_Info extract( GrB_Index * R , GrB_Index * C , T * X ,
457 GrB_Index * k , GrB_Matrix A )
459 auto * x =
reinterpret_cast< Number< T > *
>( X );
460 return(
ADS_GRB_TYPED( GrB_Matrix_extractTuples , T , R , C , x , k , A ) );
472 requires( detail::ArcRows< G > )
475 constexpr bool weighted = G::weighted;
476 using T = std::conditional_t< weighted , typename G::Weight , bool >;
477 const std::size_t n = std::size_t( g.n() ) , a = std::size_t( g.arcs() );
478 std::vector< GrB_Index > R( a ) , C( a );
480 auto X = std::make_unique< T[] >( a + 1 );
481 std::fill( X.get() , X.get() + a , T( 1 ) );
482 const auto h = g.arcHeads();
483 for(
int u = 0 ; u < g.n() ; ++u )
484 for(
int k = g.offsets()[ u ] ; k < g.offsets()[ u + 1 ] ; ++k ) {
485 R[ std::size_t( k ) ] = GrB_Index( u );
486 C[ std::size_t( k ) ] = GrB_Index( h[ k ] );
487 if constexpr( weighted )
488 X[ std::size_t( k ) ] = g.arcWeights()[ k ];
490 GrB_Matrix A =
nullptr;
491 check( GrB_Matrix_new( &A ,
type< T >() , GrB_Index( n ) , GrB_Index( n ) ) ,
494 check( detail::build( A , R.data() , C.data() , X.get() , GrB_Index( a ) ) ,
495 "GrB_Matrix_build" );
502template<
class T ,
class I = std::uint32_t ,
503 class P = ADS::detail::CsrOffset< I > >
506 GrB_Index m = 0 , n = 0 , k = 0;
507 check( GrB_Matrix_nrows( &m , A ) ,
"GrB_Matrix_nrows" );
508 check( GrB_Matrix_ncols( &n , A ) ,
"GrB_Matrix_ncols" );
509 check( GrB_Matrix_nvals( &k , A ) ,
"GrB_Matrix_nvals" );
510 std::vector< GrB_Index > R( k ) , C( k );
511 auto X = std::make_unique< T[] >( std::size_t( k ) + 1 );
513 check( detail::extract( R.data() , C.data() , X.get() , &got , A ) ,
514 "GrB_Matrix_extractTuples" );
515 std::vector< typename BasicCsrMatrix< T , I , P >::Triplet > E;
516 E.reserve( std::size_t( got ) );
517 for( GrB_Index q = 0 ; q < got ; ++q )
518 E.emplace_back( std::size_t( R[ q ] ) , std::size_t( C[ q ] ) , X[ q ] );
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A sparse matrix in compressed sparse rows (CSR), beyond the book: the nonzeros of each row,...
The graphs and the sparse matrices of ADS as matrices of SuiteSparse:GraphBLAS (https://github....
A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse ro...
a sparse matrix of values T in compressed sparse rows, the columns of the type I and the offsets of ...
Definition CsrMatrix.h:129
the owner of a GrB_Matrix, freed at its end
Definition GraphBLAS.h:175
void reset(void) noexcept
the matrix freed, if any
Definition GraphBLAS.h:207
Matrix & operator=(const Matrix &)=delete
Matrix & operator=(Matrix &&o) noexcept
the matrix of , which keeps none, this one freed
Definition GraphBLAS.h:190
Matrix(const Matrix &)=delete
Matrix(Matrix &&o) noexcept
the matrix of , which keeps none
Definition GraphBLAS.h:187
~Matrix(void)
Definition GraphBLAS.h:198
Matrix(GrB_Matrix A)
the owner of
Definition GraphBLAS.h:181
Matrix(void)=default
no matrix
GrB_Matrix get(void) const
the handle, for the calls of GraphBLAS
Definition GraphBLAS.h:201
GrB_Type indexType(void)
the type of GraphBLAS of the indices I, not negative, read by their size
Definition GraphBLAS.h:159
BasicCsrMatrix< T, I, P > toCsr(GrB_Matrix A)
the matrix of GraphBLAS copied into a BasicCsrMatrix of the values T, the columns I and the offsets ...
Definition GraphBLAS.h:504
Matrix copy(const G &g)
the matrix of the arcs of , copied by GrB_Matrix_build() (for the layouts that view() does not take)...
Definition GraphBLAS.h:473
Matrix view(const G &g)
the matrix of the arcs of by rows, its offsets, heads and weights read in place,...
Definition GraphBLAS.h:325
void check(GrB_Info r, const char *what)
throws if is not a success of GraphBLAS, naming what
Definition GraphBLAS.h:118
#define ADS_GRB_TYPED(F, T,...)
the call of the typed function of GraphBLAS F##_< T >, for the type T of the values of the arguments
Definition GraphBLAS.h:360
GrB_Type type(void)
the type of GraphBLAS of the numbers T: GrB_BOOL, the integers of 8 to 64 bits with or without sign,...
Definition GraphBLAS.h:128
Definition GraphBLAS.h:108
std::vector< std::vector< T > > Matrix
by rows
Definition Matrix.h:74