Skip to content

About

Hardware-accelerated C# / .NET SIMD vectorization benchmarks and string/numeric throughput profiling

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

SIMD and Vectorized Operations in C#

Table of Contents

  1. Introduction to SIMD
  2. Vectors and their Role in SIMD
  3. Leveraging SIMD in C#
  4. Benchmark Results and Analysis

Introduction to SIMD

Single Instruction, Multiple Data (SIMD) is a class of parallel computers in Flynn's taxonomy. It describes computers with multiple processing elements that perform the same operation on multiple data points simultaneously. To put it simply, SIMD operations allow a single operation to be performed on multiple pieces of data at once. This plays a crucial role in optimizing operations that can be performed in parallel, making it particularly effective in processing large datasets and enhancing computational speed.

The concept of SIMD lies at the foundation of many high-performance computing systems. SIMD's nature of performing the same instruction on different data points makes it extremely useful for tasks that require repetitive computation, such as image and signal processing, scientific simulations, financial analytics, and machine learning algorithms.

The power of SIMD comes from data parallelism. When dealing with large amounts of data, it's often the case that the same operation needs to be applied to each data point. In a typical sequential or scalar execution, these operations would be performed one at a time. However, with SIMD, multiple operations can be performed simultaneously, leading to a significant increase in speed.

Consider an operation such as adding two arrays of numbers. In a scalar execution, each pair of numbers would be added sequentially, one pair at a time. However, with SIMD instructions, multiple pairs of numbers can be added simultaneously. This parallelism is what provides the speedup.

Modern CPU architectures like x86 and ARM provide support for SIMD instructions. These instructions operate on small vectors of data. For example, the x86 architecture provides the SSE and AVX instruction sets, which operate on vectors of 128 and 256 bits respectively. This means that, for example, four 32-bit floating-point numbers can be added together in a single operation with SSE, and eight with AVX.

The use of SIMD is not limited to low-level programming or assembly language. High-level programming languages like C# provide support for SIMD operations through specific libraries or extensions. In C#, the System.Numerics namespace provides SIMD-enabled types like Vector, which allow developers to write code that takes advantage of the SIMD capabilities of the CPU to achieve significant performance improvements.

In addition to improving performance, SIMD operations can also reduce power consumption. By performing multiple operations at once, the CPU can finish its work more quickly and go back to a low power state. This makes SIMD a key tool for improving the energy efficiency of applications, which is particularly important in mobile and battery-powered devices.

In the coming sections, we'll explore how to leverage SIMD in C#, and provide some benchmark results to demonstrate the performance advantages of SIMD operations.

Vectors and their Role in SIMD

In the context of SIMD, a vector refers to a single, one-dimensional array of data that SIMD instructions operate on. A vector's length will often be a multiple of the word size of the machine: 32-bits, 64-bits, 128-bits, etc. These fixed-size data structures are used to hold the multiple data points that SIMD instructions will operate on simultaneously.

This ability to process multiple data elements in a single operation is what gives SIMD its power. For example, consider a simple operation like adding two arrays of integers. Without SIMD, this operation would require looping through each pair of elements in the arrays and adding them together individually. However, with SIMD and the use of vectors, multiple pairs of integers can be added together in a single operation, significantly speeding up the computation.

The size of the vector, or the number of data elements that can be processed per instruction, is determined by the CPU's architecture. For example, a CPU that supports the SSE instruction set can process vectors of 128 bits, while a CPU that supports the AVX instruction set can process vectors of 256 bits. This means that on an SSE machine, four 32-bit integers can be added together in a single operation, while on an AVX machine, eight 32-bit integers can be added together.

In high-level languages like C#, the System.Numerics namespace provides a generic Vector class that represents a single vector. The size of a Vector instance is determined by the machine's hardware capabilities. At runtime, the .NET runtime will use the largest SIMD instruction set that the machine supports to perform vector operations. This means that the same C# code can automatically take advantage of wider vectors on machines that support them, leading to better performance.

Vector<int> vector1 = new Vector<int>(new int[] {1, 2, 3, 4});
Vector<int> vector2 = new Vector<int>(new int[] {5, 6, 7, 8});
Vector<int> sum = Vector.Add(vector1, vector2);

