Skip to content

Dev: cpu support for c extension - #34

Merged
hzhangxyz merged 1 commit into
mainfrom
dev/cpu-support-for-c-extension
Jun 16, 2025
Merged

Dev: cpu support for c extension#34
hzhangxyz merged 1 commit into
mainfrom
dev/cpu-support-for-c-extension

Conversation

@windy-pig

Copy link
Copy Markdown
Collaborator

Description

修改 _hamiltonian_cuda.cu,将gpu的代码转化为cpu的,从而提供对CPU运行的支持。

Checklist:

@windy-pig
windy-pig force-pushed the dev/cpu-support-for-c-extension branch from 05e9a13 to a86cefc Compare June 12, 2025 15:42
@hzhangxyz
hzhangxyz requested a review from Copilot June 13, 2025 03:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds CPU support for the C extension by including a CPU implementation and adjusting build flags

  • Introduce _hamiltonian_cpu.cpp to the source list for CPU execution
  • Modify extra_cflags to use debug flags (-g, -O0) instead of optimization
Comments suppressed due to low confidence (1)

qmb/hamiltonian.py:34

  • Switching from -O3 to -O0 disables optimizations and adds debug info, which will significantly degrade performance. Consider restoring -O3 or making optimization levels configurable per build type.
extra_cflags=["-g", "-O0", "-ffast-math", "-march=native", f"-DN_QUBYTES={n_qubytes}", f"-DPARTICLE_CUT={particle_cut}"],

Comment thread qmb/hamiltonian.py
name=name,
sources=[
f"{folder}/_hamiltonian.cpp",
f"{folder}/_hamiltonian_cpu.cpp",

Copilot AI Jun 13, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The CPU source file is always included in the build. To avoid unnecessary compilation overhead, consider making _hamiltonian_cpu.cpp conditional—e.g., based on a CPU/GPU flag or environment variable.

Suggested change
f"{folder}/_hamiltonian_cpu.cpp",
*(f"{folder}/_hamiltonian_cpu.cpp",) if os.getenv("USE_CPU", "True") == "True" else (),

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't fix

@hzhangxyz hzhangxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

意见见上面的具体评论,同时参考分支: dev/cpu-support-for-c-extension-comment-by-hzhangxyz 。

Comment thread qmb/hamiltonian.py
name=name,
sources=[
f"{folder}/_hamiltonian.cpp",
f"{folder}/_hamiltonian_cpu.cpp",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't fix

Comment thread qmb/hamiltonian.py
f"{folder}/_hamiltonian_cuda.cu",
],
is_python_module=n_qubytes == 0,
extra_cflags=["-O3", "-ffast-math", "-march=native", f"-DN_QUBYTES={n_qubytes}", f"-DPARTICLE_CUT={particle_cut}"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要在运行的代码里开-g

Comment thread qmb/_hamiltonian_cpu.cpp Outdated
return ((*data) >> index) & 1;
}

bool set_bit(std::uint8_t* data, std::uint8_t index, bool value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里我之前写错了,不影响运行,但是会有warning,这里顺便修改一下吧,see #37

Comment thread qmb/_hamiltonian_cpu.cpp
auto sorted_result_psi = torch::zeros({result_batch_size, 2}, torch::TensorOptions().dtype(torch::kFloat64).device(device, device_id));

thrust::sort_by_key(
reinterpret_cast<std::array<std::uint8_t, n_qubytes>*>(sorted_result_configs.data_ptr()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以用 thrust::host 作为policy。

Comment thread qmb/_hamiltonian_cpu.cpp Outdated
std::int64_t batch_index;

for(term_index = 0; term_index < term_number; term_index++){
for(batch_index = 0; term_index < term_number; batch_index++){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里写错了

Comment thread qmb/_hamiltonian_cpu.cpp
@@ -0,0 +1,595 @@
#include <thrust/sort.h>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有空可以尝试学着使用precommit进行格式检查。

@hzhangxyz

Copy link
Copy Markdown
Member

参考 https://github.com/USTC-KnowledgeComputingLab/qmb/tree/dev/cpu-support-for-c-extension-comment-by-hzhangxyz 分支,照着这个分支的改动做就可以了,由于你的中间commit比较混乱,请squash成一个commit。

实现CPU版torch kernel这件事结束了,后续你可以做的优化:

  1. python方的条件编译(在cpu上跑的时候不编译cuda的扩展)
  2. 由于mutex,add_into_heap函数中,存在多次判断,你需要去重。

@windy-pig
windy-pig force-pushed the dev/cpu-support-for-c-extension branch from f7f7786 to a1c6002 Compare June 16, 2025 13:31
Modified `_hamiltonian_cuda.cu` to support CPU as `_hamiltonian_cpu.cpp`.
The changes have been registered in `hamiltonian.py`.
@hzhangxyz
hzhangxyz force-pushed the dev/cpu-support-for-c-extension branch from a1c6002 to 0ffbc5e Compare June 16, 2025 14:37
@hzhangxyz
hzhangxyz merged commit 6f670c5 into main Jun 16, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants