Skip to content

Conversation

@PMQ9
Copy link
Contributor

@PMQ9 PMQ9 commented Oct 21, 2025

For this issue: #218

@PMQ9 PMQ9 force-pushed the improve_constant_memory_placement branch from 15b1306 to 638b478 Compare October 21, 2025 03:54
@PMQ9 PMQ9 force-pushed the improve_constant_memory_placement branch from 638b478 to 25d8ac1 Compare October 21, 2025 04:15
Copy link
Contributor

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

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

Thank you and welcome to the project! Looks pretty good, just some small things

@PMQ9 PMQ9 force-pushed the improve_constant_memory_placement branch from a1c7207 to d841974 Compare October 24, 2025 01:58
@PMQ9
Copy link
Contributor Author

PMQ9 commented Oct 29, 2025

One more change:

  • The flag --use-constant-memory-space seems to be missing, I just added that.

Test example to trigger the error:

  • /examples/cuda/const_memory_overflow/
  • kernels/src/lib.rs
#![no_std]
#![crate_type = "staticlib"]
use cuda_std::*;

// 35KB per array, 3 arrays = 105KB total (well above 64KB limit)
const ARRAY_SIZE: usize = 35 * 1024 / 4; 

static BIG_ARRAY_1: [u32; ARRAY_SIZE] = [111u32; ARRAY_SIZE];
static BIG_ARRAY_2: [u32; ARRAY_SIZE] = [222u32; ARRAY_SIZE];
static BIG_ARRAY_3: [u32; ARRAY_SIZE] = [333u32; ARRAY_SIZE];

pub unsafe fn test_kernel(out: *mut u32) {
    *out = BIG_ARRAY_1[0] + BIG_ARRAY_2[0] + BIG_ARRAY_3[0];
}
  • build.rs
use std::path::PathBuf;

fn main() {
    let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
    let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());

    println!("cargo::warning=Building GPU kernels with constant memory ENABLED");
    println!("cargo::warning=This should trigger constant memory overspill errors");
    
    cuda_builder::CudaBuilder::new(manifest_dir.join("kernels"))
        .copy_to(out_path.join("kernels.ptx"))
        // Enable constant memory placement to trigger the overspill error
        .use_constant_memory_space(true)
        .build()
        .unwrap();
}
  • Cargo.toml
[workspace]

[package]
name = "const_memory_overflow"
version = "0.1.0"
edition = "2021"

[dependencies]
cust = { path = "../../../crates/cust" }

[build-dependencies]
cuda_builder = { path = "../../../crates/cuda_builder", features = ["rustc_codegen_nvvm"] }
  • kernels/Cargo.toml
[workspace]

[package]
name = "const_memory_overflow_kernels"
version = "0.1.0"
edition = "2021"

[dependencies]
cuda_std = { path = "../../../../crates/cuda_std" }

[lib]
crate-type = ["cdylib", "rlib"]
  • src/main,rs
use cust::prelude::*;

fn main() {  
    println!("This example is meant to fail at compile time!");
    println!("It demonstrates the improved error messages for constant memory overflow.");
}

Expected Failure

error: cannot place static `BIG_ARRAY_2` (35840 bytes) in constant memory:
cumulative constant memory usage would be 71680 bytes, exceeding the 65536 byte limit
= note: current constant memory usage: 35840 bytes
= note: static size: 35840 bytes
= note: would result in: 71680 bytes total
= help: move this or other statics to global memory...

@PMQ9
Copy link
Contributor Author

PMQ9 commented Oct 29, 2025

Option 1: Move BIG_ARRAY_2 to global

static BIG_ARRAY_1: [u32; ARRAY_SIZE] = [111u32; ARRAY_SIZE];
#[cuda_std::address_space(global)]
static BIG_ARRAY_2: [u32; ARRAY_SIZE] = [222u32; ARRAY_SIZE];

Option 2: Disable feature competely

.use_constant_memory_space(false)

@PMQ9 PMQ9 requested a review from LegNeato October 29, 2025 05:52
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.

2 participants