summaryrefslogtreecommitdiff
path: root/runtime/onert/core/src/exec/ParallelExecutor.cc
blob: f7e5de67d829dcad863e55e42ba5f791fb5f3463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ParallelExecutor.h"

#include <cassert>

#include "util/logging.h"
#include "exec/IFunction.h"

namespace onert
{
namespace exec
{

class HookFunction : public IFunction
{
public:
  HookFunction(IFunction *fn, const std::function<void()> &setup,
               const std::function<void()> &teardown)
      : _fn{fn}, _setup{setup}, _teardown{teardown}
  {
  }

public:
  void run() override
  {
    _setup();
    _fn->run();
    _teardown();
  }
  void runSync() override { throw("runSync is needed just for profiling in Dataflow executor"); }

private:
  IFunction *_fn;
  std::function<void()> _setup;
  std::function<void()> _teardown;
};

void ParallelExecutor::notify(uint32_t finished_job_id)
{
  std::unique_lock<std::mutex> lock{_mu_jobs};

  DataflowExecutor::notify(finished_job_id);

  lock.unlock();
  _cv_jobs.notify_all();
}

ParallelExecutor::ParallelExecutor(std::unique_ptr<ir::LoweredGraph> lowered_graph,
                                   const backend::TensorBuilderSet &tensor_builders,
                                   compiler::CodeMap &&code_map)
    : DataflowExecutor{std::move(lowered_graph), tensor_builders, std::move(code_map)}
{
  VERBOSE(ParallelExecutor) << "Constructing Parallel Executor" << std::endl;
}

void ParallelExecutor::executeImpl()
{
  // Init scheduler
  // TODO Consider to have distinct backend set in LowerInfoMap
  ir::BackendSet backends;
  for (auto &itr : _lowered_graph->getLowerInfo()->operation)
  {
    backends.add(itr.second->backend());
  }
  _scheduler = std::make_unique<ParallelScheduler>(backends);

  assert(noWaitingJobs());

  // Execution setup
  _waiting_jobs.swap(_finished_jobs); // Move finished jobs to waiting jobs

  for (uint32_t i = 0; i < _waiting_jobs.size(); ++i)
  {
    VERBOSE(ParallelExecutor) << i << ": " << _input_info[i] << std::endl;
    if (_input_info[i] == 0)
    {
      emplaceToReadyJobs(i);
    }
  }
  assert(!_ready_jobs.empty()); // Cannot begin if there is no initial jobs

  VERBOSE(ParallelExecutor) << "INITIAL JOBS : " << _ready_jobs.size() << std::endl;

  _subject.notifyModelBegin(this);
  while (true)
  {
    std::unique_lock<std::mutex> lock{_mu_jobs};

    if (_ready_jobs.empty())
    {
      _cv_jobs.wait(lock, [this] { return !_ready_jobs.empty() || noWaitingJobs(); });
      // Check finish condition
      if (_ready_jobs.empty() && noWaitingJobs())
      {
        break;
      }
    }

    auto job = std::move(_ready_jobs.begin()->second);
    _ready_jobs.erase(_ready_jobs.begin());

    lock.unlock();

    VERBOSE(ParallelExecutor) << "Assigning fn #" << job->index() << std::endl;

    auto job_index = job->index();
    auto op_sequence_index = _job_to_op_seq[job_index];
    auto op_seq = &_lowered_graph->op_seqs().at(op_sequence_index);
    auto backend = _lowered_graph->getLowerInfo()->operation.at(op_sequence_index)->backend();
    auto setup = [&, op_seq, backend]() { _subject.notifyJobBegin(this, op_seq, backend); };
    auto teardown = [&, job_index, op_seq, backend]() {
      _subject.notifyJobEnd(this, op_seq, backend);
      notify(job_index);
    };

    _scheduler->assign(std::make_unique<HookFunction>(job->fn(), setup, teardown), backend);
    _finished_jobs[job_index] = std::move(job);
  }

  assert(noWaitingJobs());

  // Wait for all the jobs done
  _scheduler->finish();
  _subject.notifyModelEnd(this);

  // Reset input info for the next execution
  _input_info = _initial_input_info;
}

} // namespace exec
} // namespace onert