PBeFS
Parallel Best-First Search
Loading...
Searching...
No Matches
SearchUtils.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File SearchUtils.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
17/*--------------------------------------------------------------------------*/
18/*------------------------------ DEFINITIONS -------------------------------*/
19/*--------------------------------------------------------------------------*/
20
21#ifndef __PBeFS_SearchUtils
22 #define __PBeFS_SearchUtils
23 /* self-identification: #endif at the end of the file */
24
25/*--------------------------------------------------------------------------*/
26/*-------------------------------- INCLUDES --------------------------------*/
27/*--------------------------------------------------------------------------*/
28
29#include <algorithm>
30
31#include <chrono>
32
33#include <cstddef>
34
35#include <cstdint>
36
37#include <cstdlib>
38
39#include <vector>
40
41#include "ADS/concurrent/Backoff.h"
42
43#include "ADS/dictionary/RandomHashing.h"
44
45#include "ADS/memory/HugePageAllocator.h"
46
47#include "ADS/parallel/Affinity.h"
48
49#include "ADS/parallel/Executor.h"
50
51#include "ADS/parallel/ThreadPool.h"
52
53/*--------------------------------------------------------------------------*/
54/*------------------------------- NAMESPACE --------------------------------*/
55/*--------------------------------------------------------------------------*/
56
58namespace PBeFS_di_unipi_it
59{
60
61/*--------------------------------------------------------------------------*/
62/*------------------------ CONSTANTS AND FUNCTIONS -------------------------*/
63/*--------------------------------------------------------------------------*/
64
66inline constexpr std::size_t cache_line = 64;
67
68/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
70
76
77inline void pin_worker( unsigned i ) noexcept
78{
79 static const bool pin = [] {
80 const char * v = std::getenv( "PBEFS_PIN" );
81 return( v && *v && ( v[ 0 ] != '0' ) );
82 }();
83 if( pin )
84 static_cast< void >( ADS::pinThread( i ) );
85 }
86
87/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
89
93
95
96 unsigned threads = 1;
97 std::size_t batch = 64;
98 unsigned flush_period = 64;
99 std::uint64_t seed = 1;
100
102 void normalize( void ) noexcept {
103 if( threads == 0 )
104 threads = 1;
105 if( batch == 0 )
106 batch = 1;
107 if( flush_period == 0 )
108 flush_period = 1;
109 }
110
111 }; // end( struct( ParallelParams ) )
112
113/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
115
119
120template< ADS::WorkerExecutor Executor , class F >
121void run_workers( Executor * ex , unsigned p , F && f )
122{
123 const auto body = [ & f ]( std::size_t i ) {
124 if( ! ADS::isRunCaller() )
125 pin_worker( unsigned( i ) );
126 f( unsigned( i ) );
127 };
128 if( ex )
129 ex->run( p , body );
130 else {
131 Executor own( p );
132 own.run( p , body );
133 }
134 }
135
136/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
138
139inline void prefetch( const void * p ) noexcept
140{
141 #if defined( __GNUC__ ) || defined( __clang__ )
142 __builtin_prefetch( p , 0 , 1 );
143 #else
144 ( void ) p;
145 #endif
146 }
147
148/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
150
154
155template< class Gen >
156[[nodiscard]] double random_unit( Gen & gen )
157{
158 return( double( std::uint64_t( gen() ) >> 11 ) * 0x1.0p-53 );
159 }
160
161/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
163
164template< class Gen >
165[[nodiscard]] std::uint64_t random_below( Gen & gen , std::uint64_t n )
166{
167 return( std::uint64_t( gen() ) % n );
168 }
169
170/*--------------------------------------------------------------------------*/
171/*---------------------------- CLASS Stopwatch -----------------------------*/
172/*--------------------------------------------------------------------------*/
174
176
177 public:
178
179 Stopwatch( void ) : f_start( std::chrono::steady_clock::now() ) {}
180
182 [[nodiscard]] double seconds( void ) const {
183 return( std::chrono::duration< double >(
184 std::chrono::steady_clock::now() - f_start ).count() );
185 }
186
187 private:
188
189 std::chrono::steady_clock::time_point f_start;
190
191 }; // end( class( Stopwatch ) )
192
193/*--------------------------------------------------------------------------*/
194
195} // end( namespace PBeFS_di_unipi_it )
196
197/*--------------------------------------------------------------------------*/
198
199#endif /* __PBeFS_SearchUtils */
200
201/*--------------------------------------------------------------------------*/
202/*------------------------- End File SearchUtils.h -------------------------*/
203/*--------------------------------------------------------------------------*/
Stopwatch(void)
Definition SearchUtils.h:179
double seconds(void) const
the seconds elapsed since the construction
Definition SearchUtils.h:182
namespace for the Parallel Best-First Search library
Definition AStar.h:83
constexpr std::size_t cache_line
the size of a cache line, used to keep the data of the workers apart
Definition SearchUtils.h:66
std::uint64_t random_below(Gen &gen, std::uint64_t n)
an integer in [ 0 , n ) from the next word of a 64-bit generator, n > 0
Definition SearchUtils.h:165
void run_workers(Executor *ex, unsigned p, F &&f)
runs the workers f( 0 ) , ... , f( p - 1 ) at once on an Executor
Definition SearchUtils.h:121
double random_unit(Gen &gen)
a real number in [ 0 , 1 ) from the next word of a 64-bit generator
Definition SearchUtils.h:156
void pin_worker(unsigned i) noexcept
pins the calling thread to the i-th CPU it may run on, if PBEFS_PIN is set
Definition SearchUtils.h:77
void prefetch(const void *p) noexcept
hints the processor to bring the cache line of p in, for reading
Definition SearchUtils.h:139
the parameters shared by the parallel searches
Definition SearchUtils.h:94
std::uint64_t seed
seed of the owners
Definition SearchUtils.h:99
std::size_t batch
nodes packed in a message
Definition SearchUtils.h:97
unsigned flush_period
steps between flushes of outboxes
Definition SearchUtils.h:98
unsigned threads
the number of workers
Definition SearchUtils.h:96
void normalize(void) noexcept
sets to 1 any parameter that is 0
Definition SearchUtils.h:102