|
9 | 9 | import pytest |
10 | 10 | from conftest import skipif_need_cuda_headers |
11 | 11 |
|
12 | | -from cuda.core.experimental import Device, LaunchConfig, LegacyPinnedMemoryResource, Program, ProgramOptions, launch |
| 12 | +from cuda.core.experimental import ( |
| 13 | + Device, |
| 14 | + DeviceMemoryResource, |
| 15 | + LaunchConfig, |
| 16 | + LegacyPinnedMemoryResource, |
| 17 | + Program, |
| 18 | + ProgramOptions, |
| 19 | + launch, |
| 20 | +) |
13 | 21 |
|
14 | 22 |
|
15 | 23 | def test_launch_config_init(init_cuda): |
@@ -197,3 +205,99 @@ def test_cooperative_launch(): |
197 | 205 | config = LaunchConfig(grid=1, block=1, cooperative_launch=True) |
198 | 206 | launch(s, config, ker) |
199 | 207 | s.sync() |
| 208 | + |
| 209 | + |
| 210 | +@pytest.mark.parametrize( |
| 211 | + "memory_resource_class", |
| 212 | + [ |
| 213 | + DeviceMemoryResource, |
| 214 | + LegacyPinnedMemoryResource, |
| 215 | + ], |
| 216 | +) |
| 217 | +def test_launch_with_buffers_allocated_by_memory_resource(init_cuda, memory_resource_class): |
| 218 | + """Test that kernels can access memory allocated by memory resources.""" |
| 219 | + dev = Device() |
| 220 | + dev.set_current() |
| 221 | + stream = dev.create_stream() |
| 222 | + |
| 223 | + # Kernel that operates on memory |
| 224 | + code = """ |
| 225 | + extern "C" |
| 226 | + __global__ void memory_ops(float* data, size_t N) { |
| 227 | + const unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x; |
| 228 | + if (tid < N) { |
| 229 | + // Access memory (device or pinned) |
| 230 | + data[tid] = data[tid] * 3.0f; |
| 231 | + } |
| 232 | + } |
| 233 | + """ |
| 234 | + |
| 235 | + # Compile kernel |
| 236 | + arch = "".join(f"{i}" for i in dev.compute_capability) |
| 237 | + program_options = ProgramOptions(std="c++17", arch=f"sm_{arch}") |
| 238 | + prog = Program(code, code_type="c++", options=program_options) |
| 239 | + mod = prog.compile("cubin") |
| 240 | + kernel = mod.get_kernel("memory_ops") |
| 241 | + |
| 242 | + # Create memory resource |
| 243 | + if memory_resource_class == DeviceMemoryResource: |
| 244 | + mr = memory_resource_class(dev.device_id) |
| 245 | + else: # LegacyPinnedMemoryResource |
| 246 | + mr = memory_resource_class() |
| 247 | + |
| 248 | + # Allocate memory |
| 249 | + size = 1024 |
| 250 | + dtype = np.float32 |
| 251 | + element_size = dtype().itemsize |
| 252 | + total_size = size * element_size |
| 253 | + |
| 254 | + buffer = mr.allocate(total_size, stream=stream) |
| 255 | + |
| 256 | + # Create array view based on memory type |
| 257 | + if mr.is_host_accessible: |
| 258 | + # For pinned memory, use numpy |
| 259 | + array = np.from_dlpack(buffer).view(dtype=dtype) |
| 260 | + else: |
| 261 | + # For device memory, use cupy |
| 262 | + import cupy as cp |
| 263 | + |
| 264 | + array = cp.from_dlpack(buffer).view(dtype=dtype) |
| 265 | + |
| 266 | + # Initialize data with random values |
| 267 | + if mr.is_host_accessible: |
| 268 | + rng = np.random.default_rng() |
| 269 | + array[:] = rng.random(size, dtype=dtype) |
| 270 | + else: |
| 271 | + import cupy as cp |
| 272 | + |
| 273 | + rng = cp.random.default_rng() |
| 274 | + array[:] = rng.random(size, dtype=dtype) |
| 275 | + |
| 276 | + # Store original values for verification |
| 277 | + original = array.copy() |
| 278 | + |
| 279 | + # Sync before kernel launch |
| 280 | + stream.sync() |
| 281 | + |
| 282 | + # Launch kernel |
| 283 | + block = 256 |
| 284 | + grid = (size + block - 1) // block |
| 285 | + config = LaunchConfig(grid=grid, block=block) |
| 286 | + |
| 287 | + launch(stream, config, kernel, buffer, np.uint64(size)) |
| 288 | + stream.sync() |
| 289 | + |
| 290 | + # Verify kernel operations |
| 291 | + if mr.is_host_accessible: |
| 292 | + assert np.allclose(array, original * 3.0), f"{memory_resource_class.__name__} operation failed" |
| 293 | + else: |
| 294 | + import cupy as cp |
| 295 | + |
| 296 | + assert cp.allclose(array, original * 3.0), f"{memory_resource_class.__name__} operation failed" |
| 297 | + |
| 298 | + # Clean up |
| 299 | + buffer.close(stream) |
| 300 | + stream.close() |
| 301 | + |
| 302 | + # Verify buffer is properly closed |
| 303 | + assert buffer.handle == 0, f"{memory_resource_class.__name__} buffer should be closed" |
0 commit comments