Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

What are multiple cores about

What are multiple cores about

When single-core CPU performance gains hit bottlenecks (the power wall, frequency wall, and pipeline complexity wall), engineers came up with a "crude but effective" solution: put multiple CPU cores on a single chip. This lesson introduces the principles and advantages of multi-core processors, and the limits that Amdahl's Law places on parallel speedup.


An analogy from life: a fast-food restaurant kitchen

Imagine a fast-food restaurant at the lunch rush.

Single-core mode: there is only one cook. He frantically speeds up (higher frequency) and optimizes the cooking process (pipelining), but no matter how he optimizes, he can only cook one dish at a time. Customers queue up and grow impatient.

Multi-core mode: the owner hires 4 cooks, each with their own stove. The 4 cooks can prepare 4 different dishes at the same time. No cook needs to get faster, yet the overall output of dishes multiplies several times over.

This is the design philosophy of multi-core: when a single core can hardly be made faster, it is better to add more cores and let them work in parallel.

Multi-core does not mean "making 4 copies of everything". Four cores usually share the same memory, the same disk interface, and the same operating system; they are duplicated only at the level of the "compute units". It is like 4 cooks sharing the same refrigerator and ingredient store.


Single-core vs multi-core: an architectural comparison

The predicament of single-core CPUs

In the early 2000s, the CPU industry improved performance by continually raising frequency—from 1 GHz to 2 GHz and then to 3 GHz. But this path soon reached its end.

Three major bottlenecks of single-core frequency:

Around 2005, Intel and AMD successively abandoned the pure "frequency race" and shifted to the multi-core route.

The structure of a multi-core CPU

The internal structure of a typical multi-core CPU is as follows:



  +-----------------------------------------------------------+

  |                         CPU 芯片                          |

  |  +-------------+  +-------------+  +-------------+  +-----+

  |  |   核心 1    |  |   核心 2    |  |   核心 3    |  |核 4 |

  |  | L1 Cache   |  | L1 Cache   |  | L1 Cache   |  | ... |

  |  | L2 Cache   |  | L2 Cache   |  | L2 Cache   |  |     |

  |  +------+------+  +------+------+  +------+------+  +--+--+

  |         |                |                |              |

  |         +----------------+----------------+--------------+

  |                          |

  |                    +-----+------+

  |                    | 共享 L3 Cache |

  |                    +-----+------+

  |                          |

  |                    +-----+------+

  |                    | 内存控制器   | ← 连接主内存 (RAM)

  |                    +------------+

  +-----------------------------------------------------------+

Key design:


Amdahl's Law: the ceiling of multi-core speedup

The intuition that "more cores means faster" is not always right. In 1967, computer scientist Gene Amdahl proposed a famous law.

Amdahl's Law: the overall speedup of a task is limited by the portion of the task that must be executed serially.

The formula is as follows:



加速比 = 1 / (S + P/N)



其中:

  S = 任务中必须串行执行的比例(0~1)

  P = 任务中可以并行执行的比例(0~1),且 S + P = 1

  N = 并行处理单元的数量(核心数)

This formula reveals a harsh fact:

Even with infinitely many cores, if only 50% of a task can be parallelized, the speedup can be at most 2x.

Derivation: as N approaches infinity, P/N approaches 0, and the speedup approaches 1/S.

Parallelizable fraction 1 core 2 cores 4 cores 8 cores 16 cores Infinitely many cores (upper bound)
50% 1.00x 1.33x 1.60x 1.78x 1.88x 2.00x
75% 1.00x 1.60x 2.29x 2.91x 3.37x 4.00x
90% 1.00x 1.82x 3.08x 4.71x 6.40x 10.00x
95% 1.00x 1.90x 3.48x 5.93x 9.14x 20.00x
99% 1.00x 1.98x 3.88x 7.48x 13.91x 100.00x

Observe the patterns in this table:

Amdahl's Law explains why "more cores does not necessarily mean faster". If your program is mostly serial logic—for example, one computation step must wait for the result of the previous step—then no matter how many cores there are, they can only stand by and cannot help.


Interactive demo: Amdahl's Law calculator

