Beatmup
multitask.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 "custom_pipeline.h"
21 
22 namespace Beatmup {
23  /** \page ProgrammingModel
24  \section secMultistage Multi-stage processing
25  The thread pools make easy running several tasks one after the other. However, when the same pattern of tasks is needed to be run repeatedly,
26  %Beatmup offers a technique to put multiple tasks together into a single compound task, Beatmup::Multitask. This enables designing complex
27  application-specific processing pipelines.
28 
29  A multitask is a pipeline of tasks processing some data in a multi-stage fashion. It can simply host multiple tasks and run them in order,
30  without explicitly submitting them into a thread pool. It also implements a set of repetition policies allowing to skip some stages at the
31  beginning of the pipeline, if no changes is made to the input data and parameters with respect to the previous run, for example.
32  */
33 
34  /**
35  Conditional multiple tasks execution.
36 
37  Multitask is a technique to build complex multi-stage image processing pipelines. It allows to concatenate different tasks into a linear
38  conveyor and run them all or selectively. To handle this selection, each task is associated with a repetition policy specifying the
39  conditions whether this given task is executed or ignored when the pipeline is running.
40 
41  Specifically, it implements two extreme modes that force the task execution every time (Multitask::RepetitionPolicy::REPEAT_ALWAYS) or its
42  unconditional skipping (Multitask::RepetitionPolicy::IGNORE_ALWAYS) and two more sophisticated modes:
43  - Multitask::RepetitionPolicy::IGNORE_IF_UPTODATE skips the task if no tasks were executed among the ones coming before the current task
44  in the pipeline;
45  - Multitask::RepetitionPolicy::REPEAT_UPDATE forces task repetition one time on next run and just after switches the repetition policy to
46  IGNORE_IF_UPTODATE.
47  */
48  class Multitask : public CustomPipeline {
49  private:
50  std::mutex policyAccess; //!< access control to modify repetition policies
51 
52  protected:
54  bool route(TaskRouter & router);
55 
56  public:
57  /**
58  * Determines when a specific task in the sequence is run when the whole sequence is invoked
59  */
60  enum class RepetitionPolicy {
61  REPEAT_ALWAYS, //!< execute the task unconditionally on each run
62  REPEAT_UPDATE, //!< execute the task one time then switch to IGNORE_IF_UPTODATE
63  IGNORE_IF_UPTODATE, //!< do not execute the task if no preceding tasks are run
64  IGNORE_ALWAYS //!< do not execute the task
65  };
66 
67  Multitask();
68 
69  /**
70  \return repetition policy of a specific task in the pipeline.
71  */
72  RepetitionPolicy getRepetitionPolicy(const TaskHolder&);
73 
74  /**
75  Sets repetition policy of a task. If the pipeline is processing at the moment of the call, it is the application responsibility to abort
76  and restart it, if the policy change needs to be applied immediately.
77  \param taskHolder TaskHolder of a task to apply the policy to
78  \param policy The new policy
79  */
81  };
82 }
Task: an operation that can be executed by multiple threads in parallel.
Definition: parallelism.h:90
A task within a pipeline.
Custom pipeline: a sequence of tasks to be executed as a whole.
Conditional multiple tasks execution.
Definition: multitask.h:48
bool route(TaskRouter &router)
Definition: multitask.cpp:46
std::mutex policyAccess
access control to modify repetition policies
Definition: multitask.h:50
TaskHolder * createTaskHolder(AbstractTask &task)
Definition: multitask.cpp:42
RepetitionPolicy getRepetitionPolicy(const TaskHolder &)
Definition: multitask.cpp:78
void setRepetitionPolicy(TaskHolder &taskHolder, RepetitionPolicy policy)
Sets repetition policy of a task.
Definition: multitask.cpp:85
RepetitionPolicy
Determines when a specific task in the sequence is run when the whole sequence is invoked.
Definition: multitask.h:60
@ IGNORE_IF_UPTODATE
do not execute the task if no preceding tasks are run
@ REPEAT_ALWAYS
execute the task unconditionally on each run
@ IGNORE_ALWAYS
do not execute the task
@ REPEAT_UPDATE
execute the task one time then switch to IGNORE_IF_UPTODATE
Interface managing the execution of a sequence of tasks.
return pipeline getTaskIndex * taskHolder
jlong jlong jint policy
Beatmup::NNets::InferenceTask * task