Caffe2 - C++ API
A deep learning, cross platform ML framework
zmq_feeder.cc
1 
17 // This binary provides an easy way to open a zeromq server and feeds data to
18 // clients connect to it. It uses the Caffe2 db as the backend, thus allowing
19 // one to convert any db-compliant storage to a zeromq service.
20 
21 #include "caffe2/core/db.h"
22 #include "caffe2/core/init.h"
23 #include "caffe2/core/logging.h"
24 #include "caffe2/utils/zmq_helper.h"
25 
26 CAFFE2_DEFINE_string(server, "tcp://*:5555", "The server address.");
27 CAFFE2_DEFINE_string(input_db, "", "The input db.");
28 CAFFE2_DEFINE_string(input_db_type, "", "The input db type.");
29 
30 using caffe2::db::DB;
31 using caffe2::db::Cursor;
32 using caffe2::string;
33 
34 int main(int argc, char** argv) {
35  caffe2::GlobalInit(&argc, &argv);
36 
37  LOG(INFO) << "Opening DB...";
38  auto in_db = caffe2::db::CreateDB(
39  caffe2::FLAGS_input_db_type, caffe2::FLAGS_input_db, caffe2::db::READ);
40  CAFFE_ENFORCE(
41  in_db,
42  "Cannot load input db " + caffe2::FLAGS_input_db + " of expected type " +
43  caffe2::FLAGS_input_db_type);
44  auto cursor = in_db->NewCursor();
45  LOG(INFO) << "DB opened.";
46 
47  LOG(INFO) << "Starting ZeroMQ server...";
48 
49  // Socket to talk to clients
50  caffe2::ZmqSocket sender(ZMQ_PUSH);
51  sender.Bind(caffe2::FLAGS_server);
52  LOG(INFO) << "Server created at " << caffe2::FLAGS_server;
53 
54  while (1) {
55  VLOG(1) << "Sending " << cursor->key();
56  sender.SendTillSuccess(cursor->key(), ZMQ_SNDMORE);
57  sender.SendTillSuccess(cursor->value(), 0);
58  cursor->Next();
59  if (!cursor->Valid()) {
60  cursor->SeekToFirst();
61  }
62  }
63  // We do not do an elegant quit since this binary is going to be terminated by
64  // control+C.
65  return 0;
66 }
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.
Definition: init.cc:34
An abstract class for the cursor of the database while reading.
Definition: db.h:38
An abstract class for accessing a database of key-value pairs.
Definition: db.h:96