Run block-wise sum demonstration. Parameters ---------- num_elements : int Number of array elements device_id : int CUDA device ID Returns ------- bool True if all tests passed
(num_elements: int = 1024 * 1024, device_id: int = 0)
| 110 | |
| 111 | |
| 112 | def run_sample(num_elements: int = 1024 * 1024, device_id: int = 0) -> bool: |
| 113 | """ |
| 114 | Run block-wise sum demonstration. |
| 115 | |
| 116 | Parameters |
| 117 | ---------- |
| 118 | num_elements : int |
| 119 | Number of array elements |
| 120 | device_id : int |
| 121 | CUDA device ID |
| 122 | |
| 123 | Returns |
| 124 | ------- |
| 125 | bool |
| 126 | True if all tests passed |
| 127 | """ |
| 128 | threads_per_block = 256 |
| 129 | num_blocks = 64 |
| 130 | |
| 131 | device = Device(device_id) |
| 132 | device.set_current() |
| 133 | stream = device.create_stream() |
| 134 | |
| 135 | arch = f"sm_{device.arch}" |
| 136 | print(f"Device: {device.name}") |
| 137 | print(f"Compute Capability: {arch}") |
| 138 | print(f"Array size: {num_elements:,} elements\n") |
| 139 | |
| 140 | try: |
| 141 | # Make CuPy use our stream |
| 142 | cp.cuda.Stream.from_external(stream).use() |
| 143 | |
| 144 | # Compile kernels |
| 145 | program = Program( |
| 146 | KERNELS_CODE, code_type="c++", options=ProgramOptions(arch=arch) |
| 147 | ) |
| 148 | module = program.compile(target_type="cubin") |
| 149 | kernel_simple = module.get_kernel("simple_indexing") |
| 150 | kernel_strided = module.get_kernel("strided_loop") |
| 151 | kernel_sum = module.get_kernel("block_partial_sum") |
| 152 | |
| 153 | # Test data |
| 154 | h_input = np.arange(num_elements, dtype=np.float32) |
| 155 | d_input = cp.asarray(h_input) |
| 156 | d_output = cp.zeros_like(d_input) |
| 157 | expected = cp.asarray(h_input * 2.0) |
| 158 | |
| 159 | # Demo 1: Simple indexing (1 thread = 1 element) |
| 160 | full_blocks = (num_elements + threads_per_block - 1) // threads_per_block |
| 161 | config = LaunchConfig(grid=full_blocks, block=threads_per_block) |
| 162 | launch( |
| 163 | stream, |
| 164 | config, |
| 165 | kernel_simple, |
| 166 | d_input.data.ptr, |
| 167 | d_output.data.ptr, |
| 168 | cp.uint64(num_elements), |
| 169 | ) |
no test coverage detected