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/containers.h"
18#include "marl/event.h"
19
20#include "benchmark/benchmark.h"
21
22BENCHMARK_DEFINE_F(Schedule, Event)(benchmark::State& state) {
23 run(state, [&](int numTasks) {
24 for (auto _ : state) {
25 marl::containers::vector<marl::Event, 1> events;
26 events.resize(numTasks + 1);
27 for (auto i = 0; i < numTasks; i++) {
28 marl::Event prev = events[i];
29 marl::Event next = events[i + 1];
30 marl::schedule([=] {
31 prev.wait();
32 next.signal();
33 });
34 }
35 events.front().signal();
36 events.back().wait();
37 }
38 });
39}
40BENCHMARK_REGISTER_F(Schedule, Event)->Apply(Schedule::args<512>);
41
42// EventBaton benchmarks alternating execution of two tasks.
43BENCHMARK_DEFINE_F(Schedule, EventBaton)(benchmark::State& state) {
44 run(state, [&](int numPasses) {
45 for (auto _ : state) {
46 marl::Event passToA(marl::Event::Mode::Auto);
47 marl::Event passToB(marl::Event::Mode::Auto);
48 marl::Event done(marl::Event::Mode::Auto);
49
50 marl::schedule(marl::Task(
51 [=] {
52 for (int i = 0; i < numPasses; i++) {
53 passToA.wait();
54 passToB.signal();
55 }
56 },
57 marl::Task::Flags::SameThread));
58
59 marl::schedule(marl::Task(
60 [=] {
61 for (int i = 0; i < numPasses; i++) {
62 passToB.wait();
63 passToA.signal();
64 }
65 done.signal();
66 },
67 marl::Task::Flags::SameThread));
68
69 passToA.signal();
70 done.wait();
71 }
72 });
73}
74BENCHMARK_REGISTER_F(Schedule, EventBaton)->Apply(Schedule::args<262144>);
75