Skip to content
Merged
Changes from 1 commit
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
Next Next commit
test(cython): use a capped mempool in test_interop_memPool
The default mempool reserves ~2x device memory of virtual address space
on first use, which cannot be satisfied in a 39-bit address space
(riscv64 Sv39) and made the test flaky. Create a 2 MiB pool instead and
set it before querying it, since the pool getters also create the
default pool.
  • Loading branch information
juenglin committed Oct 2, 2026
commit a8c7196999ce1125e21a71032ca762b52fcb5925
38 changes: 29 additions & 9 deletions cuda_bindings/tests/cython/test_interoperability_cython.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# distutils: language=c++
from libc.stdlib cimport calloc, free
from libc.string cimport memset
import cuda.bindings.driver as cuda
import cuda.bindings.runtime as cudart
import numpy as np
Expand All @@ -11,6 +12,13 @@ import pytest
cimport cuda.bindings.cydriver as ccuda
cimport cuda.bindings.cyruntime as ccudart

# Cap for the memory pool created by test_interop_memPool. Touching the device's
# default pool (e.g. via cuDeviceGetDefaultMemPool, cudaDeviceGetDefaultMemPool,
# or cuDeviceGetMemPool / cudaDeviceGetMemPool before a pool has been set)
# reserves virtual address space of about twice the device memory, which
# cannot be satisfied in a 39-bit address space (e.g. riscv64 Sv39).
POOL_SIZE = 2 * 1024 * 1024 # 2 MiB


def supportsMemoryPool():
err, isSupported = cudart.cudaDeviceGetAttribute(cudart.cudaDeviceAttr.cudaDevAttrMemoryPoolsSupported, 0)
Expand Down Expand Up @@ -147,22 +155,34 @@ def test_interop_memPool():
err_dr, ctx = cuda.cuCtxCreate(None, 0, device)
assert(err_dr == cuda.CUresult.CUDA_SUCCESS)

# DRV to RT
cdef ccuda.CUmemoryPool* mempool_dr = <ccuda.CUmemoryPool*>calloc(1, sizeof(ccuda.CUmemoryPool))
cerr_dr = ccuda.cuDeviceGetDefaultMemPool(mempool_dr, 0)
# Use a small, capped pool instead of the device's default pool. The
# pool must be set before it is queried below because the getters create
# (and reserve address space for) the default pool if none has been set.
cdef ccuda.CUmemPoolProps props
memset(&props, 0, sizeof(props))
props.allocType = ccuda.CU_MEM_ALLOCATION_TYPE_PINNED
props.handleTypes = ccuda.CU_MEM_HANDLE_TYPE_NONE
props.location.type = ccuda.CU_MEM_LOCATION_TYPE_DEVICE
props.location.id = 0
props.maxSize = POOL_SIZE
cdef ccuda.CUmemoryPool pool
cerr_dr = ccuda.cuMemPoolCreate(&pool, &props)
assert(cerr_dr == ccuda.CUDA_SUCCESS)
cerr_rt = ccudart.cudaDeviceSetMemPool(0, mempool_dr[0])

# DRV to RT
cerr_rt = ccudart.cudaDeviceSetMemPool(0, pool)
assert(cerr_rt == ccudart.cudaSuccess)

# RT to DRV
cdef ccudart.cudaMemPool_t* mempool_rt = <ccudart.cudaMemPool_t*>calloc(1, sizeof(ccudart.cudaMemPool_t))
cerr_rt = ccudart.cudaDeviceGetDefaultMemPool(mempool_rt, 0)
cdef ccudart.cudaMemPool_t mempool_rt
cerr_rt = ccudart.cudaDeviceGetMemPool(&mempool_rt, 0)
assert(cerr_rt == ccudart.cudaSuccess)
cerr_dr = ccuda.cuDeviceSetMemPool(cuda.CUdevice(0), mempool_rt[0])
assert(<void*>mempool_rt == <void*>pool)
cerr_dr = ccuda.cuDeviceSetMemPool(cuda.CUdevice(0), mempool_rt)
assert(cerr_dr == ccuda.CUDA_SUCCESS)

free(mempool_dr)
free(mempool_rt)
cerr_dr = ccuda.cuMemPoolDestroy(pool)
assert(cerr_dr == ccuda.CUDA_SUCCESS)

err_dr, = cuda.cuCtxDestroy(ctx)
assert(err_dr == cuda.CUresult.CUDA_SUCCESS)
Expand Down
Loading