-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.h
169 lines (143 loc) · 4.45 KB
/
cache.h
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <hip/hip_runtime_api.h>
#include <list>
#include <unordered_map>
#include <functional>
//#include "llvm/ADT/DenseMap.h"
template <
class Key,
class Value,
class Hash,
class KeyEqual,
class Allocator
>
class IpcHandleCache
{
typedef std::unordered_map<Key, std::pair<Value, typename std::list<Key>::iterator>, Hash, KeyEqual, Allocator> LRUCache;
public:
using iterator = typename LRUCache::iterator;
IpcHandleCache(size_t size,
size_t bucket_count = 100,
const Hash& hash = Hash(),
const KeyEqual& eql = KeyEqual(),
const Allocator& alloc = Allocator() ) : cache(bucket_count, hash, eql, alloc)
{
capacity = size;
}
~IpcHandleCache()
{
lruHistory.clear();
cache.clear();
}
iterator begin()
{
return cache.begin();
}
iterator end()
{
return cache.end();
}
iterator find(const Key& key)
{
iterator it = cache.find(key);
if (it != cache.end())
{
updateHistory(key);
}
return it;
}
std::pair<iterator, bool> insert(const Key& key, const Value& value)
{
if (cache.size() == capacity)
{
// remove entry
pop();
}
typename LRUCache::iterator it = cache.find(key);
bool inserted;
if (it == cache.end())
{
typename std::list<Key>::iterator it = lruHistory.insert(lruHistory.end(), key);
cache.insert(std::make_pair(key, std::make_pair(value, it)));
inserted = true;
}
else
{
inserted = false;
}
return std::pair<iterator, bool>(it, inserted);
}
private:
void pop()
{
typename LRUCache::iterator it = cache.find(lruHistory.front());
cache.erase(it);
lruHistory.pop_front();
}
void updateHistory(const Key& key)
{
if (lruHistory.size() > 0)
{
lruHistory.splice(lruHistory.end(), lruHistory, cache[key].second);
}
}
size_t capacity;
std::list<Key> lruHistory;
LRUCache cache;
};
// djb2 hash function for hashing char array in hipIpcMemHandle_t
unsigned long HandleHash(const hipIpcMemHandle_t& handle)
{
const char* str = handle.reserved;
unsigned long hash = 5381;
int c;
while ((c = *(str)++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
// equality function required for unordered_map
auto HandleEqual = [](const hipIpcMemHandle_t& l, const hipIpcMemHandle_t& r)
{
return memcmp(l.reserved, r.reserved, sizeof(l.reserved)) == 0;
};
//typedef llvm::DenseMap<uint64_t, hipIpcMemHandle_t> SendCache;
//typedef llvm::DenseMap<hipIpcMemHandle_t, void*, decltype(&HandleHash), decltype(HandleEqual)> RecvCache;
typedef IpcHandleCache<uint64_t, hipIpcMemHandle_t, std::hash<uint64_t>, std::equal_to<uint64_t>, std::allocator< std::pair<const uint64_t, hipIpcMemHandle_t>>> SendCache;
typedef IpcHandleCache<hipIpcMemHandle_t, void*, decltype(&HandleHash), decltype(HandleEqual), std::allocator< std::pair<const uint64_t, hipIpcMemHandle_t>>> RecvCache;
//typedef std::unordered_map<uint64_t, hipIpcMemHandle_t> SendCache;
//typedef std::unordered_map<hipIpcMemHandle_t, void*, decltype(&HandleHash), decltype(HandleEqual)> RecvCache;
hipIpcMemHandle_t CheckCacheForPtr(void* devPtr, SendCache& cache, int rank)
{
hipIpcMemHandle_t handle;
uint64_t addr = (uint64_t)devPtr;
//std::cout << rank << " finding" << std::endl;
SendCache::iterator it = cache.find(addr);
//std::cout << rank << " done finding" << std::endl;
if (it == cache.end())
{
hipIpcGetMemHandle(&handle, devPtr);
std::pair<uint64_t, hipIpcMemHandle_t> ptrHandleMap(addr, handle) ;
cache.insert(addr, handle);
//std::cout << rank << " done inserting" << std::endl;
}
else
{
handle = (it->second).first;
}
return handle;
}
void* CheckCacheForHandle(hipIpcMemHandle_t handle, RecvCache& cache)
{
void* ptr = nullptr;
RecvCache::iterator it = cache.find(handle);
if (it == cache.end())
{
hipIpcOpenMemHandle((void**)&ptr, handle, hipIpcMemLazyEnablePeerAccess);
//std::pair<uint64_t, void*> handlePtrMap(target, ptr);
cache.insert(handle, ptr);
}
else
{
ptr = (it->second).first;
}
return ptr;
}