Beatmup
Beatmup::CustomPipeline::Impl Class Reference
Inheritance diagram for Beatmup::CustomPipeline::Impl:
Beatmup::TaskRouter

Public Member Functions

 Impl ()
 
virtual ~Impl ()
 
TaskHoldergetCurrentTask ()
 Returns currently pointed task. More...
 
const TaskHoldergetCurrentTask () const
 
void runTask ()
 Executes the pointed task. More...
 
void goToNextTask ()
 Goes to the next task in the list. More...
 
bool allTasksDone () const
 Returns true if all tasks are done. More...
 
bool allTasksAborted () const
 Returns true if the current session is aborted. More...
 
int getTaskCount ()
 
TaskHoldergetTask (int index)
 
int getTaskIndex (const TaskHolder *holder)
 
void addTask (TaskHolder *taskHolder)
 
void insertTask (TaskHolder *newbie, const TaskHolder *before)
 
bool removeTask (const TaskHolder *target)
 
void measure ()
 Determining execution mode (GPU or CPU) and thread count for each task. More...
 
AbstractTask::TaskDeviceRequirement getUsedDevices () const
 
ThreadIndex getMaxThreads () const
 
void beforeProcessing (GraphicPipeline *gpu)
 
bool process (GraphicPipeline *gpu, TaskThread &thread, CustomPipeline &pipeline)
 Processing entry point. More...
 

Private Attributes

std::vector< TaskHolder * >::iterator currentTask
 
std::vector< TaskHolder * > tasks
 the list of tasks More...
 
std::mutex tasksAccess
 task list access control More...
 
GraphicPipelinegpu
 
TaskThreadthread
 
AbstractTask::TaskDeviceRequirement executionMode
 
ThreadIndex maxThreadCount
 
bool measured
 if true, the execution mode and the thread count are determined More...
 
bool abort
 if true, one of threads executing the current task caused its aborting More...
 

Detailed Description

Definition at line 26 of file custom_pipeline.cpp.

Constructor & Destructor Documentation

◆ Impl()

Beatmup::CustomPipeline::Impl::Impl ( )
inline

Definition at line 39 of file custom_pipeline.cpp.

39  :
40  measured(false)
41  {}
bool measured
if true, the execution mode and the thread count are determined

◆ ~Impl()

virtual Beatmup::CustomPipeline::Impl::~Impl ( )
inlinevirtual

Definition at line 43 of file custom_pipeline.cpp.

43  {
44  // destroying taskholders
45  for (auto task : tasks)
46  delete task;
47  }
std::vector< TaskHolder * > tasks
the list of tasks
Beatmup::NNets::InferenceTask * task

Member Function Documentation

◆ getCurrentTask() [1/2]

TaskHolder& Beatmup::CustomPipeline::Impl::getCurrentTask ( )
inlinevirtual

Returns currently pointed task.

Implements Beatmup::TaskRouter.

Definition at line 49 of file custom_pipeline.cpp.

49  {
50  return **currentTask;
51  }
std::vector< TaskHolder * >::iterator currentTask

◆ getCurrentTask() [2/2]

const TaskHolder& Beatmup::CustomPipeline::Impl::getCurrentTask ( ) const
inlinevirtual

Implements Beatmup::TaskRouter.

Definition at line 53 of file custom_pipeline.cpp.

53  {
54  return **currentTask;
55  }

◆ runTask()

void Beatmup::CustomPipeline::Impl::runTask ( )
inlinevirtual

Executes the pointed task.

Implements Beatmup::TaskRouter.

Definition at line 57 of file custom_pipeline.cpp.

57  {
58  auto startTime = std::chrono::high_resolution_clock::now();
59  TaskHolder& task = **currentTask;
60  task.getTask().beforeProcessing(
61  task.threadCount,
62  task.executionMode != AbstractTask::TaskDeviceRequirement::CPU_ONLY && gpu ? ProcessingTarget::GPU : ProcessingTarget::CPU,
63  gpu
64  );
65 
66  // wait for other workers
68 
69  // perform the task
70  bool result;
71  if (task.executionMode != AbstractTask::TaskDeviceRequirement::CPU_ONLY && gpu)
72  result = task.getTask().processOnGPU(*gpu, *thread);
73  else
74  result = task.getTask().process(*thread);
75 
76  if (!result)
77  abort = true;
78 
79  // wait for other workers
81 
82  task.getTask().afterProcessing(
83  task.threadCount,
84  task.executionMode != AbstractTask::TaskDeviceRequirement::CPU_ONLY && gpu ? gpu : nullptr,
85  !abort
86  );
87 
88  auto endTime = std::chrono::high_resolution_clock::now();
89  task.time = std::chrono::duration<float, std::milli>(endTime - startTime).count();
90  }
void afterProcessing(ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted) override
Instruction called after the task is executed.
bool process(TaskThread &thread) override
Executes the task on CPU within a given thread.
void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu) override
Instruction called before the task is executed.
bool processOnGPU(GraphicPipeline &gpu, TaskThread &thread) override
Executes the task on GPU.
virtual void synchronize()=0
Blocks until all the other threads running the same task reach the same point.
bool abort
if true, one of threads executing the current task caused its aborting
Beatmup::IntPoint result

