Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to copy data to device constant memory? #88

Open
patrickcsullivan opened this issue Sep 19, 2022 · 3 comments
Open

Is it possible to copy data to device constant memory? #88

patrickcsullivan opened this issue Sep 19, 2022 · 3 comments

Comments

@patrickcsullivan
Copy link

When using C CUDA it's possible to allocate constant memory on the GPU by using cudaMemcpyToSymbol rather than cudaMemcpy. Is there a similar mechanism in the cust library for copying an array with a constant size from the host to the device constant memory?

@RDambrosio016
Copy link
Member

I believe module::Symbol's impl of CopyDestination should work for this, i don't think the driver API actually has memcpyToSymbol, at least from what i can see, we just use HtoD memcpys which will probably work the same.

@patrickcsullivan
Copy link
Author

patrickcsullivan commented Sep 23, 2022

Thanks for the response!

Using module::Symbol makes sense to me. However, do you know how I can declare a variable in my GPU module so that it is compiled as a global symbol which I can then access by calling the Module::get_global function?

Here’s an example of what I’m trying to do:

// GPU Code

use cuda_std::prelude::*;

// I assume this is the wrong way to declare a global symbol.
// If this were C CUDA, then I'd annotate MY_NUM with __const__
pub static MY_NUM: i64 = 0;

#[kernel]
#[allow(improper_ctypes_definitions, clippy::missing_safety_doc)]
pub unsafe fn simple_kernel(out: *mut i64, n: usize) {
    let mut tid = (thread::thread_idx_x() + thread::block_idx_x() * thread::block_dim_x()) as usize;
    while tid < n {
        *out.add(tid) = MY_NUM;
        tid += (thread::block_dim_x() * thread::grid_dim_x()) as usize;
    }
}
// CPU Code

let _ctx = cust::quick_init()?;
let module = Module::from_ptx(PTX, &[])?;
let kernel = module.get_function("simple_kernel")?;

let symbol_name = CString::new("MY_NUM")?;
let mut symbol = module.get_global::<i64>(symbol_name.as_c_str())?;

let my_num: i64 = 1234;
symbol.copy_from(&my_num)?;

When I run this I get the following error:

Error: NotFound

I assume that MY_NUM is not getting compiled as a global symbol, so module.get_global can’t find it.

If I were writing C CUDA then I assume I’d need to annotate MY_NUM with __const__. Do you know what I need to do in my Rust GPU code to ensure that MY_NUM gets compiled as a global symbol?

@Stock84-dev
Copy link

#[no_mangle]
static DATA: [f32; 1024] = [0.; 1024];

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

No branches or pull requests

3 participants