reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-17 f7c4a3cfd07adede3308f8d9d3d7315427d90a7c
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
#pragma once
 
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
 
#include "caffe2/core/blob_stats.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/stats.h"
#include "caffe2/core/tensor.h"
#include "caffe2/core/workspace.h"
 
namespace caffe2 {
 
// A thread-safe, bounded, blocking queue.
// Modelled as a circular buffer.
 
// Containing blobs are owned by the workspace.
// On read, we swap out the underlying data for the blob passed in for blobs
 
class CAFFE2_API BlobsQueue : public std::enable_shared_from_this<BlobsQueue> {
 public:
  BlobsQueue(
      Workspace* ws,
      const std::string& queueName,
      size_t capacity,
      size_t numBlobs,
      bool enforceUniqueName,
      const std::vector<std::string>& fieldNames = {});
 
  ~BlobsQueue() {
    close();
  }
 
  bool blockingRead(
      const std::vector<Blob*>& inputs,
      float timeout_secs = 0.0f);
  bool tryWrite(const std::vector<Blob*>& inputs);
  bool blockingWrite(const std::vector<Blob*>& inputs);
  void close();
  size_t getNumBlobs() const {
    return numBlobs_;
  }
 
 private:
  bool canWrite();
  void doWrite(const std::vector<Blob*>& inputs);
 
  std::atomic<bool> closing_{false};
 
  size_t numBlobs_;
  std::mutex mutex_; // protects all variables in the class.
  std::condition_variable cv_;
  int64_t reader_{0};
  int64_t writer_{0};
  std::vector<std::vector<Blob*>> queue_;
  const std::string name_;
 
  struct QueueStats {
    CAFFE_STAT_CTOR(QueueStats);
    CAFFE_EXPORTED_STAT(queue_balance);
    CAFFE_EXPORTED_STAT(queue_dequeued_records);
    CAFFE_DETAILED_EXPORTED_STAT(queue_dequeued_bytes);
    CAFFE_AVG_EXPORTED_STAT(read_time_ns);
    CAFFE_AVG_EXPORTED_STAT(write_time_ns);
  } stats_;
};
} // namespace caffe2