@@ -75,6 +75,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
75
75
return UR_RESULT_SUCCESS;
76
76
}
77
77
78
+ std::scoped_lock<ur_shared_mutex> Guard (Mutex);
78
79
auto &Allocation = Allocations[Device];
79
80
ur_result_t URes = UR_RESULT_SUCCESS;
80
81
if (!Allocation) {
@@ -104,9 +105,16 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
104
105
}
105
106
}
106
107
108
+ Handle = Allocation;
109
+
110
+ if (!LastSyncedDevice.hDevice ) {
111
+ LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
112
+ return URes;
113
+ }
114
+
107
115
// If the device required to allocate memory is not the previous one, we
108
116
// need to do data migration.
109
- if (Device != LastSyncedDevice && LastSyncedDevice != nullptr ) {
117
+ if (Device != LastSyncedDevice. hDevice ) {
110
118
auto &HostAllocation = Allocations[nullptr ];
111
119
if (!HostAllocation) {
112
120
ur_usm_desc_t USMDesc{};
@@ -125,11 +133,10 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
125
133
126
134
// Copy data from last synced device to host
127
135
{
128
- ManagedQueue Queue (Context, LastSyncedDevice);
129
- char *Handle;
130
- UR_CALL (getHandle (LastSyncedDevice, Handle));
136
+ ManagedQueue Queue (Context, LastSyncedDevice.hDevice );
131
137
URes = getContext ()->urDdiTable .Enqueue .pfnUSMMemcpy (
132
- Queue, true , HostAllocation, Handle, Size, 0 , nullptr , nullptr );
138
+ Queue, true , HostAllocation, LastSyncedDevice.MemHandle , Size,
139
+ 0 , nullptr , nullptr );
133
140
if (URes != UR_RESULT_SUCCESS) {
134
141
getContext ()->logger .error (
135
142
" Failed to migrate memory buffer data" );
@@ -151,8 +158,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
151
158
}
152
159
}
153
160
154
- LastSyncedDevice = Device;
155
- Handle = Allocation;
161
+ LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
156
162
157
163
return URes;
158
164
}
0 commit comments