Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
[engine-refactor] DAGEngine -> Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
hotpxl committed Sep 7, 2015
1 parent 9794621 commit ad97494
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 111 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ endif

BIN = tests/test_threaded_engine
OBJ = narray_function_cpu.o
OBJCXX11 = narray.o c_api.o operator.o symbol.o storage.o static_graph.o graph_executor.o io.o iter_mnist.o dag_engine.o naive_engine.o threaded_engine.o
OBJCXX11 = narray.o c_api.o operator.o symbol.o storage.o static_graph.o graph_executor.o io.o iter_mnist.o engine.o naive_engine.o threaded_engine.o
CUOBJ = narray_function_gpu.o
SLIB = lib/libmxnet.so
ALIB = lib/libmxnet.a
Expand All @@ -77,9 +77,9 @@ $(DMLC_CORE)/libdmlc.a:
+ cd $(DMLC_CORE); make libdmlc.a config=$(ROOTDIR)/$(config); cd $(ROOTDIR)

storage.o: src/storage/storage.cc
naive_engine.o: src/dag_engine/naive_engine.cc
dag_engine.o: src/dag_engine/dag_engine.cc
threaded_engine.o: src/dag_engine/threaded_engine.cc
naive_engine.o: src/engine/naive_engine.cc
engine.o: src/engine/engine.cc
threaded_engine.o: src/engine/threaded_engine.cc
narray.o: src/narray/narray.cc
narray_function_cpu.o: src/narray/narray_function.cc src/narray/narray_function-inl.h
narray_function_gpu.o: src/narray/narray_function.cu src/narray/narray_function-inl.h
Expand Down
36 changes: 18 additions & 18 deletions include/mxnet/dag_engine.h → include/mxnet/engine.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*!
* Copyright (c) 2015 by Contributors
* \file dag_engine.h
* \brief DAG engine that schedules data.
* \file engine.h
* \brief Engine that schedules data.
*/
#ifndef MXNET_DAG_ENGINE_H_
#define MXNET_DAG_ENGINE_H_
#ifndef MXNET_ENGINE_H_
#define MXNET_ENGINE_H_
#include <dmlc/base.h>

#if DMLC_USE_CXX11 == 0
#error "C++11 was required for DAG engine module."
#error "C++11 was required for engine module."
#endif

#include <functional>
Expand Down Expand Up @@ -36,24 +36,24 @@ struct Opr;
} // namespace engine

/*!
* \brief Dynamic dataflow DAG engine that schedules operations.
* \brief Dynamic dataflow engine that schedules operations.
*/
class DAGEngine {
class Engine {
public:
/*!
* \brief Operation to pass to DAG engine.
* \brief Operation to pass to 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.
* \brief Asynchronous operation to pass to engine.
*/
using AsyncFn = std::function<void(RunContext, Callback)>;
/*!
* \brief Variable of dag engine, used to specify dependencies defined to be a
* \brief Variable of engine, used to specify dependencies defined to be a
* pointer, that points to an internal data structure of the engine
* itself.
*/
Expand Down Expand Up @@ -93,7 +93,7 @@ class DAGEngine {
*/
virtual void Push(OprHandle op, Context exec_ctx) = 0;
/*!
* \brief Push an synchronous operation to the DAG engine.
* \brief Push an synchronous operation to the 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
Expand All @@ -104,7 +104,7 @@ class DAGEngine {
std::vector<Variable> const& use_vars,
std::vector<Variable> const& mutate_vars) = 0;
/*!
* \brief Push an asynchronous operation to the DAG engine.
* \brief Push an asynchronous operation to the engine.
* \param exec_fun Execution function, this function takes a parameter
* on_complete that must be called when the execution
* completes.
Expand Down Expand Up @@ -135,19 +135,19 @@ class DAGEngine {
*/
virtual void WaitForVar(Variable var) = 0;
/*!
* \brief Wait until all the activity of dag engine finishes.
* \brief Wait until all the activity of engine finishes.
*/
virtual void WaitForAll() = 0;
/*!
* \brief Virtual destructor.
*/
virtual ~DAGEngine() noexcept(false);
virtual ~Engine() noexcept(false);
/*!
* \return DAG engine singleton.
* \return Engine singleton.
*/
static DAGEngine* Get();
}; // class DAGEngine
static Engine* Get();
}; // class Engine

} // namespace mxnet

