mirror of
https://github.com/Noratrieb/mandelbrot-rust.git
synced 2026-01-14 15:25:07 +01:00
wooo it works
This commit is contained in:
parent
c03b0c399a
commit
647e8c69e0
5 changed files with 30 additions and 33 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -216,6 +216,7 @@ dependencies = [
|
|||
"cust_derive",
|
||||
"cust_raw",
|
||||
"find_cuda_helper",
|
||||
"vek",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -412,6 +413,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"cuda_builder",
|
||||
"cust",
|
||||
"gpu",
|
||||
"image",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -10,8 +10,9 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
cust = "0.2.2"
|
||||
cust = { version = "0.2.2", features = ["vek"] }
|
||||
image = "0.23.14"
|
||||
gpu = { path = "./gpu" }
|
||||
|
||||
[profile.dev]
|
||||
opt-level = 3
|
||||
|
|
|
|||
BIN
gpu.png
Normal file
BIN
gpu.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.3 MiB |
|
|
@ -47,6 +47,7 @@ pub unsafe fn mandelbrot(
|
|||
|
||||
let idx = thread::index_2d();
|
||||
|
||||
|
||||
if idx.x >= width || idx.y >= height {
|
||||
return;
|
||||
}
|
||||
|
|
@ -61,12 +62,12 @@ fn check_part_of_mandelbrot(cfg: Cfg, id: Vec2<u32>) -> u32 {
|
|||
let x_step_size = (cfg.end.real - cfg.start.real) / cfg.width as f64;
|
||||
let y_step_size = (cfg.end.imag - cfg.start.imag) / cfg.height as f64;
|
||||
|
||||
let x = x_step_size * id.x as f64;
|
||||
let y = y_step_size * id.y as f64;
|
||||
let x_offset = x_step_size * id.x as f64;
|
||||
let y_offset = y_step_size * id.y as f64;
|
||||
|
||||
let sample_pos = CNumber {
|
||||
real: cfg.start.real + x,
|
||||
imag: cfg.start.imag + y,
|
||||
real: cfg.start.real + x_offset,
|
||||
imag: cfg.start.imag + y_offset,
|
||||
};
|
||||
|
||||
check_mandelbrot(sample_pos, cfg.iterations, cfg.threshold)
|
||||
|
|
|
|||
49
src/lib.rs
49
src/lib.rs
|
|
@ -6,18 +6,26 @@ use std::fmt::{Display, Formatter};
|
|||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use cust::prelude::*;
|
||||
use cust::vek::Vec2;
|
||||
use gpu::{CNumber, Cfg};
|
||||
use image::{ImageBuffer, ImageResult, Rgb, RgbImage};
|
||||
|
||||
static PTX: &str = include_str!("../target/gpu.ptx");
|
||||
|
||||
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||
let start_time = SystemTime::now();
|
||||
pub fn run(_: Config) -> Result<(), Box<dyn Error>> {
|
||||
let cfg = Cfg {
|
||||
start: CNumber::new(-3.0, 1.),
|
||||
end: CNumber::new(1., -1.),
|
||||
height: 500 * 10,
|
||||
width: 1000 * 10,
|
||||
iterations: 100000,
|
||||
threshold: 100,
|
||||
};
|
||||
|
||||
let debug = config.debug;
|
||||
let height = config.width * 2.0 / 3.0;
|
||||
|
||||
let amount = (height * config.width) as usize;
|
||||
let amount = cfg.height as usize * cfg.width as usize;
|
||||
|
||||
println!("{} points will be calculated", amount);
|
||||
|
||||
let _ctx = cust::quick_init()?;
|
||||
|
||||
|
|
@ -29,22 +37,13 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
|||
|
||||
let func = module.get_function("mandelbrot")?;
|
||||
|
||||
let (_, block_size) = func.suggested_launch_configuration(amount, 0.into())?;
|
||||
|
||||
let grid_size = (amount as u32 + block_size - 1) / block_size;
|
||||
|
||||
let cfg = Cfg {
|
||||
start: CNumber::new(-0.5, 0.5),
|
||||
end: CNumber::new(0.5, 0.5),
|
||||
height: 500,
|
||||
width: 500,
|
||||
iterations: 1000,
|
||||
threshold: 100,
|
||||
};
|
||||
let threads = Vec2::new(16, 16);
|
||||
let blocks = (Vec2::new(cfg.width, cfg.height) / threads) + 1;
|
||||
|
||||
let start_time = SystemTime::now();
|
||||
unsafe {
|
||||
launch!(
|
||||
func<<<grid_size, block_size, 0, stream>>>(
|
||||
func<<<blocks, threads, 0, stream>>>(
|
||||
cfg.start.real,
|
||||
cfg.start.imag,
|
||||
cfg.end.real,
|
||||
|
|
@ -62,23 +61,17 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
|||
|
||||
out_buf.copy_to(&mut out)?;
|
||||
|
||||
create_image(&out, cfg, "gpu.png")?;
|
||||
|
||||
// now do things with out
|
||||
println!("expected {}, got {} numbers!", amount, out.len());
|
||||
|
||||
if debug {
|
||||
println!("calculated in: {}", format_time(start_time.elapsed()?));
|
||||
}
|
||||
create_image(&out, cfg, "gpu.png")?;
|
||||
|
||||
println!("calculated in: {}", format_time(start_time.elapsed()?));
|
||||
println!("Total Time: {}", format_time(start_time.elapsed()?));
|
||||
|
||||
if debug {
|
||||
println!("Total Time: {}", format_time(start_time.elapsed()?));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_image(values: &[u32], cfg: Cfg, path: &str) -> ImageResult<()> {
|
||||
|
||||
let mut image: RgbImage = ImageBuffer::new(cfg.width, cfg.height);
|
||||
|
||||
for y in 0..cfg.height {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue