Caffe2 - C++ API
A deep learning, cross platform ML framework
conv_op_cache_cudnn.h
1 
17 #ifndef CAFFE2_OPERATORS_CONV_OP_CACHE_H_
18 #define CAFFE2_OPERATORS_CONV_OP_CACHE_H_
19 
20 #include <functional>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "caffe2/core/logging.h"
25 #include "caffe2/core/tensor.h"
26 
27 namespace caffe2 {
28 template <typename T>
30  public:
31  T getAlgorithm(
32  const std::vector<TIndex>& bottom,
33  const std::vector<TIndex>& desc,
34  std::function<T()> generatingFunc);
35 
36  private:
37  std::unordered_map<int64_t, T> hash_;
38 };
39 
40 template <typename T>
42  const std::vector<TIndex>& vec1,
43  const std::vector<TIndex>& vec2,
44  std::function<T()> generatingFunc) {
45  int64_t seed = 0;
46  std::hash<TIndex> hashFn;
47  for (const auto num : vec1) {
48  // Copied from boost::hash_combine.
49  // Adding 1 to differentiate between first and second vector.
50  seed ^= hashFn(num) + 0x9e3779b9 + (seed << 6) + (seed >> 2) + 1;
51  }
52 
53  for (const auto num : vec2) {
54  // Copied from boost::hash_combine.
55  seed ^= hashFn(num) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
56  }
57 
58  if (seed == 0) {
59  return generatingFunc();
60  }
61 
62  if (hash_.find(seed) == hash_.end()) {
63  T value = generatingFunc();
64  hash_[seed] = value;
65  }
66 
67  return hash_[seed];
68 }
69 }
70 #endif
Copyright (c) 2016-present, Facebook, Inc.