Welcome to mirror list, hosted at ThFree Co, Russian Federation.

prepacked_cache.cc « ruy - github.com/google/ruy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa8f89462076ba083b101d975081789596d019aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Copyright 2019 Google LLC. All Rights Reserved.

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

    http://www.apache.org/licenses/LICENSE-2.0

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.
==============================================================================*/

#include "ruy/prepacked_cache.h"

#include <utility>

#include "ruy/matrix.h"
#include "ruy/profiler/instrumentation.h"

namespace ruy {

using CacheIterator = PrepackedCache::CacheIterator;

// Looks for an entry with `key`. If found, update its time stamp.
CacheIterator PrepackedCache::FindAndUpdate(const CacheKey &key) {
  auto itr = cache_.find(key);
  // If found, update with new access time for this entry.
  if (itr != cache_.end()) {
    const TimePoint time = CacheNow();
    itr->second.second = time;
  }
#ifdef _MSC_VER
  // std::move() is required in MSVC when NDEBUG is not set
  return std::move(itr);  // NOLINT
#else
  return itr;
#endif
}

void PrepackedCache::Insert(const CacheKey &key,
                            const PrepackedMatrix &matrix) {
  // Calculate size of this new item.
  const int size_bytes = matrix.data_size + matrix.sums_size;

  // While we are above the threshold of ejection, eject the LRU entry.
  while (!cache_.empty() &&
         ((TotalSize() + size_bytes) > ejection_threshold_)) {
    EjectOne();
  }
  DoInsert(key, matrix);
  cache_size_ += matrix.data_size + matrix.sums_size;
}

void PrepackedCache::EjectOne() {
  TimePoint oldest_time = CacheNow();
  auto oldest = cache_.begin();
  {
    profiler::ScopeLabel label("PepackedCacheEjection");
    for (auto itr = cache_.begin(); itr != cache_.end(); ++itr) {
      if (itr->second.second < oldest_time) {
        oldest_time = itr->second.second;
        oldest = itr;
      }
    }
  }
  PrepackedMatrix &pmatrix = oldest->second.first;
  cache_size_ -= pmatrix.data_size;
  cache_size_ -= pmatrix.sums_size;
  allocator_.Free(pmatrix.data);
  allocator_.Free(pmatrix.sums);
  cache_.erase(oldest);
}

void PrepackedCache::AllocatePrepackedMatrix(PrepackedMatrix *pmatrix) {
  pmatrix->data = allocator_.Alloc(pmatrix->data_size);
  pmatrix->sums = allocator_.Alloc(pmatrix->sums_size);
}

void PrepackedCache::DoInsert(const CacheKey &key,
                              const PrepackedMatrix &matrix) {
  const TimePoint t = CacheNow();
  const MatrixWithTimeStamp mts({matrix, t});
  cache_.insert({key, mts});
}

}  // namespace ruy