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

Better multithreading options #84

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
env.sh
env.bat
shell.nix
.vscode
18 changes: 0 additions & 18 deletions .vscode/launch.json

This file was deleted.

10 changes: 0 additions & 10 deletions .vscode/settings.json

This file was deleted.

16 changes: 0 additions & 16 deletions .vscode/tasks.json

This file was deleted.

146 changes: 104 additions & 42 deletions core/src/channel_group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,35 @@ const MAX_EVENT_CACHE_SIZE: u32 = 1024 * 1024;
///
/// Manages multiple VoiceChannel objects at once.
pub struct ChannelGroup {
thread_pool: rayon::ThreadPool,
thread_pool: Option<rayon::ThreadPool>,
cached_event_count: u32,
channel_events_cache: Box<[Vec<ChannelAudioEvent>]>,
sample_cache_vecs: Box<[Vec<f32>]>,
channels: Box<[VoiceChannel]>,
audio_params: AudioStreamParams,
}

/// Options regarding which parts of the ChannelGroup should be multithreaded.
///
MyBlackMIDIScore marked this conversation as resolved.
Show resolved Hide resolved
/// The following apply for all the values:
/// - A value of `None` means no multithreading.
/// - If the value is set to `Some(0)` then the number of threads will be
/// determined automatically by `rayon`. Please read
/// [this](https://docs.rs/rayon-core/1.11.0/rayon_core/struct.ThreadPoolBuilder.html#method.num_threads)
/// for more information.
#[derive(Clone)]
pub struct ParallelismOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some(0) isn't really a good magic value, that's why we use enums lol.

E.g. "pub enum ThreadCount" with options One, Auto, Manual(u32) or something like that, where Auto just uses the core count

Also It's nice to have constants for these things, because 90% of use cases are just "auto multithreading per channel" or "auto multithreading per channel per key"

Copy link
Member Author

Choose a reason for hiding this comment

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

E.g. "pub enum ThreadCount" with options One, Auto, Manual(u32) or something like that, where Auto just uses the core count

True that sounds like a better approach. Should also be easier for the user to understand what is going on.

Also It's nice to have constants for these things, because 90% of use cases are just "auto multithreading per channel" or "auto multithreading per channel per key"

You mean make a constant of the enum:

const AUTO_MULTITHREADING: ThreadCount = ThreadCount::Auto

or the struct itself:

const AUTO_MULTITHREADING: ParallelismOptions = ParallelismOptions {
    channel: ThreadCount::Auto,
    key: ThreadCount::Auto,
}

?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nah I meant making it a constant implemented on the struct, you can have const fields inside impl blocks

Copy link
Member Author

Choose a reason for hiding this comment

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

So like this or a function that returns it?

impl ParallelismOptions {
    const AUTO: Self = ParallelismOptions {
        channel: ThreadCount::Auto,
        key: ThreadCount::Auto,
    };
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah that, except AUTO_PER_KEY (key and channel) and AUTO_PER_CHANNEL (just channel) with descriptions for each, and an Default implementation that does auto per key

Copy link
Collaborator

Choose a reason for hiding this comment

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

In my opinion at least

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that, except AUTO_PER_KEY (key and channel) and AUTO_PER_CHANNEL (just channel) with descriptions for each, and an Default implementation that does auto per key

Alright yeah that sounds good. Shouldn't default be auto for both though? It was like that before (at least for channel).

/// Render the MIDI channels parallel in a threadpool with the specified
/// thread count.
pub channel: Option<usize>,

/// Render the individisual keys of each channel parallel in a threadpool
/// with the specified thread count.
pub key: Option<usize>,
}

/// Options for initializing a new ChannelGroup.
#[derive(Clone)]
pub struct ChannelGroupConfig {
/// Channel initialization options (same for all channels).
/// See the `ChannelInitOptions` documentation for more information.
Expand All @@ -45,10 +65,9 @@ pub struct ChannelGroupConfig {
/// See the `AudioStreamParams` documentation for more information.
pub audio_params: AudioStreamParams,

/// Whether or not to use a threadpool to render individual keys' voices.
/// Regardless, each MIDI channel uses its own thread. This setting
/// adds more fine-grained threading per key rather than per channel.
pub use_threadpool: bool,
/// Options about the `ChannelGroup` instance's parallelism. See the `ParallelismOptions`
/// documentation for more information.
pub parallelism: ParallelismOptions,
}

impl ChannelGroup {
Expand All @@ -60,26 +79,38 @@ impl ChannelGroup {
let mut sample_cache_vecs = Vec::new();

// Thread pool for individual channels to split between keys
let pool = if config.use_threadpool {
Some(Arc::new(rayon::ThreadPoolBuilder::new().build().unwrap()))
} else {
None
};
let channel_pool = config.parallelism.key.map(|threads| {
Arc::new(
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build()
.unwrap(),
)
});

// Thread pool for splitting channels between threads
let group_pool = config.parallelism.channel.map(|threads| {
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build()
.unwrap()
});

for i in 0..config.channel_count {
let mut init = config.channel_init_options;
init.drums_only = config.drums_channels.clone().into_iter().any(|c| c == i);

channels.push(VoiceChannel::new(init, config.audio_params, pool.clone()));
channels.push(VoiceChannel::new(
init,
config.audio_params,
channel_pool.clone(),
));
channel_events_cache.push(Vec::new());
sample_cache_vecs.push(Vec::new());
}

// Thread pool for splitting channels between threads
let thread_pool = rayon::ThreadPoolBuilder::new().build().unwrap();

Self {
thread_pool,
thread_pool: group_pool,
cached_event_count: 0,
channel_events_cache: channel_events_cache.into_boxed_slice(),
channels: channels.into_boxed_slice(),
Expand Down Expand Up @@ -121,44 +152,75 @@ impl ChannelGroup {
return;
}

let thread_pool = &mut self.thread_pool;
let channels = &mut self.channels;
let channel_events_cache = &mut self.channel_events_cache;

thread_pool.install(move || {
channels
.par_iter_mut()
.zip(channel_events_cache.par_iter_mut())
.for_each(|(channel, events)| {
channel.push_events_iter(events.drain(..).map(ChannelEvent::Audio));
match self.thread_pool.as_ref() {
Some(pool) => {
let channels = &mut self.channels;
let channel_events_cache = &mut self.channel_events_cache;

pool.install(move || {
channels
.par_iter_mut()
.zip(channel_events_cache.par_iter_mut())
.for_each(|(channel, events)| {
channel.push_events_iter(events.drain(..).map(ChannelEvent::Audio));
});
});
});
}
None => {
for (channel, events) in self
.channels
.iter_mut()
.zip(self.channel_events_cache.iter_mut())
{
channel.push_events_iter(events.drain(..).map(ChannelEvent::Audio));
}
}
}

self.cached_event_count = 0;
}

fn render_to(&mut self, buffer: &mut [f32]) {
self.flush_events();

let thread_pool = &mut self.thread_pool;
let channels = &mut self.channels;
let sample_cache_vecs = &mut self.sample_cache_vecs;

buffer.fill(0.0);
thread_pool.install(move || {
channels
.par_iter_mut()
.zip(sample_cache_vecs.par_iter_mut())
.for_each(|(channel, samples)| {
samples.resize(buffer.len(), 0.0);
channel.read_samples(samples.as_mut_slice());

match self.thread_pool.as_ref() {
Some(pool) => {
let channels = &mut self.channels;
let sample_cache_vecs = &mut self.sample_cache_vecs;
pool.install(move || {
channels
.par_iter_mut()
.zip(sample_cache_vecs.par_iter_mut())
.for_each(|(channel, samples)| {
samples.resize(buffer.len(), 0.0);
channel.read_samples(samples.as_mut_slice());
});

for vec in sample_cache_vecs.iter_mut() {
sum_simd(vec, buffer);
vec.clear();
}
});
}
None => {
let len = buffer.len();

for (channel, samples) in self
.channels
.iter_mut()
.zip(self.sample_cache_vecs.iter_mut())
{
samples.resize(len, 0.0);
channel.read_samples(samples.as_mut_slice());
}

for vec in sample_cache_vecs.iter_mut() {
sum_simd(vec, buffer);
vec.clear();
for vec in self.sample_cache_vecs.iter_mut() {
sum_simd(vec, buffer);
vec.clear();
}
}
});
}
}

/// Returns the active voice count of the synthesizer.
Expand Down
1 change: 0 additions & 1 deletion kdmapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ pub extern "C" fn GetVoiceCount() -> u64 //This entire function is custom to xsy
pub extern "C" fn InitializeKDMAPIStream() -> i32 {
let config = XSynthRealtimeConfig {
render_window_ms: 5.0,
use_threadpool: true,
..Default::default()
};

Expand Down
13 changes: 9 additions & 4 deletions realtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ pub struct XSynthRealtimeConfig {
/// Default: `[9]`
pub drums_channels: Vec<u32>,

/// Whether or not to use a threadpool to render individual keys' voices.
/// Controls the use a threadpool to render individual keys' voices.
/// When a value is set, the specified number of threads will be used
/// for that operation, while `None` means no per-key concurrency.
/// If the value is set to `Some(0)` then the number of threads will
/// be determined automatically by `rayon`.
///
/// Regardless, each MIDI channel uses its own thread. This setting
/// adds more fine-grained threading per key rather than per channel.
///
/// Default: `false`
pub use_threadpool: bool,
/// Default: `None`
pub threadpool: Option<usize>,
MyBlackMIDIScore marked this conversation as resolved.
Show resolved Hide resolved

/// A range of velocities that will not be played.
///
Expand All @@ -45,7 +50,7 @@ impl Default for XSynthRealtimeConfig {
render_window_ms: 10.0,
channel_count: 16,
drums_channels: vec![9],
use_threadpool: false,
threadpool: None,
ignore_range: 0..=0,
}
}
Expand Down
13 changes: 8 additions & 5 deletions realtime/src/realtime_synth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,14 @@ impl RealtimeSynth {
let sample_rate = stream_config.sample_rate().0;
let stream_params = AudioStreamParams::new(sample_rate, stream_config.channels().into());

let pool = if config.use_threadpool {
Some(Arc::new(rayon::ThreadPoolBuilder::new().build().unwrap()))
} else {
None
};
let pool = config.threadpool.map(|threads| {
Arc::new(
rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build()
.unwrap(),
)
});

let (output_sender, output_receiver) = bounded::<Vec<f32>>(config.channel_count as usize);

Expand Down
15 changes: 9 additions & 6 deletions render/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use midi_toolkit::{
},
};

pub use xsynth_core::channel_group::ParallelismOptions;

/// Statistics of an XSynthRender object.
pub struct XSynthRenderStats {
/// The progress of the render in seconds.
Expand Down Expand Up @@ -64,11 +66,12 @@ pub struct XSynthRenderBuilder<'a, StatsCallback: FnMut(XSynthRenderStats)> {

/// Initializes an XSynthRenderBuilder object.
pub fn xsynth_renderer<'a>(
config: XSynthRenderConfig,
midi_path: &'a str,
out_path: &'a str,
) -> XSynthRenderBuilder<'a, impl FnMut(XSynthRenderStats)> {
XSynthRenderBuilder {
config: XSynthRenderConfig::default(),
config,
midi_path,
soundfonts: vec![],
layer_count: Some(4),
Expand All @@ -84,12 +87,12 @@ impl<'a, ProgressCallback: FnMut(XSynthRenderStats)> XSynthRenderBuilder<'a, Pro
}

pub fn with_channel_count(mut self, channels: u32) -> Self {
self.config.channel_count = channels;
self.config.group_options.channel_count = channels;
self
}

pub fn use_threadpool(mut self, use_threadpool: bool) -> Self {
self.config.use_threadpool = use_threadpool;
pub fn with_parallelism(mut self, options: ParallelismOptions) -> Self {
self.config.group_options.parallelism = options;
self
}

Expand All @@ -99,12 +102,12 @@ impl<'a, ProgressCallback: FnMut(XSynthRenderStats)> XSynthRenderBuilder<'a, Pro
}

pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
self.config.sample_rate = sample_rate;
self.config.group_options.audio_params.sample_rate = sample_rate;
self
}

pub fn with_audio_channels(mut self, audio_channels: u16) -> Self {
self.config.audio_channels = audio_channels;
self.config.group_options.audio_params.channels = audio_channels.into();
self
}

Expand Down
Loading
Loading