# CUDA 错误：无效的程序计数器 (cudaErrorInvalidPc)

- **ID:** `cuda/cuda-error-invalid-pc`
- **领域:** cuda
- **类别:** runtime_error
- **错误码:** `cudaErrorInvalidPc (805)`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

GPU 尝试执行具有无效程序计数器的内核，通常是由于损坏的设备函数指针、编译错误的内核或设备代码中的越界跳转（例如，误用函数指针或间接调用）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CUDA 12.4 | active | — | — |
| CUDA 12.6 | active | — | — |
| NVIDIA Driver 550.54.10 | active | — | — |
| NVIDIA Driver 560.35.03 | active | — | — |
| PyTorch 2.5.0 | active | — | — |

## 解决方案

1. ```
   Compile the kernel with `-lineinfo` and run with `cuda-memcheck` or `compute-sanitizer` to identify the exact source line causing the invalid jump: `compute-sanitizer --tool memcheck ./my_app`
   ```
2. ```
   Avoid using function pointers in device code if possible; replace them with switch statements or templates to eliminate indirect jumps.
   ```
3. ```
   Ensure all device function pointers are initialized correctly and not left as null or garbage. For example, in CUDA C++: `typedef void (*func_t)(); func_t f = &my_device_func;`
   ```

## 无效尝试

- **** — The error occurs during kernel execution, not during API calls; post-hoc checks do not prevent the invalid program counter from being reached. (80% 失败率)
- **** — Scheduling policy does not affect kernel correctness; the invalid PC is a code bug, not a synchronization issue. (90% 失败率)
