|
| 1 | +#include <boost/thread.hpp> |
| 2 | +#include <map> |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "caffe/common.hpp" |
| 7 | +#include "caffe/data_layers.hpp" |
| 8 | +#include "caffe/data_reader.hpp" |
| 9 | +#include "caffe/proto/caffe.pb.h" |
| 10 | + |
| 11 | +namespace caffe { |
| 12 | + |
| 13 | +using boost::weak_ptr; |
| 14 | + |
| 15 | +map<const string, weak_ptr<DataReader::Body> > DataReader::bodies_; |
| 16 | +static boost::mutex bodies_mutex_; |
| 17 | + |
| 18 | +DataReader::DataReader(const LayerParameter& param) |
| 19 | + : queue_pair_(new QueuePair( // |
| 20 | + param.data_param().prefetch() * param.data_param().batch_size())) { |
| 21 | + // Get or create a body |
| 22 | + boost::mutex::scoped_lock lock(bodies_mutex_); |
| 23 | + string key = source_key(param); |
| 24 | + weak_ptr<Body>& weak = bodies_[key]; |
| 25 | + body_ = weak.lock(); |
| 26 | + if (!body_) { |
| 27 | + body_.reset(new Body(param)); |
| 28 | + bodies_[key] = weak_ptr<Body>(body_); |
| 29 | + } |
| 30 | + body_->new_queue_pairs_.push(queue_pair_); |
| 31 | +} |
| 32 | + |
| 33 | +DataReader::~DataReader() { |
| 34 | + string key = source_key(body_->param_); |
| 35 | + body_.reset(); |
| 36 | + boost::mutex::scoped_lock lock(bodies_mutex_); |
| 37 | + if (bodies_[key].expired()) { |
| 38 | + bodies_.erase(key); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// |
| 43 | + |
| 44 | +DataReader::QueuePair::QueuePair(int size) { |
| 45 | + // Initialize the free queue with requested number of datums |
| 46 | + for (int i = 0; i < size; ++i) { |
| 47 | + free_.push(new Datum()); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +DataReader::QueuePair::~QueuePair() { |
| 52 | + Datum* datum; |
| 53 | + while (free_.try_pop(&datum)) { |
| 54 | + delete datum; |
| 55 | + } |
| 56 | + while (full_.try_pop(&datum)) { |
| 57 | + delete datum; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// |
| 62 | + |
| 63 | +DataReader::Body::Body(const LayerParameter& param) |
| 64 | + : param_(param), |
| 65 | + new_queue_pairs_() { |
| 66 | + StartInternalThread(); |
| 67 | +} |
| 68 | + |
| 69 | +DataReader::Body::~Body() { |
| 70 | + StopInternalThread(); |
| 71 | +} |
| 72 | + |
| 73 | +void DataReader::Body::InternalThreadEntry() { |
| 74 | + shared_ptr<db::DB> db(db::GetDB(param_.data_param().backend())); |
| 75 | + db->Open(param_.data_param().source(), db::READ); |
| 76 | + shared_ptr<db::Cursor> cursor(db->NewCursor()); |
| 77 | + vector<shared_ptr<QueuePair> > qps; |
| 78 | + try { |
| 79 | + // int solver_count = param_.phase() == TRAIN ? Caffe::solver_count() : 1; |
| 80 | + // TODO single solver until multi-gpu merge |
| 81 | + int solver_count = 1; |
| 82 | + |
| 83 | + // To ensure deterministic runs, only start running once all solvers |
| 84 | + // are ready. But solvers need to peek on one item during initialization, |
| 85 | + // so read one item, then wait for the next solver. |
| 86 | + for (int i = 0; i < solver_count; ++i) { |
| 87 | + shared_ptr<QueuePair> qp(new_queue_pairs_.pop()); |
| 88 | + read_one(cursor.get(), qp.get()); |
| 89 | + qps.push_back(qp); |
| 90 | + } |
| 91 | + // Main loop |
| 92 | + while (!must_stop()) { |
| 93 | + for (int i = 0; i < solver_count; ++i) { |
| 94 | + read_one(cursor.get(), qps[i].get()); |
| 95 | + } |
| 96 | + // Check no additional readers have been created. This can happen if |
| 97 | + // more than one net is trained at a time per process, whether single |
| 98 | + // or multi solver. It might also happen if two data layers have same |
| 99 | + // name and same source. |
| 100 | + CHECK_EQ(new_queue_pairs_.size(), 0); |
| 101 | + } |
| 102 | + } catch (boost::thread_interrupted&) { |
| 103 | + // Interrupted exception is expected on shutdown |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void DataReader::Body::read_one(db::Cursor* cursor, QueuePair* qp) { |
| 108 | + Datum* datum = qp->free_.pop(); |
| 109 | + // TODO deserialize in-place instead of copy? |
| 110 | + datum->ParseFromString(cursor->value()); |
| 111 | + qp->full_.push(datum); |
| 112 | + |
| 113 | + // go to the next iter |
| 114 | + cursor->Next(); |
| 115 | + if (!cursor->valid()) { |
| 116 | + DLOG(INFO) << "Restarting data prefetching from start."; |
| 117 | + cursor->SeekToFirst(); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace caffe |
0 commit comments