Caffe2 - C++ API
A deep learning, cross platform ML framework
workspace.h
1 #ifndef CAFFE2_CORE_WORKSPACE_H_
2 #define CAFFE2_CORE_WORKSPACE_H_
3 
4 #include "caffe2/core/common.h"
5 
6 #ifndef CAFFE2_MOBILE
7 #error "mobile build state not defined"
8 #endif
9 
10 #include <climits>
11 #include <cstddef>
12 #include <mutex>
13 #include <typeinfo>
14 #include <vector>
15 
16 #include "caffe2/core/blob.h"
17 #include "caffe2/core/registry.h"
18 #include "caffe2/core/net.h"
19 #include "caffe2/proto/caffe2.pb.h"
20 #include "caffe2/utils/signal_handler.h"
21 #if CAFFE2_MOBILE
22 #include "caffe2/utils/threadpool/ThreadPool.h"
23 #endif // CAFFE2_MOBILE
24 
25 CAFFE2_DECLARE_bool(caffe2_print_blob_sizes_at_exit);
26 
27 namespace caffe2 {
28 
29 class NetBase;
30 struct CompiledExecutionStep;
31 
32 struct StopOnSignal {
33  StopOnSignal()
34  : handler_(std::make_shared<SignalHandler>(
35  SignalHandler::Action::STOP,
36  SignalHandler::Action::STOP)) {}
37 
38  StopOnSignal(const StopOnSignal& other) : handler_(other.handler_) {}
39 
40  bool operator()(int iter) {
41  return handler_->CheckForSignals() != SignalHandler::Action::STOP;
42  }
43 
44  std::shared_ptr<SignalHandler> handler_;
45 };
46 
47 
53 class Workspace {
54  public:
55  typedef std::function<bool(int)> ShouldContinue;
56  typedef CaffeMap<string, unique_ptr<Blob> > BlobMap;
57  typedef CaffeMap<string, unique_ptr<NetBase> > NetMap;
62  }
70  explicit Workspace(const string& root_folder)
71  : root_folder_(root_folder) {}
81  explicit Workspace(Workspace* const shared)
82  : shared_(shared) {}
86  Workspace(const string& root_folder, Workspace* shared)
87  : root_folder_(root_folder), shared_(shared) {}
88  ~Workspace() {
89  if (FLAGS_caffe2_print_blob_sizes_at_exit) {
90  PrintBlobSizes();
91  }
92  }
93 
98  void SetParentWorkspace(Workspace* shared) {
99  shared_ = shared;
100  }
101 
106  vector<string> LocalBlobs() const;
107 
113  vector<string> Blobs() const;
114 
118  const string& RootFolder() { return root_folder_; }
122  inline bool HasBlob(const string& name) const {
123  return (blob_map_.count(name) || (shared_ && shared_->HasBlob(name)));
124  }
125 
126  void PrintBlobSizes();
127 
133  Blob* CreateBlob(const string& name);
139  bool RemoveBlob(const string& name);
144  const Blob* GetBlob(const string& name) const;
149  Blob* GetBlob(const string& name);
150 
160  NetBase* CreateNet(const NetDef& net_def, bool overwrite = false);
161 
166  NetBase* GetNet(const string& net_name);
170  void DeleteNet(const string& net_name);
176  bool RunNet(const string& net_name);
177 
181  vector<string> Nets() const {
182  vector<string> names;
183  for (auto& entry : net_map_) {
184  names.push_back(entry.first);
185  }
186  return names;
187  }
188 
192  bool RunPlan(const PlanDef& plan_def,
193  ShouldContinue should_continue = StopOnSignal{});
194 
195 #if CAFFE2_MOBILE
196  /*
197  * Returns a CPU threadpool instace for parallel execution of
198  * work. The threadpool is created lazily; if no operators use it,
199  * then no threadpool will be created.
200  */
201  ThreadPool* GetThreadPool();
202 #endif
203 
204  // RunOperatorOnce and RunNetOnce runs an operator or net once. The difference
205  // between RunNet and RunNetOnce lies in the fact that RunNet allows you to
206  // have a persistent net object, while RunNetOnce creates a net and discards
207  // it on the fly - this may make things like database read and random number
208  // generators repeat the same thing over multiple calls.
209  bool RunOperatorOnce(const OperatorDef& op_def);
210  bool RunNetOnce(const NetDef& net_def);
211 
212  protected:
213  bool ExecuteStepRecursive(CompiledExecutionStep& execution);
214 
215  private:
216  BlobMap blob_map_;
217  NetMap net_map_;
218  string root_folder_ = ".";
219  Workspace* shared_ = nullptr;
220 #if CAFFE2_MOBILE
221  std::unique_ptr<ThreadPool> thread_pool_;
222  std::mutex thread_pool_creation_mutex_;
223 #endif // CAFFE2_MOBILE
224 
225  DISABLE_COPY_AND_ASSIGN(Workspace);
226 };
227 
228 } // namespace caffe2
229 
230 #endif // CAFFE2_CORE_WORKSPACE_H_
Workspace(Workspace *const shared)
Initializes a workspace with a shared workspace.
Definition: workspace.h:81
const string & RootFolder()
Return the root folder of the workspace.
Definition: workspace.h:118
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:53
Workspace(const string &root_folder)
Initializes an empty workspace with the given root folder.
Definition: workspace.h:70
Workspace(const string &root_folder, Workspace *shared)
Initializes a workspace with a root folder and a shared workspace.
Definition: workspace.h:86
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
Blob is a general container that hosts a typed pointer.
Definition: blob.h:25
vector< string > Nets() const
Returns a list of names of the currently instantiated networks.
Definition: workspace.h:181
Workspace()
Initializes an empty workspace.
Definition: workspace.h:61
unique_ptr< NetBase > CreateNet(const NetDef &net_def, Workspace *ws)
Creates a network, accessing / creating blobs in the given workspace.
Definition: net.cc:66
void SetParentWorkspace(Workspace *shared)
Allows to add a parent workspace post factum after the object was already constructed.
Definition: workspace.h:98
bool HasBlob(const string &name) const
Checks if a blob with the given name is present in the current workspace.
Definition: workspace.h:122