Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
Browse files Browse the repository at this point in the history
… fix-7346
  • Loading branch information
pkuyym committed Jan 9, 2018
2 parents 427e474 + 45e7715 commit 106d063
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions paddle/memory/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,21 @@ void Free<platform::CUDAPlace>(platform::CUDAPlace place, void* p) {

#endif

size_t Usage::operator()(const platform::CPUPlace& cpu) const {
return Used(cpu);
}

size_t Usage::operator()(const platform::CUDAPlace& gpu) const {
#ifdef PADDLE_WITH_CUDA
return Used(gpu);
#else
PADDLE_THROW("'CUDAPlace' is not supported in CPU only device.");
#endif
}

size_t memory_usage(const platform::Place& p) {
return boost::apply_visitor(Usage(), p);
}

} // namespace memory
} // namespace paddle
7 changes: 7 additions & 0 deletions paddle/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ void Free(Place place, void* ptr);
template <typename Place>
size_t Used(Place place);

struct Usage : public boost::static_visitor<size_t> {
size_t operator()(const platform::CPUPlace& cpu) const;
size_t operator()(const platform::CUDAPlace& gpu) const;
};

size_t memory_usage(const platform::Place& p);

/**
* \brief Free memory block in one place.
*
Expand Down
6 changes: 6 additions & 0 deletions paddle/memory/memory_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ TEST(BuddyAllocator, CPUAllocation) {

EXPECT_NE(p, nullptr);

paddle::platform::Place place = cpu;
EXPECT_EQ(paddle::memory::Used(cpu), paddle::memory::memory_usage(place));

paddle::memory::Free(cpu, p);
}

Expand Down Expand Up @@ -99,6 +102,9 @@ TEST(BuddyAllocator, GPUAllocation) {

EXPECT_NE(p, nullptr);

paddle::platform::Place place = gpu;
EXPECT_EQ(paddle::memory::Used(gpu), paddle::memory::memory_usage(place));

paddle::memory::Free(gpu, p);
}

Expand Down

0 comments on commit 106d063

Please sign in to comment.