workqueue.h 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2021 PaddlePaddle Authors. 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.

#pragma once

#include <functional>
#include <memory>
L
liutiexing 已提交
19
#include <vector>
20 21 22 23

namespace paddle {
namespace framework {

L
liutiexing 已提交
24 25 26
constexpr const char* kQueueEmptyEvent = "QueueEmpty";
class EventsWaiter;

L
liutiexing 已提交
27
struct WorkQueueOptions {
28 29 30 31 32
  WorkQueueOptions(size_t num_threads, bool allow_spinning, bool track_task)
      : num_threads(num_threads),
        allow_spinning(allow_spinning),
        track_task(track_task) {}

L
liutiexing 已提交
33 34 35 36 37 38 39
  WorkQueueOptions(size_t num_threads, bool allow_spinning, bool track_task,
                   EventsWaiter* waiter)
      : num_threads(num_threads),
        allow_spinning(allow_spinning),
        track_task(track_task),
        queue_empty_waiter(waiter) {}

40 41
  size_t num_threads;
  bool allow_spinning;
L
liutiexing 已提交
42 43 44 45
  // If you need to blocking the calling  thread to wait "queue empty", set
  // track_task = true and set queue_empty_waiter. EventsWaiter::WaitEvent will
  // block the calling thread until any of events (including "queue empty")
  // occured.
46
  bool track_task;
L
liutiexing 已提交
47
  EventsWaiter* queue_empty_waiter{nullptr};  // not owned
L
liutiexing 已提交
48 49
};

50 51
class WorkQueue {
 public:
L
liutiexing 已提交
52
  explicit WorkQueue(const WorkQueueOptions& options) : options_(options) {}
53 54 55 56 57 58 59 60 61

  WorkQueue(const WorkQueue&) = delete;

  WorkQueue& operator=(const WorkQueue&) = delete;

  virtual ~WorkQueue() = default;

  virtual void AddTask(std::function<void()> fn) = 0;

L
liutiexing 已提交
62 63
  // See WorkQueueOptions.track_task for details
  // virtual void WaitQueueEmpty() = 0;
64

L
liutiexing 已提交
65 66 67 68
  virtual size_t NumThreads() const = 0;

 protected:
  WorkQueueOptions options_;
69 70
};

L
liutiexing 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83
class WorkQueueGroup {
 public:
  explicit WorkQueueGroup(const std::vector<WorkQueueOptions>& queues_options)
      : queues_options_(queues_options) {}

  WorkQueueGroup(const WorkQueueGroup&) = delete;

  WorkQueueGroup& operator=(const WorkQueueGroup&) = delete;

  virtual ~WorkQueueGroup() = default;

  virtual void AddTask(size_t queue_idx, std::function<void()> fn) = 0;

L
liutiexing 已提交
84 85
  // See WorkQueueOptions.track_task for details
  // virtual void WaitQueueGroupEmpty() = 0;
L
liutiexing 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99

  virtual size_t QueueNumThreads(size_t queue_idx) const = 0;

  virtual size_t QueueGroupNumThreads() const = 0;

 protected:
  std::vector<WorkQueueOptions> queues_options_;
};

std::unique_ptr<WorkQueue> CreateSingleThreadedWorkQueue(
    const WorkQueueOptions& options);

std::unique_ptr<WorkQueue> CreateMultiThreadedWorkQueue(
    const WorkQueueOptions& options);
100

L
liutiexing 已提交
101 102
std::unique_ptr<WorkQueueGroup> CreateWorkQueueGroup(
    const std::vector<WorkQueueOptions>& queues_options);
103 104 105

}  // namespace framework
}  // namespace paddle