Skip to content

Commit

Permalink
recover code
Browse files Browse the repository at this point in the history
test=develop
  • Loading branch information
Superjomn committed Sep 27, 2018
1 parent f1b0fea commit af5c86c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
47 changes: 46 additions & 1 deletion paddle/fluid/framework/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ limitations under the License. */
#include "paddle/fluid/framework/threadpool.h"
#include "paddle/fluid/string/printf.h"

// The mutex is not needed by training and inference, only for distribution.
#if PADDLE_WITH_DISTRIBUTE
#define WITH_LOCK 1
#else
#define WITH_LOCK 0
#endif

DEFINE_bool(benchmark, false,
"Doing memory benchmark. It will make deleting scope synchronized, "
"and add some memory usage logs."
Expand Down Expand Up @@ -49,13 +56,24 @@ int64_t GetEagerDeletionThreshold() {
Scope::~Scope() { DropKids(); }

Scope& Scope::NewScope() const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
kids_.push_back(new Scope(this));
return *kids_.back();
}

Variable* Scope::Var(const std::string& name) { return VarInternal(name); }
Variable* Scope::Var(const std::string& name) {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
return VarInternal(name);
}

Variable* Scope::Var(std::string* name) {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
if (name != nullptr) {
*name = new_name;
Expand All @@ -64,24 +82,39 @@ Variable* Scope::Var(std::string* name) {
}

Variable* Scope::FindVar(const std::string& name) const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
return FindVarInternal(name);
}

const Scope* Scope::FindScope(const Variable* var) const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
return FindScopeInternal(var);
}

void Scope::DropKids() {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
for (Scope* s : kids_) delete s;
kids_.clear();
}

bool Scope::HasKid(const Scope* scope) const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
auto it = std::find(this->kids_.begin(), this->kids_.end(), scope);
return it != this->kids_.end();
}

std::vector<std::string> Scope::LocalVarNames() const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
std::vector<std::string> known_vars;
known_vars.reserve(this->vars_.size());
for (auto& p : vars_) {
Expand All @@ -91,6 +124,9 @@ std::vector<std::string> Scope::LocalVarNames() const {
}

void Scope::DeleteScope(Scope* scope) const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
auto it = std::find(this->kids_.begin(), this->kids_.end(), scope);
PADDLE_ENFORCE(it != this->kids_.end(), "Cannot find %p as kid scope", scope);
this->kids_.erase(it);
Expand All @@ -103,6 +139,9 @@ void Scope::DeleteScope(Scope* scope) const {
}

void Scope::EraseVars(const std::vector<std::string>& var_names) {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
std::set<std::string> var_set(var_names.begin(), var_names.end());
for (auto it = vars_.begin(); it != vars_.end();) {
if (var_set.find(it->first) != var_set.end()) {
Expand All @@ -115,10 +154,16 @@ void Scope::EraseVars(const std::vector<std::string>& var_names) {

void Scope::Rename(const std::string& origin_name,
const std::string& new_name) const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
RenameInternal(origin_name, new_name);
}

std::string Scope::Rename(const std::string& origin_name) const {
#if WITH_LOCK
std::unique_lock<std::mutex> lock(mutex_);
#endif
auto new_name = string::Sprintf("%p.%d", this, vars_.size());
RenameInternal(origin_name, new_name);
return new_name;
Expand Down
1 change: 0 additions & 1 deletion paddle/fluid/inference/tests/api/analyzer_rnn1_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ void PrepareZeroCopyInputs(ZeroCopyTensor *lod_attention_tensor,
// assign data
float arr0[] = {0, 0};
std::vector<float> zeros(batch_size * 15, 0);

std::copy_n(arr0, 2,
lod_attention_tensor->mutable_data<float>(PaddlePlace::kCPU));
std::copy_n(arr0, 2, data_tensor->mutable_data<float>(PaddlePlace::kCPU));
Expand Down
10 changes: 6 additions & 4 deletions paddle/fluid/memory/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ namespace memory {
using BuddyAllocator = detail::BuddyAllocator;

BuddyAllocator* GetCPUBuddyAllocator() {
static thread_local std::once_flag init_flag;
static thread_local detail::BuddyAllocator* a = nullptr;
// We tried thread_local for inference::RNN1 model, but that not works much
// for multi-thread test.
static std::once_flag init_flag;
static detail::BuddyAllocator* a = nullptr;

std::call_once(init_flag, []() {
a = new detail::BuddyAllocator(
Expand All @@ -48,6 +50,8 @@ BuddyAllocator* GetCPUBuddyAllocator() {
return a;
}

// We compared the NaiveAllocator with BuddyAllocator in CPU memory allocation,
// seems they are almost the same overhead.
struct NaiveAllocator {
void* Alloc(size_t size) { return malloc(size); }

Expand All @@ -69,7 +73,6 @@ template <>
void* Alloc<platform::CPUPlace>(platform::CPUPlace place, size_t size) {
VLOG(10) << "Allocate " << size << " bytes on " << platform::Place(place);
void* p = GetCPUBuddyAllocator()->Alloc(size);
// void* p = NaiveAllocator::Instance()->Alloc(size);
if (FLAGS_init_allocated_mem) {
memset(p, 0xEF, size);
}
Expand All @@ -81,7 +84,6 @@ template <>
void Free<platform::CPUPlace>(platform::CPUPlace place, void* p) {
VLOG(10) << "Free pointer=" << p << " on " << platform::Place(place);
GetCPUBuddyAllocator()->Free(p);
// NaiveAllocator::Instance()->Free(p);
}

template <>
Expand Down

0 comments on commit af5c86c

Please sign in to comment.