◆ goToNextTask()

void Beatmup::CustomPipeline::Impl::goToNextTask ( )
inlinevirtual

Goes to the next task in the list.

Implements Beatmup::TaskRouter.

Definition at line 92 of file custom_pipeline.cpp.

92  {
93  currentTask++;
94  }

◆ allTasksDone()

bool Beatmup::CustomPipeline::Impl::allTasksDone ( ) const
inlinevirtual

Returns true if all tasks are done.

Implements Beatmup::TaskRouter.

Definition at line 96 of file custom_pipeline.cpp.

96  {
97  return currentTask >= tasks.end();
98  }

◆ allTasksAborted()

bool Beatmup::CustomPipeline::Impl::allTasksAborted ( ) const
inlinevirtual

Returns true if the current session is aborted.

Implements Beatmup::TaskRouter.

Definition at line 100 of file custom_pipeline.cpp.

100  {
101  return abort && thread->isTaskAborted();
102  }
virtual bool isTaskAborted() const =0
Returns true if the task is asked to stop from outside.

◆ getTaskCount()

int Beatmup::CustomPipeline::Impl::getTaskCount ( )
inline

Definition at line 105 of file custom_pipeline.cpp.

105  {
106  std::lock_guard<std::mutex> lock(tasksAccess);
107  return (int) tasks.size();
108  }
std::mutex tasksAccess
task list access control

◆ getTask()

TaskHolder* Beatmup::CustomPipeline::Impl::getTask ( int  index)
inline

Definition at line 110 of file custom_pipeline.cpp.

110  {
111  std::lock_guard<std::mutex> lock(tasksAccess);
112  BEATMUP_ASSERT_DEBUG(0 <= index && index < tasks.size());
113  return tasks[index];
114  }
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163
jlong jint index

◆ getTaskIndex()

int Beatmup::CustomPipeline::Impl::getTaskIndex ( const TaskHolder holder)
inline

Definition at line 116 of file custom_pipeline.cpp.

116  {
117  std::lock_guard<std::mutex> lock(tasksAccess);
118  const auto& it = std::find(tasks.cbegin(), tasks.cend(), holder);
119  if (it == tasks.cend())
120  return -1;
121  return (int) (it - tasks.cbegin());
122  }

◆ addTask()

void Beatmup::CustomPipeline::Impl::addTask ( TaskHolder taskHolder)
inline

Definition at line 124 of file custom_pipeline.cpp.

124  {
125  std::lock_guard<std::mutex> lock(tasksAccess);
126  measured = false;
127  tasks.push_back(taskHolder);
128  }
return pipeline getTaskIndex * taskHolder

◆ insertTask()

void Beatmup::CustomPipeline::Impl::insertTask ( TaskHolder newbie,
const TaskHolder before 
)
inline

Definition at line 130 of file custom_pipeline.cpp.

130  {
131  std::lock_guard<std::mutex> lock(tasksAccess);
132  const auto& nextHolder = std::find(tasks.cbegin(), tasks.cend(), before);
133  if (nextHolder == tasks.cend())
134  throw RuntimeError("Reference task holder is not found in the task list");
135  measured = false;
136  tasks.insert(nextHolder , newbie);
137  }
Beatmup::CustomPipeline::TaskHolder * newbie

◆ removeTask()

bool Beatmup::CustomPipeline::Impl::removeTask ( const TaskHolder target)
inline

Definition at line 139 of file custom_pipeline.cpp.

139  {
140  std::lock_guard<std::mutex> lock(tasksAccess);
141  const auto& pointer = std::find(tasks.cbegin(), tasks.cend(), target);
142  if (pointer == tasks.cend())
143  return false;
144  delete *pointer;
145  tasks.erase(pointer);
146  return true;
147  }

◆ measure()

void Beatmup::CustomPipeline::Impl::measure ( )
inline

Determining execution mode (GPU or CPU) and thread count for each task.

Definition at line 152 of file custom_pipeline.cpp.

