Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix free-before-alloc in multithreaded test #992

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions tests/mr/device/mr_multithreaded_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ void allocate_loop(rmm::mr::device_memory_resource* mr,
std::size_t num_allocations,
std::list<allocation>& allocations,
std::mutex& mtx,
cudaEvent_t& event,
rmm::cuda_stream_view stream)
{
constexpr std::size_t max_size{1_MiB};
Expand All @@ -191,6 +192,7 @@ void allocate_loop(rmm::mr::device_memory_resource* mr,
void* ptr = mr->allocate(size, stream);
{
std::lock_guard<std::mutex> lock(mtx);
RMM_CUDA_TRY(cudaEventRecord(event, stream.value()));
allocations.emplace_back(ptr, size);
}
}
Expand All @@ -200,12 +202,14 @@ void deallocate_loop(rmm::mr::device_memory_resource* mr,
std::size_t num_allocations,
std::list<allocation>& allocations,
std::mutex& mtx,
cudaEvent_t& event,
rmm::cuda_stream_view stream)
{
for (std::size_t i = 0; i < num_allocations;) {
std::lock_guard<std::mutex> lock(mtx);
if (allocations.empty()) { continue; }
i++;
RMM_CUDA_TRY(cudaStreamWaitEvent(stream.value(), event));
allocation alloc = allocations.front();
allocations.pop_front();
mr->deallocate(alloc.ptr, alloc.size, stream);
Expand All @@ -220,15 +224,30 @@ void test_allocate_free_different_threads(rmm::mr::device_memory_resource* mr,

std::mutex mtx;
std::list<allocation> allocations;

std::thread producer(
allocate_loop, mr, num_allocations, std::ref(allocations), std::ref(mtx), streamA);

std::thread consumer(
deallocate_loop, mr, num_allocations, std::ref(allocations), std::ref(mtx), streamB);
cudaEvent_t event;

RMM_CUDA_TRY(cudaEventCreate(&event));

std::thread producer(allocate_loop,
mr,
num_allocations,
std::ref(allocations),
std::ref(mtx),
std::ref(event),
streamA);

std::thread consumer(deallocate_loop,
mr,
num_allocations,
std::ref(allocations),
std::ref(mtx),
std::ref(event),
streamB);

producer.join();
consumer.join();

RMM_CUDA_TRY(cudaEventDestroy(event));
}

TEST_P(mr_test_mt, AllocFreeDifferentThreadsDefaultStream)
Expand Down