@@ -75,6 +75,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
7575 return UR_RESULT_SUCCESS;
7676 }
7777
78+ std::scoped_lock<ur_shared_mutex> Guard (Mutex);
7879 auto &Allocation = Allocations[Device];
7980 ur_result_t URes = UR_RESULT_SUCCESS;
8081 if (!Allocation) {
@@ -104,9 +105,16 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
104105 }
105106 }
106107
108+ Handle = Allocation;
109+
110+ if (!LastSyncedDevice.hDevice ) {
111+ LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
112+ return URes;
113+ }
114+
107115 // If the device required to allocate memory is not the previous one, we
108116 // need to do data migration.
109- if (Device != LastSyncedDevice && LastSyncedDevice != nullptr ) {
117+ if (Device != LastSyncedDevice. hDevice ) {
110118 auto &HostAllocation = Allocations[nullptr ];
111119 if (!HostAllocation) {
112120 ur_usm_desc_t USMDesc{};
@@ -125,11 +133,10 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
125133
126134 // Copy data from last synced device to host
127135 {
128- ManagedQueue Queue (Context, LastSyncedDevice);
129- char *Handle;
130- UR_CALL (getHandle (LastSyncedDevice, Handle));
136+ ManagedQueue Queue (Context, LastSyncedDevice.hDevice );
131137 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 );
133140 if (URes != UR_RESULT_SUCCESS) {
134141 getContext ()->logger .error (
135142 " Failed to migrate memory buffer data" );
@@ -151,8 +158,7 @@ ur_result_t MemBuffer::getHandle(ur_device_handle_t Device, char *&Handle) {
151158 }
152159 }
153160
154- LastSyncedDevice = Device;
155- Handle = Allocation;
161+ LastSyncedDevice = MemBuffer::Device_t{Device, Handle};
156162
157163 return URes;
158164}
0 commit comments