Skip to content

Commit bf513b4

Browse files
CopilotBinyang2014
andauthored
[WIP] Address feedback on handle cache implementation (#700)
- [x] Move hash specialization and equality operator from std/global namespace to custom namespace - [x] Update unordered_map to use custom hash and equality as template parameters - [x] Add noexcept to equality operator - [x] Verify the changes build correctly - [x] Run code review and security checks <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/microsoft/mscclpp/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Binyang2014 <9415966+Binyang2014@users.noreply.github.com> Co-authored-by: Binyang Li <binyli@microsoft.com>
1 parent 4ebe37e commit bf513b4

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/registered_memory.cc

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,22 @@
2929
} \
3030
} while (false)
3131

32-
namespace std {
33-
template <>
34-
struct hash<cudaIpcMemHandle_t> {
32+
namespace {
33+
34+
// Custom hash and equality for cudaIpcMemHandle_t
35+
struct CudaIpcMemHandleHash {
3536
size_t operator()(const cudaIpcMemHandle_t& handle) const {
3637
std::string_view view(handle.reserved, sizeof(handle.reserved));
3738
return std::hash<std::string_view>{}(view);
3839
}
3940
};
40-
} // namespace std
4141

42-
inline bool operator==(const cudaIpcMemHandle_t& lhs, const cudaIpcMemHandle_t& rhs) {
43-
return std::memcmp(lhs.reserved, rhs.reserved, sizeof(lhs.reserved)) == 0;
44-
}
42+
struct CudaIpcMemHandleEqual {
43+
bool operator()(const cudaIpcMemHandle_t& lhs, const cudaIpcMemHandle_t& rhs) const noexcept {
44+
return std::memcmp(lhs.reserved, rhs.reserved, sizeof(lhs.reserved)) == 0;
45+
}
46+
};
4547

46-
namespace {
4748

4849
CUmemAllocationHandleType getNvlsMemHandleType() {
4950
#if (CUDA_NVLS_API_AVAILABLE)
@@ -68,7 +69,8 @@ std::shared_ptr<void> getPeerMemoryHandle(cudaIpcMemHandle_t ipcHandle) {
6869
}
6970
};
7071
#if defined(__HIP_PLATFORM_AMD__)
71-
static std::unordered_map<cudaIpcMemHandle_t, std::weak_ptr<void>> peerMemoryHandleMap;
72+
static std::unordered_map<cudaIpcMemHandle_t, std::weak_ptr<void>, CudaIpcMemHandleHash, CudaIpcMemHandleEqual>
73+
peerMemoryHandleMap;
7274
static std::mutex mutex;
7375
std::lock_guard<std::mutex> lock(mutex);
7476
auto it = peerMemoryHandleMap.find(ipcHandle);

0 commit comments

Comments
 (0)