blob: 14acf04439cdc00dd4b6cdbd2545122a727a335a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas53d12272018-02-01 20:23:25 +00002 * Copyright (c) 2017-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_ISCHEDULER_H__
25#define __ARM_COMPUTE_ISCHEDULER_H__
26
Moritz Pflanzerc186b572017-09-07 09:48:04 +010027#include "arm_compute/core/CPP/CPPTypes.h"
28
Anthony Barbier52ecb062018-05-25 13:32:10 +010029#include <functional>
30
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031namespace arm_compute
32{
33class ICPPKernel;
34
35/** Scheduler interface to run kernels */
36class IScheduler
37{
38public:
Anthony Barbier376c85f2018-05-25 14:17:21 +010039 /** Strategies available to split a workload */
40 enum class StrategyHint
41 {
42 STATIC, /**< Split the workload evenly among the threads */
43 DYNAMIC, /**< Split the workload dynamically using a bucket system */
44 };
45 /** Scheduler hints
46 *
47 * Collection of preferences set by the function regarding how to split a given workload
48 */
49 class Hints
50 {
51 public:
52 /** Constructor
53 *
54 * @param[in] split_dimension Dimension along which to split the kernel's execution window.
55 * @param[in] strategy (Optional) Split strategy.
56 */
57 Hints(unsigned int split_dimension, StrategyHint strategy = StrategyHint::STATIC)
58 : _split_dimension(split_dimension), _strategy(strategy)
59 {
60 }
61 /** Set the split_dimension hint
62 *
63 * @param[in] split_dimension Dimension along which to split the kernel's execution window.
64 *
65 * @return the Hints object
66 */
67 Hints &set_split_dimension(unsigned int split_dimension)
68 {
69 _split_dimension = split_dimension;
70 return *this;
71 }
72 /** Return the prefered split dimension
73 *
74 * @return The split dimension
75 */
76 unsigned int split_dimension() const
77 {
78 return _split_dimension;
79 }
80
81 /** Set the strategy hint
82 *
83 * @param[in] strategy Prefered strategy to use to split the workload
84 *
85 * @return the Hints object
86 */
87 Hints &set_strategy(StrategyHint strategy)
88 {
89 _strategy = strategy;
90 return *this;
91 }
92 /** Return the prefered strategy to use to split workload.
93 *
94 * @return The strategy
95 */
96 StrategyHint strategy() const
97 {
98 return _strategy;
99 }
100
101 private:
102 unsigned int _split_dimension;
103 StrategyHint _strategy;
104 };
Anthony Barbier52ecb062018-05-25 13:32:10 +0100105 /** Signature for the workloads to execute */
106 using Workload = std::function<void(const ThreadInfo &)>;
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100107 /** Default constructor. */
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100108 IScheduler();
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100109
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 /** Destructor. */
111 virtual ~IScheduler() = default;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 /** Sets the number of threads the scheduler will use to run the kernels.
114 *
115 * @param[in] num_threads If set to 0, then one thread per CPU core available on the system will be used, otherwise the number of threads specified.
116 */
117 virtual void set_num_threads(unsigned int num_threads) = 0;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100118
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 /** Returns the number of threads that the SingleThreadScheduler has in his pool.
120 *
121 * @return Number of threads available in SingleThreadScheduler.
122 */
123 virtual unsigned int num_threads() const = 0;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 /** Runs the kernel in the same thread as the caller synchronously.
126 *
Anthony Barbier376c85f2018-05-25 14:17:21 +0100127 * @param[in] kernel Kernel to execute.
128 * @param[in] hints Hints for the scheduler.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 */
Anthony Barbier376c85f2018-05-25 14:17:21 +0100130 virtual void schedule(ICPPKernel *kernel, const Hints &hints) = 0;
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100131
Anthony Barbier52ecb062018-05-25 13:32:10 +0100132 /** Execute all the passed workloads
133 *
134 * @note there is no guarantee regarding the order in which the workloads will be executed or whether or not they will be executed in parallel.
135 *
136 * @param[in] workloads Array of workloads to run
Anthony Barbier148b0752018-09-11 14:19:39 +0100137 * @param[in] tag String that can be used by profiling tools to identify the workloads run by the scheduler (Can be null).
Anthony Barbier52ecb062018-05-25 13:32:10 +0100138 */
Anthony Barbier148b0752018-09-11 14:19:39 +0100139 virtual void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag);
Anthony Barbier52ecb062018-05-25 13:32:10 +0100140
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100141 /** Get CPU info.
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100142 *
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100143 * @return CPU info.
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100144 */
Pablo Tello7fad9b12018-03-14 17:55:27 +0000145 CPUInfo &cpu_info();
Georgios Pinitas53d12272018-02-01 20:23:25 +0000146 /** Get a hint for the best possible number of execution threads
147 *
148 * @warning In case we can't work out the best number of threads,
149 * std::thread::hardware_concurrency() is returned else 1 in case of bare metal builds
150 *
151 * @return Best possible number of execution threads to use
152 */
153 unsigned int num_threads_hint() const;
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100154
155protected:
Anthony Barbier148b0752018-09-11 14:19:39 +0100156 /** Execute all the passed workloads
157 *
158 * @note there is no guarantee regarding the order in which the workloads will be executed or whether or not they will be executed in parallel.
159 *
160 * @param[in] workloads Array of workloads to run
161 */
162 virtual void run_workloads(std::vector<Workload> &workloads) = 0;
Pablo Tello7fad9b12018-03-14 17:55:27 +0000163 CPUInfo _cpu_info;
Georgios Pinitas53d12272018-02-01 20:23:25 +0000164
165private:
166 unsigned int _num_threads_hint = {};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168}
169#endif /* __ARM_COMPUTE_ISCHEDULER_H__ */