ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
GraphBLAS.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File GraphBLAS.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
63/*--------------------------------------------------------------------------*/
64/*----------------------------- DEFINITIONS --------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#ifndef __ADS_interop_graphblas_GraphBLAS
68 #define __ADS_interop_graphblas_GraphBLAS
69/* self-identification: #endif constant */
70
71/*--------------------------------------------------------------------------*/
72/*------------------------------ INCLUDES ----------------------------------*/
73/*--------------------------------------------------------------------------*/
74
75#include "ADS/Contract.h"
78
79#if __has_include( <suitesparse/GraphBLAS.h> )
80 #include <suitesparse/GraphBLAS.h>
81#elif __has_include( <GraphBLAS.h> )
82 #include <GraphBLAS.h>
83#else
84 #error "the interoperation with GraphBLAS needs the header of GraphBLAS"
85#endif
86
87// the macro I of C, the imaginary unit of <complex.h>, which the header of
88// GraphBLAS includes: no program of C++ expects it, and it would rename the
89// parameters I of the templates (of BasicCsrMatrix< T , I , P >, say)
90#undef I
91
92#include <algorithm>
93#include <concepts>
94#include <cstddef>
95#include <cstdint>
96#include <memory>
97#include <stdexcept>
98#include <string>
99#include <type_traits>
100#include <utility>
101#include <vector>
102
103/*--------------------------------------------------------------------------*/
104/*--------------------------- NAMESPACE ------------------------------------*/
105/*--------------------------------------------------------------------------*/
106
108{
109
110static_assert( GxB_IMPLEMENTATION_MAJOR >= 10 ,
111 "the views need the containers of GraphBLAS 10" );
112
113/*--------------------------------------------------------------------------*/
114/*------------------------------ THE TYPES ---------------------------------*/
115/*--------------------------------------------------------------------------*/
116
118inline void check( GrB_Info r , const char * what )
119{
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 ) ) ) );
123 }
124
127template< class T >
128[[nodiscard]] GrB_Type type( void )
129{
130 if constexpr( std::is_same_v< T , bool > )
131 return( GrB_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 )
136 return( GrB_FP32 );
137 else
138 return( GrB_FP64 );
139 }
140 else {
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 );
149 else {
150 static_assert( sizeof( T ) == 8 , "an integer of 8 to 64 bits" );
151 return( s ? GrB_INT64 : GrB_UINT64 );
152 }
153 }
154 }
155
158template< std::integral I >
159[[nodiscard]] GrB_Type indexType( void )
160{
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 );
165 else
166 return( GrB_UINT64 );
167 }
168
169/*--------------------------------------------------------------------------*/
170/*----------------------------- CLASS Matrix -------------------------------*/
171/*--------------------------------------------------------------------------*/
172
175{
176 public:
178 Matrix( void ) = default;
179
181 explicit Matrix( GrB_Matrix A ) : f_A( A ) { }
182
183 Matrix( const Matrix & ) = delete;
184 Matrix & operator=( const Matrix & ) = delete;
185
187 Matrix( Matrix && o ) noexcept : f_A( std::exchange( o.f_A , nullptr ) ) { }
188
190 Matrix & operator=( Matrix && o ) noexcept {
191 if( this != &o ) {
192 reset();
193 f_A = std::exchange( o.f_A , nullptr );
194 }
195 return( *this );
196 }
197
198 ~Matrix( void ) { reset(); }
199
201 [[nodiscard]] GrB_Matrix get( void ) const { return( f_A ); }
202
204 operator GrB_Matrix( void ) const { return( f_A ); }
205
207 void reset( void ) noexcept {
208 if( f_A != nullptr )
209 GrB_Matrix_free( &f_A );
210 f_A = nullptr;
211 }
212
213 private:
214 GrB_Matrix f_A = nullptr;
215 }; // end( class Matrix )
216
217/*--------------------------------------------------------------------------*/
218/*-------------------------------- THE VIEWS -------------------------------*/
219/*--------------------------------------------------------------------------*/
220
221namespace detail
222{
223
227inline void loadReadOnly( GrB_Vector v , const void * x , GrB_Type t ,
228 std::size_t k , std::size_t s , const void * z )
229{
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 ) ,
235 "GxB_Vector_load" );
236 }
237
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 ,
244 bool iso )
245{
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 ) ) ,
251 "GrB_Matrix_new" );
252 Matrix M( A );
253 GxB_Container C = nullptr;
254 check( GxB_Container_new( &C ) , "GxB_Container_new" );
255 try {
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;
263 C->iso = iso;
264 C->jumbled = false;
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" );
270 }
271 catch( ... ) {
272 GxB_Container_free( &C );
273 throw;
274 }
275 GxB_Container_free( &C );
276 return( M );
277 }
278
280template< class G >
281inline constexpr bool kSortedRows = false;
282
283template< class W , ArcLayout L >
284inline constexpr bool kSortedRows< StaticGraph< W , L > > = true;
285
288template< class G >
289[[nodiscard]] bool increasingRows( const G & g )
290{
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 ] )
296 return( false );
297 return( true );
298 }
299
302template< class G >
303concept ArcRows = requires( const G & g )
304{
305 g.offsets();
306 g.arcHeads();
307 g.arcs();
308 G::weighted;
309 };
310
311} // end( namespace detail )
312
313template< class G >
314 requires( detail::ArcRows< G > )
315[[nodiscard]] Matrix copy( const G & g );
316
324template< CsrArrays G >
325[[nodiscard]] Matrix view( const G & g )
326{
327 if constexpr( ! detail::kSortedRows< G > )
328 if( ! detail::increasingRows( g ) )
329 return( copy( 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() ,
333 g.weightData() ,
334 false ) );
335 else {
336 static constexpr bool one = true;
337 return( detail::csrView( n , n , a , g.offsets() , g.heads() , &one ,
338 true ) );
339 }
340 }
341
344template< class T , class I , class P >
345[[nodiscard]] Matrix view( const BasicCsrMatrix< T , I , P > & A )
346{
347 return( detail::csrView( A.rows() , A.cols() , A.nonzeros() , A.rowStart() ,
348 A.columns() , A.values() , false ) );
349 }
350
351/*--------------------------------------------------------------------------*/
352/*------------------------------- THE COPIES -------------------------------*/
353/*--------------------------------------------------------------------------*/
354
355namespace detail
356{
357
360#define ADS_GRB_TYPED( F, T, ... ) \
361 [ & ]( void ) { \
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__ ) ); \
367 else \
368 return( F##_FP64( __VA_ARGS__ ) ); \
369 } \
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__ ) ); \
377 else \
378 return( F##_INT64( __VA_ARGS__ ) ); \
379 } \
380 else { \
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__ ) ); \
387 else \
388 return( F##_UINT64( __VA_ARGS__ ) ); \
389 } \
390 }()
391
393template< std::size_t S , bool Signed >
394struct IntOf;
395template<>
396struct IntOf< 1 , true >
397{
398 using type = std::int8_t;
399 };
400template<>
401struct IntOf< 2 , true >
402{
403 using type = std::int16_t;
404 };
405template<>
406struct IntOf< 4 , true >
407{
408 using type = std::int32_t;
409 };
410template<>
411struct IntOf< 8 , true >
412{
413 using type = std::int64_t;
414 };
415template<>
416struct IntOf< 1 , false >
417{
418 using type = std::uint8_t;
419 };
420template<>
421struct IntOf< 2 , false >
422{
423 using type = std::uint16_t;
424 };
425template<>
426struct IntOf< 4 , false >
427{
428 using type = std::uint32_t;
429 };
430template<>
431struct IntOf< 8 , false >
432{
433 using type = std::uint64_t;
434 };
435
438template< class 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;
443
445template< class T >
446[[nodiscard]] GrB_Info build( GrB_Matrix A , const GrB_Index * R ,
447 const GrB_Index * C , const T * X , GrB_Index k )
448{
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 ) );
451 }
452
455template< class T >
456[[nodiscard]] GrB_Info extract( GrB_Index * R , GrB_Index * C , T * X ,
457 GrB_Index * k , GrB_Matrix A )
458{
459 auto * x = reinterpret_cast< Number< T > * >( X );
460 return( ADS_GRB_TYPED( GrB_Matrix_extractTuples , T , R , C , x , k , A ) );
461 }
462
463#undef ADS_GRB_TYPED
464
465} // end( namespace detail )
466
471template< class G >
472 requires( detail::ArcRows< G > )
473[[nodiscard]] Matrix copy( const G & g )
474{
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 );
479 // not a std::vector, which has no data() for bool
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 ];
489 }
490 GrB_Matrix A = nullptr;
491 check( GrB_Matrix_new( &A , type< T >() , GrB_Index( n ) , GrB_Index( n ) ) ,
492 "GrB_Matrix_new" );
493 Matrix M( A );
494 check( detail::build( A , R.data() , C.data() , X.get() , GrB_Index( a ) ) ,
495 "GrB_Matrix_build" );
496 return( M );
497 }
498
502template< class T , class I = std::uint32_t ,
503 class P = ADS::detail::CsrOffset< I > >
504[[nodiscard]] BasicCsrMatrix< T , I , P > toCsr( GrB_Matrix A )
505{
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 );
512 GrB_Index got = k;
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 ] );
519 return( BasicCsrMatrix< T , I , P >( std::size_t( m ) , std::size_t( n ) ,
520 std::move( E ) ) );
521 }
522
523/*--------------------------------------------------------------------------*/
524
525} // end( namespace ADS::graphblas )
526
527/*--------------------------------------------------------------------------*/
528
529#endif /* GraphBLAS.h included */
530
531/*--------------------------------------------------------------------------*/
532/*-------------------------- End File GraphBLAS.h --------------------------*/
533/*--------------------------------------------------------------------------*/
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