The Python code below computes and prints the Amdahl's Law speedup comparison table, and also plots the trend of speedup versus the number of cores, letting you intuitively feel the impact of the "serial bottleneck".

Example

"""
Amdahl's Law Calculator (runoob demo)
Compute the speedup for different parallel fractions and core counts
Print the comparison table and trend analysis
"""

def

amdahl_speedup

(

parallel_fraction

,

num_cores

)

:

   

"""
    The core formula of Amdahl's Law
    parallel_fraction: the fraction that can be executed in parallel (0.0 ~ 1.0)
    num_cores: the number of cores
    Returns the speedup
    """

   

if

num_cores

==

1

:

       

return

1.0

    serial_fraction

=

1.0

    speedup

=

1.0

/

(

serial_fraction + parallel_fraction / num_cores

)

   

return

speedup

def

amdahl_limit

(

parallel_fraction

)

:

   

"""
    Compute the theoretical speedup upper bound of Amdahl's Law (as the number of cores tends to infinity)
    As N→∞, P/N→0, speedup → 1/S
    """

    serial_fraction

=

1.0

   

if

serial_fraction

==

0

:

       

return

float

(

'inf'

)

 

Fully parallelizable, theoretically unlimited speedup

   

return

1.0

/ serial_fraction

def

print_comparison_table

(

parallel_fractions

,

core_counts

)

:

   

"""Print the complete speedup comparison table"""

   

Header

    header

=

f

"{'Cores':>6} |"

   

for

pf

in

parallel_fractions:

        header +

=

f

" {pf*100:>5.0f}% parallel |"

   

print

(

"="

(

12

12

len

(

parallel_fractions

)

)

)

   

print

(

" Amdahl's Law - Multi-core Speedup Comparison Table"

)

   

print

(

"="

(

12

12

len

(

parallel_fractions

)

)

)

   

print

(

header

)

   

print

(

"-"

(

12

12

len

(

parallel_fractions

)

)

)

   

for

cores

in

core_counts:

        row

=

f

" {cores:>5} |"

       

for

pf

in

parallel_fractions:

            speedup

=

amdahl_speedup

(

pf

,

cores

)

            row +

=

f

"  {speedup:>6.2f}x   |"

       

print

(

row

)

   

Theoretical upper bound

   

print

(

"-"

(

12

12

len

(

parallel_fractions

)

)

)

    limit_row

=

f

" {'Limit':>5} |"

   

for

pf

in

parallel_fractions:

        limit

=

amdahl_limit

(

pf

)

       

if

limit

==

float

(

'inf'

)

:

            limit_row +

=

f

" No limit |"

       

else

:

            limit_row +

=

f

"  {limit:>6.2f}x   |"

   

print

(

limit_row

)

   

print

(

"="

(

12

12

len

(

parallel_fractions

)

)

)

def

analyze_diminishing_returns

(

parallel_fraction

,

max_cores

=

64

)

:

   

"""Analyze the diminishing marginal returns of multi-core speedup"""

   

print

(

f

"\nMarginal benefit analysis (parallel fraction {parallel_fraction*100:.0f}%):"

)

   

print

(

f

" {'Cores':>6} | {'Speedup':>8} | {'This gain':>10} | {'Efficiency':>8}"

)

   

print

(

"-"

42

)

    prev_speedup

=

1.0

    cores

=

1

   

while

cores

<=

max_cores:

        speedup

=

amdahl_speedup

(

parallel_fraction

,

cores

)

        improvement

=

speedup - prev_speedup

       

Efficiency = actual speedup / number of cores (100% for perfect linear speedup)

        efficiency

=

(

speedup / cores

)

100

       

print

(

f

" {cores:>6} | {speedup:>8.2f}x | +{improvement:>8.3f}x | {efficiency:>7.1f}%"

)

        prev_speedup

=

speedup

       

if

cores

==

1

:

            cores

=

2

       

else

:

            cores *

=

2

def

compare_serial_vs_parallel

(

serial_work_ms

,

parallel_work_ms

,

num_cores

)

:

   

