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

Commit

Permalink
Reference engine from chunk via weak pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
KellenSunderland committed Apr 16, 2019
1 parent 413fe97 commit 041f17b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
22 changes: 15 additions & 7 deletions include/mxnet/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -850,15 +850,20 @@ class NDArray {
mxnet::ShapeVector aux_shapes;
/*! \brief Reference to the storage to ensure proper destruct order */
std::shared_ptr<Storage> storage_ref_;
/*! \brief Reference to the engine to ensure we cleanup without calling a destructed engine */
std::weak_ptr<Engine> engine_ref_;

/*! \brief default cosntructor */

/*! \brief default constructor */
Chunk() : static_data(true), delay_alloc(false),
storage_ref_(Storage::_GetSharedRef()) {}
storage_ref_(Storage::_GetSharedRef()),
engine_ref_(Engine::_GetSharedRef()) {}

/*! \brief construct a new chunk */
Chunk(mxnet::TShape shape, Context ctx_, bool delay_alloc_, int dtype)
: static_data(false), delay_alloc(true), ctx(ctx_),
storage_ref_(Storage::_GetSharedRef()) {
storage_ref_(Storage::_GetSharedRef()),
engine_ref_(Engine::_GetSharedRef()) {
auto size = shape.Size();
storage_shape = shape;
var = Engine::Get()->NewVariable();
Expand All @@ -869,7 +874,8 @@ class NDArray {

Chunk(const TBlob &data, int dev_id)
: static_data(true), delay_alloc(false),
storage_ref_(Storage::_GetSharedRef()) {
storage_ref_(Storage::_GetSharedRef()),
engine_ref_(Engine::_GetSharedRef()) {
CHECK(storage_type == kDefaultStorage);
var = Engine::Get()->NewVariable();
if (data.dev_mask() == cpu::kDevMask) {
Expand All @@ -887,7 +893,8 @@ class NDArray {

Chunk(int shared_pid, int shared_id, const mxnet::TShape& shape, int dtype)
: static_data(false), delay_alloc(false),
storage_ref_(Storage::_GetSharedRef()) {
storage_ref_(Storage::_GetSharedRef()),
engine_ref_(Engine::_GetSharedRef()) {
var = Engine::Get()->NewVariable();
ctx = Context::CPUShared(0);
shandle.size = shape.Size() * mshadow::mshadow_sizeof(dtype);
Expand All @@ -903,7 +910,8 @@ class NDArray {
const mxnet::ShapeVector &aux_shapes_)
: static_data(false), delay_alloc(delay_alloc_), storage_type(storage_type_),
aux_types(aux_types_), ctx(ctx_), storage_shape(storage_shape_),
aux_shapes(aux_shapes_), storage_ref_(Storage::_GetSharedRef()) {
aux_shapes(aux_shapes_), storage_ref_(Storage::_GetSharedRef()),
engine_ref_(Engine::_GetSharedRef()) {
shandle.ctx = ctx;
var = Engine::Get()->NewVariable();
// aux_handles always reflect the correct number of aux data
Expand All @@ -921,7 +929,7 @@ class NDArray {
Chunk(const NDArrayStorageType storage_type_, const TBlob &data,
const std::vector<TBlob> &aux_data, int dev_id)
: static_data(true), delay_alloc(false), storage_type(storage_type_),
storage_ref_(Storage::_GetSharedRef()) {
storage_ref_(Storage::_GetSharedRef()), engine_ref_(Engine::_GetSharedRef()) {
using namespace mshadow;
CHECK_NE(storage_type, kDefaultStorage);
// init var
Expand Down
24 changes: 13 additions & 11 deletions src/ndarray/ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,22 @@ NDArray::Chunk::~Chunk() {
// We want to delete mkldnn memory after deleting the variable.
mem.mem = this->mkl_mem_;
#endif
Engine::Get()->DeleteVariable([mem, skip_free](RunContext s) {
if (skip_free == false) {
if (auto engine = engine_ref_.lock()) {
engine->DeleteVariable([mem, skip_free](RunContext s) {
if (skip_free == false) {
#if MXNET_USE_MKLDNN == 1
if (mem.mem) {
CHECK_LE(mem.mem->GetSize(), mem.h.size);
CHECK_EQ(mem.mem->GetDataHandle(), mem.h.dptr);
}
if (mem.mem) {
CHECK_LE(mem.mem->GetSize(), mem.h.size);
CHECK_EQ(mem.mem->GetDataHandle(), mem.h.dptr);
}
#endif
Storage::Get()->Free(mem.h);
for (const auto& aux : mem.aux_h) {
Storage::Get()->Free(aux);
Storage::Get()->Free(mem.h);
for (const auto &aux : mem.aux_h) {
Storage::Get()->Free(aux);
}
}
}
}, shandle.ctx, var);
}, shandle.ctx, var);
}
}

void NDArray::Chunk::CheckAndAllocData(const mxnet::TShape &shape, int dtype) {
Expand Down
9 changes: 9 additions & 0 deletions tests/cpp/engine/threaded_engine_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ double EvaluateWorloads(const std::vector<Workload>& workloads,
return dmlc::GetTime() - t;
}

TEST(Engine, stop_cleanup) {
static std::unique_ptr<mxnet::NDArray> ndArray;
{
auto engine = mxnet::Engine::_GetSharedRef();
ndArray = std::make_unique<mxnet::NDArray>(mxnet::Context::CPU());
engine->Stop();
}
}

TEST(Engine, start_stop) {
const int num_engine = 3;
std::vector<mxnet::Engine*> engine(num_engine);
Expand Down

0 comments on commit 041f17b

Please sign in to comment.