From 8f5cdc534a475acb3ac58dd010fa331ee0035c46 Mon Sep 17 00:00:00 2001 From: hamed Mohammedi Date: Fri, 5 Jul 2019 20:10:56 +0430 Subject: [PATCH 1/9] mera added --- docs/standard/simd.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 docs/standard/simd.md diff --git a/docs/standard/simd.md b/docs/standard/simd.md new file mode 100644 index 0000000000000..329470ca95206 --- /dev/null +++ b/docs/standard/simd.md @@ -0,0 +1,12 @@ +--- +title: SIMD in .NET +description: This tutorial demonstrates how to use SIMD in C#. +author: FIVIL +#ms.author: [MICROSOFT ALIAS OF INTERNAL OWNER] +ms.date: 07/05/2019 +#ms.topic: [TOPIC TYPE] +#ms.prod: [PRODUCT VALUE] +ms.technology: dotnet-standard +--- + +# Overview From 64bcbd2f697074bb612af0ed9699735823287c1f Mon Sep 17 00:00:00 2001 From: hamed Mohammedi Date: Fri, 5 Jul 2019 20:37:44 +0430 Subject: [PATCH 2/9] basic --- docs/standard/simd.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index 329470ca95206..b9c749fc8567d 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -10,3 +10,23 @@ ms.technology: dotnet-standard --- # Overview + +SIMD (Single instruction, multiple data) provides the functionality to do multiple processes on a single core to achieve peak performance. In .NET there is set of SIMD-enabled types under namespace. SIMD operations can be parallelized at the hardware level. That increases the throughput of the vectorized computations, which are common in mathematical, scientific, and graphics apps. + +# .NET SIMD-enabled types + +The .NET SIMD-enabled types include the following: + +- The , , and types, which represent vectors with 2, 3, and 4 values. + +- Two matrix types, , which represents a 3x2 matrix, and , which represents a 4x4 matrix. + +- The type, which represents a plane in three-dimensional space. + +- The type, which represents a vector that is used to encode three-dimensional physical rotations. + +- The type, which represents a vector of a specified numeric type and provides a broad set of operators that benefit from SIMD support. The count of a instance is fixed, but its value depends on the CPU of the machine, on which code is executed. + > [!NOTE] + > The type is not included into the .NET Framework. You must install the [System.Numerics.Vectors](https://www.nuget.org/packages/System.Numerics.Vectors) NuGet package to get access to this type. + +The SIMD-enabled types are implemented in such a way that they can be used with non-SIMD-enabled hardware or JIT compilers. To take advantage of SIMD instructions, your 64-bit apps must be run by the runtime that uses the RyuJIT compiler, which is included in .NET Core and in the .NET Framework 4.6 and later versions. It adds SIMD support when targeting 64-bit processors. From c79e9e17e6eb899f300c4deaeaca553cd6d4fcc9 Mon Sep 17 00:00:00 2001 From: hamed Mohammedi Date: Fri, 5 Jul 2019 23:45:35 +0430 Subject: [PATCH 3/9] vector and matrix --- docs/standard/simd.md | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index b9c749fc8567d..c9197e22a6d84 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -30,3 +30,74 @@ The .NET SIMD-enabled types include the following: > The type is not included into the .NET Framework. You must install the [System.Numerics.Vectors](https://www.nuget.org/packages/System.Numerics.Vectors) NuGet package to get access to this type. The SIMD-enabled types are implemented in such a way that they can be used with non-SIMD-enabled hardware or JIT compilers. To take advantage of SIMD instructions, your 64-bit apps must be run by the runtime that uses the RyuJIT compiler, which is included in .NET Core and in the .NET Framework 4.6 and later versions. It adds SIMD support when targeting 64-bit processors. + +# How to use SIMD? + + Before executing custom SIMD algorithms, it is possible to check if the host machine supports SIMD using , which returns a . + +## Simple Vectors + +The most primitive SIMD-enabled types in .NET are , , and types, which represent vectors with 2, 3, and 4 values. The example blew uses to add two vectors. + +```csharp + var v1 = new Vector2(0.1f, 0.2f); + var v2 = new Vector2(1.1f, 2.2f); + var vResutl = v1 + v2; +``` + +It is also possible to use .NET vectors in order to calculate other mathematical properties of vectors such as `Dot product`, `Transform`, `Clamp` etc. + +```csharp + var v1 = new Vector2(0.1f, 0.2f); + var v2 = new Vector2(1.1f, 2.2f); + var vResutl1 = Vector2.Dot(v1, v2); + var vResutl2 = Vector2.Distance(v1, v2); + var vResutl3 = Vector2.Clamp(v1, Vector2.Zero, Vector2.One); +``` + +## Matrix + +, which represents a 3x2 matrix, and , which represents a 4x4 matrix. Can be used for matrix related calculations. The example below demonstrates multiplication of a matrix to it's corespondent transpose matrix using SIMD. + +```csharp + var m1 = new Matrix4x4( + 1.1f, 1.2f, 1.3f, 1.4f, + 2.1f, 2.2f, 3.3f, 4.4f, + 3.1f, 3.2f, 3.3f, 3.4f, + 4.1f, 4.2f, 4.3f, 4.4f); + + var m2 = Matrix4x4.Transpose(m1); + var mResult = Matrix4x4.Multiply(m1, m2); +``` + +## Vector + +The gives the ability to utilize and longer vectors, The count of a instance is fixed, but its value depends on the CPU of the machine, on which code is executed. + +The example below demonstrates adding long arrays elements using . + +```csharp + double[] SimdVectorProd(double[] left, double[] right) + { + var offset = Vector.Count; + double[] result = new double[left.Length]; + int i = 0; + for (i = 0; i < left.Length; i += offset) + { + var v1 = new Vector(left, i); + var v2 = new Vector(right, i); + (v1 * v2).CopyTo(result, i); + } + + //remaining items + for (; i < left.Length; ++i) + { + result[i] = left[i] * right[i]; + } + + return result; + } +``` + +# Dont's of SIMD + From 696d0e335041b5aa6b85619fefb046ab05137832 Mon Sep 17 00:00:00 2001 From: hamed Mohammedi Date: Sat, 6 Jul 2019 00:32:09 +0430 Subject: [PATCH 4/9] remarks --- docs/standard/simd.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index c9197e22a6d84..570e5ad00a9b1 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -99,5 +99,6 @@ The example below demonstrates adding long arrays elements using Date: Sat, 6 Jul 2019 17:05:43 +0430 Subject: [PATCH 5/9] fixing headers problem --- docs/standard/simd.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index 570e5ad00a9b1..fceb8475c7420 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -13,7 +13,7 @@ ms.technology: dotnet-standard SIMD (Single instruction, multiple data) provides the functionality to do multiple processes on a single core to achieve peak performance. In .NET there is set of SIMD-enabled types under namespace. SIMD operations can be parallelized at the hardware level. That increases the throughput of the vectorized computations, which are common in mathematical, scientific, and graphics apps. -# .NET SIMD-enabled types +## .NET SIMD-enabled types The .NET SIMD-enabled types include the following: @@ -31,7 +31,7 @@ The .NET SIMD-enabled types include the following: The SIMD-enabled types are implemented in such a way that they can be used with non-SIMD-enabled hardware or JIT compilers. To take advantage of SIMD instructions, your 64-bit apps must be run by the runtime that uses the RyuJIT compiler, which is included in .NET Core and in the .NET Framework 4.6 and later versions. It adds SIMD support when targeting 64-bit processors. -# How to use SIMD? +## How to use SIMD? Before executing custom SIMD algorithms, it is possible to check if the host machine supports SIMD using , which returns a . @@ -99,6 +99,6 @@ The example below demonstrates adding long arrays elements using Date: Tue, 31 Mar 2020 09:51:10 -0700 Subject: [PATCH 6/9] Update simd.md - Updated the date - Changed terms from **SIMD-enabled** to **SIMD-accelerated** - Fixed any spelling errors - Fixed code spacing - Adjusted markdown structure - Brought acro score from 77 to 92 --- docs/standard/simd.md | 107 +++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index fceb8475c7420..8ee41f8ef8d93 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -1,21 +1,19 @@ --- title: SIMD in .NET -description: This tutorial demonstrates how to use SIMD in C#. +description: This tutorial demonstrates how to use hardware SIMD operations in C# and .NET Core. author: FIVIL -#ms.author: [MICROSOFT ALIAS OF INTERNAL OWNER] -ms.date: 07/05/2019 -#ms.topic: [TOPIC TYPE] -#ms.prod: [PRODUCT VALUE] +ms.author: tagoo +ms.date: 03/31/2020 ms.technology: dotnet-standard --- # Overview -SIMD (Single instruction, multiple data) provides the functionality to do multiple processes on a single core to achieve peak performance. In .NET there is set of SIMD-enabled types under namespace. SIMD operations can be parallelized at the hardware level. That increases the throughput of the vectorized computations, which are common in mathematical, scientific, and graphics apps. +SIMD (Single instruction, multiple data) provides hardware support for performing an operation on multiple pieces of data, in parallel, using a single instruction. In .NET, there's set of SIMD-accelerated types under the namespace. SIMD operations can be parallelized at the hardware level. That increases the throughput of the vectorized computations, which are common in mathematical, scientific, and graphics apps. -## .NET SIMD-enabled types +## .NET SIMD-accelerated types -The .NET SIMD-enabled types include the following: +The .NET SIMD-accelerated types include the following types: - The , , and types, which represent vectors with 2, 3, and 4 values. @@ -25,80 +23,81 @@ The .NET SIMD-enabled types include the following: - The type, which represents a vector that is used to encode three-dimensional physical rotations. -- The type, which represents a vector of a specified numeric type and provides a broad set of operators that benefit from SIMD support. The count of a instance is fixed, but its value depends on the CPU of the machine, on which code is executed. +- The type, which represents a vector of a specified numeric type and provides a broad set of operators that benefit from SIMD support. The count of a instance is fixed, but its value depends on the CPU of the machine running the code. + > [!NOTE] - > The type is not included into the .NET Framework. You must install the [System.Numerics.Vectors](https://www.nuget.org/packages/System.Numerics.Vectors) NuGet package to get access to this type. + > The type is not included in the .NET Framework. You must install the [System.Numerics.Vectors](https://www.nuget.org/packages/System.Numerics.Vectors) NuGet package to get access to this type. -The SIMD-enabled types are implemented in such a way that they can be used with non-SIMD-enabled hardware or JIT compilers. To take advantage of SIMD instructions, your 64-bit apps must be run by the runtime that uses the RyuJIT compiler, which is included in .NET Core and in the .NET Framework 4.6 and later versions. It adds SIMD support when targeting 64-bit processors. +The SIMD-accelerated types are implemented in such a way that they can be used with non-SIMD-accelerated hardware or JIT compilers. To take advantage of SIMD instructions, your 64-bit apps must be run by the runtime that uses the **RyuJIT** compiler. A **RyuJIT** compiler is included in .NET Core and in .NET Framework 4.6 and later. SIMD support is only provided when targeting 64-bit processors. ## How to use SIMD? - Before executing custom SIMD algorithms, it is possible to check if the host machine supports SIMD using , which returns a . +Before executing custom SIMD algorithms, it's possible to check if the host machine supports SIMD by using , which returns a . This doesn't guarantee that SIMD-acceleration is enabled for a specific type, but is an indicator that it's supported by some types. ## Simple Vectors -The most primitive SIMD-enabled types in .NET are , , and types, which represent vectors with 2, 3, and 4 values. The example blew uses to add two vectors. +The most primitive SIMD-accelerated types in .NET are , , and types, which represent vectors with 2, 3, and 4 values. The example below uses to add two vectors. ```csharp - var v1 = new Vector2(0.1f, 0.2f); - var v2 = new Vector2(1.1f, 2.2f); - var vResutl = v1 + v2; +var v1 = new Vector2(0.1f, 0.2f); +var v2 = new Vector2(1.1f, 2.2f); +var vResutl = v1 + v2; ``` -It is also possible to use .NET vectors in order to calculate other mathematical properties of vectors such as `Dot product`, `Transform`, `Clamp` etc. +It's also possible to use .NET vectors to calculate other mathematical properties of vectors such as `Dot product`, `Transform`, `Clamp` and so on. ```csharp - var v1 = new Vector2(0.1f, 0.2f); - var v2 = new Vector2(1.1f, 2.2f); - var vResutl1 = Vector2.Dot(v1, v2); - var vResutl2 = Vector2.Distance(v1, v2); - var vResutl3 = Vector2.Clamp(v1, Vector2.Zero, Vector2.One); +var v1 = new Vector2(0.1f, 0.2f); +var v2 = new Vector2(1.1f, 2.2f); +var vResutl1 = Vector2.Dot(v1, v2); +var vResutl2 = Vector2.Distance(v1, v2); +var vResutl3 = Vector2.Clamp(v1, Vector2.Zero, Vector2.One); ``` ## Matrix -, which represents a 3x2 matrix, and , which represents a 4x4 matrix. Can be used for matrix related calculations. The example below demonstrates multiplication of a matrix to it's corespondent transpose matrix using SIMD. +, which represents a 3x2 matrix, and , which represents a 4x4 matrix. Can be used for matrix-related calculations. The example below demonstrates multiplication of a matrix to its correspondent transpose matrix using SIMD. ```csharp - var m1 = new Matrix4x4( - 1.1f, 1.2f, 1.3f, 1.4f, - 2.1f, 2.2f, 3.3f, 4.4f, - 3.1f, 3.2f, 3.3f, 3.4f, - 4.1f, 4.2f, 4.3f, 4.4f); - - var m2 = Matrix4x4.Transpose(m1); - var mResult = Matrix4x4.Multiply(m1, m2); +var m1 = new Matrix4x4( + 1.1f, 1.2f, 1.3f, 1.4f, + 2.1f, 2.2f, 3.3f, 4.4f, + 3.1f, 3.2f, 3.3f, 3.4f, + 4.1f, 4.2f, 4.3f, 4.4f); + +var m2 = Matrix4x4.Transpose(m1); +var mResult = Matrix4x4.Multiply(m1, m2); ``` -## Vector +## Vector\ -The gives the ability to utilize and longer vectors, The count of a instance is fixed, but its value depends on the CPU of the machine, on which code is executed. +The gives the ability to use longer vectors. The count of a instance is fixed, but its value depends on the CPU of the machine running the code. The example below demonstrates adding long arrays elements using . ```csharp - double[] SimdVectorProd(double[] left, double[] right) - { - var offset = Vector.Count; - double[] result = new double[left.Length]; - int i = 0; - for (i = 0; i < left.Length; i += offset) - { - var v1 = new Vector(left, i); - var v2 = new Vector(right, i); - (v1 * v2).CopyTo(result, i); - } - - //remaining items - for (; i < left.Length; ++i) - { - result[i] = left[i] * right[i]; - } - - return result; - } +double[] SimdVectorProd(double[] left, double[] right) +{ + var offset = Vector.Count; + double[] result = new double[left.Length]; + int i = 0; + for (i = 0; i < left.Length; i += offset) + { + var v1 = new Vector(left, i); + var v2 = new Vector(right, i); + (v1 * v2).CopyTo(result, i); + } + + //remaining items + for (; i < left.Length; ++i) + { + result[i] = left[i] * right[i]; + } + + return result; +} ``` ## Remarks -SIMD is more likely to remove one bottleneck and expose the next, for example memory throughput. In general the performance benefit of using SIMD varies depending on the specific scenario, and in some cases it can even perform worse than simpler non-SIMD equivalent code. +SIMD is more likely to remove one bottleneck and expose the next, for example memory throughput. In general the performance benefit of using SIMD varies depending on the specific scenario, and in some cases it can even perform worse than simpler non-SIMD equivalent code. From f2b7c846e59b1775eca2e7d1e37a716291439ba3 Mon Sep 17 00:00:00 2001 From: Andy De George <2672110+Thraka@users.noreply.github.com> Date: Tue, 28 Apr 2020 12:31:57 -0700 Subject: [PATCH 7/9] Update docs/standard/simd.md Co-Authored-By: Tanner Gooding --- docs/standard/simd.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index 8ee41f8ef8d93..6e82db6a02b95 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -17,7 +17,7 @@ The .NET SIMD-accelerated types include the following types: - The , , and types, which represent vectors with 2, 3, and 4 values. -- Two matrix types, , which represents a 3x2 matrix, and , which represents a 4x4 matrix. +- Two matrix types, , which represents a 3x2 matrix, and , which represents a 4x4 matrix of values. - The type, which represents a plane in three-dimensional space. From 657404f5de62ecb44811d2cd929177f450d5a8f6 Mon Sep 17 00:00:00 2001 From: Andy De George <2672110+Thraka@users.noreply.github.com> Date: Tue, 28 Apr 2020 12:32:05 -0700 Subject: [PATCH 8/9] Update docs/standard/simd.md Co-Authored-By: Tanner Gooding --- docs/standard/simd.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index 6e82db6a02b95..de6cfcbb8c611 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -23,7 +23,7 @@ The .NET SIMD-accelerated types include the following types: - The type, which represents a vector that is used to encode three-dimensional physical rotations. -- The type, which represents a vector of a specified numeric type and provides a broad set of operators that benefit from SIMD support. The count of a instance is fixed, but its value depends on the CPU of the machine running the code. +- The type, which represents a vector of a specified numeric type and provides a broad set of operators that benefit from SIMD support. The count of a instance is fixed for the lifetime of an application, but its value depends on the CPU of the machine running the code. > [!NOTE] > The type is not included in the .NET Framework. You must install the [System.Numerics.Vectors](https://www.nuget.org/packages/System.Numerics.Vectors) NuGet package to get access to this type. From 3856a18f7f1dc67848acefdc36001c2c40179c16 Mon Sep 17 00:00:00 2001 From: Andy De George <2672110+Thraka@users.noreply.github.com> Date: Tue, 28 Apr 2020 12:33:24 -0700 Subject: [PATCH 9/9] Apply suggestions from code review Co-Authored-By: Tanner Gooding --- docs/standard/simd.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/standard/simd.md b/docs/standard/simd.md index de6cfcbb8c611..4bc2f7f169530 100644 --- a/docs/standard/simd.md +++ b/docs/standard/simd.md @@ -19,9 +19,9 @@ The .NET SIMD-accelerated types include the following types: - Two matrix types, , which represents a 3x2 matrix, and , which represents a 4x4 matrix of values. -- The type, which represents a plane in three-dimensional space. +- The type, which represents a plane in three-dimensional space using values. -- The type, which represents a vector that is used to encode three-dimensional physical rotations. +- The type, which represents a vector that is used to encode three-dimensional physical rotations using values. - The type, which represents a vector of a specified numeric type and provides a broad set of operators that benefit from SIMD support. The count of a instance is fixed for the lifetime of an application, but its value depends on the CPU of the machine running the code.