"""
    Demonstrate Amdahl's Law with the execution time of a concrete task
    serial_work_ms: time spent on the serial portion (milliseconds)
    parallel_work_ms: time spent on the parallelizable portion (single-core, milliseconds)
    num_cores: the number of available cores
    """

    total_single_ms

=

serial_work_ms + parallel_work_ms

    parallel_fraction

=

parallel_work_ms / total_single_ms

    parallel_time_multi

=

parallel_work_ms / num_cores

    total_multi_ms

=

serial_work_ms + parallel_time_multi

    speedup

=

total_single_ms / total_multi_ms

   

print

(

f

"\nConcrete task execution time simulation:"

)

   

print

(

f

" Serial portion time: {serial_work_ms} ms (cannot be sped up)"

)

   

print

(

f

" Parallelizable portion time: {parallel_work_ms} ms (single-core)"

)

   

print

(

f

" Parallel fraction: {parallel_fraction*100:.0f}%"

)

   

print

(

f

""

)

   

print

(

f

" Single-core total time: {total_single_ms} ms"

)

   

print

(

f

" {num_cores}-core total time: {total_multi_ms:.1f} ms (serial {serial_work_ms} + parallel {parallel_work_ms}/{num_cores} = {parallel_time_multi:.1f})"

)

   

print

(

f

" Actual speedup: {speedup:.2f}x"

)

   

print

(

f

" Amdahl upper bound: {amdahl_limit(parallel_fraction):.2f}x"

)

   

return

speedup

===== Main program =====

print

(

"="

60

)

print

(

" Amdahl's Law Calculator (runoob)"

)

print

(

"="

60

)

1. Basic comparison table

parallel_levels

=

[

0.50

,

0.75

,

0.90

,

0.95

,

0.99

]

core_list

=

[

1

,

2

,

4

,

8

,

16

,

32

,

64

,

128

,

256

]

print_comparison_table

(

parallel_levels

,

core_list

)

2. Marginal benefit analysis (using 75% parallelizable as an example)

analyze_diminishing_returns

(

0.75

,

max_cores

=

64

)

3. Concrete task simulation

print

(

"\n"

"="

60

)

print

(

" Real-world scenario simulation"

)

print

(

"="

60

)

Scenario 1: video rendering (highly parallelizable, 95%)

print

(

"\n[Scenario 1] Video rendering task"

)

print

(

" Serial portion: read files, initialize encoder = 10ms"

)

print

(

" Parallel portion: frame-by-frame rendering = 200ms (single-core)"

)

compare_serial_vs_parallel

(

10

,

200

,

16

)

Scenario 2: database transactions (moderately parallelizable, 50%)

print

(

"\n[Scenario 2] Database transaction processing"

)

print

(

" Serial portion: log writing, lock management = 50ms"

)

print

(

" Parallel portion: query execution = 50ms (single-core)"

)

compare_serial_vs_parallel

(

50

,

50

,

16

)

Scenario 3: pure computation (highly parallelizable, 99%)

print

(

"\n[Scenario 3] Scientific computing / matrix operations"

)

print

(

" Serial portion: result aggregation = 1ms"

)

print

(

" Parallel portion: matrix multiplication = 1000ms (single-core)"

)

compare_serial_vs_parallel

(

1

,

1000

,

16

)

4. Recommended optimal core count

print

(

"\n"

"="

60

)

print

(

" Practical advice: how to tell whether you need more cores?"

)

print

(

"="

60

)

Compute the "cost-performance" inflection point for different parallel fractions

print

(

f

" {'Parallel frac':>10} | {'2-core gain':>10} | {'4-core gain':>10} | {'8-core gain':>10} | {'16-core gain':>9} | {'Suggested cores':>10}"

)

print

(

"-"

70

)

for

pf

in

[

0.50

,

0.60

,

0.70

,

0.75

,

0.80

,

0.85

,

0.90

,

0.95

,

0.99

]

:

    ratios

=

[

]

    prev

=

1.0

    recommendations

=

[

]

   

for

cores

in

[

2

,

4

,

8

,

16

]

:

        sp

=

amdahl_speedup

