Caffe2 - C++ API
A deep learning, cross platform ML framework
store_handler.h
1 #pragma once
2 
3 #include <chrono>
4 #include <cstdint>
5 #include <stdexcept>
6 #include <string>
7 #include <vector>
8 
9 #include "caffe2/core/common.h"
10 
11 namespace caffe2 {
12 
13 class CAFFE2_API StoreHandler {
14  public:
15  static constexpr std::chrono::milliseconds kDefaultTimeout =
16  std::chrono::seconds(30);
17  static constexpr std::chrono::milliseconds kNoTimeout =
18  std::chrono::milliseconds::zero();
19 
20  virtual ~StoreHandler();
21 
22  /*
23  * Set data for the key if it doesn't exist.
24  * If the key exists the data should be the same as the existing key.
25  */
26  virtual void set(const std::string& name, const std::string& data) = 0;
27 
28  /*
29  * Get the data for the key.
30  * The call should wait until the key is stored with specified timeout
31  * and return data if set else fail.
32  */
33  virtual std::string get(
34  const std::string& name,
35  const std::chrono::milliseconds& timeout = kDefaultTimeout) = 0;
36 
37  /*
38  * Does an atomic add operation on the key and returns the latest updated
39  * value.
40  * Note: To access the current value for this counter call with value = 0
41  */
42  virtual int64_t add(const std::string& name, int64_t value) = 0;
43 
44  /*
45  * Check if a keys exist in the store.
46  */
47  virtual bool check(const std::vector<std::string>& names) = 0;
48 
49  /*
50  * Wait for Keys to be stored.
51  */
52  virtual void wait(
53  const std::vector<std::string>& names,
54  const std::chrono::milliseconds& timeout = kDefaultTimeout) = 0;
55 };
56 
57 /*
58  * The backing store is no longer available. It may have been deleted.
59  */
61  : public std::runtime_error {
62  explicit StoreHandlerNotAvailableException(const std::string& msg)
63  : std::runtime_error(msg) {}
64 };
65 
66 #define STORE_HANDLER_NOT_AVAILABLE(...) \
67  throw ::caffe2::StoreHandlerNotAvailableException( \
68  ::c10::str("[", __FILE__, ":", __LINE__, "] ", __VA_ARGS__));
69 
70 /*
71  * Timeout accessing the store.
72  */
73 struct CAFFE2_API StoreHandlerTimeoutException : public std::runtime_error {
74  explicit StoreHandlerTimeoutException(const std::string& msg)
75  : std::runtime_error(msg) {}
76 };
77 
78 #define STORE_HANDLER_TIMEOUT(...) \
79  throw ::caffe2::StoreHandlerTimeoutException( \
80  ::c10::str("[", __FILE__, ":", __LINE__, "] ", __VA_ARGS__));
81 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13