Skip to content

Commit

Permalink
[OpenMP] Wait for kernel prior to memory deallocation
Browse files Browse the repository at this point in the history
Summary:
In the function `target`, memory deallocation and `target_data_end` is called
immediately returning from launching kernel. This might cause a race condition
that the corresponding memory is still being used by the kernel and a potential
issue that when the kernel starts to execute, its required data have already
been deallocated, especially when multiple kernels running concurrently. Since
nevertheless, we will block the thread issuing the target offloading at the end
of the target, we just move the synchronization ahead a little bit to make sure
the correctness.

Reviewers: jdoerfert

Reviewed By: jdoerfert

Subscribers: yaxunl, guansong, sstefan1, openmp-commits

Tags: #openmp

Differential Revision: https://reviews.llvm.org/D84381
  • Loading branch information
shiltian committed Jul 23, 2020
1 parent c4cf250 commit 9b2832c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions openmp/libomptarget/src/omptarget.cpp
Expand Up @@ -927,6 +927,14 @@ int target(int64_t device_id, void *host_ptr, int32_t arg_num,
return OFFLOAD_FAIL;
}

if (Device.RTL->synchronize) {
rc = Device.RTL->synchronize(device_id, &AsyncInfo);
if (rc != OFFLOAD_SUCCESS) {
DP("Failed to synchronize.\n");
return OFFLOAD_FAIL;
}
}

// Deallocate (first-)private arrays
for (auto it : fpArrays) {
int rt = Device.RTL->data_delete(Device.RTLDeviceID, it);
Expand All @@ -944,8 +952,5 @@ int target(int64_t device_id, void *host_ptr, int32_t arg_num,
return OFFLOAD_FAIL;
}

if (Device.RTL->synchronize)
return Device.RTL->synchronize(device_id, &AsyncInfo);

return OFFLOAD_SUCCESS;
}

0 comments on commit 9b2832c

Please sign in to comment.