meta_cache.cc 1.7 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

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

namespace paddle {
namespace memory {
namespace detail {

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

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

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

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

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

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