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

CodeCache.h « src - github.com/marian-nmt/FBGEMM.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8ee15c672d240567b45cb0dbefc9eed8534c7a0 (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
/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 * All rights reserved.
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */
#pragma once
#include <map>

namespace fbgemm {

/**
 * @brief Thread safe cache for microkernels, ensures single creation per key.
 * @tparam Key Type of unique key (typically a tuple)
 * @tparam Value Type of the microkernel function (Typically a function pointer)
 */
template <typename KEY, typename VALUE>
class CodeCache {
 private:
  std::map<KEY, VALUE> values_;

 public:
  CodeCache(const CodeCache&) = delete;
  CodeCache& operator=(const CodeCache&) = delete;

  CodeCache(){};

  VALUE getOrCreate(const KEY& key, std::function<VALUE()> generatorFunction) {
    auto it = values_.find(key);
    if (it != values_.end()) {
      return it->second;
    } else {
      //std::cerr << "create" << std::endl;
      auto fn = generatorFunction();
      values_[key] = fn;
      return fn;
    }
  }
};

} // namespace fbgemm