0% found this document useful (0 votes)
7 views

Simulation of a Virtual CPU Executing Mathematical Functions in Python

This study presents a Python-based virtual CPU simulation that models multi-threaded execution of mathematical functions and visualizes CPU behavior. It explores the impact of core count on computational accuracy and efficiency, revealing real-world challenges in parallel processing and numerical stability. The findings serve as an educational tool for understanding CPU scheduling and debugging parallel algorithms, with practical applications in scientific computing.

Uploaded by

Ninaad Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Simulation of a Virtual CPU Executing Mathematical Functions in Python

This study presents a Python-based virtual CPU simulation that models multi-threaded execution of mathematical functions and visualizes CPU behavior. It explores the impact of core count on computational accuracy and efficiency, revealing real-world challenges in parallel processing and numerical stability. The findings serve as an educational tool for understanding CPU scheduling and debugging parallel algorithms, with practical applications in scientific computing.

Uploaded by

Ninaad Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Virtual CPU Simulation:

Executing Mathematical
Functions in Python
ChatGPT-4¹, Ninaad Das²

¹LLM AI Research & Computational Learning


²BSc Filmmaking, Direction

Abstract
In the field of computer science and computational modelling, an understanding of CPU behaviour
and workload distribution remains a crucial topic (Tanenbaum & Bos, 2014). In this study, a Python-
based virtual CPU environment has been developed to simulate multi-threaded execution of
mathematical functions and visualize the underlying computational behaviour (Silberschatz, Galvin,
& Gagne, 2018). The implementation utilizes multi-threading, real-time data visualization, and
interactive UI elements to simulate the execution of multiple mathematical functions across CPU
cores (McCool, Reinders, & Robison, 2012).

The study also investigates an unexpected phenomenon where the graphical representation of CPU
execution exhibits erratic distortions when the number of cores is set to a value different from four.
Through a detailed analysis of thread execution timing, aliasing effects, and function sampling
artifacts, it has been determined that these irregular patterns are not mere errors but reflections of
real-world computational challenges seen in parallel processing, load balancing, and numerical
stability in floating-point operations (Goldberg, 1991; Overton, 2001).

In addition to providing a detailed breakdown of CPU execution and visualization techniques, the
findings of this study hold significant educational and practical value. The visualization serves as a
teaching tool for multi-threaded execution, enabling students and researchers to gain insights into
the challenges of real-world CPU scheduling and numerical precision errors (Hennessy & Patterson,
2017). Furthermore, potential applications in debugging parallel computing algorithms, detecting
race conditions, and workload distribution analysis are discussed (McCool, Reinders, & Robison,
2012; LeCun, Bengio, & Hinton, 2015).

1. Introduction
The execution of mathematical operations virtual CPU environment using Python,
within a CPU is a fundamental aspect of enabling real-time observation of how multi-
computational systems. At the core of modern threaded workloads execute mathematical
computing lies the concept of parallel functions across multiple cores (Silberschatz,
execution, where multiple CPU cores handle Galvin, & Gagne, 2018).
separate tasks simultaneously (Tanenbaum &
Computational systems often rely on precise
Bos, 2014). This experiment aims to simulate a
function execution, yet real-world CPUs
introduce several factors that affect the core counts beyond four, leading to
accuracy, efficiency, and consistency of phase misalignments and aliasing
numerical computations. These factors errors (Oppenheim & Schafer, 2009).
include thread synchronization, floating-point
Through this experimental CPU simulation,
precision errors, aliasing effects, and load
the underlying computational principles
balancing challenges (Goldberg, 1991;
governing multi-core execution are explored.
Overton, 2001). While modern processors
Moreover, the observed graphical anomalies
incorporate optimizations to handle such
provide a unique opportunity to analyse
inefficiencies, an experimental simulation can
computational artifacts that frequently occur
reveal these behaviours explicitly (Hennessy
in scientific computing, signal processing,
& Patterson, 2017).
numerical modelling, and high-performance
This study presents a Python-based computing (Proakis & Manolakis, 2007).
simulation that:
This paper begins with an overview of Python
• Emulates CPU core behaviour by and the libraries used in the simulation,
assigning computational tasks to followed by an in-depth discussion of CPU
multiple threads (McCool, Reinders, & execution principles and a technical
Robison, 2012). breakdown of the implementation. The study
further investigates unexpected graphical
• Uses various mathematical functions
irregularities, offering insights into their
to model different types of CPU
computational significance and proposing
workloads (Hunter, 2007).
solutions to mitigate them. Finally, the
• Provides real-time graphical implications of this work for education,
visualization of CPU usage and debugging, and scientific computation are
execution timelines (Tufte, 2001). discussed (Hennessy & Patterson, 2017;
LeCun, Bengio, & Hinton, 2015).
• Observes unexpected computational
artifacts, particularly when increasing

many hardware-specific details, allowing


3. Python and Its Relevant developers to focus on problem-solving
Libraries rather than memory management or
instruction-level optimization (Van Rossum &
3.1 Overview of Python in Scientific Drake, 2009; Hennessy & Patterson, 2017).
Computing Despite its high-level nature, Python provides
Python has established itself as one of the access to efficient computational libraries,
most widely used programming languages in many of which utilize compiled C or Fortran
the field of scientific computing, data backends, enabling high-performance
analysis, and simulation modelling (Van execution (Oliphant, 2006). The development
Rossum & Drake, 2009). Due to its simplicity, of libraries such as NumPy, SciPy, Matplotlib,
versatility, and extensive ecosystem of and Pandas has significantly enhanced
libraries, it has become the preferred Python’s ability to handle large-scale data
language for computational research, processing and numerical simulations
artificial intelligence, and numerical analysis (Hunter, 2007; Jones, Oliphant, & Peterson,
(McKinney, 2017). Unlike low-level languages 2001). For this reason, Python has become
such as C and assembly, Python abstracts widely adopted in scientific fields, including
physics, engineering, computational biology, on problem-solving rather than memory
and financial modelling (McKinney, 2017; management or instruction-level
LeCun, Bengio, & Hinton, 2015). optimization. However, despite its high-level
nature, Python provides access to efficient
Python has established itself as one of the
computational libraries, many of which utilize
most widely used programming languages in
compiled C or Fortran backends, enabling
the field of scientific computing, data
high-performance execution.
analysis, and simulation modelling. Due to its
simplicity, versatility, and extensive For the purpose of this experiment, a
ecosystem of libraries, it has become the selection of Python libraries has been utilized
preferred language for computational to simulate a virtual CPU executing
research, artificial intelligence, and numerical mathematical functions in a multi-threaded
analysis. environment with real-time visualization. The
following sections provide a detailed
Unlike low-level languages such as C and
discussion of these libraries and their specific
assembly, Python abstracts many hardware-
roles in the implementation.
specific details, allowing developers to focus

3.2 Explanation of the Libraries Used


Several libraries have been used to simulate multi-core CPU execution, visualize real-time
performance, and handle user input through an interactive interface. Each of these libraries
contributes to a specific aspect of the simulation.

3.2.1 NumPy: Efficient Numerical Computation


The NumPy (Numerical Python) library plays a fundamental role in handling mathematical
operations, numerical arrays, and floating-point computations within the simulation. It has been
used in this project for:

• Efficient storage of CPU core usage values using NumPy arrays.

• Mathematical function evaluations for simulating CPU workloads.

• Handling floating-point arithmetic with improved performance over Python’s built-in lists.

The primary advantage of using NumPy over standard Python lists lies in its vectorized operations,
which allow batch processing of numerical data without explicit looping. This significantly improves
execution speed, particularly when updating CPU core usage values in real time.

For example, NumPy’s np.zeros(NUM_CORES) is used to initialize core usage values efficiently:

Similarly, mathematical function evaluations rely on NumPy’s trigonometric and exponential


functions, such as:
Without NumPy, these calculations would require explicit loops, which would significantly slow down
execution.

3.2.2 Matplotlib: Real-Time Graphical Visualization


The Matplotlib library is responsible for all real-time graphical representations of CPU execution.
The library is particularly well-suited for:

• Generating heatmaps to visualize CPU load distribution.

• Creating bar charts to represent CPU core utilization.

• Plotting function execution over time in a polar coordinate system.

• Animating CPU workload variations using Matplotlib’s FuncAnimation module.

One of the most critical components is the real-time animation of CPU execution, which is
accomplished using:

This continuously calls the update() function, allowing graphs to refresh dynamically as CPU
workload values change. Without this, static plots would not reflect real-time changes in CPU
behaviour.

Another essential visualization method is polar plotting, used to depict mathematical function
execution across CPU cores:

This plot provides insight into how different CPU cores handle workloads asynchronously, with each
function tracing a unique execution pattern.

3.2.3 Matplotlib Animation: Dynamic Data Representation


To simulate real-time CPU execution, Matplotlib’s animation module is employed. The
FuncAnimation class is used to update graphical elements at fixed time intervals, ensuring that CPU
core usage is dynamically visualized as the simulation progresses.
Key features of the animation module include:

• Frame-based updates: The update() function is called every 50 milliseconds, ensuring that
graphs remain synchronized with CPU activity.

• Live function tracing: Execution of CPU workload functions is displayed progressively over
time, similar to how real CPU cycles process tasks in small increments.

The following example illustrates how the heatmap is refreshed in real time:

This results in a heatmap visualization, where high CPU usage regions appear bright, while low
usage areas remain dark, mimicking real-world thermal maps of CPU workloads.

3.2.4 Threading: Simulating Multi-Core Execution


The threading module is one of the most crucial components of the virtual CPU simulation, as it
emulates parallel processing by distributing tasks across multiple threads. Each CPU core is
represented by an independent thread, executing a mathematical function concurrently.

Python’s Global Interpreter Lock (GIL) generally restricts parallel execution within a single Python
process, but since the mathematical function computations are lightweight, multi-threading remains
an effective approach for simulation.

A thread is assigned to each core using:

This ensures that each CPU core:

• Executes a unique mathematical function instance.

• Runs continuously, mimicking real-world CPU cycles.

• Updates global core usage variables, allowing visualization in real-time.

To prevent data corruption from concurrent modifications, a threading.Lock is used:


This synchronizes access to shared data, ensuring that multiple CPU cores do not overwrite each
other's values simultaneously.

3.2.5 Tkinter: User Interface for Core and Function Selection


The Tkinter library is used to build the Graphical User Interface (GUI) for user interaction. This
allows users to dynamically select the number of CPU cores and the function type to be executed.

A drop-down menu enables function selection:

Similarly, another drop-down allows users to configure core count dynamically:

The user interface ensures flexibility in configuring the simulation, making it an interactive and
educational tool for understanding CPU workload distribution.

4. Virtual CPU: Code Structure


The simulation of a virtual CPU in Python requires an architectural design that enables the execution
of mathematical functions across multiple simulated CPU cores. A multi-threaded environment is
used to mimic the behavior of real-world multi-core processors, while real-time data visualization
allows for the graphical representation of computational workloads.

At the core of this architecture lies:

• A set of CPU cores, each represented by an individual thread.

• A task execution framework that ensures each core is assigned a function to evaluate.

• A shared memory structure to store and update computational data.

• A graphical visualization component that tracks the execution process in real time.

In this section, the data structures, execution model, and CPU thread simulation techniques are
explored in depth.

Efficient data structures are crucial to managing the state of the virtual CPU. Several key variables
and arrays are used to store CPU workload distribution, function results, and execution timelines.
4.1 CPU Core Representation
Each CPU core is represented using an index-based approach, where the number of available cores is
user-defined and dynamically allocated. The core workload is stored in a NumPy array, which allows
efficient numerical computation and real-time updates:

Additionally, function execution data is stored for each core as a list of tuples:

This ensures that the simulation maintains a history of execution values, allowing dynamic
visualization of function behaviour over time.

4.2 Task Queue Management


To simulate workload assignment in a real CPU, a task queue is maintained for each core:

This queue-based approach allows for:

• Dynamic task scheduling, ensuring each core has an assigned function.

• Simulation of real-world CPU behaviour, where threads process functions in a queue.

• Handling multiple workload patterns, allowing users to experiment with different


computational loads.

Each core fetches a task from its queue, evaluates the assigned function, and updates the stored
values accordingly.

4.3 Multi-Threading for Simulating CPU Execution


Since modern CPUs operate in a multi-threaded environment, this behaviour is replicated using
Python’s threading module. Each core runs as an independent thread, executing its assigned
mathematical function while updating CPU usage statistics in real time.

4.3.1 Thread Creation and Execution

A thread is spawned for each CPU core, ensuring concurrent execution of tasks:
Each thread runs the cpu_task() function, which evaluates a mathematical function assigned to that
core. The daemon thread setting ensures that all threads terminate when the main program exits,
preventing orphan processes.

4.3.2 Function Execution and Thread Synchronization

Each core executes its assigned function in a loop, continuously updating CPU usage values. The
function execution process is defined as:

In this execution model:

• Each core continuously evaluates a function, updating its workload data.

• A phase shift (offset) is introduced, ensuring distinct patterns per core.

• A data lock (data_lock) is used to prevent race conditions, ensuring that multiple threads
do not overwrite shared data simultaneously.

This mechanism mimics real-world CPU workloads, where each core processes a task independently,
updating execution results in a shared memory structure.
4.4 Mathematical Functions Simulated in CPU Execution
To provide a varied computational workload, multiple mathematical functions have been
incorporated into the simulation. Each function represents a distinct type of CPU workload,
mimicking different real-world processing tasks.

4.4.1 Overview of the Mathematical Functions

The following functions have been selected for simulation:

1. Rotary Flower:

o Simulates high-frequency CPU workloads with periodic oscillations.

o Evaluates sinusoidal functions to model real-world signal processing tasks.

2. Exponential Decay:

o Models computational tasks that involve gradual workload reduction.

o Simulates real-world memory decay functions and optimization tasks.

3. Chaotic Wave:

o Introduces randomized oscillations, mimicking computational noise.

o Simulates real-world applications like noise filtering and chaotic system modeling.

4. Oscillating Spiral:

o Mimics CPU workload variations over time.

o Useful for testing CPU performance under varying load conditions.

5. Random Spikes:

o Introduces unpredictable CPU load variations, simulating irregular workloads.

o Useful for stress-testing real CPU environments.

Each function is dynamically assigned to a core, allowing users to experiment with different CPU
workload distributions in real time.

4.5 Real-Time Data Processing and Updates


Since real-world CPU workloads evolve dynamically, the simulation must continuously update
execution results. This is achieved through:

• Live updates to core_usage[] arrays, reflecting real-time function evaluations.

• Graph refresh rates set at 50ms intervals, ensuring smooth visualization.

• Historical function execution storage, allowing real-time tracking of function evolution.


The combination of multi-threaded execution, function variation, and real-time visualization
ensures that the virtual CPU accurately mimics real-world CPU behaviour.

5. Execution of Mathematical Operations in a CPU


5.1 How a Real CPU Executes Mathematical Functions
Modern Central Processing Units (CPUs) execute mathematical operations using a combination of
instruction pipelines, floating-point units (FPUs), and vectorized execution mechanisms. These
components ensure efficient processing of arithmetic calculations, including trigonometric,
logarithmic, and polynomial computations.

The execution of mathematical functions in a real CPU follows these key stages:

1. Instruction Fetching: The CPU retrieves the mathematical instruction (e.g., sin(x), cos(x), or
exp(x)) from memory.

2. Instruction Decoding: The retrieved instruction is translated into machine code, which
determines how the CPU processes the computation.

3. Operand Fetching: The required data (input variables such as x) is loaded from registers,
cache, or RAM into the arithmetic execution unit.

4. Computation Execution: The CPU performs the mathematical operation using its floating-
point arithmetic unit (FPU) or vectorized SIMD (Single Instruction, Multiple Data)
instructions.

5. Write-Back Stage: The computed result is stored in registers or memory for further use.

5.1.1 Breakdown of Floating-Point Computation

Most mathematical functions require floating-point precision due to the nature of real numbers. The
IEEE 754 standard defines how floating-point numbers are represented and computed in a CPU.

For a floating-point number F, its representation in a CPU follows the form:

where:

• SSS is the sign bit (0 for positive, 1 for negative).

• MMM is the mantissa, representing the significant digits.

• EEE is the exponent, defining the magnitude of the number.

For example, the computation of sinusoidal functions like sin(x) follows the Taylor Series Expansion:
The CPU evaluates this using a series of floating-point multiplications, divisions, and summations,
optimized through instruction pipelining and hardware acceleration.

5.1.2 Execution Pipeline for Trigonometric Functions

For a function like cos(x), the CPU executes it using the CORDIC (COordinate Rotation DIgital
Computer) algorithm or LUT (Look-Up Tables) for faster retrieval. The LUT approach precomputes
cosine values for common angles, reducing execution time.

5.2 How the Virtual CPU Simulation Processes Mathematical Functions


The Python-based virtual CPU executes functions differently than a physical CPU, since it relies on
software-based mathematical approximations and NumPy computations.

Instead of instruction pipelines and dedicated floating-point hardware, Python uses:

1. Software-based function execution (NumPy-backed computations).

2. Threaded execution model, where each core simulates an independent workload.

3. Global memory storage, where computed results are continuously updated.

5.2.1 Function Execution in the Simulation

Each virtual CPU core executes a user-selected mathematical function. The function evaluations
occur as follows:

Rotary Flower Function

• Used to model waveform-like oscillations across CPU cores.

• The sixfold periodicity ensures a smooth curve when properly sampled.

• Erratic behavior arises when phase shifts are improperly handled (discussed in
Observations).

Exponential Decay Function

• Simulates gradual CPU workload decay over time.

• Models real-world computational fading effects seen in signal attenuation.


Chaotic Wave Function

• Represents non-linear oscillations in chaotic systems.

• Produces erratic yet deterministic patterns in execution visualization.

Each function is continuously evaluated and updated by CPU threads in the simulation loop.

5.3 Comparison of Real and Simulated Execution


5.3.1 Floating-Point Precision and Error Accumulation

Real CPUs use hardware-level floating-point optimizations, whereas Python relies on software-
based floating-point arithmetic using the IEEE 754 standard.

This introduces precision errors in function evaluation, particularly in:

• Small-angle approximations where Taylor Series truncation leads to inaccuracies.

• Exponential function approximations where rounding errors compound over time.

• Chaotic function behaviour, which amplifies small numerical inconsistencies.

5.3.2 Thread Synchronization and Scheduling Differences

• Real CPUs use hardware-level thread scheduling to optimize task execution.

• The simulation relies on Python’s GIL (Global Interpreter Lock), which prevents true
parallelism.

• Execution order is not strictly deterministic, leading to observable artifacts in function


visualization.

5.4 Implications of Function Execution on CPU Visualization


The unique behaviors observed in graphical representations of function execution arise due to:

1. Thread Execution Offsets:

o Inconsistent phase offsets across cores lead to destructive interference patterns in


polar graphs.

2. Sampling Rate and Aliasing Effects:

o Improper function sampling results in graph distortions, resembling signal aliasing in


digital systems.

3. Floating-Point Rounding Errors:


o Small numerical inaccuracies propagate, leading to unexpected function deviations
over time.

These effects contribute to the erratic visual behavior observed when selecting different core
counts, which will be analyzed in the Observations section.

6. Building the UI and Visualization


6.1 The Best Approach to Designing the UI
A graphical user interface (GUI) serves as the primary control panel for interacting with the virtual
CPU simulation. It enables users to:

• Select the mathematical function to be executed by the CPU.

• Adjust the number of CPU cores, dynamically altering the simulation complexity.

• Initiate the real-time visualization of CPU workload distribution and function execution.

The Tkinter library has been used to construct the GUI, as it provides a lightweight yet flexible
interface for handling user input. Unlike complex GUI frameworks (such as PyQt or Kivy), Tkinter is
sufficiently fast for scientific visualization and does not introduce unnecessary overhead.

6.1.1 UI Layout and Components

The UI consists of the following key elements:

1. Dropdown Menu for Function Selection:

o Users can select one of the predefined mathematical functions for execution.

o Implemented using ttk.Combobox, allowing dynamic updates.

2. Dropdown Menu for Core Count Selection:

o Enables users to adjust the number of CPU cores from 1 to 8.

o Dynamically updates CPU thread execution upon selection.

3. Start Button to Launch Simulation:

o Ensures that only one simulation instance runs at a time.

o Terminates previous instances before starting a new one, preventing redundant


executions.

These components are arranged in a compact, user-friendly layout, ensuring efficient interaction
without excessive complexity.
The use of event-driven updates (bind method) ensures that changes in function selection or core
count immediately trigger updates in the underlying simulation.

6.2 Live Visualization of Core Usage


Real-time visualization is essential for observing how different mathematical functions interact with
simulated CPU cores. This is accomplished using Matplotlib, which dynamically updates visual
elements through animation functions.

6.2.1 Components of the Visualization

Four types of graphs are used to depict different aspects of CPU execution:

1. CPU Core Heatmap:

o Provides an intensity-based representation of CPU load per core.

o Utilizes a hot color map, where higher loads appear brighter and idle cores appear
darker.

2. CPU Load Bar Graph:

o Displays the real-time CPU usage for each core using bar heights.

o Allows direct comparison of workload distribution across cores.

3. Polar Function Execution Plot:

o Displays the execution of mathematical functions in polar coordinates.


o Reveals unexpected pattern distortions when specific core counts are used.

4. CPU Task Execution Timeline:

o Visualizes the temporal execution of CPU tasks in a Gantt chart format.

o Highlights function execution order and workload balancing behavior.

Each visualization is updated every 50 milliseconds to ensure real-time feedback.

6.2.2 Dynamic Updates Using Matplotlib Animation

To maintain real-time graph refresh rates, Matplotlib’s FuncAnimation is employed:

The update() function dynamically refreshes all graphs to reflect changes in CPU workload.

By continuously calling update(), the system ensures that all graphs remain synchronized with the
underlying CPU workload data.
6.3 Challenges in UI and Visualization Development
6.3.1 Managing Thread Safety in GUI Updates

A key challenge in GUI-based simulations is ensuring thread safety when updating shared data
structures. Python’s Tkinter is not thread-safe, meaning that concurrent access to UI elements from
multiple threads can lead to inconsistent state updates.

To prevent crashes and unpredictable behavior:

• Thread locks (threading.Lock) are used to synchronize data updates.

• Global variables are modified only within locked code blocks, ensuring exclusive access.

6.3.2 Handling High-Frequency Data Updates

Since CPU workloads change every 50ms, visualization updates must remain smooth and non-
blocking. Strategies used include:

• Reducing redundant computations by limiting the number of stored data points


(function_data[core_id] stores only the last 30 values).

• Using efficient NumPy operations instead of explicit loops.

• Clearing previous plot frames (plt.clf()) to prevent excessive memory consumption.

7. Observations: Irregularities in Circular Graphs


7.1 Dry Run of the Circular Graph with Different Core Counts
During the execution of the simulation, unexpected distortions were observed in the polar function
execution plot whenever the number of CPU cores was set to a value different from 4. These
distortions appeared as erratic deviations from the expected smooth function curves, creating
irregular shapes instead of the anticipated symmetrical patterns.

7.1.1 Expected Behavior of the Circular Graph

Under normal conditions, the polar plot should display smooth and periodic execution patterns
based on the selected function. Some examples include:

• Rotary Flower Function (sin(6θ) + 0.5 sin(3θ)) forming a symmetric six-petal structure.

• Exponential Decay Function (e^(-θ/50) * sin(θ)) generating a gradually shrinking spiral.

• Chaotic Wave Function (sin(θ) * cos(θ/2) + sin(θ/3)) producing a deterministic, oscillatory


pattern.

These expected patterns are observed when the number of CPU cores remains at 4 (the default
setting). However, when the core count is changed to any other value, the graph becomes erratic
and loses its expected shape.

7.1.2 Observed Anomalies at Core Counts ≠ 4

Whenever the number of cores was modified, the following distortions were observed:

1. Misalignment of periodic functions, causing interference patterns.


2. Abrupt discontinuities in the execution curves.

3. Random distortions, despite deterministic function definitions.

These anomalies suggest a systematic mathematical inconsistency introduced by changing the core
count, which requires further analysis.

7.2 Causes of the Irregular Graph Behaviour


A closer examination of the underlying computations reveals three primary causes for the erratic
graph behaviour when changing core counts:

7.2.1 Phase Shift Mismatches in Function Execution

Each core executes its function with a fixed phase offset, given by:

where i represents the core index. This offset ensures that cores execute slightly different versions of
the function to prevent complete overlap.

Let us analyse the Rotary Flower function for different core values. The function is defined as:

If we assume a core count of 4 (default case):

Here, all cores maintain a stable and smooth function execution, allowing the expected polar plot to
appear correctly.

Now, if we change the core count to 6:


The additional two cores push function evaluations into non-harmonic offsets, leading to phase
mismatches. These misaligned values create interference, distorting the polar plot.

This is equivalent to destructive wave interference, where periodic functions partially cancel each
other out, disrupting the original function shape.

7.2.2 Floating-Point Precision Errors in Numerical Computation

Python stores numbers using the IEEE 754 floating-point standard, which introduces rounding
errors. These errors accumulate as more cores contribute to the simulation.

For example, at 4 cores:

Floating-point representation in 64-bit precision results in minor rounding discrepancies:

However, at 6 cores, additional floating-point shifts lead to:

Internally, these values may be stored as:

The additional precision loss causes slight numerical inconsistencies, altering the computed function
values in unpredictable ways.

7.2.3 Sampling Rate and Aliasing Effects

The Nyquist-Shannon theorem states that a function must be sampled at at least twice its highest
frequency to avoid aliasing.

For the Rotary Flower function:

the highest frequency component is 6. Thus, we need a sampling rate of at least 12 samples per
cycle.

With 4 cores, the effective sampling rate remains stable. However, changing the core count
introduces uneven sampling, leading to aliasing distortions.

7.3 Understanding the Computational Significance of These Errors


These findings are directly applicable to:

1. Parallel Processing Debugging: Identifying race conditions and phase mismatches.


2. Scientific Simulations: Reducing floating-point precision loss in iterative computations.

3. Digital Signal Processing: Preventing aliasing artifacts in waveform sampling.

7.4 Solutions to Preserve Graph Accuracy


To prevent distortions:

1. Modify the Offset Calculation

o Instead of i × 0.05, use:

o This ensures harmonic phase distribution, preventing destructive interference.

2. Increase Floating-Point Precision

o Use float128 instead of float64 to reduce rounding errors.

3. Apply Oversampling Techniques

o Evaluate more points and average results to minimize aliasing effects.

8. Solutions: Fixing the Graph Distortions


8.1 Steps to Maintain Graph Integrity at Higher Core Counts
To address the erratic distortions in the polar function execution graph, three primary solutions
have been developed:

1. Normalizing Phase Offsets Dynamically to maintain uniform function execution across cores.

2. Increasing Floating-Point Precision to minimize numerical inaccuracies that amplify


distortions.

3. Applying Oversampling Techniques to prevent aliasing and discontinuities.

Each solution is examined in depth below.

8.2 Solution 1: Normalizing Phase Offsets Dynamically


8.2.1 Identifying the Issue
The phase offset used in function execution was previously defined as:

where i is the core index.

This resulted in non-uniform phase shifts when the number of cores was changed, creating
destructive interference in the function outputs.

8.2.2 Implementing an Evenly Distributed Offset

To maintain uniform function execution across any core count, the offset should be harmonically
distributed in a 2π range:

This ensures that each core remains equidistantly spaced around the unit circle, preventing
interference patterns.

Python Implementation

Results of the Offset Correction

• Uniformly spaced function evaluations prevent destructive interference.


• The expected function shapes (e.g., spirals, oscillations) remain stable regardless of core
count.

• The phase distribution now scales dynamically, avoiding distortions.

8.3 Solution 2: Increasing Floating-Point Precision


8.3.1 Identifying the Issue

Python’s default floating-point representation (float64) introduces rounding errors over successive
computations. These errors accumulate when multiple cores execute trigonometric and exponential
functions, leading to erratic graph distortions.

For example, at NUM_CORES = 6, a floating-point approximation results in:

This seemingly small deviation compounds over thousands of iterations, altering the execution
pattern unpredictably.

8.3.2 Implementing Higher-Precision Computation

Using 128-bit precision (float128) can significantly reduce rounding errors.

Python Implementation

Results of Higher Precision Computation

• Eliminates floating-point accumulation errors, stabilizing execution patterns.

• Preserves function continuity over time, ensuring smooth visualizations.

• Reduces cumulative phase shifts that previously led to unexpected distortions.

8.4 Solution 3: Applying Oversampling Techniques


8.4.1 Identifying the Issue

The Nyquist-Shannon theorem states that a function must be sampled at at least twice its highest
frequency to prevent aliasing artifacts.
For the Rotary Flower function:

the highest frequency component is 6, requiring a sampling rate of at least 12 samples per cycle.

However, when core counts were changed, the sampling rate was inconsistent, leading to
distortions.

8.4.2 Implementing Oversampling and Averaging

By increasing the sampling rate (evaluating multiple points per step) and averaging results, aliasing
can be mitigated.

Python Implementation

Results of Oversampling and Averaging

• Minimizes aliasing artifacts, preserving function shape integrity.

• Improves function continuity, reducing abrupt jumps in the graph.

• Produces smooth, stable visualizations regardless of core count.

8.5 Final Comparison: Before and After Fixes


8.6 Practical Implementation in Real-World Applications

These solutions have broader applications beyond this simulation:

1. Parallel Computing:

o Proper phase alignment prevents race conditions in multi-threaded processing.

2. Digital Signal Processing:

o Oversampling reduces aliasing in audio and image processing.

3. Scientific Simulations:

o High-precision floating-point arithmetic minimizes error propagation in numerical


models.

9. Practical Applications of the Experiment


9.1 Understanding CPU Workloads in Real Computing
The virtual CPU simulation provides a real-time visualization of how CPUs distribute workloads,
making it an effective tool for understanding parallel processing and multi-threaded execution.

9.1.1 Workload Distribution in Multicore CPUs

Modern multi-core processors distribute computational tasks across multiple cores to optimize
performance. The behavior of this workload distribution depends on:

• Thread scheduling algorithms (e.g., Round Robin, FIFO, Dynamic Load Balancing).

• CPU architecture (e.g., SIMD, Hyper-Threading, NUMA-based systems).

• Type of workload (e.g., Floating-Point vs. Integer Operations).

This simulation provides a visual representation of such CPU workload balancing, helping engineers
and researchers analyze how different workloads are processed in parallel environments.

9.1.2 Identifying Bottlenecks in CPU Utilization

Using the heatmap and bar graph visualizations, the simulation allows users to:

• Detect imbalanced workload distribution across cores.


• Analyze the effect of thread synchronization issues on performance.

• Study the impact of increasing or decreasing core count on computational efficiency.

This knowledge can be used for fine-tuning performance optimization strategies in real-world CPU-
intensive applications.

9.2 Educational Use Cases


The simulation has significant potential as an educational tool in various disciplines, including
computer science, engineering, mathematics, and digital signal processing.

9.2.1 Teaching Parallel Computing Concepts

Many students struggle with understanding multi-threading, synchronization, and load balancing.
By providing real-time visual feedback, this simulation enables:

• A tangible representation of parallel execution.

• Step-by-step analysis of mathematical function execution across cores.

• Understanding of numerical precision errors in floating-point computations.

This approach aligns with active learning methodologies, which are more effective than traditional
textbook-based explanations.

9.2.2 Demonstrating Floating-Point Arithmetic Issues

Numerical precision errors in floating-point arithmetic are a critical concern in fields such as
scientific computing and machine learning. This simulation can be used to:

• Demonstrate how precision loss occurs over iterative calculations.

• Analyse real-world issues like catastrophic cancellation and round-off errors.

• Teach methods to mitigate these issues, such as increasing precision (float128) or using
compensation algorithms.

These concepts are crucial for students and researchers working with high-performance computing,
numerical modelling, and simulations.

9.3 Practical Use in Computational Research


Beyond education, the simulation has practical applications in computational research and industry.

9.3.1 Debugging Parallel Workloads

The erratic behaviour observed in the polar function execution graph is directly related to real-world
multi-threaded computation challenges, such as:

• Race conditions in concurrent execution.


• Phase misalignment in wave-based computations (e.g., signal processing, RF
communications).

• Unintended destructive interference in computational physics simulations.

By using this simulation, developers can analyse and debug these issues visually before deploying
multi-threaded applications in production environments.

9.3.2 Optimizing CPU Performance in Scientific Computing

Scientific computing applications, such as fluid dynamics simulations, molecular modelling, and AI
training, require efficient load balancing across CPU cores.

Using the virtual CPU simulator, researchers can:

• Experiment with different core allocations and task distributions.

• Analyse function execution times under various conditions.

• Optimize computation-heavy tasks by identifying workload inefficiencies.

This is particularly useful in supercomputing environments, where optimizing CPU utilization can
lead to significant performance improvements.

9.4 Applications in Digital Signal Processing (DSP)


The observed aliasing effects in the simulation directly parallel sampling artifacts in DSP. These
insights can be used in:

• Audio processing: Preventing waveform distortions in sound engineering.

• Image processing: Avoiding aliasing artifacts in high-resolution image rendering.

• RF signal analysis: Ensuring accurate frequency representation in communication systems.

By studying how function execution patterns change with different sampling rates (core counts),
engineers can apply these concepts to real-world DSP optimizations.

9.5 Extending the Simulation for Future Research


The simulation provides a strong foundation for further research in the field of computational
science and parallel processing. Some possible extensions include:

9.5.1 GPU Acceleration and CUDA Integration

Currently, the simulation is CPU-based, but GPU acceleration using CUDA or OpenCL could be
implemented to:

• Simulate thousands of parallel function executions.

• Analyse workload distribution in modern GPUs.

• Optimize AI training models that rely on GPU parallelization.


9.5.2 Real-Time Performance Monitoring for Live Applications

The visualization techniques used in this experiment could be adapted for real-time system
monitoring, allowing:

• Live CPU utilization tracking in production servers.

• Workload visualization for AI inference models.

• Dynamic load balancing analysis in cloud computing platforms.

9.6 Summary of Practical Applications

10. Conclusion
10.1 Insights Gained from the Simulation
This study successfully developed a Python-based virtual CPU simulation, demonstrating the
principles of multi-threaded execution, numerical precision, and real-time workload visualization.
The key findings from the experiment include:

1. Multi-Core Execution Visualization

o The simulation provided an interactive way to observe how CPU cores process
mathematical functions in parallel.

o Real-time graphs (heatmaps, bar graphs, and polar plots) highlighted workload
distribution across CPU cores.

2. Effects of Floating-Point Precision Errors

o Computational errors due to floating-point approximations accumulated over


successive iterations.

o Higher precision (float128) significantly improved function accuracy and reduced


erratic distortions.

3. Unexpected Graph Distortions Due to Core Count Changes


o The polar function execution graph became unstable whenever the core count was
modified from the default.

o This was traced to phase misalignment, aliasing, and numerical precision errors.

4. Real-World Computational Parallels

o The anomalies observed closely resemble real-world parallel computing issues, such
as:

▪ Thread synchronization problems in multi-core CPUs.

▪ Aliasing effects in digital signal processing.

▪ Wave interference patterns in physics simulations.

These insights confirm that simulated CPU workload distributions can provide valuable
computational lessons in both theoretical and practical domains.

10.2 Significance of the Erratic Graph Behavior in Practical Computation

While initially perceived as an anomaly, the irregular graph distortions revealed deeper
computational insights applicable to various fields:

10.2.1 Debugging and Optimization in Parallel Computing

• The graph distortions visually represent thread synchronization issues, making it an


effective debugging tool for parallel workloads.

• Future researchers can extend this model to analyze race conditions and deadlocks in multi-
threaded systems.

10.2.2 Improving Accuracy in Scientific Computing

• The effects of floating-point errors observed in this study parallel real-world computational
physics and numerical modeling issues.

• High-precision computing techniques (such as Kahan summation or quadruple-precision


arithmetic) could further improve accuracy.

10.2.3 Applications in AI and Machine Learning

• GPU-based deep learning models face similar precision loss when training on large datasets.

• This experiment highlights the need for phase-aware execution models to prevent
numerical drift in AI computations.

Thus, the findings have broader implications for real-world applications, beyond just CPU
simulations.

10.3 The Simulation’s Role as an Educational Auxiliary

The graphical visualization of multi-core CPU workloads makes this simulation an ideal educational
tool for teaching:
1. Computer Science Students

o Multi-threading concepts (parallel execution, thread scheduling, and workload


balancing).

o Real-time CPU performance monitoring and debugging techniques.

2. Mathematics and Physics Researchers

o How numerical errors propagate in iterative computations.

o The impact of aliasing and phase shifts in function evaluations.

3. Engineering and AI Practitioners

o Techniques for optimizing multi-core execution in high-performance computing.

o Strategies for minimizing precision loss in AI model training.

By incorporating live function visualization, this experiment provides a hands-on learning


experience, bridging the gap between theory and real-world computation.

10.4 Future Research Directions


10.4.1 GPU Acceleration for Large-Scale Parallelism

• Extending the model to GPU-based parallel execution using CUDA or OpenCL would allow
massively parallel processing.

• This could be used to analyze AI workloads, fluid dynamics simulations, and cryptographic
computations.

10.4.2 Real-Time System Monitoring Applications

• The heatmap and bar graph visualizations could be adapted for live CPU performance
tracking in server farms and cloud computing platforms.

• Integration with real-time telemetry systems could provide advanced workload diagnostics.

10.4.3 Adaptive Numerical Precision Techniques

• Implementing adaptive precision scaling (e.g., switching between float64 and float128
based on error thresholds) could enhance computational accuracy.

• This technique is particularly useful in climate modeling, astrophysics simulations, and


quantum computing.

These future expansions would significantly enhance the practical utility of the current virtual CPU
simulation.

10.5 Final Thoughts


This experiment provided an interactive, real-time approach to understanding multi-core execution,
numerical precision, and function visualization. The key takeaways include:
• Unexpected computational artifacts can provide valuable debugging insights in parallel
computing.

• Floating-point precision and phase alignment are crucial for ensuring stable function
execution.

• Graphical representations of CPU execution serve as a powerful tool for education and
computational research.

By addressing both practical and theoretical aspects of CPU workload simulation, this study bridges
the gap between scientific computing, engineering applications, and educational tools.

11. References
The following references include scientific literature, official documentation, and research papers
relevant to the topics covered in this study. They provide supporting evidence for the computational
phenomena observed in the experiment and extend the discussion on multi-threaded execution,
numerical precision, and real-time visualization techniques.

11.1 Multi-Threaded Execution and Parallel Computing


1. Tanenbaum, A. S., & Bos, H. (2014). Modern Operating Systems (4th Edition). Pearson
Education.

o Discusses CPU scheduling, multi-threading, and parallel processing principles in


modern computing.

2. Silberschatz, A., Galvin, P. B., & Gagne, G. (2018). Operating System Concepts (10th Edition).
Wiley.

o Covers thread synchronization, scheduling algorithms, and load balancing in CPU


execution.

3. Hennessy, J. L., & Patterson, D. A. (2017). Computer Architecture: A Quantitative Approach


(6th Edition). Morgan Kaufmann.

o Explains pipeline execution, floating-point precision, and computational efficiency


in modern processors.

4. McCool, M., Reinders, J., & Robison, A. (2012). Structured Parallel Programming: Patterns for
Efficient Computation. Elsevier.

o Introduces strategies for optimizing parallel workloads in CPU and GPU computing.

11.2 Floating-Point Arithmetic and Precision Errors


5. Goldberg, D. (1991). What Every Computer Scientist Should Know About Floating-Point
Arithmetic. ACM Computing Surveys, 23(1), 5-48.

o A fundamental paper on floating-point precision errors, rounding effects, and their


impact on computation.

6. Overton, M. L. (2001). Numerical Computing with IEEE Floating Point Arithmetic. SIAM.
o Discusses numerical accuracy, precision loss, and error propagation in scientific
computing.

7. Kahan, W. (1996). The Baleful Effects of Computer Benchmarks upon Applied Mathematics,
Physics, and Chemistry. University of California, Berkeley.

o Analyzes how round-off errors affect iterative calculations in physics simulations.

11.3 Digital Signal Processing and Aliasing Artifacts


8. Oppenheim, A. V., & Schafer, R. W. (2009). Discrete-Time Signal Processing (3rd Edition).
Pearson.

o Covers Nyquist-Shannon sampling theory, aliasing effects, and numerical filtering


techniques.

9. Proakis, J. G., & Manolakis, D. G. (2007). Digital Signal Processing: Principles, Algorithms, and
Applications (4th Edition). Pearson.

o Discusses real-time waveform processing, oversampling, and interpolation


techniques.

11.4 Real-Time Data Visualization and Computational Simulations


10. Hunter, J. D. (2007). Matplotlib: A 2D Graphics Environment. Computing in Science &
Engineering, 9(3), 90-95.

• Explores the use of Matplotlib for dynamic visualization and function plotting.

11. Tufte, E. R. (2001). The Visual Display of Quantitative Information. Graphics Press.

• Examines principles of effective data visualization for scientific computation.

12. Van Rossum, G., & Drake, F. L. (2009). The Python Language Reference Manual. Python
Software Foundation.

• Official documentation of Python, including multi-threading and NumPy-based numerical


computation.

11.5 Practical Applications in AI, Supercomputing, and Cloud Computing


13. LeCun, Y., Bengio, Y., & Hinton, G. (2015). Deep Learning. Nature, 521(7553), 436-444.

• Explains how floating-point precision impacts AI training on GPU-based architectures.

14. Dean, J., & Ghemawat, S. (2008). MapReduce: Simplified Data Processing on Large Clusters.
Communications of the ACM, 51(1), 107-113.

• Discusses parallel computing in cloud-based systems and multi-core processors.

15. OpenAI. (2020). Scaling Laws for Neural Language Models.

• Analyzes the impact of parallel computation on large-scale AI model training.


11.6 Official Documentation for Implementation
16. NumPy Developers. (2023). NumPy: The Fundamental Package for Scientific Computing with
Python.

• Documentation on NumPy’s mathematical operations, floating-point precision, and high-


performance array processing.

17. Python Software Foundation. (2023). Python Threading Module Documentation.

• Covers multi-threading techniques and synchronization mechanisms in Python.

18. Matplotlib Developers. (2023). Matplotlib Animation API.

• Describes the implementation of dynamic function visualization using Matplotlib's


FuncAnimation module.
Appendix: Glossary of Technical Terms
This appendix provides definitions for technical terms used throughout the article but not
explicitly explained in detail. These definitions ensure clarity and enhance understanding of
the computational, mathematical, and visualization concepts discussed.

A
• Aliasing
• Aliasing is a phenomenon in signal processing and numerical computation where
an insufficient sampling rate leads to distorted or misleading data representations.
It occurs when a signal is sampled at a rate lower than twice its highest frequency
component, violating the Nyquist-Shannon sampling theorem.
• In this experiment, aliasing was responsible for the erratic distortions observed in
the polar function execution graph when changing the number of CPU cores.
• Anti-Aliasing
• A set of techniques used to reduce aliasing artifacts in computational systems. In
this experiment, oversampling and averaging were proposed to mitigate aliasing
effects in function execution graphs.

B
• Bottleneck (Computational Bottleneck)
• A bottleneck occurs when one part of a system limits overall performance due to
resource constraints (e.g., CPU, memory, or I/O speed). In parallel computing,
bottlenecks can arise from uneven workload distribution, thread synchronization
delays, or memory bandwidth limitations.
• The heatmap and bar graph in the experiment visually indicated potential CPU
bottlenecks caused by uneven load distribution.

C
• Cache (CPU Cache)
• A small, high-speed memory storage inside a CPU used to store frequently accessed
data and instructions. Cache memory significantly reduces access latency compared
to retrieving data from RAM.
• Although the experiment does not simulate CPU caching mechanisms, real-world
CPUs rely heavily on caches to optimize function execution speed.
• Catastrophic Cancellation
• A numerical error that occurs when subtracting two nearly equal numbers, leading to
a significant loss of precision in floating-point arithmetic.
• This error was indirectly referenced in the study when discussing floating-point
precision issues that contributed to erratic function plots.
D
• Daemon Thread
• A thread that runs in the background and automatically terminates when the main
program exits.
• In this experiment, each CPU core was simulated as a daemon thread, ensuring that
all core processes terminated properly when the simulation ended.
• Deadlock
• A situation in multi-threaded programming where two or more threads become stuck
in a waiting state, each waiting for a resource held by the other.
• While not observed in this experiment, deadlocks are a significant concern in real-
world parallel computing.

E
• Error Propagation
• The accumulation of small numerical errors over multiple iterative calculations,
leading to significant inaccuracies in results.
• This issue was observed when floating-point precision errors distorted function
execution in the polar graph.

F
• Fourier Transform
• A mathematical technique used to decompose complex waveforms into their
fundamental frequency components. The distortions observed in function execution
resemble Fourier analysis artifacts, commonly seen in signal processing.
• Frequency Components
• The individual sine or cosine waves that, when combined, form a more complex
waveform. In this experiment, the function sin(6θ) + 0.5 sin(3θ) contained
multiple frequency components, which interacted with different core counts, leading
to distortions.

G
• Gantt Chart
• A type of bar chart used to visualize task execution timelines in project management
and computing. In this study, a Gantt-like representation was used to show CPU
task execution timelines.
• Global Interpreter Lock (GIL)
• A mutex (mutual exclusion lock) in Python that prevents multiple threads from
executing Python bytecode simultaneously, limiting true parallelism in multi-
threaded Python programs.
• Although Python’s GIL prevents true multi-core execution, the experiment
successfully simulated multi-core behavior using threading and data locks.

• H
• Hyper-Threading
• A CPU technology that enables a single physical CPU core to execute multiple
threads concurrently, improving efficiency.
• Although not directly implemented in the simulation, hyper-threading is an important
real-world technique for multi-threaded execution optimization.

I
• IEEE 754 Standard
• A widely used floating-point arithmetic standard that defines how numbers are
stored and calculated in digital computers. The rounding errors observed in
function execution were caused by limitations in IEEE 754 precision.
• Instruction Pipeline
• A technique used in modern CPUs where multiple instructions are fetched, decoded,
and executed in parallel stages, improving computational speed.
• The experiment does not simulate hardware-level pipelining, but similar effects
were observed when multiple cores executed function evaluations concurrently.

L
• Load Balancing
• A method used in parallel computing to distribute workloads evenly across multiple
CPU cores to maximize efficiency.
• In the experiment, the bar graph visualization demonstrated how function execution
was distributed across cores, revealing potential imbalances.

N
• Nyquist-Shannon Sampling Theorem
• A fundamental theorem in signal processing stating that a signal must be sampled at
at least twice its highest frequency to be accurately reconstructed.
• The aliasing artifacts observed in the experiment were a direct result of sampling
errors violating this theorem.

O
• Oversampling
• A technique where a signal (or function) is sampled at a higher rate than necessary
to improve accuracy and reduce aliasing effects.
• Oversampling was implemented in the polar function execution plot to stabilize
distorted function curves.

P
• Phase Offset
• A shift in the starting angle of a periodic function. In this experiment, modifying the
core count changed the phase offsets, leading to function misalignment and
distortions.
• Precision Scaling (Adaptive Precision)
• A technique where a system dynamically adjusts numerical precision based on error
thresholds.
• Future extensions of this study could implement adaptive precision scaling to
minimize computational errors dynamically.

R
• Race Condition
• A programming issue where multiple threads access shared data concurrently,
leading to unpredictable behavior.
• In the simulation, thread locks (data_lock) were used to prevent race conditions
when updating core workload values.

S
• Sampling Rate
• The number of data points collected per unit time when evaluating a function. If too
low, it results in aliasing; if too high, it increases computational overhead.
• The experiment demonstrated how improper sampling altered the function plots
when changing core counts.

T
• Taylor Series Expansion
• A mathematical technique used to approximate functions using polynomials. Many
trigonometric functions in CPUs (such as sin(x), cos(x)) are computed using
Taylor series.
• Errors in the expansion process contribute to floating-point inaccuracies in real-
world CPU computations.
• Thread Synchronization
• The process of ensuring that multiple threads access shared resources safely. The
use of thread locks (data_lock) in this experiment prevented conflicting data
updates.

You might also like