Currently, working on learning parallelization and am investigating why a test program I wrote is not scaling well. I have a simple program that does a CPU bound computation through L iterations and spreads those iterations across the number of threads in the test (from 1 to 8). While I don't expect perfect scaling (8 threads is 8 times faster than 1 thread), the scaling I am seeing seems bad enough that I believe there must be something I am missing.
I'm assuming that there is either something wrong with my code or that there's some aspect to parallelization that I'm missing.
Things that I feel can be ruled out:
- The work being done uses only local variables so I don't believe memory bandwidth or cache issues are a problem.
- I have tried this test with each thread pinned to a different core and did not see any improvement performance.
Hardware:
Lenovo T495
Operating System: Fedora 32
KDE Plasma Version: 5.18.5
KDE Frameworks Version: 5.75.0
Qt Version: 5.14.2
Kernel Version: 5.11.13-100.fc32.x86_64
OS Type: 64-bit
Processors: 8 × AMD Ryzen 5 PRO 3500U w/ Radeon Vega Mobile Gfx
Memory: 21.5 GiB of RAM
Here's the code I wrote:
use std::thread;
use std::time::Instant;
fn main() {
let loops = 10_000_000_000;
for threads in 1..=8 {
// As threads are added to the test, evenly split the total number of iterations
// across all threads, so that 1 thread test can be compared to 4 thread test.
// For `threads` that are not divisors of `loops` some threads may have one more
// iteration than the others but that will be 1 out of 10,000,000 and should have
// negligible effect on the run time.
n_threads(threads, loops / threads);
}
}
/// Have `num_threads` threads each run a function that will
/// iterate a computation `loops` times.
fn n_threads(num_threads: usize, loops: usize) {
let sw = Instant::now();
let mut threads = Vec::new();
for _ in 0..num_threads {
let t = thread::spawn(move || {
let sw = Instant::now();
let v = work(loops);
(v, sw.elapsed().as_millis())
});
threads.push(t);
}
let mut durations = vec![0; num_threads];
let mut idx = 0;
for t in threads.into_iter() {
let (_, dur) = t.join().unwrap();
durations[idx] = dur;
idx += 1;
}
let time = sw.elapsed();
let avg = durations.iter().sum::<u128>() as f64 / num_threads as f64;
println!("{}, {}, {}", num_threads, time.as_millis(), avg);
}
fn work(loops: usize) -> f64 {
let mut x = 0.5;
for i in 0..loops {
x += (i as f64 / 10000.).sin();
}
x
}
When I run my test, I get the following results:
| Threads | Time (ms) | Scale Factor |
| -------:| ---------:| ------------:|
| 1 | 1702 | 1 |
| 2 | 993 | 1.713997986 |
| 3 | 757 | 2.248348745 |
| 4 | 650 | 2.618461538 |
| 5 | 582 | 2.924398625 |
| 6 | 495 | 3.438383838 |
| 7 | 475 | 3.583157895 |
| 8 | 455 | 3.740659341 |
Here's a chart showing the change in time to run the test vs the number of threads for the computation:

Here's a chart showing the performance multiplier vs threads along with a perfect multiplier:

Updated Test with 10,000,000,000 Total Iterations Spread Across Threads
Per request for a test that took longer, I've increased the number of iterations by 100x. I've also moved the timing to within the thread (and updated the code above):
Thread | Avg In Thread Time | Times Faster
1 | 155564 | 1
2 | 79400.5 | 1.959232
3 | 57965 | 2.683757
4 | 47753.25 | 3.257663
5 | 42054.6 | 3.699096
6 | 40028.66667 | 3.886315
7 | 39479.28571 | 3.940396
8 | 37376.625 | 4.162067


loops? It's hard to know whether this is normal or not without more information. How many CPUs do you have?