Skip to content

Commit 187dc4b

Browse files
authored
feat: Make qdp-kernels build/link on Linux without nvcc by providing stub symbols (#887)
* Make qdp-kernels build/link on Linux without nvcc by providing stub symbolsy * fix errors * fix unsafe error * fix errors * fix thr test error * fix errors * fix error * fix stupid error * try to figure out this stupid error!
1 parent d052d4e commit 187dc4b

File tree

12 files changed

+90
-23
lines changed

12 files changed

+90
-23
lines changed

qdp/qdp-core/src/encoding/amplitude.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
//! Amplitude encoding implementation.
1818
19+
// Allow unused_unsafe: qdp_kernels functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
20+
// The compiler can't statically determine which path is taken.
21+
#![allow(unused_unsafe)]
22+
1923
use std::ffi::c_void;
2024

2125
use cudarc::driver::{CudaSlice, DevicePtrMut};

qdp/qdp-core/src/encoding/angle.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
//! Angle encoding implementation.
1818
19+
// Allow unused_unsafe: qdp_kernels functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
20+
// The compiler can't statically determine which path is taken.
21+
#![allow(unused_unsafe)]
22+
1923
use std::ffi::c_void;
2024

2125
use qdp_kernels::launch_angle_encode_batch;
@@ -103,22 +107,22 @@ impl ChunkEncoder for AngleEncoder {
103107
}
104108
}
105109

106-
unsafe {
107-
crate::profile_scope!("GPU::BatchEncode");
108-
let ret = launch_angle_encode_batch(
110+
crate::profile_scope!("GPU::BatchEncode");
111+
let ret = unsafe {
112+
launch_angle_encode_batch(
109113
dev_ptr as *const f64,
110114
state_ptr_offset,
111115
samples_in_chunk,
112116
state_len,
113117
num_qubits as u32,
114118
ctx.stream_compute.stream as *mut c_void,
115-
);
116-
if ret != 0 {
117-
return Err(MahoutError::KernelLaunch(format!(
118-
"Angle encode kernel error: {}",
119-
ret
120-
)));
121-
}
119+
)
120+
};
121+
if ret != 0 {
122+
return Err(MahoutError::KernelLaunch(format!(
123+
"Angle encode kernel error: {}",
124+
ret
125+
)));
122126
}
123127
Ok(())
124128
}

qdp/qdp-core/src/encoding/basis.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
//! Basis encoding implementation.
1818
19+
// Allow unused_unsafe: qdp_kernels functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
20+
// The compiler can't statically determine which path is taken.
21+
#![allow(unused_unsafe)]
22+
1923
use std::ffi::c_void;
2024

2125
use cudarc::driver::{CudaSlice, DevicePtr};

qdp/qdp-core/src/gpu/encodings/amplitude.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
// Amplitude encoding: state injection with L2 normalization
1818

19+
// Allow unused_unsafe: qdp_kernels functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
20+
// The compiler can't statically determine which path is taken.
21+
#![allow(unused_unsafe)]
22+
1923
use std::sync::Arc;
2024

2125
use super::QuantumEncoder;

qdp/qdp-core/src/gpu/encodings/angle.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
// Angle encoding: map per-qubit angles to product state amplitudes.
1818

19+
// Allow unused_unsafe: qdp_kernels functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
20+
// The compiler can't statically determine which path is taken.
21+
#![allow(unused_unsafe)]
22+
1923
use super::QuantumEncoder;
2024
#[cfg(target_os = "linux")]
2125
use crate::error::cuda_error_to_string;

qdp/qdp-core/src/gpu/encodings/basis.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
// Basis encoding: map integers to computational basis states
1818

19+
// Allow unused_unsafe: qdp_kernels functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
20+
// The compiler can't statically determine which path is taken.
21+
#![allow(unused_unsafe)]
22+
1923
use super::QuantumEncoder;
2024
#[cfg(target_os = "linux")]
2125
use crate::error::cuda_error_to_string;

qdp/qdp-core/src/gpu/memory.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
16+
17+
// Allow unused_unsafe: qdp_kernels functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
18+
// The compiler can't statically determine which path is taken.
19+
#![allow(unused_unsafe)]
20+
1621
use crate::error::{MahoutError, Result};
1722
use cudarc::driver::{CudaDevice, CudaSlice, DevicePtr};
1823
use qdp_kernels::{CuComplex, CuDoubleComplex};

qdp/qdp-core/src/gpu/pipeline.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
// Provides generic double-buffered execution for large data processing.
2020
// Separates the "streaming mechanics" from the "kernel logic".
2121

22+
// Allow unused_unsafe: CUDA FFI functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
23+
// The compiler can't statically determine which path is taken.
24+
#![allow(unused_unsafe)]
25+
2226
use crate::error::{MahoutError, Result};
2327
#[cfg(target_os = "linux")]
2428
use crate::gpu::buffer_pool::{PinnedBufferHandle, PinnedBufferPool};

qdp/qdp-core/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
// Allow unused_unsafe: CUDA FFI and kernel functions are unsafe in CUDA builds but safe stubs in no-CUDA builds.
18+
// The compiler can't statically determine which path is taken.
19+
#![allow(unused_unsafe)]
20+
1721
pub mod dlpack;
1822
#[cfg(target_os = "linux")]
1923
mod encoding;

qdp/qdp-kernels/build.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,27 @@ use std::env;
2727
use std::process::Command;
2828

2929
fn main() {
30+
// Let rustc know about our build-script-defined cfg flags (avoids `unexpected_cfgs` warnings).
31+
println!("cargo::rustc-check-cfg=cfg(qdp_no_cuda)");
32+
3033
// Tell Cargo to rerun this script if the kernel sources change
3134
println!("cargo:rerun-if-changed=src/amplitude.cu");
3235
println!("cargo:rerun-if-changed=src/basis.cu");
3336
println!("cargo:rerun-if-changed=src/angle.cu");
37+
println!("cargo:rerun-if-env-changed=QDP_NO_CUDA");
3438
println!("cargo:rerun-if-changed=src/kernel_config.h");
3539

3640
// Check if CUDA is available by looking for nvcc
37-
let has_cuda = Command::new("nvcc").arg("--version").output().is_ok();
41+
let force_no_cuda = env::var("QDP_NO_CUDA")
42+
.map(|v| v == "1" || v.eq_ignore_ascii_case("true") || v.eq_ignore_ascii_case("yes"))
43+
.unwrap_or(false);
44+
45+
let has_cuda = !force_no_cuda && Command::new("nvcc").arg("--version").output().is_ok();
3846

3947
if !has_cuda {
48+
// Expose a cfg for conditional compilation of stub symbols on Linux.
49+
// This allows qdp-kernels (and dependents) to link on Linux machines without CUDA.
50+
println!("cargo:rustc-cfg=qdp_no_cuda");
4051
println!("cargo:warning=CUDA not found (nvcc not in PATH). Skipping kernel compilation.");
4152
println!("cargo:warning=This is expected on macOS or non-CUDA environments.");
4253
println!(

0 commit comments

Comments
 (0)