cuStateVecEx backend changes the current CUDA device without restoring it

未关闭
#1,145 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
56/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp

调研方向

Read lib/vectorspace_custatevecex.h and lib/simulator_custatevecex.h, focusing on the listed cudaSetDevice calls in Vector::assign, Vector::reduce, and ExpectationValue. Check how the scoped guard in pybind_interface/pybind_main.cpp handles device restoration, then reproduce on a machine with at least two visible GPUs using the supplied Python snippet. Done means the caller's current CUDA device is unchanged after these operations, including the multi GPU paths.

由索引模型根据 Issue 内容生成。

描述

Describe the issue

qsim's cuStateVecEx code calls cudaSetDevice in nine places and never saves or restores the caller's current device. There is no cudaGetDevice anywhere in lib/, so whichever GPU was selected last stays selected after the call returns.

  • lib/vectorspace_custatevecex.h:
  • lib/simulator_custatevecex.h: the callback in ExpectationValue, L203

When the switch happens.

  • The eight calls in vectorspace_custatevecex.h only run when the state is split across several GPUs or MPI processes. With a single sub-state, the helpers call the callback without switching (L206, L253, L295, L346).
  • The call in ExpectationValue runs every time, even on a single GPU.

Default options trigger it on any machine with two or more visible GPUs. Parameter::num_devices defaults to 0 (L64), so for all but the smallest circuits Create splits the state across the visible GPUs (L412, L443-L455). A plain gpu_mode=2 simulation then runs init_state → SetAllZeros → Vector::assign (pybind_main.cpp L633-L637, statespace_custatevecex.h L86-L97) and returns with the last GPU selected.

Why it matters. Each thread has a current CUDA device that Python code and other libraries rely on. After a gpu_mode=2 simulation, anything that uses the default device silently runs on a different GPU. That includes CuPy, PyTorch, and qsim's own gpu_mode=0/1 backends, which allocate on the current device.

While testing #1100 on 2× A100, before that PR added a workaround, this became a crash. Reading a gpu_mode=0 state that lives on device 0 launched its reorder kernel on device 1:

[FAIL] mode 0: a cuStateVecEx simulation leaves the current device unchanged -- current device 0 -> 1
CUDA error: an illegal memory access was encountered at .../lib/statespace_cuda.h 94

Possible fix. Save the current device on entry and restore it on exit in the four Vector::assign/reduce helpers and in SimulatorCuStateVecEx::ExpectationValue. For example, with a small RAII helper (sketch):

struct CudaDeviceRestorer {
  int device = -1;
  CudaDeviceRestorer() { if (cudaGetDevice(&device) != cudaSuccess) device = -1; }
  ~CudaDeviceRestorer() { if (device >= 0) cudaSetDevice(device); }
};

#1100 works around this in the Python bindings with a scoped guard (ScopedCudaDevice in pybind_interface/pybind_main.cpp). Code that uses lib/ directly is still affected.

Tell us how to reproduce the issue

You need a machine with at least two visible GPUs and qsimcirq built with cuQuantum, so that gpu_mode=2 is available.

import cirq
import cupy as cp
import qsimcirq

qubits = cirq.LineQubit.range(5)
circuit = cirq.Circuit(cirq.H.on_each(*qubits))

before = cp.cuda.runtime.getDevice()
qsimcirq.QSimSimulator(qsimcirq.QSimOptions(use_gpu=True, gpu_mode=2)).simulate(circuit)
after = cp.cuda.runtime.getDevice()
print(before, "->", after)

Expected: 0 -> 0. Observed on 2× A100 in an equivalent test: 0 -> 1.

With only one visible GPU there is nothing to switch to, so this only shows up on multi-GPU machines.

Tell us the version of qsim or qsimcirq (if relevant)

main @ 50cac4b (qsimcirq 0.23.0.dev0); the cudaSetDevice calls were added with cuStateVecEx support in #965

Tell us the computing environment (if relevant)

Linux (Ubuntu 22.04 container), 2x NVIDIA A100, CUDA 12.4.1, cuStateVec 1.11.0 (custatevec-cu12), Python 3.10

主要语言
C++
星标
702
派生
219
平均合并
1 天 2 小时
30 天内合并 PR
8

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

quantumlib/qsim 的其他 Issue

查看 quantumlib/qsim 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。