mxnet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
engine.h
Go to the documentation of this file.
1 
6 #ifndef MXNET_ENGINE_H_
7 #define MXNET_ENGINE_H_
8 
9 #include <dmlc/base.h>
10 #if DMLC_USE_CXX11
11 #include <memory>
12 #include <functional>
13 #endif
14 #include <vector>
15 #include "./base.h"
16 
17 namespace mxnet {
18 
19 // forward declare engine
20 class Engine;
21 
23 namespace engine {
25 struct Var;
27 struct Opr;
29 typedef Var* VarHandle;
31 typedef Opr* OprHandle;
37  public:
38  // use implicit copy and assign
40  inline void operator()() const {
41  (*callback_)(engine_, param_);
42  }
43 
44  private:
46  friend class ::mxnet::Engine;
48  void (*callback_)(Engine *, void *);
50  Engine* engine_;
52  void* param_;
53 };
54 } // namespace engine
55 
56 #if DMLC_USE_CXX11
57 
58 enum class FnProperty {
60  kNormal,
64  kCopyToGPU,
68  kAsync
69 }; // enum class FnProperty
70 
75  public:
79  typedef std::function<void(RunContext)> SyncFn;
81  typedef std::function<void(RunContext, CallbackOnComplete)> AsyncFn;
93  virtual void NotifyShutdown() = 0;
100  virtual VarHandle NewVariable() = 0;
111  virtual OprHandle NewOperator(AsyncFn fn,
112  std::vector<VarHandle> const& const_vars,
113  std::vector<VarHandle> const& mutable_vars,
114  FnProperty prop = FnProperty::kNormal) = 0;
122  virtual void DeleteOperator(OprHandle op) = 0;
129  virtual void Push(OprHandle op, Context exec_ctx, int priority = 0) = 0;
142  virtual void PushAsync(AsyncFn exec_fun, Context exec_ctx,
143  std::vector<VarHandle> const& const_vars,
144  std::vector<VarHandle> const& mutable_vars,
146  int priority = 0) = 0;
158  virtual void DeleteVariable(SyncFn delete_fn,
159  Context exec_ctx,
160  VarHandle var) = 0;
166  virtual void WaitForVar(VarHandle var) = 0;
170  virtual void WaitForAll() = 0;
172  virtual ~Engine() noexcept(false) {}
176  static Engine* Get();
185  static std::shared_ptr<Engine> _GetSharedRef();
197  template<typename SyncFn>
198  inline void PushSync(SyncFn exec_fn, Context exec_ctx,
199  std::vector<VarHandle> const& const_vars,
200  std::vector<VarHandle> const& mutable_vars,
202  int priority = 0) {
203  this->PushAsync([exec_fn](RunContext ctx, CallbackOnComplete on_complete) {
204  exec_fn(ctx);
205  on_complete();
206  }, exec_ctx, const_vars, mutable_vars, prop, priority);
207  }
208 
209  protected:
216  void (*callback)(Engine *, void *), void *param) {
217  CallbackOnComplete ret;
218  ret.callback_ = callback;
219  ret.engine_ = this;
220  ret.param_ = param;
221  return ret;
222  }
223 }; // class Engine
224 #endif // DMLC_USE_CXX11
225 } // namespace mxnet
226 #endif // MXNET_ENGINE_H_
FnProperty
Function property, used to hint what action is pushed to engine.
Definition: engine.h:58
std::function< void(RunContext)> SyncFn
Synchronous operation to pass to engine.
Definition: engine.h:79
std::function< void(RunContext, CallbackOnComplete)> AsyncFn
Asynchronous operation to pass to engine.
Definition: engine.h:81
void PushSync(SyncFn exec_fn, Context exec_ctx, std::vector< VarHandle > const &const_vars, std::vector< VarHandle > const &mutable_vars, FnProperty prop=FnProperty::kNormal, int priority=0)
Push an synchronous operation to the engine.
Definition: engine.h:198
Asynchronous function call.
CallbackOnComplete CreateCallback(void(*callback)(Engine *, void *), void *param)
factory function to create OnComplete callback.
Definition: engine.h:215
void operator()() const
involve the callback
Definition: engine.h:40
execution time context. The information needed in runtime for actual execution.
Definition: base.h:181
Normal operation.
virtual ~Engine() noexcept(false)
virtual destructor
Definition: engine.h:172
#define MXNET_API
define compatible keywords in g++ Used to support g++-4.6 and g++4.7
Definition: base.h:62
Copy operation from GPU to other devices.
engine::OprHandle OprHandle
Operator pointer.
Definition: engine.h:85
engine::VarHandle VarHandle
Variable pointer.
Definition: engine.h:83
Var * VarHandle
Variable pointer type, usually hold by user used to specify dependencies.
Definition: engine.h:27
Prioritized sync operation on CPU.
engine::CallbackOnComplete CallbackOnComplete
callback on complete
Definition: engine.h:77
Dependency engine that schedules operations.
Definition: engine.h:74
OnComplete Callback to the engine, called by AsyncFn when action completes.
Definition: engine.h:36
configuation of mxnet as well as basic data structure.
Context information about the execution enviroment.
Definition: base.h:90
Copy operation from CPU to other devices.
Opr * OprHandle
Operator pointer type, usually hold by user.
Definition: engine.h:31