Beatmup
custom_pipeline.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "../parallelism.h"
21 #include "../exception.h"
22 #include <vector>
23 #include <mutex>
24 
25 namespace Beatmup {
26  class TaskRouter;
27 
28  /**
29  Custom pipeline: a sequence of tasks to be executed as a whole.
30  Acts as an AbstractTask. Built by adding tasks one by one and calling measure() at the end.
31  */
32  class CustomPipeline : public AbstractTask {
33  public:
34  class TaskHolder;
35  private:
36  class Impl;
38 
39  protected:
41  ThreadIndex getMaxThreads() const;
42  void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline* gpu);
43  void afterProcessing(ThreadIndex threadCount, GraphicPipeline* gpu, bool aborted);
44  bool process(TaskThread& thread);
45  bool processOnGPU(GraphicPipeline& gpu, TaskThread& thread);
46 
48  virtual bool route(TaskRouter& router) = 0;
49 
51 
52  public:
54 
55  /**
56  * \return number of tasks in the pipeline
57  */
58  int getTaskCount() const;
59 
60  /**
61  * Retrieves a task by its index
62  */
63  TaskHolder& getTask(int) const;
64 
65  /**
66  * Retrieves task index if it is in the pipeline; returns -1 otherwise
67  */
68  int getTaskIndex(const TaskHolder&);
69 
70  /**
71  * Adds a new task to the end of the pipeline
72  */
74 
75  /**
76  * Inserts a task in a specified position of the pipeline before another task.
77  * \param task The task to insert
78  * \param before TaskHolder specifying position of the task that will follow the newly inserted task
79  * \return TaskHolder with the newly inserted task.
80  */
82 
83  /**
84  * Removes a task from the pipeline.
85  * \param task The task to remove
86  * \return `true` on success, `false` if this TaskHolder is not in the pipeline.
87  */
88  bool removeTask(const TaskHolder& task);
89 
90  /**
91  * Determines pipeline execution mode and required thread count
92  */
93  void measure();
94 
95  /**
96  A task within a pipeline
97  */
98  class TaskHolder : public Object {
99  friend class CustomPipeline;
100  friend class CustomPipeline::Impl;
101  private:
102  void operator = (const TaskHolder&) = delete;
103  protected:
107  float time;
109  public:
112 
113  /**
114  \return the task in the current holder.
115  */
116  AbstractTask& getTask() const { return task; }
117 
118  /**
119  \return last execution time in milliseconds.
120  */
121  float getRunTime() const { return time; }
122  };
123 
124  class PipelineNotReady : public Exception {
125  public:
127  };
128  };
129 
130 
131  /**
132  Interface managing the execution of a sequence of tasks
133  */
134  class TaskRouter {
135  public:
136  /**
137  Returns currently pointed task
138  */
140  virtual const CustomPipeline::TaskHolder& getCurrentTask() const = 0;
141 
142  /**
143  Executes the pointed task
144  */
145  virtual void runTask() = 0;
146 
147  /**
148  Goes to the next task in the list
149  */
150  virtual void goToNextTask() = 0;
151 
152  /**
153  Returns `true` if all tasks are done
154  */
155  virtual bool allTasksDone() const = 0;
156 
157  /**
158  Returns `true` if the current session is aborted
159  */
160  virtual bool allTasksAborted() const = 0;
161  };
162 
164 }
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
TaskDeviceRequirement
Specifies which device (CPU and/or GPU) is used to run the task.
Definition: parallelism.h:95
A task within a pipeline.
void operator=(const TaskHolder &)=delete
AbstractTask::TaskDeviceRequirement executionMode
Custom pipeline: a sequence of tasks to be executed as a whole.
bool removeTask(const TaskHolder &task)
Removes a task from the pipeline.
TaskDeviceRequirement getUsedDevices() const
Communicates devices (CPU and/or GPU) the task is run on.
void afterProcessing(ThreadIndex threadCount, GraphicPipeline *gpu, bool aborted)
Instruction called after the task is executed.
TaskHolder & getTask(int) const
Retrieves a task by its index.
int getTaskIndex(const TaskHolder &)
Retrieves task index if it is in the pipeline; returns -1 otherwise.
TaskHolder & insertTask(AbstractTask &task, const TaskHolder &before)
Inserts a task in a specified position of the pipeline before another task.
TaskHolder & addTask(AbstractTask &)
Adds a new task to the end of the pipeline.
bool processOnGPU(GraphicPipeline &gpu, TaskThread &thread)
Executes the task on GPU.
virtual bool route(TaskRouter &router)=0
bool process(TaskThread &thread)
Executes the task on CPU within a given thread.
ThreadIndex getMaxThreads() const
Gives the upper limint on the number of threads the task may be performed by.
void measure()
Determines pipeline execution mode and required thread count.
virtual TaskHolder * createTaskHolder(AbstractTask &task)=0
void beforeProcessing(ThreadIndex threadCount, ProcessingTarget target, GraphicPipeline *gpu)
Instruction called before the task is executed.
Base class for all exceptions.
Definition: exception.h:37
std::string message
Definition: exception.h:39
Internal low-level GPU control API.
Definition: pipeline.h:33
Beatmup object base class
Definition: basic_types.h:67
Interface managing the execution of a sequence of tasks.
virtual void goToNextTask()=0
Goes to the next task in the list.
virtual const CustomPipeline::TaskHolder & getCurrentTask() const =0
virtual void runTask()=0
Executes the pointed task.
virtual CustomPipeline::TaskHolder & getCurrentTask()=0
Returns currently pointed task.
virtual bool allTasksDone() const =0
Returns true if all tasks are done.
virtual bool allTasksAborted() const =0
Returns true if the current session is aborted.
Thread executing tasks.
Definition: parallelism.h:154
bool operator==(const CustomRectangle< numeric > &lhs, const CustomRectangle< numeric > &rhs)
Checks whether two rectangles are actually the same.
Definition: geometry.h:622
unsigned char ThreadIndex
number of threads / thread index
Definition: parallelism.h:68
ProcessingTarget
Definition: basic_types.h:55
Beatmup::NNets::InferenceTask * task