152  {
153  std::lock_guard<std::mutex> lock(tasksAccess);
154  executionMode = TaskDeviceRequirement::CPU_ONLY;
155  maxThreadCount = 0;
156  for (auto& it : tasks) {
157  switch (it->executionMode = it->getTask().getUsedDevices()) {
158  case TaskDeviceRequirement::GPU_ONLY:
159  executionMode = TaskDeviceRequirement::GPU_ONLY;
160  break;
161  case TaskDeviceRequirement::GPU_OR_CPU:
162  if (executionMode == TaskDeviceRequirement::CPU_ONLY)
163  executionMode = TaskDeviceRequirement::GPU_OR_CPU;
164  break;
165  default:
166  break;
167  }
168  it->threadCount = it->getTask().getMaxThreads();
169  maxThreadCount = std::max(maxThreadCount, it->threadCount);
170  }
171  measured = true;
172  }
AbstractTask::TaskDeviceRequirement executionMode
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728

◆ getUsedDevices()

AbstractTask::TaskDeviceRequirement Beatmup::CustomPipeline::Impl::getUsedDevices ( ) const
inline

Definition at line 174 of file custom_pipeline.cpp.

174  {
175  if (!measured)
176  throw PipelineNotReady("Pipeline not measured; call measure() first.");
177  return executionMode;
178  }

◆ getMaxThreads()

ThreadIndex Beatmup::CustomPipeline::Impl::getMaxThreads ( ) const
inline

Definition at line 180 of file custom_pipeline.cpp.

180  {
181  if (!measured)
182  throw PipelineNotReady("Pipeline not measured; call measure() first.");
183  return maxThreadCount;
184  }

◆ beforeProcessing()

void Beatmup::CustomPipeline::Impl::beforeProcessing ( GraphicPipeline gpu)
inline

Definition at line 186 of file custom_pipeline.cpp.

186  {
187  this->gpu = gpu;
188  abort = false;
189  }

◆ process()

bool Beatmup::CustomPipeline::Impl::process ( GraphicPipeline gpu,
TaskThread thread,
CustomPipeline pipeline 
)
inline

Processing entry point.

Definition at line 194 of file custom_pipeline.cpp.

194  {
195  // managing worker thread
196  if (thread.isManaging()) {
197  this->thread = &thread;
198  std::lock_guard<std::mutex> lock(tasksAccess);
199  currentTask = tasks.begin();
200  pipeline.route(*this);
201  }
202 
203  // secondary worker thread
204  else {
205  do {
207  if (!allTasksDone() && !allTasksAborted() && thread.currentThread() < (*currentTask)->threadCount)
208  if (!(*currentTask)->getTask().process(thread))
209  abort = true;
211  } while (!allTasksDone() && !allTasksAborted());
212  }
213 
214  return !abort;
215  }
bool isManaging() const
Definition: parallelism.h:172
ThreadIndex currentThread() const
Definition: parallelism.h:165
bool allTasksAborted() const
Returns true if the current session is aborted.
bool allTasksDone() const
Returns true if all tasks are done.
return $pool getJavaReference & pipeline(index)

Member Data Documentation

◆ currentTask

std::vector<TaskHolder*>::iterator Beatmup::CustomPipeline::Impl::currentTask
private

Definition at line 28 of file custom_pipeline.cpp.

◆ tasks

std::vector<TaskHolder*> Beatmup::CustomPipeline::Impl::tasks
private

the list of tasks

Definition at line 29 of file custom_pipeline.cpp.

◆ tasksAccess

std::mutex Beatmup::CustomPipeline::Impl::tasksAccess
private

task list access control

Definition at line 30 of file custom_pipeline.cpp.

◆ gpu

GraphicPipeline* Beatmup::CustomPipeline::Impl::gpu
private

Definition at line 31 of file custom_pipeline.cpp.

◆ thread

TaskThread* Beatmup::CustomPipeline::Impl::thread
private

Definition at line 32 of file custom_pipeline.cpp.

◆ executionMode

AbstractTask::TaskDeviceRequirement Beatmup::CustomPipeline::Impl::executionMode
private

Definition at line 33 of file custom_pipeline.cpp.

◆ maxThreadCount

ThreadIndex Beatmup::CustomPipeline::Impl::maxThreadCount
private

Definition at line 34 of file custom_pipeline.cpp.

◆ measured

bool Beatmup::CustomPipeline::Impl::measured
private

if true, the execution mode and the thread count are determined

Definition at line 35 of file custom_pipeline.cpp.

◆ abort

bool Beatmup::CustomPipeline::Impl::abort
private

if true, one of threads executing the current task caused its aborting

Definition at line 36 of file custom_pipeline.cpp.


The documentation for this class was generated from the following file: