4

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:

  1. The work being done uses only local variables so I don't believe memory bandwidth or cache issues are a problem.
  2. 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: Time to Run Test vs Threads

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

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

enter image description here enter image description here

4
  • 1
    Are you running in release mode? Does the trend hold if you add another 0 to loops? It's hard to know whether this is normal or not without more information. How many CPUs do you have?
    – trent
    Apr 20, 2021 at 17:57
  • and what are the spec of your 2 thread cpu ? oups (please include EVERY information about your hardware, including temp while running this, frequency, etc
    – Stargateur
    Apr 20, 2021 at 19:44
  • @trentcl this is compiled in release mode. Hardware information: AMD Ryzen 5 Pro 3500U. This has 4 physical cores and 8 with SMT
    – egerhard
    Apr 20, 2021 at 21:32
  • @Stargateur what's the appropriate tool to measure temp while running this test?
    – egerhard
    Apr 20, 2021 at 22:07

2 Answers 2

4

Contrary to what CPU manufacturers would like you to believe, hyperthreading is not the same as physical cores. In particular, hyperthreading is only effective when the threads run different operations at any given time (the threads may be running the same algorithm, but then HT is only useful if one thread is waiting for the cache while the other is running).

In your case, you get a 3.25× performance increase for 4 threads on 4 physical cores, which is not completely unreasonable depending on the work and overall system load. When running more than 4 threads, you get threads that run on the same core, and must share the same FPU which can only do one operation at a time, explaining why you can't get much more than a 4× performance increase.

1
  • Test is very short
  • Includes the time spawning the threads
  • Real cores vs smt
  • Freq scaling, power states, parking
2
  • What does "very short" mean? What would the appropriate length of time be? Likewise, how would freq scaling, power states, and parking play into this?
    – egerhard
    Apr 20, 2021 at 21:35
  • 1
    @egerhard Test was too short because you saw scaling changed when increasing total time. Explaining what freq scaling, smt and everything else is would be different questions...
    – Acorn
    Apr 21, 2021 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.