SimdParallelism .NET 7

CSharp · Console

/Program.cs

// See https://habr.com/ru/companies/skbkontur/articles/737858/

using System;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello");
		Console.WriteLine();

		Console.WriteLine($"Vector<byte>.IsSupported = {Vector<byte>.IsSupported}");
		Console.WriteLine($"Vector<sbyte>.IsSupported = {Vector<sbyte>.IsSupported}");
		Console.WriteLine($"Vector<short>.IsSupported = {Vector<short>.IsSupported}");
		Console.WriteLine($"Vector<ushort>.IsSupported = {Vector<ushort>.IsSupported}");
		Console.WriteLine($"Vector<int>.IsSupported = {Vector<int>.IsSupported}");
		Console.WriteLine($"Vector<uint>.IsSupported = {Vector<uint>.IsSupported}");
		Console.WriteLine($"Vector<long>.IsSupported = {Vector<long>.IsSupported}");
		Console.WriteLine($"Vector<ulong>.IsSupported = {Vector<ulong>.IsSupported}");
		Console.WriteLine($"Vector<Int128>.IsSupported = {Vector<Int128>.IsSupported}");
		Console.WriteLine($"Vector<UInt128>.IsSupported = {Vector<UInt128>.IsSupported}");
		Console.WriteLine($"Vector<nint>.IsSupported = {Vector<nint>.IsSupported}");
		Console.WriteLine($"Vector<nuint>.IsSupported = {Vector<nuint>.IsSupported}");
		Console.WriteLine($"nint.Size = {nint.Size}");
		Console.WriteLine();
		
		Console.WriteLine($"Is64BitOperatingSystem = {Environment.Is64BitOperatingSystem}");
		Console.WriteLine($"Is64BitProcess = {Environment.Is64BitProcess}");
		Console.WriteLine($"ProcessorCount = {Environment.ProcessorCount}");
		Console.WriteLine($"OSVersion = {Environment.OSVersion}");
		Console.WriteLine($".NET Version = {Environment.Version}");
		Console.WriteLine();

		Console.WriteLine($"Vector<long>.IsSupported = {Vector<long>.IsSupported}");
		Console.WriteLine($"Vector.IsHardwareAccelerated = {Vector.IsHardwareAccelerated}");
		Console.WriteLine($"Vector<long>.Count = {Vector<long>.Count}");
		Console.WriteLine();

		//var array = new long[16_000];
		var array = new long[1_600_000];
		{
			var random = new Random();
			for(var i = 0; i < array.Length; i++)
				array[i] = random.Next(100);
		}
		
		long sum = 0;
		var repeat = 100;
		{
			for(var r = 0; r < repeat; r++)
				sum = array.Sum();
			
			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();
			for(var r = 0; r < repeat; r++)
				sum = array.Sum();
			stopwatch.Stop();

			Console.WriteLine($"Sum = {sum} for {stopwatch.ElapsedTicks:n0} ticks");
		}
		
		{
			for(var r = 0; r < repeat; r++)
				sum = SumSimd(array);

			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();
			for(var r = 0; r < repeat; r++)
				sum = SumSimd(array);
			stopwatch.Stop();

			Console.WriteLine($"SumSimd = {sum} for {stopwatch.ElapsedTicks:n0} ticks");
		}
		
		{
			for(var r = 0; r < repeat; r++)
				sum = SumSimdParallel(array);

			Stopwatch stopwatch = new Stopwatch();
			stopwatch.Start();
			for(var r = 0; r < repeat; r++)
				sum = SumSimdParallel(array);
			stopwatch.Stop();
			Console.WriteLine($"SumSimdParallel = {sum} for {stopwatch.ElapsedTicks:n0} ticks");
		}

		Console.WriteLine("Goodbye");
	}
	
	public static long SumSimd(long[] array)
	{
		var vectorSize = Vector<long>.Count;

		var vectorSum = new Vector<long>(array);
		var span = array.AsSpan(vectorSize);
		while (span.Length >= vectorSize)
		{
			vectorSum += new Vector<long>(span);
			span = span.Slice(vectorSize);
		}
		return Vector.Dot(vectorSum, Vector<long>.One);
	}	

	public static long SumSimdParallel(long[] array)
	{
		var degreeOfParallelism = Math.Max(Vector<long>.Count, (int)BitOperations.RoundUpToPowerOf2((uint)Environment.ProcessorCount));
		var sums = new long[degreeOfParallelism];
		var len = array.Length / degreeOfParallelism;
		
		//Console.WriteLine($"degreeOfParallelism = {degreeOfParallelism}");
		//Console.WriteLine($"len = {len}");
		//Console.WriteLine($"Count = {Vector<long>.Count}");

		Parallel.For(0, degreeOfParallelism, idx =>
		{
			var vectorSize = Vector<long>.Count;
			
			var span = array.AsSpan(idx * len, len);
			var vectorSum = new Vector<long>(span);
			span = span.Slice(vectorSize);
			while (span.Length >= vectorSize)
			{
				vectorSum += new Vector<long>(span);
				span = span.Slice(vectorSize);
			}
			sums[idx] = Vector.Dot(vectorSum, Vector<long>.One);
		});
		
		return SumSimd(sums);
		//return -1;
	}	
}
;