Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add general memory usage interface for both CPU/CUDA #7352

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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