Files
foxhunt/vendor/cudarc/examples/06-threading.rs
jgrusewski 07d0e60fe4 feat(bf16): remove nvrtc from entire workspace + wire ml-core precompiled cubins
- Fork cudarc locally (vendor/cudarc): add CudaContext::load_cubin()
  that calls cuModuleLoadData directly — zero nvrtc dependency
- Remove "nvrtc" feature from ml-core, ml-dqn, ml-ppo Cargo.toml
- Replace all 89 Ptx::from_binary + load_module calls with load_cubin
- ml-core cuda_autograd: wire 9 stub constructors to precompiled cubins
  (activation, elementwise, linear, loss, reduction, dropout, layer_norm, optimizer)
- ml-core build.rs: compile 8 BF16-native CUDA kernels via nvcc
- cubin_loader.rs: thin wrapper around CudaContext::load_cubin()
- Fix size_of::<f32> in gpu_tensor.rs, stream_ops.rs, layer_norm.rs
- Fix test data: Vec<f32> → Vec<half::bf16> for memcpy_htod
- Stub ml-ppo/ml-dqn runtime compile_ptx calls (dead code)
- backtest_metrics_kernel.cu: full native BF16 rewrite (no float)
- backtest_env_kernel.cu: shared memory → __nv_bfloat16

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 10:11:46 +01:00

63 lines
2.0 KiB
Rust

use cudarc::driver::*;
use cudarc::nvrtc::compile_ptx;
use std::thread;
const KERNEL_SRC: &str = "
extern \"C\" __global__ void hello_world(int i) {
printf(\"Hello from the cuda kernel in thread %d\\n\", i);
}
";
fn main() -> Result<(), DriverError> {
{
// Option 1: sharing ctx & module between threads
thread::scope(|s| {
let ptx = compile_ptx(KERNEL_SRC).unwrap();
let ctx = CudaContext::new(0)?;
let module = ctx.load_module(ptx)?;
for i in 0..10i32 {
let thread_ctx = ctx.clone();
let thread_module = module.clone();
s.spawn(move || {
let stream = thread_ctx.default_stream();
let f = thread_module.load_function("hello_world")?;
unsafe {
stream
.launch_builder(&f)
.arg(&i)
.launch(LaunchConfig::for_num_elems(1))
}
});
}
Ok(())
})?;
}
{
// Option 2: initializing different context in each
// Note that this will still schedule to the same stream since we are using the
// default stream here on the same device.
thread::scope(move |s| {
for i in 0..10i32 {
s.spawn(move || {
let ptx = compile_ptx(KERNEL_SRC).unwrap();
let ctx = CudaContext::new(0)?;
let module = ctx.load_module(ptx)?;
let stream = ctx.default_stream();
let f = module.load_function("hello_world")?;
unsafe {
stream
.launch_builder(&f)
.arg(&i)
.launch(LaunchConfig::for_num_elems(1))
}
});
}
Ok(())
})?;
}
Ok(())
}