Skip to content

Commit 057f970

Browse files
committed
add CUDA multi context test
1 parent d3daaf6 commit 057f970

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

test/providers/cuda_helpers.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -251,15 +251,18 @@ int InitCUDAOps() {
251251
}
252252
#endif // USE_DLOPEN
253253

254-
static CUresult set_context(CUcontext required_ctx, CUcontext *restore_ctx) {
254+
CUresult set_context(CUcontext required_ctx, CUcontext *restore_ctx) {
255255
CUcontext current_ctx = NULL;
256256
CUresult cu_result = libcu_ops.cuCtxGetCurrent(&current_ctx);
257257
if (cu_result != CUDA_SUCCESS) {
258258
fprintf(stderr, "cuCtxGetCurrent() failed.\n");
259259
return cu_result;
260260
}
261261

262-
*restore_ctx = current_ctx;
262+
if (restore_ctx != NULL) {
263+
*restore_ctx = current_ctx;
264+
}
265+
263266
if (current_ctx != required_ctx) {
264267
cu_result = libcu_ops.cuCtxSetCurrent(required_ctx);
265268
if (cu_result != CUDA_SUCCESS) {

test/providers/cuda_helpers.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -30,6 +30,8 @@ int get_cuda_device(CUdevice *device);
3030

3131
int create_context(CUdevice device, CUcontext *context);
3232

33+
CUresult set_context(CUcontext required_ctx, CUcontext *restore_ctx);
34+
3335
int destroy_context(CUcontext context);
3436

3537
int cuda_fill(CUcontext context, CUdevice device, void *ptr, size_t size,

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+
set_context(ctx2, NULL);
364+
// free memory from ctx1
365+
umf_result = umfMemoryProviderFree(provider1, ptr1, size);
366+
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
367+
368+
set_context(ctx1, NULL);
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)