This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from dmlc/simple-engine
Simple engine
- Loading branch information
Showing
13 changed files
with
953 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,162 @@ | ||
/*! | ||
* Copyright (c) 2015 by Contributors | ||
* Copyright (c) 2015 by Contributors | ||
* \file dag_engine.h | ||
* \brief dynamic data-flow dag engine that schedules | ||
* operations in a concurrent way | ||
* \brief DAG engine that schedules data. | ||
*/ | ||
#ifndef MXNET_DAG_ENGINE_H_ | ||
#define MXNET_DAG_ENGINE_H_ | ||
#include <dmlc/base.h> | ||
// check c++11 | ||
|
||
#if DMLC_USE_CXX11 == 0 | ||
#error "cxx11 was required for narray module" | ||
#error "C++11 was required for DAG engine module." | ||
#endif | ||
|
||
#include <functional> | ||
#include <vector> | ||
#include "./base.h" | ||
#include "./context.h" | ||
#include "base.h" | ||
#include "context.h" | ||
|
||
namespace mxnet { | ||
|
||
/*! | ||
* \brief Namespace of engine implementation. | ||
*/ | ||
namespace engine { | ||
|
||
/*! | ||
* \brief dynamic data-flow dag engine that schedules | ||
* operations in a concurrent way | ||
* \brief Inner representation of variable. | ||
*/ | ||
struct Var; | ||
|
||
/*! | ||
* \brief Inner representation of operator. | ||
*/ | ||
struct Opr; | ||
|
||
} // namespace engine | ||
|
||
/*! | ||
* \brief Dynamic dataflow DAG engine that schedules operations. | ||
*/ | ||
class DAGEngine { | ||
public: | ||
/*! | ||
* \brief operation to pass to DAG engine | ||
* \param ctx runtime context | ||
* \brief Operation to pass to DAG engine. | ||
*/ | ||
using Fn = std::function<void(RunContext)>; | ||
/*! | ||
* \brief Callback function to notify operation complete. | ||
*/ | ||
using Callback = std::function<void()>; | ||
/*! | ||
* \brief Asynchronous operation to pass to DAG engine. | ||
*/ | ||
using AsyncFn = std::function<void(RunContext, Callback)>; | ||
/*! | ||
* \brief Variable of dag engine, used to specify dependencies defined to be a | ||
* pointer, that points to an internal data structure of the engine | ||
* itself. | ||
*/ | ||
using Variable = engine::Var*; | ||
/*! | ||
* \brief Operator of the engine. | ||
*/ | ||
using OprHandle = engine::Opr*; | ||
/*! | ||
* \brief Allocate a new variable, the variable can then | ||
* be used to schedule the operation concurrently via dependency | ||
* patterns. | ||
* \return The new variable allocated. | ||
*/ | ||
virtual Variable NewVar() = 0; | ||
/*! | ||
* \brief Create a new operator. The returned operator could be saved | ||
* externally so that it could be resued for scheduling. | ||
* \param fn The execution function. | ||
* \param use_vars The variables that current operation will use but not | ||
* mutate. | ||
* \param mutate_vars Teh variables that current operation will mutate. | ||
* \return The new operator allocated. | ||
*/ | ||
virtual OprHandle NewOperator(AsyncFn fn, | ||
std::vector<Variable> const& use_vars, | ||
std::vector<Variable> const& mutate_vars) = 0; | ||
/*! | ||
* \brief Delete the given operator. | ||
* \param op The operator to delete. | ||
*/ | ||
typedef std::function<void(RunContext rctx)> Op; | ||
/*! \brief callback function to notify operation complete */ | ||
typedef std::function<void()> Callback; | ||
virtual void DeleteOperator(OprHandle op) = 0; | ||
/*! | ||
* \brief operation to pass to DAG engine | ||
* \param ctx runtime context | ||
* \param on_complete a callback function used to notify the engine the action completes | ||
* \brief Push an operator to the engine. | ||
* \param op The operator to push. | ||
* \param exec_ctx Execution context. | ||
*/ | ||
typedef std::function<void(RunContext ctx, Callback on_complete)> AsyncOp; | ||
virtual void Push(OprHandle op, Context exec_ctx) = 0; | ||
/*! | ||
* \brief variable of dag engine, used to specify dependencies | ||
* defined to be a pointer, that can points to a internal data structure | ||
* of the engine itself | ||
* \brief Push an synchronous operation to the DAG engine. | ||
* \param exec_fun Execution function that executes the operation. | ||
* \param exec_ctx Execution context. | ||
* \param use_vars The variables that current operation will use but not | ||
* mutate. | ||
* \param mutate_vars The variables that current operation will mutate. | ||
*/ | ||
void Push(Fn exec_fun, Context exec_ctx, | ||
std::vector<Variable> const& use_vars, | ||
std::vector<Variable> const& mutate_vars); | ||
/*! | ||
* \brief Push an asynchronous operation to the DAG engine. | ||
* \param exec_fun Execution function, this function takes a parameter | ||
* on_complete that must be called when the execution | ||
* completes. | ||
* \param exec_ctx Execution context. | ||
* \param use_vars The variables that current operation will use but not | ||
* mutate. | ||
* \param mutate_vars The variables that current operation will mutate. | ||
*/ | ||
virtual void PushAsync(AsyncFn exec_fun, Context exec_ctx, | ||
std::vector<Variable> const& use_vars, | ||
std::vector<Variable> const& mutate_vars) = 0; | ||
/*! | ||
* \brief Schedule the delete of a variable. | ||
* | ||
* Design detail: we choose pointer instead of some ID to avoid | ||
* indirect map lookup. usually, Variable directly points to the content we need | ||
*/ | ||
typedef void *Variable; | ||
/*! | ||
* \brief Push an asynchronize operation to the DAG engine | ||
* \param exec_fun execution funtion, this function takes a parameter on_complete | ||
* that must be called when the execution completes. For synchronize operations | ||
* \param exec_ctx execution context | ||
* \param use_vars the variables that current operation will use(but not mutate) | ||
* \param mutate_vars the variables that current operation will mutate | ||
*/ | ||
virtual void PushAsync(AsyncOp exec_fun, | ||
Context exec_ctx, | ||
const std::vector<Variable> &use_vars, | ||
const std::vector<Variable> &mutate_vars) = 0; | ||
/*! | ||
* \brief Push an synchronize operation to the DAG engine | ||
* \param exec_fun execution funtion that executes the operation | ||
* \param exec_ctx execution context | ||
* \param use_vars the variables that current operation will use(but not mutate) | ||
* \param mutate_vars the variables that current operation will mutate | ||
*/ | ||
virtual void Push(Op exec_fun, | ||
Context exec_ctx, | ||
const std::vector<Variable> &use_vars, | ||
const std::vector<Variable> &mutate_vars) { | ||
AsyncOp f = [exec_fun](RunContext ctx, Callback on_complete) { | ||
exec_fun(ctx); on_complete(); | ||
}; | ||
this->PushAsync(f, exec_ctx, use_vars, mutate_vars); | ||
} | ||
/*! | ||
* \brief schedule the delete of variable var, | ||
* The delete will not happen immediately, but will wait until all the operations | ||
* depending on var is completed | ||
* The delete will not happen immediately, but will wait until all the | ||
* operations depending on var is completed. | ||
* | ||
* \param delete_fun a function that will be called after var is deleted | ||
* \param exec_ctx execution context | ||
* \param var the variable to be deleted | ||
* \param delete_fun A function that will be called after the variable is | ||
* deleted. | ||
* \param exec_ctx Execution context. | ||
* \param var The variable to be deleted. | ||
*/ | ||
virtual void PushDelete(Op delete_fun, | ||
Context exec_ctx, | ||
Variable var) = 0; | ||
virtual void PushDelete(Fn delete_fun, Context exec_ctx, Variable var) = 0; | ||
/*! | ||
* \brief allocate a new variable, the variable can then | ||
* be used to schedul the operation concurrently via dependency patterns | ||
* \return thew new variable allocated | ||
* \brief Wait for variable. | ||
* \param var The variable we should wait for, this function returns when all | ||
* the operations related to var has been completed. | ||
*/ | ||
virtual Variable NewVar() = 0; | ||
virtual void WaitForVar(Variable var) = 0; | ||
/*! | ||
* \brief wait for variable var | ||
* \param var the variable we should wait for, this function returns when all the operations | ||
* related to var has been completed | ||
* \brief Wait until all the activity of dag engine finishes. | ||
*/ | ||
virtual void WaitForVar(Variable var) {} | ||
/*! \brief wait until all the activity of dag engine finishes */ | ||
virtual void WaitForAll() {} | ||
/*! \return DAG engine singleton */ | ||
static DAGEngine *Get(); | ||
}; | ||
virtual void WaitForAll() = 0; | ||
/*! | ||
* \brief Virtual destructor. | ||
*/ | ||
virtual ~DAGEngine() noexcept(false); | ||
/*! | ||
* \return DAG engine singleton. | ||
*/ | ||
static DAGEngine* Get(); | ||
|
||
protected: | ||
/*! | ||
* \brief Hidden constructors. | ||
*/ | ||
DAGEngine(); | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN(DAGEngine); | ||
}; // class DAGEngine | ||
|
||
} // namespace mxnet | ||
|
||
#endif // MXNET_DAG_ENGINE_H_ |
Oops, something went wrong.