xref: /aosp_15_r20/external/swiftshader/third_party/marl/src/scheduler_bench.cpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2020 The Marl Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "marl_bench.h"
16 
17 #include "marl/waitgroup.h"
18 
19 #include "benchmark/benchmark.h"
20 
BENCHMARK_DEFINE_F(Schedule,Empty)21 BENCHMARK_DEFINE_F(Schedule, Empty)(benchmark::State& state) {
22   run(state, [&](int numTasks) {
23     for (auto _ : state) {
24       for (auto i = 0; i < numTasks; i++) {
25         marl::schedule([] {});
26       }
27     }
28   });
29 }
30 BENCHMARK_REGISTER_F(Schedule, Empty)->Apply(Schedule::args);
31 
BENCHMARK_DEFINE_F(Schedule,SomeWork)32 BENCHMARK_DEFINE_F(Schedule, SomeWork)
33 (benchmark::State& state) {
34   run(state, [&](int numTasks) {
35     for (auto _ : state) {
36       marl::WaitGroup wg;
37       wg.add(numTasks);
38       for (auto i = 0; i < numTasks; i++) {
39         marl::schedule([=] {
40           uint32_t value = doSomeWork(i);
41           benchmark::DoNotOptimize(value);
42           wg.done();
43         });
44       }
45       wg.wait();
46     }
47   });
48 }
49 BENCHMARK_REGISTER_F(Schedule, SomeWork)->Apply(Schedule::args);
50 
BENCHMARK_DEFINE_F(Schedule,MultipleForkAndJoin)51 BENCHMARK_DEFINE_F(Schedule, MultipleForkAndJoin)(benchmark::State& state) {
52   run(state, [&](int numTasks) {
53     const int batchSize = std::max(1, Schedule::numThreads(state));
54     for (auto _ : state) {
55       marl::WaitGroup wg;
56       for (int i = 0; i < numTasks; i++) {
57         wg.add(1);
58         marl::schedule([=] {
59           // Give each task a significant amount of work so that concurrency matters.
60           // If any worker performs more than one task, it will affect the results.
61           int value = i;
62           for (int j = 0; j < 256; ++j) {
63             value = doSomeWork(value);
64           }
65           benchmark::DoNotOptimize(value);
66           wg.done();
67         });
68         // Wait for completion after every batch. This simulates the fork-and-join pattern.
69         if ((i + 1) % batchSize == 0) {
70           wg.wait();
71         }
72       }
73       wg.wait();
74     }
75   });
76 }
77 
78 BENCHMARK_REGISTER_F(Schedule, MultipleForkAndJoin)->Apply(Schedule::args<512>);
79 
BENCHMARK_DEFINE_F(Schedule,SomeWorkWorkerAffinityOneOf)80 BENCHMARK_DEFINE_F(Schedule, SomeWorkWorkerAffinityOneOf)
81 (benchmark::State& state) {
82   marl::Scheduler::Config cfg;
83   cfg.setWorkerThreadAffinityPolicy(
84       marl::Thread::Affinity::Policy::oneOf(marl::Thread::Affinity::all()));
85   run(state, cfg, [&](int numTasks) {
86     for (auto _ : state) {
87       marl::WaitGroup wg;
88       wg.add(numTasks);
89       for (auto i = 0; i < numTasks; i++) {
90         marl::schedule([=] {
91           uint32_t value = doSomeWork(i);
92           benchmark::DoNotOptimize(value);
93           wg.done();
94         });
95       }
96       wg.wait();
97     }
98   });
99 }
100 BENCHMARK_REGISTER_F(Schedule, SomeWorkWorkerAffinityOneOf)
101     ->Apply(Schedule::args);
102