Caffe2 - C++ API
A deep learning, cross platform ML framework
rebatching_queue.h
1 #pragma once
2 
3 #include <atomic>
4 #include <condition_variable>
5 #include <memory>
6 #include <mutex>
7 #include <queue>
8 
9 #include "caffe2/core/logging.h"
10 #include "caffe2/core/operator.h"
11 #include "caffe2/core/stats.h"
12 #include "caffe2/core/tensor.h"
13 
14 namespace caffe2 {
15 
16 // TODO: This is a very naive implementation with a single mutex. We can do the
17 // atomic index + circular queue optimizations or pull something more
18 // heavy-weight later
19 
21  public:
22  RebatchingQueue(size_t capacity, size_t numBlobs);
23 
24  ~RebatchingQueue();
25 
26  bool enqueueOne(
27  CPUContext& context,
28  const std::vector<const TensorCPU*>& inputs);
29 
30  bool enqueueMany(
31  CPUContext& context,
32  const std::vector<const TensorCPU*>& inputs);
33 
34  bool dequeue(
35  CPUContext& context,
36  size_t numElements,
37  const std::vector<TensorCPU*>& outputs);
38 
39  size_t capacity() const;
40 
41  size_t numBlobs() const;
42 
43  bool isClosed() const;
44 
45  void close();
46 
47  private:
48  bool enqueue(std::vector<std::vector<TensorCPU>> splittedInputs);
49 
50  bool canWrite() const;
51  bool canRead() const;
52 
53  const size_t capacity_;
54  const size_t numBlobs_;
55 
56  mutable std::mutex mutex_;
57 
58  bool isClosed_{false};
59 
60  uint64_t head_{0};
61  uint64_t tail_{0};
62 
63  std::condition_variable cvEmpty_;
64  std::condition_variable cvOverflow_;
65 
66  std::vector<std::vector<TensorCPU>> queue_;
67 };
68 } // caffe2
The CPU Context, representing the bare minimum of what a Context class in Caffe2 should implement...
Definition: context.h:40
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13