Add flag to set number of threads.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bill Thiede 2019-10-12 14:29:36 -07:00
parent 1687077f4a
commit 235a9d1204
11 changed files with 20 additions and 5 deletions

View File

@ -119,6 +119,9 @@ pub struct Opt {
/// Image height /// Image height
#[structopt(short = "h", long = "height", default_value = "1024")] #[structopt(short = "h", long = "height", default_value = "1024")]
pub height: usize, pub height: usize,
/// Number of threads
#[structopt(short = "t", long = "num_threads")]
pub num_threads: Option<usize>,
/// Sub-samples per pixel /// Sub-samples per pixel
#[structopt(short = "s", long = "subsample", default_value = "8")] #[structopt(short = "s", long = "subsample", default_value = "8")]
pub subsamples: usize, pub subsamples: usize,
@ -139,6 +142,7 @@ pub struct Opt {
} }
pub fn opt_hash(opt: &Opt) -> String { pub fn opt_hash(opt: &Opt) -> String {
// TODO(wathiede): add threads.
format!( format!(
"w:{}-h:{}-s:{}-pprof:{}-model:{}-use_accel:{}-{}", "w:{}-h:{}-s:{}-pprof:{}-model:{}-use_accel:{}-{}",
opt.width, opt.width,
@ -155,6 +159,7 @@ pub struct Scene {
pub world: Box<Hit>, pub world: Box<Hit>,
pub camera: Camera, pub camera: Camera,
pub subsamples: usize, pub subsamples: usize,
pub num_threads: Option<usize>,
pub width: usize, pub width: usize,
pub height: usize, pub height: usize,
pub global_illumination: bool, pub global_illumination: bool,
@ -269,13 +274,13 @@ fn render_worker(
} }
pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::io::Error> { pub fn render(scene: Scene, output_dir: &Path) -> std::result::Result<(), std::io::Error> {
let cpus = num_cpus::get(); let num_threads = scene.num_threads.unwrap_or_else(num_cpus::get);
let (pixel_req_tx, pixel_req_rx) = channel::bounded(2 * cpus); let (pixel_req_tx, pixel_req_rx) = channel::bounded(2 * num_threads);
let (pixel_resp_tx, pixel_resp_rx) = channel::bounded(2 * cpus); let (pixel_resp_tx, pixel_resp_rx) = channel::bounded(2 * num_threads);
let scene = sync::Arc::new(scene); let scene = sync::Arc::new(scene);
println!("Creating {} render threads", cpus); println!("Creating {} render threads", num_threads);
for i in 0..cpus { for i in 0..num_threads {
let s = sync::Arc::clone(&scene); let s = sync::Arc::clone(&scene);
let pixel_req_rx = pixel_req_rx.clone(); let pixel_req_rx = pixel_req_rx.clone();
let pixel_resp_tx = pixel_resp_tx.clone(); let pixel_resp_tx = pixel_resp_tx.clone();

View File

@ -69,6 +69,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: true, global_illumination: true,

View File

@ -51,6 +51,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: true, global_illumination: true,

View File

@ -63,6 +63,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: true, global_illumination: true,

View File

@ -130,6 +130,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: false, global_illumination: false,

View File

@ -115,6 +115,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: false, global_illumination: false,

View File

@ -166,6 +166,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world: Box::new(HitableList::new(list)), world: Box::new(HitableList::new(list)),
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: false, global_illumination: false,

View File

@ -144,6 +144,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: false, global_illumination: false,

View File

@ -65,6 +65,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: true, global_illumination: true,

View File

@ -142,6 +142,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: false, global_illumination: false,

View File

@ -70,6 +70,7 @@ pub fn new(opt: &Opt) -> Scene {
camera, camera,
world, world,
subsamples: opt.subsamples, subsamples: opt.subsamples,
num_threads: opt.num_threads,
width: opt.width, width: opt.width,
height: opt.height, height: opt.height,
global_illumination: true, global_illumination: true,