#endif // MXNET_DAG_ENGINE_H_
#endif // MXNET_ENGINE_H_
22 changes: 11 additions & 11 deletions include/mxnet/narray.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "./context.h"
#include "./storage.h"
#include "./context.h"
#include "./dag_engine.h"
#include "./engine.h"
// check c++11
#if DMLC_USE_CXX11 == 0
#error "cxx11 was required for narray module"
Expand Down Expand Up @@ -76,10 +76,10 @@ class NArray {
/*! \brief wait until the result of the NArray is computed */
inline void Wait() const {
if (is_none()) return;
DAGEngine::Get()->WaitForVar(ptr_->var);
Engine::Get()->WaitForVar(ptr_->var);
}
/*! \return the associated DAG variable of the narray.*/
inline DAGEngine::Variable var() const {
/*! \return the associated variable of the narray.*/
inline Engine::Variable var() const {
return ptr_->var;
}
/*!
Expand Down Expand Up @@ -206,8 +206,8 @@ class NArray {
struct Chunk {
/*! \brief storage handlefrom storage engine */
Storage::Handle shandle;
/*! \brief variable from DAG engine */
DAGEngine::Variable var;
/*! \brief variable from engine */
Engine::Variable var;
/*!
* \brief if this is true, this means the data do not come
* from Storage, and do not need to be freed
Expand All @@ -217,21 +217,21 @@ class NArray {
bool delay_alloc;
/*! \brief default cosntructor */
Chunk() : static_data(true), delay_alloc(false) {
var = DAGEngine::Get()->NewVar();
var = Engine::Get()->NewVar();
}
/*! \brief construct from static data */
Chunk(const TBlob &data, int dev_id)
: static_data(true),
delay_alloc(false) {
var = DAGEngine::Get()->NewVar();
var = Engine::Get()->NewVar();
shandle.ctx = Context(data.dev_mask_, dev_id);
shandle.dptr = data.dptr_;
shandle.size = data.shape_.Size() * sizeof(real_t);
}
/*! \brief construct a new chunk */
Chunk(uint64_t size, Context ctx, bool delay_alloc_)
: static_data(false), delay_alloc(true) {
var = DAGEngine::Get()->NewVar();
var = Engine::Get()->NewVar();
shandle.size = size * sizeof(real_t);
shandle.ctx = ctx;
if (!delay_alloc_) this->CheckAndAlloc();
Expand All @@ -246,11 +246,11 @@ class NArray {
/*! \brief destructor */
~Chunk() {
if (static_data) {
DAGEngine::Get()->PushDelete([](RunContext s) {}, shandle.ctx, var);
Engine::Get()->PushDelete([](RunContext s) {}, shandle.ctx, var);
} else {
CHECK(!delay_alloc) << "deleted before allocation";
Storage::Handle h = this->shandle;
DAGEngine::Get()->PushDelete([h](RunContext s) {
Engine::Get()->PushDelete([h](RunContext s) {
Storage::Get()->Free(h);
}, shandle.ctx, var);
}
Expand Down
2 changes: 1 addition & 1 deletion src/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ int MXNArrayListLoad(const char* fname,

int MXNArrayWaitAll() {
API_BEGIN();
DAGEngine::Get()->WaitForAll();
Engine::Get()->WaitForAll();
API_END();
}

Expand Down
8 changes: 4 additions & 4 deletions src/dag_engine/dag_engine.cc → src/engine/engine.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*!
* Copyright (c) 2015 by Contributors
*/
#include "mxnet/dag_engine.h"
#include "dag_engine_impl.h"
#include "mxnet/engine.h"
#include "engine_impl.h"
#include "naive_engine.h"
#include "threaded_engine.h"

namespace mxnet {

DAGEngine::~DAGEngine() noexcept(false) {}
Engine::~Engine() noexcept(false) {}

DAGEngine* DAGEngine::Get() {
Engine* Engine::Get() {
/*!
* \brief Change specific engine to use.
*/
Expand Down
30 changes: 15 additions & 15 deletions src/dag_engine/dag_engine_impl.h → src/engine/engine_impl.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
/*!
* Copyright (c) 2015 by Contributors
*/
#ifndef MXNET_DAG_ENGINE_DAG_ENGINE_IMPL_H_
#define MXNET_DAG_ENGINE_DAG_ENGINE_IMPL_H_
#ifndef MXNET_ENGINE_ENGINE_IMPL_H_
#define MXNET_ENGINE_ENGINE_IMPL_H_

#include <utility>
#include "mxnet/dag_engine.h"
#include "mxnet/engine.h"

#define DAG_ENGINE_DEBUG 0
#define ENGINE_DEBUG 0

namespace mxnet {
namespace engine {

struct Var {
#if DAG_ENGINE_DEBUG
#if ENGINE_DEBUG
virtual ~Var() = default;
#endif // DAG_ENGINE_DEBUG
#endif // ENGINE_DEBUG
template <typename T>
T* Cast();
}; // struct Var

struct Opr {
#if DAG_ENGINE_DEBUG
#if ENGINE_DEBUG
virtual ~Opr() = default;
#endif // DAG_ENGINE_DEBUG
#endif // ENGINE_DEBUG
template <typename T>
T* Cast();
}; // struct Opr
Expand All @@ -32,25 +32,25 @@ template <typename T>
T* Var::Cast() {
static_assert(std::is_base_of<Var, T>::value,
"must inherit `mxnet::engine::Var`");
#if DAG_ENGINE_DEBUG
#if ENGINE_DEBUG
return dynamic_cast<T*>(this);
#else // DAG_ENGINE_DEBUG
#else // ENGINE_DEBUG
return static_cast<T*>(this);
#endif // DAG_ENGINE_DEBUG
#endif // ENGINE_DEBUG
}

template <typename T>
T* Opr::Cast() {
static_assert(std::is_base_of<Opr, T>::value,
"must inherit `mxnet::engine::Opr`");
#if DAG_ENGINE_DEBUG
#if ENGINE_DEBUG
return dynamic_cast<T*>(this);
#else // DAG_ENGINE_DEBUG
#else // ENGINE_DEBUG
return static_cast<T*>(this);
#endif // DAG_ENGINE_DEBUG
#endif // ENGINE_DEBUG
}

} // namespace engine
} // namespace mxnet

#endif // MXNET_DAG_ENGINE_DAG_ENGINE_IMPL_H_
#endif // MXNET_ENGINE_ENGINE_IMPL_H_
File renamed without changes.
10 changes: 5 additions & 5 deletions src/dag_engine/naive_engine.h → src/engine/naive_engine.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*!
* Copyright (c) 2015 by Contributors
*/
#ifndef MXNET_DAG_ENGINE_NAIVE_ENGINE_H_
#define MXNET_DAG_ENGINE_NAIVE_ENGINE_H_
#ifndef MXNET_ENGINE_NAIVE_ENGINE_H_
#define MXNET_ENGINE_NAIVE_ENGINE_H_

#include <vector>
#include "dag_engine_impl.h"
#include "engine_impl.h"

namespace mxnet {

namespace engine {

class NaiveEngine final : public DAGEngine {
class NaiveEngine final : public Engine {
public:
NaiveEngine();
~NaiveEngine();
Expand Down Expand Up @@ -41,4 +41,4 @@ class NaiveEngine final : public DAGEngine {

} // namespace mxnet

#endif // MXNET_DAG_ENGINE_NAIVE_ENGINE_H_
#endif // MXNET_ENGINE_NAIVE_ENGINE_H_
6 changes: 3 additions & 3 deletions src/dag_engine/thread_pool.h → src/engine/thread_pool.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*!
* Copyright (c) 2015 by Contributors
*/
#ifndef MXNET_DAG_ENGINE_THREAD_POOL_H_
#define MXNET_DAG_ENGINE_THREAD_POOL_H_
#ifndef MXNET_ENGINE_THREAD_POOL_H_
#define MXNET_ENGINE_THREAD_POOL_H_

#include <dmlc/base.h>
#include <cstddef>
Expand Down Expand Up @@ -64,4 +64,4 @@ ThreadPool<kSize>::~ThreadPool() noexcept(false) {
} // namespace engine
} // namespace mxnet

#endif // MXNET_DAG_ENGINE_THREAD_POOL_H_
#endif // MXNET_ENGINE_THREAD_POOL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ namespace mxnet {

namespace engine {

#if DAG_ENGINE_DEBUG
#if ENGINE_DEBUG
std::atomic<std::size_t> OprBlock::counter{0};
std::atomic<std::size_t> VersionedVarBlock::counter{0};
std::atomic<std::size_t> ThreadedVar::counter{0};
std::atomic<std::size_t> ThreadedOpr::counter{0};
#endif // DAG_ENGINE_DEBUG
#endif // ENGINE_DEBUG

ThreadedVar::ThreadedVar(VersionedVarBlock* head) : head_{head} {
#if DAG_ENGINE_DEBUG
#if ENGINE_DEBUG
LOG(INFO) << __func__ << " " << ++counter;
#endif // DAG_ENGINE_DEBUG
#endif // ENGINE_DEBUG
}
void ThreadedVar::AppendReadDependency(OprBlock* opr_block) {
std::lock_guard<std::mutex> lock{m_};
Expand Down Expand Up @@ -148,7 +148,7 @@ ThreadedOpr* ThreadedEngine::NewOperator(
ThreadedVar::CastFromBase);
std::transform(mutate_vars.begin(), mutate_vars.end(),
ret->mutate_vars.begin(), ThreadedVar::CastFromBase);
#if DAG_ENGINE_DEBUG
#if ENGINE_DEBUG
// Check for duplicates.
auto use = use_vars;
auto mutate = mutate_vars;
Expand Down Expand Up @@ -179,7 +179,7 @@ ThreadedOpr* ThreadedEngine::NewOperator(
<< "duplicate items found between `use_vars` and `mutate_vars`";
}
}
#endif // DAG_ENGINE_DEBUG
#endif // ENGINE_DEBUG
return ret;
}

Expand Down
Loading

0 comments on commit ad97494

Please sign in to comment.