(

pf

,

cores

)

        gain

=

(

sp / prev -

1

)

100

 

Percentage improvement relative to the previous level

        ratios.

append

(

gain

)

        prev

=

sp

       

if

gain

<

10

:

            recommendations.

append

(

cores

)

   

Advice: if the improvement is below 10%, it is not worth upgrading

    suggested

=

">=16 cores"

if

not

recommendations

else

f

"{recommendations[0]} cores are enough"

   

print

(

f

" {pf*100:>9.0f}% | {ratios[0]:>9.1f}% | {ratios[1]:>9.1f}% | {ratios[2]:>9.1f}% | {ratios[3]:>8.1f}% | {suggested:>10}"

)

print

(

)

print

(

" Rule of thumb:"

)

print

(

" - Daily office work, web browsing (low parallel fraction): 4-6 cores are enough"

)

print

(

" - Programming/compiling, light gaming: 6-8 cores"

)

print

(

" - Video rendering, 3D modeling: 8-16 cores"

)

print

(

" - Scientific computing, AI training: the more cores the better (but also consider video memory and memory bandwidth)"

)

Run the code above and you will see several key phenomena:

Interactive demo: multi-core task allocation animation

The demo below assigns 12 computing tasks to 1 core and 4 cores for processing. In 1-core mode the tasks are queued and executed serially; in 4-core mode 4 tasks are processed in parallel at the same time. Observe the difference in progress-bar fill speed and completion time between the two modes.

Multi-core task allocation animation demo (runoob)

Pending task queue (total

12

tasks)

1 core (serial processing)

Core 1 progress

4 cores (parallel processing)

Core 1 progress

Core 2 progress

Core 3 progress

Core 4 progress

1-core elapsed time

0.0 s

4-core elapsed time

0.0 s

Speedup

—


Multi-core in real-world applications

How the operating system schedules multiple cores

The scheduler in the operating system is responsible for assigning different processes and threads to different cores.

It considers:

Multi-core vs Hyper-Threading

Besides physical multi-core, there is another technology called Hyper-Threading (Hyper-Threading / SMT).

Hyper-Threading makes one physical core look like two "logical cores". When one thread is waiting for data (a cache miss), the core can switch to another thread and keep working.

Solution Hardware cost Performance Improvement Applicable scenarios
Physical multi-core High (requires duplicating the entire set of execution units) Nearly double (ideal case) Sustained high-load tasks
Hyper-Threading (SMT) Low (only needs extra registers + control logic) About 10%~30% Make full use of waiting time

Many modern CPUs use both technologies at the same time. For example, an Intel Core i7 may be "4 physical cores + Hyper-Threading = 8 logical cores".


Summary and check

One-sentence summary: multi-core executes different tasks in parallel by placing multiple CPU cores on a single chip. But Amdahl's Law points out that the actual speedup depends on the parallelizable fraction of the task—the serial portion sets the upper bound on speedup, and the more cores, the smaller the marginal benefit.

Self-test questions

  1. A task is 80% parallelizable. On a 4-core CPU, what speedup does Amdahl's Law give?

    Answer: S=0.2, P=0.8, N=4. Speedup = 1/(0.2 + 0.8/4) = 1/(0.2 + 0.2) = 1/0.4 = 2.5x. That is, 4 cores only bring a 2.5x speedup, with an efficiency of just 62.5%.

  2. If a task is almost 100% parallelizable, what is the theoretical upper bound? Why is it still not reached in practice?

    Answer: the theoretical upper bound is infinite (speedup = number of cores). But in practice it is limited by: (1) the overhead of splitting and aggregating tasks; (2) latency of synchronization and communication between cores; (3) contention for shared resources (such as memory bandwidth).

  3. Why can modern server CPUs have 64 or even 128 cores, while personal computers usually have only 4~16 cores?

    Answer: the requests handled by servers (such as web requests and database queries) are inherently highly parallel—each user request is independent of the others, so the parallelizable fraction is close to 100%. The everyday tasks of a personal computer (such as browser rendering and Office document processing) have limited parallelism; too many cores go unused and instead increase power consumption and cost.