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

Unknown Error when doing Module::from_ptx #127

Open
GabrieleC07 opened this issue Dec 3, 2024 · 0 comments
Open

Unknown Error when doing Module::from_ptx #127

GabrieleC07 opened this issue Dec 3, 2024 · 0 comments

Comments

@GabrieleC07
Copy link

When retrieving ptx code, by doing Module::from_ptx, it returns UnknownError

FULL CODE

use std::ffi::CString;
use std::io::{BufWriter, Write};
use std::fs;
use std::time::Instant;
use cust::module::ModuleJitOption;
use cust::prelude::*;

mod is_near_square_prime;

fn main() {
    // Init CUDA and cust
    let _ctx = cust::quick_init().unwrap();
    let ptx = include_str!("../near_sq_prime.ptx");
    let module = Module::from_ptx(&ptx, &[ModuleJitOption::OptLevel(cust::module::OptLevel::O0)]).unwrap();
    let stream = Stream::new(StreamFlags::NON_BLOCKING, None).unwrap();

    // Allocate memory for input and output arrays
    let mem = 1024;
    let nums: Vec<i32> = vec![1; mem];
    let size: usize = nums.len();
    let mut results: Vec<i32> = vec![0; mem];

    

    // Allocate device memory
    let device_nums = DeviceBuffer::from_slice(&nums).unwrap();
    let device_size = DeviceVariable::new(size).unwrap();
    let device_result = DeviceBuffer::from_slice(&results).unwrap();

    unsafe {
        let result = launch!(module.check_near_square_prime<<<1, 1, 0, stream>>>(
            device_nums.as_device_ptr(),
            device_result.as_device_ptr(),
            device_size.as_device_ptr(),
        ));
        // Ensure there were no errors
        result.unwrap();
    }
    let file = fs::File::create("output.txt").unwrap();    
    let num_to_search = get_number_to_search();
    
    
    let start = Instant::now();
    let start_find = Instant::now();

    println!("Finished searching, beginning to sort!");
    let start_sort = Instant::now();
    results.sort();
    let results_sorted = results.into_iter().map(|x| x.to_string());
    let end_sort = start_sort.elapsed();
    println!("Finished sorting, to sort it took {}, beginning to write", end_sort.as_secs());

    let results_len = results_sorted.len();
    let duration_find = start_find.elapsed();
    println!("To find near-square primes it took {} seconds, found {} near-square primes, searched {} numbers",
        duration_find.as_secs(), results_len, num_to_search);

    let start_write = Instant::now();
    let mut writer = BufWriter::new(&file);

    // Clear file contents before writing to it again
    file.set_len(0).unwrap();

    for line in results_sorted {
        writer.write(format!("{}\n", line).as_bytes()).unwrap();
    }
    let duration_write = start_write.elapsed();
    println!("To write near-square primes it took {} seconds, wrote {} near-square primes",
        duration_write.as_secs(), results_len);

    let duration = start.elapsed();
    println!("Total time taken: {} seconds", duration.as_secs());
}

fn get_number_to_search() -> u128 {
    let mut input = String::new();

    println!("Input until which number to search");
    std::io::stdin()
        .read_line(&mut input)
        .unwrap();
    let num_to_search = input.trim().parse::<u128>().expect("ONLY INPUT DIGITS");
    
    num_to_search
}
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

1 participant