ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SpeculativeFor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File SpeculativeFor.h --------------------------*/
3/*--------------------------------------------------------------------------*/
56/*--------------------------------------------------------------------------*/
57/*----------------------------- DEFINITIONS --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#ifndef __ADS_SpeculativeFor
61 #define __ADS_SpeculativeFor
62/* self-identification: #endif constant */
63
64/*--------------------------------------------------------------------------*/
65/*------------------------------ INCLUDES ----------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#include "ADS/Contract.h"
70
71#include <atomic>
72#include <concepts>
73#include <cstddef>
74#include <limits>
75#include <vector>
76
77/*--------------------------------------------------------------------------*/
78/*--------------------------- NAMESPACE ------------------------------------*/
79/*--------------------------------------------------------------------------*/
80
81namespace ADS
82{
83
84/*--------------------------------------------------------------------------*/
85/*--------------------------- CLASS Reservation ----------------------------*/
86/*--------------------------------------------------------------------------*/
87
90{
91 public:
93 static constexpr std::size_t NONE = std::numeric_limits< std::size_t >::max();
94
97 void reserve( std::size_t i ) noexcept {
98 std::size_t c = f_r.load( std::memory_order_relaxed );
99 while( ( i < c ) &&
100 ! f_r.compare_exchange_weak( c , i , std::memory_order_relaxed ) )
101 ;
102 }
103
105 [[nodiscard]] bool check( std::size_t i ) const noexcept {
106 return( f_r.load( std::memory_order_relaxed ) == i );
107 }
108
110 [[nodiscard]] bool reserved( void ) const noexcept {
111 return( f_r.load( std::memory_order_relaxed ) != NONE );
112 }
113
115 void reset( void ) noexcept { f_r.store( NONE , std::memory_order_relaxed ); }
116
118 bool checkReset( std::size_t i ) noexcept {
119 if( ! check( i ) )
120 return( false );
121 reset();
122 return( true );
123 }
124
125 private:
126 std::atomic< std::size_t > f_r{ NONE };
127 }; // end( class Reservation )
128
132{
133 long long rounds = 0;
134 long long tried = 0;
135 };
136
138template< class S >
139concept SpeculativeStep = requires( S & s , std::size_t i )
140{
141 { s.reserve( i ) } -> std::convertible_to< bool >;
142 { s.commit( i ) } -> std::convertible_to< bool >;
143 };
144
178template< Executor E , SpeculativeStep S >
179void speculativeFor( E & ex , S & step , std::size_t s , std::size_t e ,
180 std::size_t g = 50 , SpeculativeCounts * c = nullptr );
181
182/*--------------------------------------------------------------------------*/
183
184} // end( namespace ADS )
185
186/*--------------------------------------------------------------------------*/
187/*-------------------------- IMPLEMENTATION --------------------------------*/
188/*--------------------------------------------------------------------------*/
189
190#include "ADS/parallel/impl/SpeculativeFor.tpp"
191
192/*--------------------------------------------------------------------------*/
193
194#endif /* SpeculativeFor.h included */
195
196/*--------------------------------------------------------------------------*/
197/*----------------------- End File SpeculativeFor.h ------------------------*/
198/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
a place reserved by the least of the iterations that want it
Definition SpeculativeFor.h:90
bool check(std::size_t i) const noexcept
whether holds the place
Definition SpeculativeFor.h:105
static constexpr std::size_t NONE
no iteration
Definition SpeculativeFor.h:93
bool reserved(void) const noexcept
whether some iteration holds the place
Definition SpeculativeFor.h:110
void reset(void) noexcept
frees the place
Definition SpeculativeFor.h:115
void reserve(std::size_t i) noexcept
keeps if it is less than the iteration there (a priority write, by compare-and-swap)
Definition SpeculativeFor.h:97
bool checkReset(std::size_t i) noexcept
frees the place if holds it; whether it did
Definition SpeculativeFor.h:118
the two steps of an iteration [see speculativeFor()]
Definition SpeculativeFor.h:139
void speculativeFor(E &ex, S &step, std::size_t s, std::size_t e, std::size_t g=50, SpeculativeCounts *c=nullptr)
the iterations of step by deterministic reservations, with the result of the sequential loop; the ro...
Definition ADT.h:96
what speculativeFor() did: its rounds and the iterations tried, the failed ones counted again
Definition SpeculativeFor.h:132
long long rounds
the rounds
Definition SpeculativeFor.h:133
long long tried
the iterations tried
Definition SpeculativeFor.h:134