Skip to content

Commit 9f5df5e

Browse files
committed
add CUDA multi context test
1 parent 76e4f12 commit 9f5df5e

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

test/providers/provider_cuda.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Intel Corporation
1+
// Copyright (C) 2024-2025 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

@@ -315,6 +315,69 @@ TEST_P(umfCUDAProviderTest, cudaProviderNullParams) {
315315
EXPECT_EQ(res, UMF_RESULT_ERROR_INVALID_ARGUMENT);
316316
}
317317

318+
TEST_P(umfCUDAProviderTest, multiContext) {
319+
CUdevice device;
320+
int ret = get_cuda_device(&device);
321+
ASSERT_EQ(ret, 0);
322+
323+
// create two CUDA contexts and two providers
324+
CUcontext ctx1, ctx2;
325+
ret = create_context(device, &ctx1);
326+
ASSERT_EQ(ret, 0);
327+
ret = create_context(device, &ctx2);
328+
ASSERT_EQ(ret, 0);
329+
330+
cuda_params_unique_handle_t params1 =
331+
create_cuda_prov_params(ctx1, device, UMF_MEMORY_TYPE_HOST);
332+
ASSERT_NE(params1, nullptr);
333+
umf_memory_provider_handle_t provider1;
334+
umf_result_t umf_result = umfMemoryProviderCreate(
335+
umfCUDAMemoryProviderOps(), params1.get(), &provider1);
336+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
337+
ASSERT_NE(provider1, nullptr);
338+
339+
cuda_params_unique_handle_t params2 =
340+
create_cuda_prov_params(ctx2, device, UMF_MEMORY_TYPE_HOST);
341+
ASSERT_NE(params2, nullptr);
342+
umf_memory_provider_handle_t provider2;
343+
umf_result = umfMemoryProviderCreate(umfCUDAMemoryProviderOps(),
344+
params2.get(), &provider2);
345+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
346+
ASSERT_NE(provider2, nullptr);
347+
348+
// use the providers
349+
// allocate from 1, then from 2, then free 1, then free 2
350+
void *ptr1, *ptr2;
351+
const int size = 128;
352+
// NOTE: we use ctx1 here
353+
umf_result = umfMemoryProviderAlloc(provider1, size, 0, &ptr1);
354+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
355+
ASSERT_NE(ptr1, nullptr);
356+
357+
// NOTE: we use ctx2 here
358+
umf_result = umfMemoryProviderAlloc(provider2, size, 0, &ptr2);
359+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
360+
ASSERT_NE(ptr2, nullptr);
361+
362+
// even if we change the context, we should be able to free the memory
363+
cuCtxSetCurrent(ctx2);
364+
// free memory from ctx1
365+
umf_result = umfMemoryProviderFree(provider1, ptr1, size);
366+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
367+
368+
cuCtxSetCurrent(ctx1);
369+
umf_result = umfMemoryProviderFree(provider2, ptr2, size);
370+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
371+
372+
// cleanup
373+
umfMemoryProviderDestroy(provider2);
374+
umfMemoryProviderDestroy(provider1);
375+
ret = destroy_context(ctx1);
376+
ASSERT_EQ(ret, 0);
377+
ret = destroy_context(ctx2);
378+
ASSERT_EQ(ret, 0);
379+
}
380+
318381
// TODO add tests that mixes CUDA Memory Provider and Disjoint Pool
319382

320383
CUDATestHelper cudaTestHelper;

0 commit comments

Comments
 (0)