25 #include "gts/platform/Atomic.h" 
   27 #include "gts/micro_scheduler/WorkerPool.h" 
   28 #include "gts/micro_scheduler/MicroScheduler.h" 
   29 #include "gts/micro_scheduler/patterns/ParallelFor.h" 
   30 #include "gts/micro_scheduler/patterns/Range1d.h" 
   34 namespace gts_examples {
 
   52     template<
typename TFunc>
 
   58         ForTask(TFunc& func, uint32_t start, uint32_t end)
 
   67             m_func(m_start, m_end, ctx);
 
   80         : m_scheduler(scheduler)
 
   84     template<
typename TFunc>
 
   85     inline void operator()(uint32_t start, uint32_t end, TFunc func)
 
   88         uint32_t taskCount = end - start;
 
   91         pRootTask->
addRef(taskCount, gts::memory_order::relaxed);
 
   94         for (uint32_t ii = 1; ii < end; ++ii)
 
   96             Task* pChildTask = m_scheduler.allocateTask<ForTask<TFunc>>(func, ii, ii + 1);
 
   98             m_scheduler.spawnTask(pChildTask);
 
  104         m_scheduler.destoryTask(pRootTask);
 
  113 GTS_NO_INLINE 
void dummyWork(
float& f)
 
  116     for (
int jj = 0; jj < 1000; ++jj)
 
  122 struct GTS_ALIGN(GTS_NO_SHARING_CACHE_LINE_SIZE) AlignedFloat
 
  124     explicit AlignedFloat(
float f) : f(f) {}
 
  131     float& f = dummyData[workerId].f;
 
  132     for (
size_t ii = start; ii < end; ++ii)
 
  139 void submissionOnSingleThread()
 
  141     printf (
"================\n");
 
  142     printf (
"submissionOnSingleThread\n");
 
  143     printf (
"================\n");
 
  146     const uint32_t elementCount = threadCount * 100;
 
  154     const size_t sampleCount = 1000;
 
  161     uint64_t samples = 0;
 
  165     for(
size_t ii = 0; ii < sampleCount; ++ii)
 
  167         auto start = std::chrono::high_resolution_clock::now();
 
  169         BadParallelFor badParallelFor(microScheduler);
 
  170         badParallelFor(0, elementCount,
 
  171             [&dummyData](uint32_t start, uint32_t end, 
TaskContext const& ctx)
 
  176         auto end = std::chrono::high_resolution_clock::now();
 
  177         samples += (end - start).count();
 
  179     std::cout << 
"BadParallelFor time:  " << samples / sampleCount << std::endl;
 
  188     for (
size_t ii = 0; ii < sampleCount; ++ii)
 
  190         auto start = std::chrono::high_resolution_clock::now();
 
  197                 loopOverRange(range.begin(), range.end(), dummyData, ctx.
workerId.
localId());
 
  202         auto end = std::chrono::high_resolution_clock::now();
 
  203         samples += (end - start).count();
 
  205     std::cout << 
"GoodParallelFor time: " << samples / sampleCount << std::endl;
 
A empty Task that can be used as dummy or placeholder.
Definition: Task.h:355
 
A work-stealing task scheduler. The scheduler is executed by the WorkerPool it is initialized with.
Definition: MicroScheduler.h:81
 
bool initialize(WorkerPool *pWorkerPool)
Initializes the MicroScheduler and attaches it to pWorkPool, where each worker in pWorkPool will exec...
 
uint32_t workerCount() const
Get the worker count of the WorkerPool this scheduler is attached to.
 
An ID for owned objects. It indicates the owner's ID, an ID local to the owner, and an overall unique...
Definition: Utils.h:304
 
GTS_INLINE SubIdType localId() const
Definition: Utils.h:317
 
A construct that maps parallel-for behavior to a MicroScheduler.
Definition: ParallelFor.h:48
 
An iteration range over a 1D data set. Splits divide the range in two based unless the minimum size i...
Definition: Range1d.h:56
 
Recursively splits a range until it is no longer divisible.
Definition: Partitioners.h:46
 
A Task payload that embeds TFunc and TArgs into the Task's data. It makes it easy to construct a Task...
Definition: Task.h:120
 
GTS_INLINE int32_t addRef(int32_t count=1, gts::memory_order order=gts::memory_order::seq_cst)
Definition: Task.inl:84
 
GTS_INLINE void addChildTaskWithoutRef(Task *pChild)
Definition: Task.inl:30
 
A re-sizable array-like ADT. A subset of the STL Vector interface.
Definition: Vector.h:56
 
A collection of running Worker threads that a MicroScheduler can be run on.
Definition: WorkerPool.h:54
 
bool initialize(uint32_t threadCount=0)
 
Definition: 1_submissionOnSingleThread.h:54
 
Definition: 1_submissionOnSingleThread.h:48
 
The context associated with the task being executed.
Definition: MicroSchedulerTypes.h:54
 
OwnedId workerId
The ID of the current Worker.
Definition: MicroSchedulerTypes.h:65