meta_cache.cc 1.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
14

D
dzhwinter 已提交
15 16
#define GLOG_NO_ABBREVIATED_SEVERITIES
#define GOOGLE_GLOG_DLL_DECL
17
#include "glog/logging.h"
Y
Yi Wang 已提交
18 19
#include "paddle/fluid/memory/detail/memory_block.h"
#include "paddle/fluid/platform/assert.h"
20 21 22 23 24 25 26

namespace paddle {
namespace memory {
namespace detail {

MetadataCache::MetadataCache(bool uses_gpu) : uses_gpu_(uses_gpu) {}

Y
Update  
Yi Wang 已提交
27
MemoryBlock::Desc MetadataCache::load(const MemoryBlock* block) const {
28
  if (uses_gpu_) {
Y
Update  
Yi Wang 已提交
29 30 31
    auto existing_desc = cache_.find(block);
    PADDLE_ASSERT(existing_desc->second.check_guards());
    return existing_desc->second;
32
  } else {
Y
Update  
Yi Wang 已提交
33
    auto* desc = reinterpret_cast<const MemoryBlock::Desc*>(block);
34
    VLOG(100) << "Load MemoryBlock::Desc type=" << desc->type;
Y
Update  
Yi Wang 已提交
35 36
    PADDLE_ASSERT(desc->check_guards());
    return *reinterpret_cast<const MemoryBlock::Desc*>(block);
37 38 39
  }
}

Y
Yi Wang 已提交
40
void MetadataCache::save(MemoryBlock* block,
Y
Update  
Yi Wang 已提交
41 42 43
                         const MemoryBlock::Desc& original_desc) {
  auto desc = original_desc;
  desc.update_guards();
44 45

  if (uses_gpu_) {
Y
Update  
Yi Wang 已提交
46
    cache_[block] = desc;
47
  } else {
Y
Update  
Yi Wang 已提交
48
    *reinterpret_cast<MemoryBlock::Desc*>(block) = desc;
49 50 51 52 53 54 55 56 57 58 59 60
  }
}

void MetadataCache::invalidate(MemoryBlock* block) {
  if (uses_gpu_) {
    cache_.erase(block);
  }
}

}  // namespace detail
}  // namespace memory
}  // namespace paddle