Repository navigation
Expand file tree
/
Copy pathinterop_examples.cpp
More file actions
738 lines (680 loc) · 24.4 KB
/
Copy pathinterop_examples.cpp
File metadata and controls
738 lines (680 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*--------------------------------------------------------------------------*/
/*------------------------ File interop_examples.cpp -----------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* The examples of appendix C of the manual, Working with other libraries,
* run: each test holds the code of one example as the manual prints it
* (`declcheck.py --tests` checks that it does), followed by the checks of
* its result. The examples use the standard library, Boost.Graph,
* Boost.Heap, Boost.Unordered, LEMON, GMP, a CBLAS, oneTBB, OpenMP, the
* parallel algorithms of the standard library, stdexec, Taskflow, Kokkos,
* Eigen, METIS, KaHIP, SuiteSparse:GraphBLAS, NetworKit, the C Data
* Interface of Arrow, CRoaring and sdsl-lite; a test whose library is not
* compiled in passes with nothing to check, and
* says so. Those of MPI and of Boost.MPI are in interop_examples_mpi.cpp.
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Università di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#include "ADSTest.h"
#include "ADS/concurrent/ConcurrentUnionFind.h"
#include "ADS/dictionary/FastHash.h"
#include "ADS/graph/ShortestPaths.h"
#include "ADS/graph/StaticGraph.h"
#include "ADS/interop/arrow/Arrow.h"
#include "ADS/interop/std/FlatMap.h"
#include "ADS/interop/std/Pmr.h"
#include "ADS/numeric/BigInteger.h"
#include "ADS/numeric/CsrMatrix.h"
#include "ADS/numeric/DenseMatrix.h"
#include "ADS/parallel/Executor.h"
#include "ADS/parallel/OpenMPExecutor.h"
#include "ADS/parallel/ParallelSort.h"
#include "ADS/parallel/TBBExecutor.h"
#include "ADS/priority/IndexedDaryHeap.h"
#include "ADS/set/RoaringBitmap.h"
#include "ADS/succinct/BitVector.h"
#include "ADS/succinct/RankSelect.h"
#include "ADS/sort/Sort.h"
#include "ADS/tree/BTreeMap.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <functional>
#include <map>
#include <memory_resource>
#include <queue>
#include <random>
#include <span>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <version>
#if defined( ADS_WITH_BOOST )
#include "ADS/interop/boost/Dijkstra.h"
#include "ADS/interop/boost/FromBGL.h"
#include "ADS/interop/boost/Graph.h"
#include "ADS/interop/boost/Heap.h"
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/heap/fibonacci_heap.hpp>
#include <boost/unordered/unordered_flat_map.hpp>
#endif
#if defined( ADS_WITH_LEMON )
#include "ADS/interop/lemon/Digraphs.h"
#include "ADS/interop/lemon/Heaps.h"
#include "ADS/interop/lemon/Observers.h"
#include <lemon/dijkstra.h>
#include <lemon/list_graph.h>
#include <lemon/preflow.h>
#endif
#if defined( ADS_WITH_CROARING )
#include "ADS/interop/croaring/CRoaring.h"
#endif
#if defined( ADS_WITH_SDSL )
#include "ADS/interop/sdsl/Sdsl.h"
#include <sdsl/rank_support_v5.hpp>
#endif
#if defined( ADS_WITH_GMP ) && __has_include( <gmpxx.h> )
#include <gmpxx.h>
#define ADS_EXAMPLES_GMPXX 1
#else
#define ADS_EXAMPLES_GMPXX 0
#endif
#if defined( ADS_WITH_BLAS ) && __has_include( <cblas.h> )
#include <cblas.h>
#define ADS_EXAMPLES_CBLAS 1
#else
#define ADS_EXAMPLES_CBLAS 0
#endif
#if defined( ADS_WITH_TBB )
#include <tbb/parallel_for.h>
#include <tbb/task_arena.h>
#endif
#if defined( ADS_WITH_STDPAR )
#include "ADS/interop/parallel/StdParExecutor.h"
#endif
#if defined( ADS_WITH_STDEXEC )
#include "ADS/interop/parallel/SenderExecutor.h"
#include <exec/static_thread_pool.hpp>
#endif
#if defined( ADS_WITH_TASKFLOW )
#include "ADS/interop/parallel/TaskflowExecutor.h"
#endif
#if defined( ADS_WITH_KOKKOS )
#include "ADS/interop/parallel/KokkosExecutor.h"
#endif
#if defined( ADS_WITH_EIGEN )
#include "ADS/interop/eigen/Eigen.h"
#include <Eigen/IterativeLinearSolvers>
#endif
#if defined( ADS_WITH_METIS )
#include "ADS/interop/metis/Metis.h"
#endif
#if defined( ADS_WITH_KAHIP )
#include "ADS/interop/kahip/KaHIP.h"
#endif
#if defined( ADS_WITH_GRAPHBLAS )
#include "ADS/interop/graphblas/GraphBLAS.h"
#endif
#if defined( ADS_WITH_NETWORKIT )
#include "ADS/graph/ListGraph.h"
#include "ADS/graph/Observable.h"
#include "ADS/interop/networkit/NetworKit.h"
#include <networkit/components/DynConnectedComponents.hpp>
#endif
static_assert( ADS_CONTRACTS == 3 , "the tests check all the contracts" );
/*--------------------------------------------------------------------------*/
/*------------------------------- HELPERS ----------------------------------*/
/*--------------------------------------------------------------------------*/
namespace
{
/// a message for the tests whose library is not compiled in
[[maybe_unused]] void notCompiled( const char * what )
{
std::cout << " (" << what << " not compiled in: nothing to check)"
<< std::endl;
}
/// whether A holds the items of a vector of 1000000 random numbers filled
/// as the examples fill it, in order
[[maybe_unused]] bool sortedSample( const std::vector< double > & A )
{
std::vector< double > B( 1000000 );
std::mt19937 r( 1 );
for( double & x : B )
x = double( r() % 1000003 );
std::sort( B.begin() , B.end() );
return( A == B );
}
} // end( namespace )
/*--------------------------------------------------------------------------*/
/*------------------------ THE STANDARD LIBRARY ----------------------------*/
/*--------------------------------------------------------------------------*/
// section C.1: std::flat_map on two TieredVector
ADS_TEST( "std-flat-map" )
{
#if defined( __cpp_lib_flat_map )
ADS::TieredFlatMap< std::string , int > m; // was std::flat_map< ... >
m[ "pear" ] = 3;
m[ "apple" ] = 5;
m.erase( "pear" );
CHECK( ( m.size() == 1 ) && ( m.begin()->first == "apple" ) &&
( m[ "apple" ] == 5 ) );
#else
notCompiled( "std::flat_map of C++23" );
#endif
}
// section C.1: the algorithms of std::ranges on a dictionary of ADS
ADS_TEST( "std-ranges-dictionary" )
{
ADS::BTreeMap< int , int > b;
for( int k = 0 ; k < 100 ; ++k )
b.insert( k , ( k * 37 ) % 101 );
const auto even = std::ranges::count_if(
b , []( const auto & x ) { return( x.first % 2 == 0 ); } );
const auto top = std::ranges::max_element(
b , {} , []( const auto & x ) { return( x.second ); } );
// even is 50, and ( *top ).first is 30, whose value is 100
CHECK( ( even == 50 ) && ( ( *top ).first == 30 ) &&
( ( *top ).second == 100 ) );
}
// section C.1: the sorts of ADS on the ranges of the standard library, the
// memory resource of a pool, a queue of the standard library as frontier
ADS_TEST( "std-sorts-pmr-queues" )
{
std::deque< int > d = { 5 , 3 , 9 , 1 , 7 };
ADS::sort( d ); // 1 3 5 7 9
std::vector< int > v = { 9 , 8 , 7 , 6 , 5 , 4 };
ADS::sort( std::span< int >( v ).subspan( 2 , 3 ) ); // 9 8 5 6 7 4
CHECK( std::ranges::is_sorted( d ) &&
( v == std::vector< int >{ 9 , 8 , 5 , 6 , 7 , 4 } ) );
ADS::PoolResource<> pool;
ADS::PmrResource<> mr( pool ); // a std::pmr::memory_resource
std::pmr::map< int , int > m( &mr ); // its nodes in the pool
for( int k = 0 ; k < 1000 ; ++k )
m[ k ] = k * k;
CHECK( ( m.size() == 1000 ) && ( m[ 30 ] == 900 ) );
const ADS::StaticGraph< double > g(
4 , { { 0 , 1 , 2. } , { 1 , 2 , 1. } , { 0 , 2 , 4. } , { 2 , 3 , 1. } } );
using P = std::pair< double , int >;
std::priority_queue< P , std::vector< P > , std::greater< P > > q;
ADS::PriorityFrontier S( q , g.n() ); // lazy: no decrease-key
const auto T = ADS::shortestPaths( g , 0 , S ); // T.d is { 0 , 2 , 3 , 4 }
CHECK( ( T.d == std::vector< double >{ 0. , 2. , 3. , 4. } ) );
}
/*--------------------------------------------------------------------------*/
/*------------------------------ BOOST.GRAPH -------------------------------*/
/*--------------------------------------------------------------------------*/
// section C.2: Dijkstra's algorithm of Boost.Graph on a StaticGraph
ADS_TEST( "bgl-dijkstra-on-ads" )
{
#if defined( ADS_WITH_BOOST )
const ADS::StaticGraph< double > g(
4 , { { 0 , 1 , 2. } , { 1 , 2 , 1. } , { 0 , 2 , 4. } , { 2 , 3 , 1. } } );
std::vector< double > d( num_vertices( g ) );
std::vector< int > p( num_vertices( g ) );
boost::dijkstra_shortest_paths(
g , 0 ,
boost::weight_map( get( boost::edge_weight , g ) )
.distance_map( d.data() )
.predecessor_map( p.data() ) ); // d is { 0 , 2 , 3 , 4 }
CHECK( ( d == std::vector< double >{ 0. , 2. , 3. , 4. } ) );
CHECK( ( p[ 3 ] == 2 ) && ( p[ 2 ] == 1 ) );
#else
notCompiled( "Boost" );
#endif
}
// section C.2: the algorithm of Johnson of ADS on a graph of Boost.Graph,
// and a heap of ADS in the visit of Boost.Graph
ADS_TEST( "bgl-ads-on-boost" )
{
#if defined( ADS_WITH_BOOST )
using AL =
boost::adjacency_list< boost::vecS , boost::vecS , boost::directedS ,
boost::no_property ,
boost::property< boost::edge_weight_t , double > >;
AL al( 4 );
add_edge( 0 , 1 , 2. , al );
add_edge( 1 , 2 , 1. , al );
add_edge( 0 , 2 , 4. , al );
add_edge( 2 , 3 , 1. , al );
const auto w = get( boost::edge_weight , al );
const ADS::bgl::FromBGL< AL , decltype( w ) > g( al , w ); // no copy
const auto T = ADS::johnson( g , 0 ); // T.d is { 0 , 2 , 3 , 4 }
CHECK( ( T.d == std::vector< double >{ 0. , 2. , 3. , 4. } ) );
std::vector< double > d( 4 );
std::vector< int > p( 4 );
ADS::IndexedDaryHeap< double , 4 > q( 4 ); // was the 4-ary heap of Boost
ADS::bgl::dijkstraShortestPaths( al , 0 , q , p.data() , d.data() );
CHECK( d == T.d );
#else
notCompiled( "Boost" );
#endif
}
/*--------------------------------------------------------------------------*/
/*----------------------- BOOST.HEAP AND BOOST.UNORDERED -------------------*/
/*--------------------------------------------------------------------------*/
// section C.3: a heap of Boost.Heap as the frontier of the shortest paths
// of ADS, and the hash of ADS in a table of Boost.Unordered
ADS_TEST( "boost-heap-unordered" )
{
#if defined( ADS_WITH_BOOST )
const ADS::StaticGraph< double > g(
4 , { { 0 , 1 , 2. } , { 1 , 2 , 1. } , { 0 , 2 , 4. } , { 2 , 3 , 1. } } );
using P = std::pair< double , int >;
using H =
boost::heap::fibonacci_heap< P ,
boost::heap::compare< std::greater< P > > >;
H h;
ADS::PriorityFrontier S( h , g.n() ); // native decrease-key
const auto T = ADS::shortestPaths( g , 0 , S ); // T.d is { 0 , 2 , 3 , 4 }
CHECK( ( T.d == std::vector< double >{ 0. , 2. , 3. , 4. } ) );
boost::unordered_flat_map< std::string , int , ADS::FastHash< std::string > >
m; // the hash of ADS, avalanching: no mixing by Boost
m[ "pear" ] = 3;
m[ "apple" ] = 5;
CHECK( ( m.size() == 2 ) && ( m[ "pear" ] == 3 ) );
#else
notCompiled( "Boost" );
#endif
}
/*--------------------------------------------------------------------------*/
/*--------------------------------- LEMON ----------------------------------*/
/*--------------------------------------------------------------------------*/
// section C.4: LEMON's Dijkstra on a heap of ADS, and the shortest paths of
// ADS on the same digraph of LEMON
ADS_TEST( "lemon-dijkstra-ads-heap" )
{
#if defined( ADS_WITH_LEMON )
using GR = lemon::ListDigraph;
GR g;
GR::ArcMap< int > l( g );
const GR::Node s = g.addNode() , a = g.addNode() , t = g.addNode();
l.set( g.addArc( s , a ) , 2 );
l.set( g.addArc( a , t ) , 1 );
l.set( g.addArc( s , t ) , 4 );
using H = ADS::lemon_interop::DaryHeap< int , GR::NodeMap< int > >;
lemon::Dijkstra< GR , GR::ArcMap< int > >::SetStandardHeap< H >::Create d( g ,
l );
d.run( s ); // d.dist( t ) is 3
CHECK( d.dist( t ) == 3 );
const ADS::lemon_interop::GraphView< GR , GR::ArcMap< int > > view( g , l );
const auto T = ADS::johnson( view , g.id( s ) ); // T.d[ g.id( t ) ] is 3
CHECK( T.d[ std::size_t( g.id( t ) ) ] == 3 );
#else
notCompiled( "LEMON" );
#endif
}
// section C.4: LEMON's Preflow on a StaticGraph, and a LemonTap that tells
// the changes of a digraph of LEMON to an observer of ADS
ADS_TEST( "lemon-preflow-tap" )
{
#if defined( ADS_WITH_LEMON )
using V = ADS::lemon_interop::StaticDigraphView< long long >;
const ADS::StaticGraph< long long > g(
4 , { { 0 , 1 , 3 } , { 0 , 2 , 2 } , { 1 , 3 , 2 } , { 2 , 3 , 3 } } );
const V lg( g ); // no copy of the rows
const auto c = lg.weights();
lemon::Preflow< V , V::WeightMap > pf( lg , c , lg.nodeFromId( 0 ) ,
lg.nodeFromId( 3 ) );
pf.run(); // pf.flowValue() is 4
CHECK( pf.flowValue() == 4 );
using GR = lemon::ListDigraph;
using Tap = ADS::lemon_interop::LemonTap< GR , GR::Arc >;
struct Count
{
int live = 0; ///< the arcs in the digraph
void on( const Tap::Insert & ) { ++live; }
void on( const Tap::Erase & ) noexcept { --live; }
};
GR h;
Tap tap( h );
Count k;
tap.attach( k );
const GR::Node u = h.addNode() , v = h.addNode();
const GR::Arc a = h.addArc( u , v );
h.addArc( v , u );
h.erase( a ); // k.live is 1
CHECK( k.live == 1 );
#else
notCompiled( "LEMON" );
#endif
}
/*--------------------------------------------------------------------------*/
/*------------------------------ GMP AND BLAS ------------------------------*/
/*--------------------------------------------------------------------------*/
// section C.5: the numbers of ADS to and from those of GMP
ADS_TEST( "gmp-numbers" )
{
#if ADS_EXAMPLES_GMPXX
const mpz_class m( "-98765432109876543210987654321" );
const ADS::BigInteger x( m ); // no string between
const mpz_class y = ( x * x ).toGmp< mpz_class >(); // the same, in GMP
CHECK( ( y == m * m ) && ( x.toString() == m.get_str() ) );
#else
notCompiled( "GMP" );
#endif
}
// section C.6: a CBLAS on the matrices of ADS
ADS_TEST( "blas-dense-matrix" )
{
#if ADS_EXAMPLES_CBLAS
ADS::DenseMatrix< double > A( 2 , 3 , 1. ) , B( 3 , 2 , 2. ) , C( 2 , 2 );
cblas_dgemm( CblasRowMajor , CblasNoTrans , CblasNoTrans , 2 , 2 , 3 , 1. ,
A.data() ,
3 , B.data() , 2 , 0. , C.data() , 2 );
const auto D = ADS::multiply( A , B ); // on cblas_dgemm() too
CHECK( ( C( 0 , 0 ) == 6. ) && ( C( 1 , 1 ) == 6. ) && ( D( 0 , 1 ) == 6. ) );
#else
notCompiled( "a CBLAS" );
#endif
}
/*--------------------------------------------------------------------------*/
/*------------------------- THE RUNTIMES OF A PROGRAM ----------------------*/
/*--------------------------------------------------------------------------*/
// section C.7: the parallel sort of ADS in an arena of oneTBB, and a
// concurrent structure of ADS in a loop of oneTBB
ADS_TEST( "tbb-arena-sort" )
{
#if defined( ADS_WITH_TBB )
std::vector< double > A( 1000000 );
std::mt19937 r( 1 );
for( double & x : A )
x = double( r() % 1000003 );
tbb::task_arena arena( 4 ); // the arena of the program
ADS::TBBExecutor ex( arena );
ADS::sort( ex , A ); // on the threads of the arena
CHECK( sortedSample( A ) );
ADS::ConcurrentUnionFind uf( 1000 );
tbb::parallel_for( 0 , 999 , [ & ]( int i ) {
if( i % 2 == 0 )
( void )uf.unite( i , i + 1 );
} ); // the sets { 0 , 1 } , { 2 , 3 } , ...
CHECK( ( uf.find( 0 ) == uf.find( 1 ) ) && ( uf.find( 1 ) != uf.find( 2 ) ) );
#else
notCompiled( "oneTBB" );
#endif
}
// section C.8: the parallel sort of ADS inside a region of OpenMP
ADS_TEST( "openmp-region-sort" )
{
#if defined( _OPENMP ) && ( _OPENMP >= 201511 )
std::vector< double > A( 1000000 );
std::mt19937 r( 1 );
for( double & x : A )
x = double( r() % 1000003 );
#pragma omp parallel num_threads( 4 )
#pragma omp single
{
ADS::OpenMPExecutor ex; // the team of the region
ADS::sort( ex , A ); // by the tasks of the team
}
CHECK( sortedSample( A ) );
#else
notCompiled( "OpenMP 4.5" );
#endif
}
// section C.9: the parallel sort of ADS on the parallel algorithms of the
// standard library
ADS_TEST( "stdpar-sort" )
{
#if defined( ADS_WITH_STDPAR )
std::vector< double > A( 1000000 );
std::mt19937 r( 1 );
for( double & x : A )
x = double( r() % 1000003 );
ADS::StdParExecutor<> ex( 4 ); // std::execution::par
ADS::sort( ex , A );
CHECK( sortedSample( A ) );
#else
notCompiled( "the parallel algorithms of the standard library" );
#endif
}
// section C.10: the parallel sort and a reduction of ADS on a scheduler of
// stdexec
ADS_TEST( "stdexec-sort" )
{
#if defined( ADS_WITH_STDEXEC )
std::vector< double > A( 1000000 );
std::mt19937 r( 1 );
for( double & x : A )
x = double( r() % 1000003 );
exec::static_thread_pool pool( 4 ); // the pool of the program
ADS::SenderExecutor ex( pool.get_scheduler() , 4 );
ADS::sort( ex , A );
const double s = ADS::parallelReduce(
ex , 0 , A.size() , 0.0 ,
[ & ]( std::size_t lo , std::size_t hi ) {
double t = 0;
for( std::size_t i = lo ; i < hi ; ++i )
t += A[ i ];
return( t );
} ,
std::plus<>() );
CHECK( sortedSample( A ) && ( s > 0 ) );
#else
notCompiled( "stdexec" );
#endif
}
// section C.11: the parallel sort of ADS on the workers of Taskflow
ADS_TEST( "taskflow-sort" )
{
#if defined( ADS_WITH_TASKFLOW )
std::vector< double > A( 1000000 );
std::mt19937 r( 1 );
for( double & x : A )
x = double( r() % 1000003 );
tf::Executor workers( 4 ); // the executor of the program
ADS::TaskflowExecutor ex( workers );
ADS::sort( ex , A );
CHECK( sortedSample( A ) );
#else
notCompiled( "Taskflow" );
#endif
}
// section C.12: the parallel sort of ADS on the space of the host of Kokkos
ADS_TEST( "kokkos-sort" )
{
#if defined( ADS_WITH_KOKKOS )
std::vector< double > A( 1000000 );
std::mt19937 r( 1 );
for( double & x : A )
x = double( r() % 1000003 );
Kokkos::ScopeGuard guard; // Kokkos started, as by the program
ADS::KokkosExecutor<> ex; // on Kokkos::DefaultHostExecutionSpace
ADS::sort( ex , A );
CHECK( sortedSample( A ) );
#else
notCompiled( "Kokkos" );
#endif
}
/*--------------------------------------------------------------------------*/
/*---------------------- THE LIBRARIES OF GRAPHS AND MATRICES --------------*/
/*--------------------------------------------------------------------------*/
// section C.14: the conjugate gradient of Eigen on a BasicCsrMatrix read in
// place, and the default CsrMatrix copied
ADS_TEST( "eigen-conjugate-gradient" )
{
#if defined( ADS_WITH_EIGEN )
const ADS::BasicCsrMatrix< double , int > A( 3 , 3 ,
{ { 0 , 0 , 4. } ,
{ 0 , 1 , -1. } ,
{ 1 , 0 , -1. } ,
{ 1 , 1 , 4. } ,
{ 1 , 2 , -1. } ,
{ 2 , 1 , -1. } ,
{ 2 , 2 , 4. } } );
const auto M = ADS::eigen::map( A ); // no copy of the rows
CHECK( M.valuePtr() == A.values() );
Eigen::ConjugateGradient< ADS::eigen::RowSparse< double > ,
Eigen::Lower | Eigen::Upper >
cg( M );
const Eigen::VectorXd x = cg.solve( Eigen::VectorXd::Ones( 3 ) );
const auto y = A.multiply( { x( 0 ) , x( 1 ) , x( 2 ) } ); // y is 1 1 1
CHECK( ( std::abs( y[ 0 ] - 1. ) < 1e-9 ) &&
( std::abs( y[ 1 ] - 1. ) < 1e-9 ) &&
( std::abs( y[ 2 ] - 1. ) < 1e-9 ) );
const auto E = ADS::eigen::toEigen( ADS::CsrMatrix( 2 , 2 ) ); // a copy
CHECK( ( E.rows() == 2 ) && ( E.nonZeros() == 0 ) );
#else
notCompiled( "Eigen" );
#endif
}
// section C.15: METIS on the rows of a StaticGraph, and the graph relabelled
// part by part
ADS_TEST( "metis-partition" )
{
#if defined( ADS_WITH_METIS )
std::vector< std::pair< int , int > > E; // a ring of 8 nodes
for( int u = 0 ; u < 8 ; ++u )
E.emplace_back( u , ( u + 1 ) % 8 );
const ADS::StaticGraph<> g( 8 , E , false );
const ADS::Partition P = ADS::metis::partition( g , 2 ); // rows in place
const ADS::StaticGraph<> h = g.relabel( P.order() ); // P.cut is 2
CHECK( ( P.cut == 2 ) && ( P.sizes()[ 0 ] == 4 ) && ( h.m() == 8 ) );
#else
notCompiled( "METIS" );
#endif
}
// section C.15: kaffpa() of KaHIP on a weighted StaticGraph, its weights
// read in place
ADS_TEST( "kahip-partition" )
{
#if defined( ADS_WITH_KAHIP )
std::vector< std::tuple< int , int , int > > E; // a ring, two light edges
for( int u = 0 ; u < 8 ; ++u )
E.emplace_back( u , ( u + 1 ) % 8 , u % 4 == 3 ? 1 : 10 );
const ADS::StaticGraph< int > g( 8 , E , false );
ADS::kahip::Options o;
o.mode = ECO;
const ADS::Partition P = ADS::kahip::partition( g , 2 , o ); // P.cut is 2
CHECK( ( P.cut == 2 ) && ( P.part[ 0 ] == P.part[ 3 ] ) &&
( P.part[ 0 ] != P.part[ 4 ] ) );
#else
notCompiled( "KaHIP" );
#endif
}
// section C.16: the triangles of a StaticGraph counted by GraphBLAS on its
// rows
ADS_TEST( "graphblas-triangles" )
{
#if defined( ADS_WITH_GRAPHBLAS )
GrB_init( GrB_NONBLOCKING ); // once, by the program
const ADS::StaticGraph<> g( 4 ,
{ { 0 , 1 } , { 0 , 2 } , { 1 , 2 } ,
{ 2 , 3 } } ,
false );
const ADS::graphblas::Matrix A = ADS::graphblas::view( g ); // no copy
GrB_Matrix C = nullptr;
GrB_Matrix_new( &C , GrB_INT64 , 4 , 4 );
GrB_mxm( C , A , nullptr , GrB_PLUS_TIMES_SEMIRING_INT64 , A , A ,
GrB_DESC_S ); // C = A .* A^2
std::int64_t t = 0;
GrB_Matrix_reduce_INT64( &t , nullptr , GrB_PLUS_MONOID_INT64 , C , nullptr );
GrB_Matrix_free( &C ); // t / 6 is 1 triangle
CHECK( t == 6 );
#else
notCompiled( "GraphBLAS" );
#endif
}
// section C.17: the dynamic components of NetworKit kept up to date by the
// changes of a ListGraph of ADS
ADS_TEST( "networkit-dynamic-components" )
{
#if defined( ADS_WITH_NETWORKIT )
using G = ADS::ListGraph<>;
using M = ADS::networkit::NetworKitMirror< G >;
G g( 4 , false );
g.insertEdge( 0 , 1 );
M m( g ); // a copy of g
NetworKit::DynConnectedComponents cc( m.graph() );
cc.run(); // 3 components
CHECK( cc.numberOfComponents() == 3 );
m.attach( cc );
ADS::ObservedGraph< G , ADS::Static< M > > og( g , m );
og.insertEdge( 2 , 3 ); // cc.numberOfComponents() is 2
CHECK( cc.numberOfComponents() == 2 );
{
auto b = ADS::batch( og );
og.insertEdge( 1 , 2 );
og.deleteEdge( 0 , 1 );
} // one updateBatch(), 2 components
CHECK( ( cc.numberOfComponents() == 2 ) &&
( cc.componentOfNode( 0 ) != cc.componentOfNode( 1 ) ) );
#else
notCompiled( "NetworKit" );
#endif
}
/*--------------------------------------------------------------------------*/
/*------------------------- THE COLUMNAR FORMATS ---------------------------*/
/*--------------------------------------------------------------------------*/
// section C.18: the sequences of ADS as the columns of Arrow, and back
ADS_TEST( "arrow-columns" )
{
ADS::BitVector b( 1000 );
b.set( 7 );
ArrowArray a;
ArrowSchema s;
ADS::exportArrow( std::move( b ) , &a , &s , "flags" ); // no copy
CHECK( a.length == 1000 && std::string( s.format ) == "b" );
a.release( &a ); // as the consumer does when done
s.release( &s );
std::vector< std::int64_t > v{ 3 , 1 , 4 };
ArrowArray c;
ArrowSchema t;
ADS::exportArrow( std::move( v ) , &c , &t );
ADS::ArrowColumn< std::int64_t > col( &c , t ); // taken back, no copy
CHECK( col.size() == 3 && col[ 2 ] == 4 && c.release == nullptr );
t.release( &t );
}
// section C.19: a set of ADS to CRoaring and back
ADS_TEST( "croaring-bitmaps" )
{
#if defined( ADS_WITH_CROARING )
const std::uint32_t x[] = { 1 , 5 , 70000 };
const ADS::RoaringBitmap s( x , 3 );
const std::vector< std::byte > file = s.writePortable(); // any Roaring
CHECK( ADS::RoaringBitmap::readPortable( file ).items() == s.items() );
roaring_bitmap_t * r = ADS::toCRoaring( s );
roaring_bitmap_add_range( r , 100 , 200 ); // in CRoaring
const ADS::RoaringBitmap t = ADS::fromCRoaring( r ); // and back
roaring_bitmap_free( r );
CHECK( t.size() == 103 && t.contains( 150 ) && t.contains( 70000 ) );
#else
notCompiled( "CRoaring" );
#endif
}
// section C.20: the bits of ADS under a structure of sdsl-lite, and back
ADS_TEST( "sdsl-vectors" )
{
#if defined( ADS_WITH_SDSL )
ADS::BitVector b( 1000 );
b.set( 3 );
b.set( 500 );
const sdsl::bit_vector v = ADS::toSdsl( b ); // a copy of the words
const sdsl::rank_support_v5< 1 > r( &v ); // a rank of sdsl
const ADS::RankSelect q( ADS::toBitVector( v ) ); // and one of ADS
CHECK( r.rank( 501 ) == 2 && q.rank1( 501 ) == 2 );
#else
notCompiled( "sdsl-lite" );
#endif
}
/*--------------------------------------------------------------------------*/
/*-------------------------------- MAIN ------------------------------------*/
/*--------------------------------------------------------------------------*/
int main( int argc , char ** argv )
{
return( ADS::test::main( argc , argv ) );
}
/*--------------------------------------------------------------------------*/
/*--------------------- End File interop_examples.cpp ----------------------*/
/*--------------------------------------------------------------------------*/