Program Listing for File Executor.h

↰ Return to documentation for file (ADS/parallel/Executor.h)

/*--------------------------------------------------------------------------*/
/*---------------------------- File Executor.h -----------------------------*/
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
/*----------------------------- DEFINITIONS --------------------------------*/
/*--------------------------------------------------------------------------*/

#ifndef __ADS_Executor
 #define __ADS_Executor
                      /* self-identification: #endif constant */

/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/Contract.h"

#include <algorithm>
#include <atomic>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <iterator>
#include <memory>
#include <mutex>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>

/*--------------------------------------------------------------------------*/
/*--------------------------- NAMESPACE ------------------------------------*/
/*--------------------------------------------------------------------------*/

namespace ADS {

inline constexpr std::size_t kBlocks = 256;

inline constexpr std::size_t kGrain = 1024;

template< class E >
concept Executor = requires( E & e , std::size_t n ,
                             void ( & f )( std::size_t ) ) {
 { e.concurrency() } -> std::convertible_to< std::size_t >;
 e.bulk( n , f );
 };

/*--------------------------------------------------------------------------*/
/*---------------------------- CLASS Sequential ----------------------------*/
/*--------------------------------------------------------------------------*/

class Sequential {
 public:

 [[nodiscard]] static constexpr std::size_t concurrency( void ) {
  return( 1 );
  }

 template< class F >
 static void bulk( std::size_t n , F && f ) {
  for( std::size_t i = 0 ; i < n ; ++i )
   f( i );
  }

 template< class F , class G >
 static void invoke( F && f , G && g ) {
  f();
  g();
  }
 };  // end( class Sequential )

/*--------------------------------------------------------------------------*/
/*------------------------------- PATTERNS ---------------------------------*/
/*--------------------------------------------------------------------------*/

template< Executor E , class F , class G >
void parallelInvoke( E & ex , F && f , G && g );

[[nodiscard]] inline std::pair< std::size_t , std::size_t > blocksOf(
                                std::size_t first , std::size_t last ,
                                std::size_t grain = kGrain );

template< Executor E , class F >
void parallelForBlocks( E & ex , std::size_t first , std::size_t last ,
                        F && f , std::size_t grain = kGrain );

template< Executor E , class F >
void parallelFor( E & ex , std::size_t first , std::size_t last , F && f ,
                  std::size_t grain = kGrain );

template< Executor E , class T , class F , class Op >
[[nodiscard]] T parallelReduce( E & ex , std::size_t first ,
                                std::size_t last , T id , F && f , Op op ,
                                std::size_t grain = kGrain );

template< Executor E , std::random_access_iterator I ,
          std::random_access_iterator O , class T ,
          class Op = std::plus<> >
T parallelInclusiveScan( E & ex , I in , std::size_t n , O out , T init ,
                         Op op = Op() );

template< Executor E , std::random_access_iterator I ,
          std::random_access_iterator O , class T ,
          class Op = std::plus<> >
T parallelExclusiveScan( E & ex , I in , std::size_t n , O out , T init ,
                         Op op = Op() );

/*--------------------------------------------------------------------------*/

}  // end( namespace ADS )

/*--------------------------------------------------------------------------*/
/*-------------------------- IMPLEMENTATION --------------------------------*/
/*--------------------------------------------------------------------------*/

#include "ADS/parallel/impl/Executor.tpp"

/*--------------------------------------------------------------------------*/

#endif  /* Executor.h included */

/*--------------------------------------------------------------------------*/
/*------------------------- End File Executor.h ----------------------------*/
/*--------------------------------------------------------------------------*/