In this example, if the machine supports the SSE instruction set, the Vector instances will be 128 bits wide, and the addition operation will add four pairs of integers together in a single operation. If the machine supports the AVX instruction set, the Vector instances will be 256 bits wide, and the addition operation will add eight pairs of integers together in a single operation.

Vector operations in C# are not limited to basic arithmetic. The Vector class provides a wide range of methods that perform vectorized operations, including absolute value, dot product, square root, and many others. This makes it possible to write complex, high-performance code that takes full advantage of SIMD.

In summary, vectors play a crucial role in SIMD by providing a way to operate on multiple data elements simultaneously. In the context of C#, the Vector class allows developers to write code that automatically adapts to the machine's SIMD capabilities, leading to efficient, high-performance applications.

Leveraging SIMD in C Sharp

Usage Example: Vectorized Addition and Scalar Multiplication

Here's a more complex example, involving both addition of vectors and multiplication by a scalar:

Vector<int> vector1 = new Vector<int>(new int[] {1, 2, 3, 4});
Vector<int> vector2 = new Vector<int>(new int[] {5, 6, 7, 8});
Vector<int> sum = Vector.Add(vector1, vector2);

// Scalar multiplication
int scalar = 2;
Vector<int> multiplyResult = Vector.Multiply(sum, new Vector<int>(scalar));

In this example, we first add vector1 and vector2 element-wise, and then multiply the resulting vector by a scalar.

Benchmark Results and Analysis

The following table presents the benchmark results for the given operations on different data types. Each row represents a combination of data type and operation, and the timings for traditional and SIMD methods are shown:

Processor Count: 16 Processor Name: AMD Ryzen 7 4800H with Radeon Graphics Processor Speed: 2900 Mhz Total Memory: 1 MB Vector Sizes: byte: 32 sbyte: 32 Int32: 8 UInt32: 8 Int64: 4 UInt64: 4 Single: 8 Double: 4

Type Operation Vector Size Traditional SIMD
Int32 Add 8 6.51 s 1.17 s
Int32 Subtract 8 6.55 s 1.14 s
Int32 Multiply 8 6.55 s 1.18 s
Byte Add 32 6.71 s 185 ms
Byte Subtract 32 6.66 s 176 ms
Byte Multiply 32 6.67 s 1.98 s
SByte Add 32 6.72 s 184 ms
SByte Subtract 32 6.75 s 181 ms
SByte Multiply 32 6.75 s 2.04 s
Int16 Add 16 6.74 s 527 ms
Int16 Subtract 16 6.76 s 482 ms
Int16 Multiply 16 6.85 s 501 ms
UInt16 Add 16 6.76 s 479 ms
UInt16 Subtract 16 6.77 s 524 ms
UInt16 Multiply 16 6.76 s 545 ms
UInt32 Add 8 6.55 s 1.15 s
UInt32 Subtract 8 6.6 s 1.2 s
UInt32 Multiply 8 6.54 s 1.13 s
Int64 Add 4 6.54 s 2.21 s
Int64 Subtract 4 6.69 s 2.62 s
Int64 Multiply 4 6.63 s 3.36 s
UInt64 Add 4 6.6 s 2.32 s
UInt64 Subtract 4 6.62 s 2.3 s
UInt64 Multiply 4 6.58 s 3.35 s
Double Add 4 6.61 s 2.31 s
Double Subtract 4 6.6 s 2.26 s
Double Multiply 4 6.73 s 2.31 s
Double Divide 4 6.7 s 2.27 s

These results clearly illustrate the performance advantages of SIMD operations. For instance, adding Int32 numbers using SIMD instructions is around 5 times faster than traditional methods. Similarly, byte addition is dramatically faster with SIMD, taking only 185 ms compared to 6.71 seconds with traditional addition.

Note: The exact speedup you can achieve with SIMD will depend on several factors, including the specific operation being performed, the data type, and the size of the data set. This is because different processors may have different SIMD capabilities, and different operations may have different levels of parallelism.

In conclusion, SIMD provides a powerful tool for accelerating computation in C#, especially for tasks involving large amounts of numerical data. By packing multiple data elements into vectors and performing operations on them in parallel, you can significantly reduce the time required to process large data sets.

About

Hardware-accelerated C# / .NET SIMD vectorization benchmarks and string/numeric throughput profiling

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages