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

Commit

Permalink
Merge pull request #30 from dmlc/simple-engine
Browse files Browse the repository at this point in the history
Simple engine
  • Loading branch information
antinucleon committed Aug 31, 2015
2 parents 8642c1e + 8628138 commit 61aa571
Show file tree
Hide file tree
Showing 13 changed files with 953 additions and 308 deletions.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ ifneq ($(ADD_LDFLAGS), NONE)
LDFLAGS += $(ADD_LDFLAGS)
endif

#BIN = test/test_threaded_engine test/api_registry_test
BIN = tests/test_simple_engine
OBJ = narray_function_cpu.o
# add threaded engine after it is done
OBJCXX11 = reshape_cpu.o engine.o narray.o c_api.o operator.o symbol.o storage.o fully_connected_cpu.o static_graph.o activation_cpu.o graph_executor.o softmax_cpu.o elementwise_sum_cpu.o pooling_cpu.o convolution_cpu.o io.o iter_mnist.o
OBJCXX11 = reshape_cpu.o dag_engine.o simple_engine.o narray.o c_api.o operator.o symbol.o storage.o fully_connected_cpu.o static_graph.o activation_cpu.o graph_executor.o softmax_cpu.o elementwise_sum_cpu.o pooling_cpu.o convolution_cpu.o io.o iter_mnist.o
CUOBJ =
SLIB = lib/libmxnet.so
ALIB = lib/libmxnet.a
Expand All @@ -82,7 +81,8 @@ $(DMLC_CORE)/libdmlc.a:
+ cd $(DMLC_CORE); make libdmlc.a config=$(ROOTDIR)/$(config); cd $(ROOTDIR)

storage.o: src/storage/storage.cc
engine.o: src/dag_engine/simple_engine.cc
dag_engine.o: src/dag_engine/dag_engine.cc
simple_engine.o: src/dag_engine/simple_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 Expand Up @@ -111,7 +111,8 @@ iter_mnist.o: src/io/iter_mnist.cc
lib/libmxnet.a: $(OBJ) $(OBJCXX11) $(CUOBJ) $(LIB_DEP)
lib/libmxnet.so: $(OBJ) $(OBJCXX11) $(CUOBJ) $(LIB_DEP)

test/test_storage: test/test_storage.cc lib/libmxnet.a
tests/test_storage: tests/test_storage.cc lib/libmxnet.a
tests/test_simple_engine: tests/test_simple_engine.cc lib/libmxnet.a

$(BIN) :
$(CXX) $(CFLAGS) -std=c++0x -o $@ $(filter %.cpp %.o %.c %.a %.cc, $^) $(LDFLAGS)
Expand All @@ -125,7 +126,7 @@ $(OBJCXX11) :
$(SLIB) :
$(CXX) $(CFLAGS) -shared -o $@ $(filter %.cpp %.o %.c %.a %.cc, $^) $(LDFLAGS)

$(ALIB):
$(ALIB): $(OBJ) $(OBJCXX11)
ar cr $@ $+

$(CUOBJ) :
Expand Down
2 changes: 1 addition & 1 deletion doc/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ PERLMOD_MAKEVAR_PREFIX =
# C-preprocessor directives found in the sources and include files.
# The default value is: YES.

ENABLE_PREPROCESSING = NO
ENABLE_PREPROCESSING = YES

# If the MACRO_EXPANSION tag is set to YES doxygen will expand all macro names
# in the source code. If set to NO only conditional compilation will be
Expand Down
207 changes: 131 additions & 76 deletions include/mxnet/dag_engine.h
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_
Loading

0 comments on commit 61aa571

Please sign in to comment.