File tree Expand file tree Collapse file tree 2 files changed +23
-9
lines changed
source/adapters/level_zero Expand file tree Collapse file tree 2 files changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,9 @@ struct ur_context_handle_t_ : _ur_object {
100100 l0_command_list_cache_info>>>
101101 ZeCopyCommandListCache;
102102
103+ std::unordered_map<ur_device_handle_t , std::list<ur_device_handle_t >>
104+ P2PDeviceCache;
105+
103106 // Store USM pool for USM shared and device allocations. There is 1 memory
104107 // pool per each pair of (context, device) per each memory type.
105108 std::unordered_map<ze_device_handle_t , umf::pool_unique_handle_t >
Original file line number Diff line number Diff line change @@ -153,15 +153,26 @@ static ur_result_t USMAllocationMakeResident(
153153 } else {
154154 Devices.push_back (Device);
155155 if (ForceResidency == USMAllocationForceResidencyType::P2PDevices) {
156- ze_bool_t P2P;
157- for (const auto &D : Context->Devices ) {
158- if (D == Device)
159- continue ;
160- // TODO: Cache P2P devices for a context
161- ZE2UR_CALL (zeDeviceCanAccessPeer,
162- (D->ZeDevice , Device->ZeDevice , &P2P));
163- if (P2P)
164- Devices.push_back (D);
156+ // Check if the P2P devices are already cached
157+ auto it = Context->P2PDeviceCache .find (Device);
158+ if (it != Context->P2PDeviceCache .end ()) {
159+ // Use cached P2P devices
160+ Devices.insert (Devices.end (), it->second .begin (), it->second .end ());
161+ } else {
162+ // Query for P2P devices and update the cache
163+ std::list<ur_device_handle_t > P2PDevices;
164+ ze_bool_t P2P;
165+ for (const auto &D : Context->Devices ) {
166+ if (D == Device)
167+ continue ;
168+ ZE2UR_CALL (zeDeviceCanAccessPeer,
169+ (D->ZeDevice , Device->ZeDevice , &P2P));
170+ if (P2P)
171+ P2PDevices.push_back (D);
172+ }
173+ // Update the cache
174+ Context->P2PDeviceCache [Device] = P2PDevices;
175+ Devices.insert (Devices.end (), P2PDevices.begin (), P2PDevices.end ());
165176 }
166177 }
167178 }
You can’t perform that action at this time.
0 commit comments