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
Yi Wang 已提交
25
Metadata MetadataCache::load(const MemoryBlock* block) const {
26 27
  if (uses_gpu_) {
    auto existing_metadata = cache_.find(block);
L
liaogang 已提交
28
    PADDLE_ASSERT(existing_metadata->second.check_guards());
29 30
    return existing_metadata->second;
  } else {
31
    auto* meta = reinterpret_cast<const Metadata*>(block);
32
    VLOG(10) << "Load MetaData type=" << meta->type;
33
    PADDLE_ASSERT(meta->check_guards());
34 35 36 37
    return *reinterpret_cast<const Metadata*>(block);
  }
}

Y
Yi Wang 已提交
38 39
void MetadataCache::save(MemoryBlock* block,
                         const Metadata& original_metadata) {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  auto metadata = original_metadata;

  metadata.update_guards();

  if (uses_gpu_) {
    cache_[block] = metadata;
  } else {
    *reinterpret_cast<Metadata*>(block) = metadata;
  }
}

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

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