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

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

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

namespace paddle {
namespace memory {
namespace detail {

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

Metadata MetadataCache::load(const MemoryBlock* block) {
  if (uses_gpu_) {
    auto existing_metadata = cache_.find(block);
L
liaogang 已提交
29
    PADDLE_ASSERT(existing_metadata->second.check_guards());
30 31
    return existing_metadata->second;
  } else {
32
    auto* meta = reinterpret_cast<const Metadata*>(block);
33
    VLOG(10) << "Load MetaData type=" << meta->type;
34
    PADDLE_ASSERT(meta->check_guards());
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    return *reinterpret_cast<const Metadata*>(block);
  }
}

void MetadataCache::store(MemoryBlock* block,
                          const Metadata& original_metadata) {
  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