From 0f56cf1cd7f32eb44a98cfa3751ff6f7abdee28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Tue, 12 Mar 2019 21:24:42 +0800 Subject: [PATCH 01/43] Adding 8, 16 and 32-bit Posit versions with es=0..4 via code generation --HG-- branch : issue/HAST-136 From 68a18265a8510bd4fa7855360f74308b412e6b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Tue, 12 Mar 2019 21:46:51 +0800 Subject: [PATCH 02/43] Code styling --HG-- branch : issue/HAST-136 From 998a7f3c857800eb3fe12575c0bd27d956e63acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Tue, 12 Mar 2019 22:02:14 +0800 Subject: [PATCH 03/43] Code styling --HG-- branch : issue/HAST-136 From 7280219759d49aaa2fd21137b2735abd750774f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Tue, 15 Dec 2020 13:53:38 +0100 Subject: [PATCH 04/43] Adding calculators and samples --- Hast.Samples.Posit/App.config | 6 + Hast.Samples.Posit/Hast.Samples.Posit.csproj | 219 ++++ Hast.Samples.Posit/Posit16_0_Calculator.cs | 179 +++ .../Posit16_0_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit16_1_Calculator.cs | 179 +++ .../Posit16_1_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit16_2_Calculator.cs | 179 +++ .../Posit16_2_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit16_3_Calculator.cs | 179 +++ .../Posit16_3_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit16_4_Calculator.cs | 179 +++ .../Posit16_4_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit32_0_Calculator.cs | 179 +++ .../Posit32_0_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit32_1_Calculator.cs | 179 +++ .../Posit32_1_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit32_2_Calculator.cs | 179 +++ .../Posit32_2_CalculatorSampleRunner.cs | 112 ++ Hast.Samples.Posit/Posit32_3_Calculator.cs | 179 +++ .../Posit32_3_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit32_4_Calculator.cs | 179 +++ .../Posit32_4_CalculatorSampleRunner.cs | 117 ++ Hast.Samples.Posit/Posit8_0_Calculator.cs | 179 +++ .../Posit8_0_CalculatorSampleRunner.cs | 119 ++ Hast.Samples.Posit/Posit8_1_Calculator.cs | 179 +++ .../Posit8_1_CalculatorSampleRunner.cs | 119 ++ Hast.Samples.Posit/Posit8_2_Calculator.cs | 179 +++ .../Posit8_2_CalculatorSampleRunner.cs | 119 ++ Hast.Samples.Posit/Posit8_3_Calculator.cs | 179 +++ .../Posit8_3_CalculatorSampleRunner.cs | 119 ++ Hast.Samples.Posit/Posit8_4_Calculator.cs | 179 +++ .../Posit8_4_CalculatorSampleRunner.cs | 119 ++ Hast.Samples.Posit/PositCalculator.cs | 21 + Hast.Samples.Posit/PositCalculator.tt | 213 ++++ .../PositCalculatorSampleRunner.cs | 21 + .../PositCalculatorSampleRunner.tt | 177 +++ ...sitCalculatorTemplateFileManager.ttinclude | 1090 +++++++++++++++++ Hast.Samples.Posit/Program.cs | 410 +++++++ Hast.Samples.Posit/Program.tt | 166 +++ Hast.Samples.Posit/Properties/AssemblyInfo.cs | 36 + Hast.Samples.Posit/Sample.cs | 55 + Hast.Samples.Posit/Sample.tt | 37 + Hastlayer.SDK.sln | 7 + 43 files changed, 6903 insertions(+) create mode 100644 Hast.Samples.Posit/App.config create mode 100644 Hast.Samples.Posit/Hast.Samples.Posit.csproj create mode 100644 Hast.Samples.Posit/Posit16_0_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit16_1_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit16_2_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit16_3_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit16_4_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit32_0_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit32_1_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit32_2_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit32_3_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit32_4_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit8_0_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit8_1_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit8_2_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit8_3_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/Posit8_4_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/PositCalculator.cs create mode 100644 Hast.Samples.Posit/PositCalculator.tt create mode 100644 Hast.Samples.Posit/PositCalculatorSampleRunner.cs create mode 100644 Hast.Samples.Posit/PositCalculatorSampleRunner.tt create mode 100644 Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude create mode 100644 Hast.Samples.Posit/Program.cs create mode 100644 Hast.Samples.Posit/Program.tt create mode 100644 Hast.Samples.Posit/Properties/AssemblyInfo.cs create mode 100644 Hast.Samples.Posit/Sample.cs create mode 100644 Hast.Samples.Posit/Sample.tt diff --git a/Hast.Samples.Posit/App.config b/Hast.Samples.Posit/App.config new file mode 100644 index 000000000..00bfd114a --- /dev/null +++ b/Hast.Samples.Posit/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj new file mode 100644 index 000000000..82308b1ce --- /dev/null +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -0,0 +1,219 @@ + + + + + Debug + AnyCPU + {D60C14F3-05FD-44BF-B109-7A3F4113CC46} + Exe + Hast.Samples.Posit + Hast.Samples.Posit + v4.6.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + PositCalculator.tt + + + PositCalculatorSampleRunner.tt + + + True + True + PositCalculator.tt + + + True + True + PositCalculatorSampleRunner.tt + + + True + True + Program.tt + + + + True + True + Sample.tt + + + + + + + + + TextTemplatingFileGenerator + PositCalculator.cs + + + TextTemplatingFileGenerator + PositCalculatorSampleRunner.cs + + + TextTemplatingFileGenerator + Program.cs + + + TextTemplatingFileGenerator + Sample.cs + + + + + + + + {9dee53c2-83ac-4048-adab-2993faffe4ea} + Hast.Transformer.Abstractions + + + {0D13986F-FA33-4898-A0DE-C173764663F0} + Hast.Transformer.Vhdl.Abstractions + + + {6EE5898A-E450-4A1D-80AF-AD27A8BD5E69} + Hast.Common + + + {0D77AFBA-1B4D-4083-B35C-3309251039B7} + Hast.Layer + + + {86d47be0-994e-48ba-b68c-1f0bcab67f02} + Hast.Remote.Client + + + {6cfe69a4-04a3-4427-818c-c0832d197a62} + Lombiq.Arithmetics + + + {cb4a8cf6-7dd4-4493-b5cd-e5e51ee5c73b} + Hast.Samples.SampleAssembly + + + + + + + \ No newline at end of file diff --git a/Hast.Samples.Posit/Posit16_0_Calculator.cs b/Hast.Samples.Posit/Posit16_0_Calculator.cs new file mode 100644 index 000000000..8936b53e7 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_0_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16_0_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16_0((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16_0(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16_0(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16_0((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16_0((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16_0_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit16_0_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit16_0_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_0(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16_0((ushort)memory.ReadUInt32( Posit16_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_0_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit16_0_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16_0_Calculator is (" + + Posit16_0_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit16_0_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16_0_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit16_0_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16_0_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs new file mode 100644 index 000000000..2195039b4 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit16_0_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_0_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + + var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit16_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; + else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_0_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; + else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_0Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_0Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_1_Calculator.cs b/Hast.Samples.Posit/Posit16_1_Calculator.cs new file mode 100644 index 000000000..2f8c20195 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_1_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16_1_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16_1((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16_1(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16_1(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16_1((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16_1((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16_1_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit16_1_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit16_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit16_1_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit16_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_1(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16_1((ushort)memory.ReadUInt32( Posit16_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_1_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit16_1_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16_1_Calculator is (" + + Posit16_1_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit16_1_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16_1_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16_1_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16_1_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit16_1_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit16_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16_1_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16_1_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs new file mode 100644 index 000000000..df48c250c --- /dev/null +++ b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit16_1_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_1_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + + var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit16_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; + else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_1Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_1_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; + else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_1Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_1Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_2_Calculator.cs b/Hast.Samples.Posit/Posit16_2_Calculator.cs new file mode 100644 index 000000000..71a8bdc49 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_2_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16_2_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16_2((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16_2(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16_2(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16_2((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16_2((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16_2_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit16_2_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit16_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit16_2_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit16_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_2(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16_2((ushort)memory.ReadUInt32( Posit16_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_2_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit16_2_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16_2_Calculator is (" + + Posit16_2_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit16_2_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16_2_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16_2_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16_2_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit16_2_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit16_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16_2_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16_2_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs new file mode 100644 index 000000000..ff7ce6ac2 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit16_2_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_2_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + + var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit16_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; + else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_2Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_2_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; + else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_2Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_2Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_3_Calculator.cs b/Hast.Samples.Posit/Posit16_3_Calculator.cs new file mode 100644 index 000000000..c986d1a98 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_3_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16_3_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16_3((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16_3(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16_3(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16_3((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16_3((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16_3_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit16_3_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit16_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit16_3_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit16_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_3(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16_3((ushort)memory.ReadUInt32( Posit16_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_3_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit16_3_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16_3_Calculator is (" + + Posit16_3_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit16_3_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16_3_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16_3_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16_3_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit16_3_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit16_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16_3_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16_3_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs new file mode 100644 index 000000000..f57a0c466 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit16_3_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_3_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + + var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit16_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; + else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_3Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_3_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; + else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_3Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_3Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_4_Calculator.cs b/Hast.Samples.Posit/Posit16_4_Calculator.cs new file mode 100644 index 000000000..6ab3ed9b0 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_4_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16_4_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16_4((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16_4(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16_4(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16_4((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16_4((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16_4_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit16_4_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit16_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit16_4_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit16_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_4(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16_4((ushort)memory.ReadUInt32( Posit16_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_4_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit16_4_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16_4_Calculator is (" + + Posit16_4_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit16_4_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16_4_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16_4_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16_4_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit16_4_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit16_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16_4_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16_4_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs new file mode 100644 index 000000000..117719576 --- /dev/null +++ b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit16_4_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_4_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + + var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit16_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; + else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_4Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_4_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; + else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_4Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_4Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_0_Calculator.cs b/Hast.Samples.Posit/Posit32_0_Calculator.cs new file mode 100644 index 000000000..8c74d4599 --- /dev/null +++ b/Hast.Samples.Posit/Posit32_0_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit32_0_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit32_0((uint)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit32_0(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit32_0(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit32_0((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit32_0((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit32_0_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit32_0_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit32_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit32_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit32_0_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit32_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_0(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit32_0((uint)memory.ReadUInt32( Posit32_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_0_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit32_0_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit32_0_Calculator is (" + + Posit32_0_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit32_0_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit32_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit32_0_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit32_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit32_0_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit32_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit32_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit32_0_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs new file mode 100644 index 000000000..607a16701 --- /dev/null +++ b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit32_0_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_0_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + + var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit32_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; + else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_0Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_0_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; + else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_0Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_0Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_1_Calculator.cs b/Hast.Samples.Posit/Posit32_1_Calculator.cs new file mode 100644 index 000000000..b646ce70d --- /dev/null +++ b/Hast.Samples.Posit/Posit32_1_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit32_1_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit32_1((uint)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit32_1(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit32_1(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit32_1((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit32_1((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit32_1_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit32_1_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit32_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit32_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit32_1_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit32_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_1(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit32_1((uint)memory.ReadUInt32( Posit32_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_1_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit32_1_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit32_1_Calculator is (" + + Posit32_1_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit32_1_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit32_1_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit32_1_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit32_1_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit32_1_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit32_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit32_1_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit32_1_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs new file mode 100644 index 000000000..e30bda029 --- /dev/null +++ b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit32_1_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_1_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + + var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit32_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; + else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_1Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_1_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; + else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_1Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_1Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_2_Calculator.cs b/Hast.Samples.Posit/Posit32_2_Calculator.cs new file mode 100644 index 000000000..c7e623792 --- /dev/null +++ b/Hast.Samples.Posit/Posit32_2_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit32_2_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit32_2((uint)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit32_2(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit32_2(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit32_2((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit32_2((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit32_2_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit32_2_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit32_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit32_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit32_2_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit32_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_2(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit32_2((uint)memory.ReadUInt32( Posit32_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_2_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit32_2_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit32_2_Calculator is (" + + Posit32_2_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit32_2_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit32_2_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit32_2_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit32_2_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit32_2_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit32_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit32_2_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit32_2_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs new file mode 100644 index 000000000..6b0c23a2d --- /dev/null +++ b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs @@ -0,0 +1,112 @@ +using Hast.Layer; +using Lombiq.Arithmetics; +using System; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace Hast.Samples.Posit +{ + internal class Posit32_2_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_2_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal(1000, (float)1.015625); + + + var numbers = new int[Posit32_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit32_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_2Array[i] = new Posit32_2((float)0.25 * 2 * i).PositBits; + else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_2Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_2_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal(1000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_2Array[i] = new Posit32_2((float)0.25 * 2 * i).PositBits; + else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray(Posit32_2Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_2Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_3_Calculator.cs b/Hast.Samples.Posit/Posit32_3_Calculator.cs new file mode 100644 index 000000000..90f03e00a --- /dev/null +++ b/Hast.Samples.Posit/Posit32_3_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit32_3_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit32_3((uint)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit32_3(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit32_3(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit32_3((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit32_3((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit32_3_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit32_3_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit32_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit32_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit32_3_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit32_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_3(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit32_3((uint)memory.ReadUInt32( Posit32_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_3_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit32_3_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit32_3_Calculator is (" + + Posit32_3_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit32_3_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit32_3_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit32_3_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit32_3_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit32_3_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit32_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit32_3_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit32_3_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs new file mode 100644 index 000000000..d4453aad0 --- /dev/null +++ b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit32_3_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_3_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + + var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit32_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; + else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_3Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_3_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; + else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_3Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_3Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_4_Calculator.cs b/Hast.Samples.Posit/Posit32_4_Calculator.cs new file mode 100644 index 000000000..46856fe91 --- /dev/null +++ b/Hast.Samples.Posit/Posit32_4_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit32_4_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit32_4((uint)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit32_4(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit32_4(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit32_4((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit32_4((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit32_4_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit32_4_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit32_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit32_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit32_4_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit32_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_4(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit32_4((uint)memory.ReadUInt32( Posit32_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_4_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit32_4_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit32_4_Calculator is (" + + Posit32_4_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit32_4_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit32_4_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit32_4_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit32_4_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit32_4_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit32_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit32_4_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit32_4_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs new file mode 100644 index 000000000..e62f128ae --- /dev/null +++ b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit32_4_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_4_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + + var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit32_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; + else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_4Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_4_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; + else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_4Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_4Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_0_Calculator.cs b/Hast.Samples.Posit/Posit8_0_Calculator.cs new file mode 100644 index 000000000..67f6f7434 --- /dev/null +++ b/Hast.Samples.Posit/Posit8_0_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8_0_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8_0((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8_0(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8_0(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8_0((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8_0((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8_0_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit8_0_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit8_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit8_0_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit8_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_0(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8_0((byte)memory.ReadUInt32( Posit8_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_0_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit8_0_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8_0_Calculator is (" + + Posit8_0_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit8_0_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8_0_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit8_0_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit8_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8_0_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs new file mode 100644 index 000000000..7894d54bd --- /dev/null +++ b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit8_0_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_0_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + + + var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit8_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; + else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_0Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_0_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit8_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; + else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit8_0Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_0Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_1_Calculator.cs b/Hast.Samples.Posit/Posit8_1_Calculator.cs new file mode 100644 index 000000000..2df896183 --- /dev/null +++ b/Hast.Samples.Posit/Posit8_1_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8_1_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8_1((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8_1(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8_1(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8_1((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8_1((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8_1_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit8_1_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit8_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit8_1_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit8_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_1(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8_1((byte)memory.ReadUInt32( Posit8_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_1_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit8_1_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8_1_Calculator is (" + + Posit8_1_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit8_1_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8_1_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8_1_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8_1_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit8_1_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit8_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8_1_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8_1_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs new file mode 100644 index 000000000..c9bf8e2b9 --- /dev/null +++ b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit8_1_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_1_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + + + var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit8_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; + else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_1Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_1_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit8_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; + else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit8_1Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_1Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_2_Calculator.cs b/Hast.Samples.Posit/Posit8_2_Calculator.cs new file mode 100644 index 000000000..0bb112036 --- /dev/null +++ b/Hast.Samples.Posit/Posit8_2_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8_2_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8_2((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8_2(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8_2(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8_2((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8_2((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8_2_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit8_2_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit8_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit8_2_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit8_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_2(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8_2((byte)memory.ReadUInt32( Posit8_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_2_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit8_2_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8_2_Calculator is (" + + Posit8_2_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit8_2_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8_2_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8_2_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8_2_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit8_2_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit8_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8_2_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8_2_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs new file mode 100644 index 000000000..f32121cbc --- /dev/null +++ b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit8_2_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_2_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + + + var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit8_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; + else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_2Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_2_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit8_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; + else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit8_2Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_2Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_3_Calculator.cs b/Hast.Samples.Posit/Posit8_3_Calculator.cs new file mode 100644 index 000000000..156c64e93 --- /dev/null +++ b/Hast.Samples.Posit/Posit8_3_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8_3_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8_3((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8_3(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8_3(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8_3((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8_3((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8_3_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit8_3_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit8_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit8_3_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit8_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_3(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8_3((byte)memory.ReadUInt32( Posit8_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_3_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit8_3_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8_3_Calculator is (" + + Posit8_3_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit8_3_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8_3_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8_3_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8_3_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit8_3_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit8_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8_3_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8_3_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs new file mode 100644 index 000000000..fc69d595e --- /dev/null +++ b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit8_3_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_3_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + + + var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit8_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; + else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_3Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_3_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit8_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; + else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit8_3Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_3Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_4_Calculator.cs b/Hast.Samples.Posit/Posit8_4_Calculator.cs new file mode 100644 index 000000000..530815fb7 --- /dev/null +++ b/Hast.Samples.Posit/Posit8_4_Calculator.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8_4_Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8_4((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8_4(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8_4(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8_4((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8_4((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8_4_CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this Posit8_4_Calculator positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( Posit8_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this Posit8_4_Calculator positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( Posit8_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_4(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8_4((byte)memory.ReadUInt32( Posit8_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_4_Calculator positCalculator, int[] numbers) + { + if (numbers.Length != Posit8_4_Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8_4_Calculator is (" + + Posit8_4_Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( Posit8_4_Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8_4_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8_4_Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8_4_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this Posit8_4_Calculator positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( Posit8_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8_4_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8_4_Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs new file mode 100644 index 000000000..87d14b80d --- /dev/null +++ b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class Posit8_4_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_4_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + + + var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var Posit8_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; + else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_4Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_4_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit8_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; + else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit8_4Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_4Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + diff --git a/Hast.Samples.Posit/PositCalculator.cs b/Hast.Samples.Posit/PositCalculator.cs new file mode 100644 index 000000000..3079bb6b3 --- /dev/null +++ b/Hast.Samples.Posit/PositCalculator.cs @@ -0,0 +1,21 @@ + + + + +// Generated helper templates +// Generated items +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_4_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_3_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_2_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_1_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_0_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_4_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_3_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_2_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_1_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_0_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_4_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_3_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_2_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_1_Calculator.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_0_Calculator.cs diff --git a/Hast.Samples.Posit/PositCalculator.tt b/Hast.Samples.Posit/PositCalculator.tt new file mode 100644 index 000000000..d62b68378 --- /dev/null +++ b/Hast.Samples.Posit/PositCalculator.tt @@ -0,0 +1,213 @@ +<#@ template debug="true" hostSpecific="true" #> +<#@ output extension=".cs" #> +<#@ include file="PositCalculatorTemplateFileManager.ttinclude" #> + + +<# + +var manager = TemplateFileManager.Create(this); + +#> + +<# + var positSizes = new byte[] {8,16,32,64}; + var underLyingStructureName= new string[] {"byte", "ushort","uint","ulong","BitMask"}; + +for (var i = 0; i<3; i++){ + for(var MaximumExponentSize = 0; MaximumExponentSize <= 4; MaximumExponentSize++){ + var positName = "Posit"+positSizes[i] + "_" + MaximumExponentSize; + var className = positName + "_Calculator"; + var quireSize = 1; + while (quireSize < (4*positSizes[i]-8)*(1< +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class <#= className#> + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new <#=positName#>((<#= underLyingStructureName[i]#>)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (<#= underLyingStructureName[i]#>)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new <#=positName#>(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new <#=positName#>(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new <#=positName#>((<#= underLyingStructureName[i]#>)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new <#=positName#>((<#= underLyingStructureName[i]#>)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class <#=className#>CalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber(this <#= className#> positCalculator, int number) + { + var memory = new SimpleMemory(1); + + memory.WriteInt32( <#= className#>.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( <#= className#>.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal(this <#= className#> positCalculator, int number, float real) + { + var memory = new SimpleMemory(2); + + memory.WriteInt32( <#= className#>.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( <#= className#>.CalculatePowerOfReal_InputPosit32Index, new <#=positName#>(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new <#=positName#>((<#= underLyingStructureName[i]#>)memory.ReadUInt32( <#= className#>.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this <#= className#> positCalculator, int[] numbers) + { + if (numbers.Length != <#= className#>.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of <#= className#> is (" + + <#= className#>.MaxDegreeOfParallelism + ")"); + } + + var memory = new SimpleMemory( <#= className#>.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( <#= className#>.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ <#= className#>.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( <#= className#>.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray(this <#= className#> positCalculator, uint[] positArray) + { + var memory = new SimpleMemory( positArray.Length + 1); + + memory.WriteUInt32( <#= className#>.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( <#= className#>.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( <#= className#>.AddPositsInArray_OutputPosit32Index), true); + } + } +} + +<# + } +}#> +<# + +manager.Process(); + +#> \ No newline at end of file diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.cs b/Hast.Samples.Posit/PositCalculatorSampleRunner.cs new file mode 100644 index 000000000..8994f7ce2 --- /dev/null +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.cs @@ -0,0 +1,21 @@ + + + + +// Generated helper templates +// Generated items +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_4_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_3_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_2_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_1_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_0_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_4_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_3_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_2_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_1_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_0_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_4_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_3_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_2_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_1_CalculatorSampleRunner.cs +// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_0_CalculatorSampleRunner.cs diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt new file mode 100644 index 000000000..1935f4b17 --- /dev/null +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt @@ -0,0 +1,177 @@ +<#@ template debug="true" hostSpecific="true" #> +<#@ output extension=".cs" #> +<#@ include file="PositCalculatorTemplateFileManager.ttinclude" #> + + +<# + +var manager = TemplateFileManager.Create(this); + +#> + +<# + var positSizes = new byte[] {8,16,32,64}; + var underLyingStructureName= new string[] {"byte", "ushort","uint","ulong","BitMask"}; + +for (var i = 0; i<3; i++){ + for(var MaximumExponentSize = 0; MaximumExponentSize <=4; MaximumExponentSize++){ + var positName = "Posit"+positSizes[i] + "_" + MaximumExponentSize; + var calculatorName = positName + "_Calculator"; + var className = calculatorName + "SampleRunner"; + var quireSize = 1; + while (quireSize < (4*positSizes[i]-8)*(1<=0 ;n--) + { + maxScaleFactor = Math.Pow((1 << (1 << MaximumExponentSize)), positSizes[i]-2-MaximumExponentSize-n) + (1<= pIntMax) break; + } + manager.StartNewFile(className + ".cs"); +#> +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + internal class <#=className#> + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType<<#=calculatorName#>>(); + } + + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new <#=calculatorName#>()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + + + <# if (i == 0) + { + #> + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + <# } else { #> + positCalculator.CalculatePowerOfReal( <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, (float)1.015625); + <# } #> + + + var numbers = new int[<#=calculatorName#>.MaxDegreeOfParallelism]; + for (int i = 0; i < <#=calculatorName#>.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + + + var <#=positName#>Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) <#=positName#>Array[i] = new <#=positName#>((float)0.25 * 2 * i).PositBits; + else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(<#=positName#>Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new <#=calculatorName#>(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); + + <# if (i == 0) + { + #> + + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); + <# } else { #> + var powerOfReal = positCalculator.CalculatePowerOfReal( <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, (float)1.015625); + <# } #> + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[<#=calculatorName#>.MaxDegreeOfParallelism]; + for (int i = 0; i < <#=calculatorName#>.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var <#=positName#>Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) <#=positName#>Array[i] = new <#=positName#>((float)0.25 * 2 * i).PositBits; + else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( <#=positName#>Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( <#=positName#>Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } +} + +<# + } +}#> +<# + +manager.Process(); + +#> \ No newline at end of file diff --git a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude new file mode 100644 index 000000000..9a50f6c3e --- /dev/null +++ b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude @@ -0,0 +1,1090 @@ +<#@ assembly name="System.Core" #> +<#@ assembly name="System.Data" #> +<#@ assembly name="System.Data.Entity" #> +<#@ assembly name="System.Xml" #> +<#@ assembly name="System.Xml.Linq"#> +<#@ assembly name="System.Windows.Forms" #> +<#@ assembly name="EnvDTE"#> +<#@ assembly name="EnvDTE80" #> +<#@ assembly name="Microsoft.VisualStudio.Shell.15.0"#> +<#@ assembly name="Microsoft.VisualStudio.Shell.Interop"#> +<#@ import namespace="Microsoft.VisualStudio.Shell.Interop"#> +<#@ import namespace="System" #> +<#@ import namespace="System.Data" #> +<#@ import namespace="System.Data.Objects" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.IO" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ import namespace="System.Data.Objects.DataClasses" #> +<#@ import namespace="System.Text.RegularExpressions" #> +<#@ import namespace="System.Xml" #> +<#@ import namespace="System.Xml.Linq" #> +<#@ import namespace="System.Globalization" #> +<#@ import namespace="System.Reflection" #> +<#@ import namespace="System.CodeDom" #> +<#@ import namespace="System.CodeDom.Compiler" #> +<#@ import namespace="Microsoft.CSharp"#> +<#@ import namespace="System.Text"#> +<#@ import namespace="EnvDTE" #> +<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #> +<#+ +/* + This software is supplied "AS IS". The authors disclaim all warranties, + expressed or implied, including, without limitation, the warranties of + merchantability and of fitness for any purpose. The authors assume no + liability for direct, indirect, incidental, special, exemplary, or + consequential damages, which may result from the use of this software, + even if advised of the possibility of such damage. + +The TemplateFileManager is based on EntityFrameworkTemplateFileManager (EFTFM) from MS. + +Differences to EFTFM +Version 2.1: +- Replace Enum BuildAction with class for more flexibility +Version 2: +- StartHeader works with Parameter $filename$ +- StartNewFile has a new named parameter FileProperties + - Support for: + - BuildAction + - CustomTool + - user defined parameter for using in StartHeader-Block +- Property IsAutoIndentEnabled for support Format Document (C#, VB) when set to true + +Version: 1.1 +Add method WriteLineToBuildPane, WriteToBuildPane + +Version 1: +- StartNewFile with named parameters projectName and folderName for generating files to different locations. +- Property CanOverrideExistingFile, to define whether existing files are can overwritten +- Property Encoding Encode type for output files. +*/ + +/// +/// Writes a line to the build pane in visual studio and activates it +/// +/// Text to output - a \n is appended +void WriteLineToBuildPane (string message){ + WriteLineToBuildPane(String.Format("{0}\n", message)); +} + +/// +/// Writes a string to the build pane in visual studio and activates it +/// +/// Text to output +void WriteToBuildPane (string message){ + IVsOutputWindow outWindow = (this.Host as IServiceProvider).GetService( +typeof( SVsOutputWindow ) ) as IVsOutputWindow; + Guid generalPaneGuid = +Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.BuildOutputPane_guid; + // P.S. There's also the GUID_OutWindowDebugPane available. + IVsOutputWindowPane generalPane; + outWindow.GetPane( ref generalPaneGuid , out generalPane ); + generalPane.OutputString( message ); + generalPane.Activate(); // Brings this pane into view +} + +/// +/// Responsible for marking the various sections of the generation, +/// so they can be split up into separate files and projects +/// +/// R. Leupold +public class TemplateFileManager +{ + private EnvDTE.ProjectItem templateProjectItem; + private Action checkOutAction; + private Action> projectSyncAction; + private EnvDTE.DTE dte; + private List templatePlaceholderList = new List(); + + /// + /// Creates files with VS sync + /// + public static TemplateFileManager Create(object textTransformation) + { + DynamicTextTransformation2 transformation = DynamicTextTransformation2.Create(textTransformation); + IDynamicHost2 host = transformation.Host; + return new TemplateFileManager(transformation); + } + + private readonly List files = new List(); + private readonly Block footer = new Block(); + private readonly Block header = new Block(); + private readonly DynamicTextTransformation2 _textTransformation; + + // reference to the GenerationEnvironment StringBuilder on the + // TextTransformation object + private readonly StringBuilder _generationEnvironment; + + private Block currentBlock; + + /// + /// Initializes an TemplateFileManager Instance with the + /// TextTransformation (T4 generated class) that is currently running + /// + private TemplateFileManager(object textTransformation) + { + if (textTransformation == null) + { + throw new ArgumentNullException("textTransformation"); + } + + _textTransformation = DynamicTextTransformation2.Create(textTransformation); + _generationEnvironment = _textTransformation.GenerationEnvironment; + + var hostServiceProvider = _textTransformation.Host.AsIServiceProvider(); + if (hostServiceProvider == null) + { + throw new ArgumentNullException("Could not obtain hostServiceProvider"); + } + + dte = (EnvDTE.DTE) hostServiceProvider.GetService(typeof(EnvDTE.DTE)); + if (dte == null) + { + throw new ArgumentNullException("Could not obtain DTE from host"); + } + + this.templateProjectItem = dte.Solution.FindProjectItem(_textTransformation.Host.TemplateFile); + this.CanOverrideExistingFile = true; + this.IsAutoIndentEnabled = false; + this.Encoding = System.Text.Encoding.UTF8; + checkOutAction = fileName => dte.SourceControl.CheckOutItem(fileName); + projectSyncAction = keepFileNames => ProjectSync(templateProjectItem, keepFileNames); + } + + /// + /// If set to false, existing files are not overwritten + /// + /// + public bool CanOverrideExistingFile { get; set; } + + /// + /// If set to true, output files (c#, vb) are formatted based on the vs settings. + /// + /// + public bool IsAutoIndentEnabled { get; set; } + + /// + /// Defines Encoding format for generated output file. (Default UTF8) + /// + /// + public System.Text.Encoding Encoding { get; set; } + + /// + /// Marks the end of the last file if there was one, and starts a new + /// and marks this point in generation as a new file. + /// + /// Filename + /// Name of the target project for the new file. + /// Name of the target folder for the new file. + /// File property settings in vs for the new File + public void StartNewFile(string name + , string projectName = "", string folderName = "", FileProperties fileProperties = null) + { + if (String.IsNullOrWhiteSpace(name) == true) + { + throw new ArgumentException("name"); + } + + CurrentBlock = new Block + { + Name = name, + ProjectName = projectName, + FolderName = folderName, + FileProperties = fileProperties ?? new FileProperties() + }; + } + + public void StartFooter() + { + CurrentBlock = footer; + } + + public void StartHeader() + { + CurrentBlock = header; + } + + public void EndBlock() + { + if (CurrentBlock == null) + { + return; + } + + CurrentBlock.Length = _generationEnvironment.Length - CurrentBlock.Start; + + if (CurrentBlock != header && CurrentBlock != footer) + { + files.Add(CurrentBlock); + } + + currentBlock = null; + } + + /// + /// Produce the template output files. + /// + public virtual IEnumerable Process(bool split = true) + { + var list = new List(); + + if (split) + { + EndBlock(); + + var headerText = _generationEnvironment.ToString(header.Start, header.Length); + var footerText = _generationEnvironment.ToString(footer.Start, footer.Length); + files.Reverse(); + + foreach (var block in files) + { + var outputPath = VSHelper.GetOutputPath(dte, block, Path.GetDirectoryName(_textTransformation.Host.TemplateFile)); + var fileName = Path.Combine(outputPath, block.Name); + var content = this.ReplaceParameter(headerText, block) + + _generationEnvironment.ToString(block.Start, block.Length) + + footerText; + + var file = new OutputFile + { + FileName = fileName, + ProjectName = block.ProjectName, + FolderName = block.FolderName, + FileProperties = block.FileProperties, + Content = content + }; + + CreateFile(file); + _generationEnvironment.Remove(block.Start, block.Length); + + list.Add(file); + } + } + + projectSyncAction.EndInvoke(projectSyncAction.BeginInvoke(list, null, null)); + this.CleanUpTemplatePlaceholders(); + var items = VSHelper.GetOutputFilesAsProjectItems(this.dte, list); + this.WriteVsProperties(items, list); + + if (this.IsAutoIndentEnabled == true && split == true) + { + this.FormatProjectItems(items); + } + + this.WriteLog(list); + + return list; + } + + private void FormatProjectItems(IEnumerable items) + { + foreach (var item in items) + { + this._textTransformation.WriteLine( + VSHelper.ExecuteVsCommand(this.dte, item, "Edit.FormatDocument")); //, "Edit.RemoveAndSort")); + this._textTransformation.WriteLine("//-> " + item.Name); + } + } + + private void WriteVsProperties(IEnumerable items, IEnumerable outputFiles) + { + foreach (var file in outputFiles) + { + var item = items.Where(p => p.Name == Path.GetFileName(file.FileName)).FirstOrDefault(); + if (item == null) continue; + + if (String.IsNullOrEmpty(file.FileProperties.CustomTool) == false) + { + VSHelper.SetPropertyValue(item, "CustomTool", file.FileProperties.CustomTool); + } + + if (String.IsNullOrEmpty(file.FileProperties.BuildActionString) == false) + { + VSHelper.SetPropertyValue(item, "ItemType", file.FileProperties.BuildActionString); + } + } + } + + private string ReplaceParameter(string text, Block block) + { + if (String.IsNullOrEmpty(text) == false) + { + text = text.Replace("$filename$", block.Name); + } + + + foreach (var item in block.FileProperties.TemplateParameter.AsEnumerable()) + { + text = text.Replace(item.Key, item.Value); + } + + return text; + } + + /// + /// Write log to the default output file. + /// + /// + private void WriteLog(IEnumerable list) + { + this._textTransformation.WriteLine("// Generated helper templates"); + foreach (var item in templatePlaceholderList) + { + this._textTransformation.WriteLine("// " + this.GetDirectorySolutionRelative(item)); + } + + this._textTransformation.WriteLine("// Generated items"); + foreach (var item in list) + { + this._textTransformation.WriteLine("// " + this.GetDirectorySolutionRelative(item.FileName)); + } + } + + /// + /// Removes old template placeholders from the solution. + /// + private void CleanUpTemplatePlaceholders() + { + string[] activeTemplateFullNames = this.templatePlaceholderList.ToArray(); + string[] allHelperTemplateFullNames = VSHelper.GetAllSolutionItems(this.dte) + .Where(p => p.Name == VSHelper.GetTemplatePlaceholderName(this.templateProjectItem)) + .Select(p => VSHelper.GetProjectItemFullPath(p)) + .ToArray(); + + var delta = allHelperTemplateFullNames.Except(activeTemplateFullNames).ToArray(); + + var dirtyHelperTemplates = VSHelper.GetAllSolutionItems(this.dte) + .Where(p => delta.Contains(VSHelper.GetProjectItemFullPath(p))); + + foreach (ProjectItem item in dirtyHelperTemplates) + { + if (item.ProjectItems != null) + { + foreach (ProjectItem subItem in item.ProjectItems) + { + subItem.Remove(); + } + } + + item.Remove(); + } + } + + /// + /// Gets a list of helper templates from the log. + /// + /// List of generated helper templates. + private string[] GetPreviousTemplatePlaceholdersFromLog() + { + string path = Path.GetDirectoryName(this._textTransformation.Host.ResolvePath(this._textTransformation.Host.TemplateFile)); + string file1 = Path.GetFileNameWithoutExtension(this._textTransformation.Host.TemplateFile) + ".txt"; + string contentPrevious = File.ReadAllText(Path.Combine(path, file1)); + + var result = contentPrevious + .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) + .Select(x => x.Split(new[] { "=>" }, StringSplitOptions.RemoveEmptyEntries).First()) + .Select(x => Regex.Replace(x, "//", String.Empty).Trim()) + .Where(x => x.EndsWith(VSHelper.GetTemplatePlaceholderName(this.templateProjectItem))) + .ToArray(); + + return result; + } + + private string GetDirectorySolutionRelative(string fullName) + { + int slnPos = fullName.IndexOf(Path.GetFileNameWithoutExtension(this.dte.Solution.FileName)); + if (slnPos < 0) + { + slnPos = 0; + } + + return fullName.Substring(slnPos); + } + + protected virtual void CreateFile(OutputFile file) + { + if (this.CanOverrideExistingFile == false && File.Exists(file.FileName) == true) + { + return; + } + + if (IsFileContentDifferent(file)) + { + CheckoutFileIfRequired(file.FileName); + File.WriteAllText(file.FileName, file.Content, this.Encoding); + } + } + + protected bool IsFileContentDifferent(OutputFile file) + { + return !(File.Exists(file.FileName) && File.ReadAllText(file.FileName) == file.Content); + } + + private Block CurrentBlock + { + get { return currentBlock; } + set + { + if (CurrentBlock != null) + { + EndBlock(); + } + + if (value != null) + { + value.Start = _generationEnvironment.Length; + } + + currentBlock = value; + } + } + + private void ProjectSync(EnvDTE.ProjectItem templateProjectItem, IEnumerable keepFileNames) + { + var groupedFileNames = from f in keepFileNames + group f by new { f.ProjectName, f.FolderName } + into l + select new { + ProjectName = l.Key.ProjectName, + FolderName = l.Key.FolderName, + FirstItem = l.First(), + OutputFiles = l + }; + + this.templatePlaceholderList.Clear(); + + foreach (var item in groupedFileNames) + { + EnvDTE.ProjectItem pi = VSHelper.GetTemplateProjectItem(templateProjectItem.DTE, item.FirstItem, templateProjectItem); + ProjectSyncPart(pi, item.OutputFiles); + + if (pi.Name.EndsWith("txt4")) + this.templatePlaceholderList.Add(VSHelper.GetProjectItemFullPath(pi)); + } + + // clean up + bool hasDefaultItems = groupedFileNames.Where(f => String.IsNullOrEmpty(f.ProjectName) && String.IsNullOrEmpty(f.FolderName)).Count() > 0; + if (hasDefaultItems == false) + { + ProjectSyncPart(templateProjectItem, new List()); + } + } + + private static void ProjectSyncPart(EnvDTE.ProjectItem templateProjectItem, IEnumerable keepFileNames) + { + var keepFileNameSet = new HashSet(keepFileNames); + var projectFiles = new Dictionary(); + var originalOutput = Path.GetFileNameWithoutExtension(templateProjectItem.FileNames[0]); + + foreach (EnvDTE.ProjectItem projectItem in templateProjectItem.ProjectItems) + { + projectFiles.Add(projectItem.FileNames[0], projectItem); + } + + // Remove unused items from the project + foreach (var pair in projectFiles) + { + bool isNotFound = keepFileNames.Where(f=>f.FileName == pair.Key).Count() == 0; + if (isNotFound == true + && !(Path.GetFileNameWithoutExtension(pair.Key) + ".").StartsWith(originalOutput + ".")) + { + pair.Value.Delete(); + } + } + + // Add missing files to the project + foreach (var fileName in keepFileNameSet) + { + if (!projectFiles.ContainsKey(fileName.FileName)) + { + templateProjectItem.ProjectItems.AddFromFile(fileName.FileName); + } + } + } + + private void CheckoutFileIfRequired(string fileName) + { + if (dte.SourceControl == null + || !dte.SourceControl.IsItemUnderSCC(fileName) + || dte.SourceControl.IsItemCheckedOut(fileName)) + { + return; + } + + // run on worker thread to prevent T4 calling back into VS + checkOutAction.EndInvoke(checkOutAction.BeginInvoke(fileName, null, null)); + } +} + +/// +/// Responsible creating an instance that can be passed +/// to helper classes that need to access the TextTransformation +/// members. It accesses member by name and signature rather than +/// by type. This is necessary when the +/// template is being used in Preprocessed mode +/// and there is no common known type that can be +/// passed instead +/// +public class DynamicTextTransformation2 +{ + private object _instance; + IDynamicHost2 _dynamicHost; + + private readonly MethodInfo _write; + private readonly MethodInfo _writeLine; + private readonly PropertyInfo _generationEnvironment; + private readonly PropertyInfo _errors; + private readonly PropertyInfo _host; + + /// + /// Creates an instance of the DynamicTextTransformation class around the passed in + /// TextTransformation shapped instance passed in, or if the passed in instance + /// already is a DynamicTextTransformation, it casts it and sends it back. + /// + public static DynamicTextTransformation2 Create(object instance) + { + if (instance == null) + { + throw new ArgumentNullException("instance"); + } + + DynamicTextTransformation2 textTransformation = instance as DynamicTextTransformation2; + if (textTransformation != null) + { + return textTransformation; + } + + return new DynamicTextTransformation2(instance); + } + + private DynamicTextTransformation2(object instance) + { + _instance = instance; + Type type = _instance.GetType(); + _write = type.GetMethod("Write", new Type[] { typeof(string) }); + _writeLine = type.GetMethod("WriteLine", new Type[] { typeof(string) }); + _generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance | BindingFlags.NonPublic); + _host = type.GetProperty("Host"); + _errors = type.GetProperty("Errors"); + } + + /// + /// Gets the value of the wrapped TextTranformation instance's GenerationEnvironment property + /// + public StringBuilder GenerationEnvironment { get { return (StringBuilder)_generationEnvironment.GetValue(_instance, null); } } + + /// + /// Gets the value of the wrapped TextTranformation instance's Errors property + /// + public System.CodeDom.Compiler.CompilerErrorCollection Errors { get { return (System.CodeDom.Compiler.CompilerErrorCollection)_errors.GetValue(_instance, null); } } + + /// + /// Calls the wrapped TextTranformation instance's Write method. + /// + public void Write(string text) + { + _write.Invoke(_instance, new object[] { text }); + } + + /// + /// Calls the wrapped TextTranformation instance's WriteLine method. + /// + public void WriteLine(string text) + { + _writeLine.Invoke(_instance, new object[] { text }); + } + + /// + /// Gets the value of the wrapped TextTranformation instance's Host property + /// if available (shows up when hostspecific is set to true in the template directive) and returns + /// the appropriate implementation of IDynamicHost + /// + public IDynamicHost2 Host + { + get + { + if (_dynamicHost == null) + { + if(_host == null) + { + _dynamicHost = new NullHost2(); + } + else + { + _dynamicHost = new DynamicHost2(_host.GetValue(_instance, null)); + } + } + return _dynamicHost; + } + } +} + +/// +/// Reponsible for abstracting the use of Host between times +/// when it is available and not +/// +public interface IDynamicHost2 +{ + /// + /// An abstracted call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost ResolveParameterValue + /// + string ResolveParameterValue(string id, string name, string otherName); + + /// + /// An abstracted call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost ResolvePath + /// + string ResolvePath(string path); + + /// + /// An abstracted call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost TemplateFile + /// + string TemplateFile { get; } + + /// + /// Returns the Host instance cast as an IServiceProvider + /// + IServiceProvider AsIServiceProvider(); +} + +/// +/// Reponsible for implementing the IDynamicHost as a dynamic +/// shape wrapper over the Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost interface +/// rather than type dependent wrapper. We don't use the +/// interface type so that the code can be run in preprocessed mode +/// on a .net framework only installed machine. +/// +public class DynamicHost2 : IDynamicHost2 +{ + private readonly object _instance; + private readonly MethodInfo _resolveParameterValue; + private readonly MethodInfo _resolvePath; + private readonly PropertyInfo _templateFile; + + /// + /// Creates an instance of the DynamicHost class around the passed in + /// Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost shapped instance passed in. + /// + public DynamicHost2(object instance) + { + _instance = instance; + Type type = _instance.GetType(); + _resolveParameterValue = type.GetMethod("ResolveParameterValue", new Type[] { typeof(string), typeof(string), typeof(string) }); + _resolvePath = type.GetMethod("ResolvePath", new Type[] { typeof(string) }); + _templateFile = type.GetProperty("TemplateFile"); + + } + + /// + /// A call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost ResolveParameterValue + /// + public string ResolveParameterValue(string id, string name, string otherName) + { + return (string)_resolveParameterValue.Invoke(_instance, new object[] { id, name, otherName }); + } + + /// + /// A call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost ResolvePath + /// + public string ResolvePath(string path) + { + return (string)_resolvePath.Invoke(_instance, new object[] { path }); + } + + /// + /// A call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost TemplateFile + /// + public string TemplateFile + { + get + { + return (string)_templateFile.GetValue(_instance, null); + } + } + + /// + /// Returns the Host instance cast as an IServiceProvider + /// + public IServiceProvider AsIServiceProvider() + { + return _instance as IServiceProvider; + } +} + +/// +/// Reponsible for implementing the IDynamicHost when the +/// Host property is not available on the TextTemplating type. The Host +/// property only exists when the hostspecific attribute of the template +/// directive is set to true. +/// +public class NullHost2 : IDynamicHost2 +{ + /// + /// An abstraction of the call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost ResolveParameterValue + /// that simply retuns null. + /// + public string ResolveParameterValue(string id, string name, string otherName) + { + return null; + } + + /// + /// An abstraction of the call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost ResolvePath + /// that simply retuns the path passed in. + /// + public string ResolvePath(string path) + { + return path; + } + + /// + /// An abstraction of the call to Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost TemplateFile + /// that returns null. + /// + public string TemplateFile + { + get + { + return null; + } + } + + /// + /// Returns null. + /// + public IServiceProvider AsIServiceProvider() + { + return null; + } +} + +public sealed class Block +{ + public String Name; + public int Start, Length; + public string ProjectName { get; set; } + public string FolderName { get; set; } + public FileProperties FileProperties { get; set; } +} + +public class ParamTextTemplate +{ + private ITextTemplatingEngineHost Host { get; set; } + + private ParamTextTemplate(ITextTemplatingEngineHost host) + { + this.Host = host; + } + + public static ParamTextTemplate Create(ITextTemplatingEngineHost host) + { + return new ParamTextTemplate(host); + } + + public static TextTemplatingSession GetSessionObject() + { + return new TextTemplatingSession(); + } + + public string TransformText(string templateName, TextTemplatingSession session) + { + return this.GetTemplateContent(templateName, session); + } + + public string GetTemplateContent(string templateName, TextTemplatingSession session) + { + string fullName = this.Host.ResolvePath(templateName); + string templateContent = File.ReadAllText(fullName); + + var sessionHost = this.Host as ITextTemplatingSessionHost; + sessionHost.Session = session; + + Engine engine = new Engine(); + return engine.ProcessTemplate(templateContent, this.Host); + } +} + +public class VSHelper +{ + /// + /// Execute Visual Studio commands against the project item. + /// + /// The current project item. + /// The vs command as string. + /// An error message if the command fails. + public static string ExecuteVsCommand(EnvDTE.DTE dte, EnvDTE.ProjectItem item, params string[] command) + { + if (item == null) + { + throw new ArgumentNullException("item"); + } + + string error = String.Empty; + + try + { + EnvDTE.Window window = item.Open(); + window.Activate(); + + foreach (var cmd in command) + { + if (String.IsNullOrWhiteSpace(cmd) == true) + { + continue; + } + + EnvDTE80.DTE2 dte2 = dte as EnvDTE80.DTE2; + dte2.ExecuteCommand(cmd, String.Empty); + } + + item.Save(); + window.Visible = false; + // window.Close(); // Ends VS, but not the tab :( + } + catch (Exception ex) + { + error = String.Format("Error processing file {0} {1}", item.Name, ex.Message); + } + + return error; + } + + /// + /// Sets a property value for the vs project item. + /// + public static void SetPropertyValue(EnvDTE.ProjectItem item, string propertyName, object value) + { + EnvDTE.Property property = item.Properties.Item(propertyName); + if (property == null) + { + throw new ArgumentException(String.Format("The property {0} was not found.", propertyName)); + } + else + { + property.Value = value; + } + } + + public static IEnumerable GetOutputFilesAsProjectItems(EnvDTE.DTE dte, IEnumerable outputFiles) + { + var fileNames = (from o in outputFiles + select Path.GetFileName(o.FileName)).ToArray(); + + return VSHelper.GetAllSolutionItems(dte).Where(f => fileNames.Contains(f.Name)); + } + + public static string GetOutputPath(EnvDTE.DTE dte, Block block, string defaultPath) + { + if (String.IsNullOrEmpty(block.ProjectName) == true && String.IsNullOrEmpty(block.FolderName) == true) + { + return defaultPath; + } + + EnvDTE.Project prj = null; + EnvDTE.ProjectItem item = null; + + if (String.IsNullOrEmpty(block.ProjectName) == false) + { + prj = GetProject(dte, block.ProjectName); + } + + if (String.IsNullOrEmpty(block.FolderName) == true && prj != null) + { + return Path.GetDirectoryName(prj.FullName); + } + else if (prj != null && String.IsNullOrEmpty(block.FolderName) == false) + { + item = GetAllProjectItemsRecursive(prj.ProjectItems).Where(i=>i.Name == block.FolderName).First(); + } + else if (String.IsNullOrEmpty(block.FolderName) == false) + { + item = GetAllProjectItemsRecursive( + dte.ActiveDocument.ProjectItem.ContainingProject.ProjectItems). + Where(i=>i.Name == block.FolderName).First(); + } + + if (item != null) + { + return GetProjectItemFullPath(item); + } + + return defaultPath; + } + public static string GetTemplatePlaceholderName(EnvDTE.ProjectItem item) + { + return String.Format("{0}.txt4", Path.GetFileNameWithoutExtension(item.Name)); + } + + public static EnvDTE.ProjectItem GetTemplateProjectItem(EnvDTE.DTE dte, OutputFile file, EnvDTE.ProjectItem defaultItem) + { + if (String.IsNullOrEmpty(file.ProjectName) == true && String.IsNullOrEmpty(file.FolderName) == true) + { + return defaultItem; + } + + string templatePlaceholder = GetTemplatePlaceholderName(defaultItem); + string itemPath = Path.GetDirectoryName(file.FileName); + string fullName = Path.Combine(itemPath, templatePlaceholder); + EnvDTE.Project prj = null; + EnvDTE.ProjectItem item = null; + + if (String.IsNullOrEmpty(file.ProjectName) == false) + { + prj = GetProject(dte, file.ProjectName); + } + + if (String.IsNullOrEmpty(file.FolderName) == true && prj != null) + { + return FindProjectItem(prj.ProjectItems, fullName, true); + } + else if (prj != null && String.IsNullOrEmpty(file.FolderName) == false) + { + item = GetAllProjectItemsRecursive(prj.ProjectItems).Where(i=>i.Name == file.FolderName).First(); + } + else if (String.IsNullOrEmpty(file.FolderName) == false) + { + item = GetAllProjectItemsRecursive( + dte.ActiveDocument.ProjectItem.ContainingProject.ProjectItems). + Where(i=>i.Name == file.FolderName).First(); + } + + if (item != null) + { + return FindProjectItem(item.ProjectItems, fullName, true); + } + + return defaultItem; + } + + private static EnvDTE.ProjectItem FindProjectItem(EnvDTE.ProjectItems items, string fullName, bool canCreateIfNotExists) + { + EnvDTE.ProjectItem item = (from i in items.Cast() + where i.Name == Path.GetFileName(fullName) + select i).FirstOrDefault(); + if (item == null) + { + File.CreateText(fullName); + item = items.AddFromFile(fullName); + } + + return item; + } + + public static EnvDTE.Project GetProject(EnvDTE.DTE dte, string projectName) + { + return GetAllProjects(dte).Where(p=>p.Name == projectName).First(); + } + + public static IEnumerable GetAllProjects(EnvDTE.DTE dte) + { + List projectList = new List(); + + var folders = dte.Solution.Projects.Cast().Where(p=>p.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder); + + foreach (EnvDTE.Project folder in folders) + { + if (folder.ProjectItems == null) continue; + + foreach (EnvDTE.ProjectItem item in folder.ProjectItems) + { + if (item.Object is EnvDTE.Project) + projectList.Add(item.Object as EnvDTE.Project); + } + } + + var projects = dte.Solution.Projects.Cast().Where(p=>p.Kind != EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder); + + if (projects.Count() > 0) + projectList.AddRange(projects); + + return projectList; + } + + public static EnvDTE.ProjectItem GetProjectItemWithName(EnvDTE.ProjectItems items, string itemName) + { + return GetAllProjectItemsRecursive(items).Cast().Where(i=>i.Name == itemName).First(); + } + + public static string GetProjectItemFullPath(EnvDTE.ProjectItem item) + { + return item.Properties.Item("FullPath").Value.ToString(); + } + + public static IEnumerable GetAllSolutionItems(EnvDTE.DTE dte) + { + List itemList = new List(); + + foreach (Project item in GetAllProjects(dte)) + { + if (item == null || item.ProjectItems == null) continue; + + itemList.AddRange(GetAllProjectItemsRecursive(item.ProjectItems)); + } + + return itemList; + } + + public static IEnumerable GetAllProjectItemsRecursive(EnvDTE.ProjectItems projectItems) + { + foreach (EnvDTE.ProjectItem projectItem in projectItems) + { + if (projectItem.ProjectItems == null) continue; + + foreach (EnvDTE.ProjectItem subItem in GetAllProjectItemsRecursive(projectItem.ProjectItems)) + { + yield return subItem; + } + + + yield return projectItem; + } + } +} + +public sealed class OutputFile +{ + public OutputFile() + { + this.FileProperties = new FileProperties + { + CustomTool = String.Empty, + BuildAction = BuildAction.None + }; + } + + public string FileName { get; set; } + public string ProjectName { get; set; } + public string FolderName { get; set; } + public string Content { get; set; } + public FileProperties FileProperties { get; set; } +} + +public class BuildAction +{ + public const string None = "None"; + public const string Compile = "Compile"; + public const string Content = "Content"; + public const string EmbeddedResource = "EmbeddedResource"; + public const string EntityDeploy = "EntityDeploy"; +} + +public sealed class FileProperties +{ + public FileProperties () + { + this.TemplateParameter = new Dictionary(); + } + + public string CustomTool { get; set; } + public string BuildAction { get; set; } + public Dictionary TemplateParameter { get; set; } + + internal string BuildActionString + { + get + { + return this.BuildAction; + } + } +} + + +#> \ No newline at end of file diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs new file mode 100644 index 000000000..1f1dd9401 --- /dev/null +++ b/Hast.Samples.Posit/Program.cs @@ -0,0 +1,410 @@ + + +using Hast.Layer; +using Hast.Transformer.Vhdl.Abstractions.Configuration; +using Lombiq.Arithmetics; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace Hast.Samples.Posit +{ + internal static class Configuration + { + public static string VhdlOutputFilePath = @"Hast_IP.vhd"; + + public static Sample SampleToRun = Sample.Posit32_2_Calculator; + } + + class Program + { + static void Main(string[] args) + { + Task.Run(async () => + { + /* + * On a high level these are the steps to use Hastlayer: + * 1. Create the Hastlayer shell. + * 2. Configure hardware generation and generate FPGA hardware representation of the given .NET code. + * 3. Generate proxies for hardware-transformed types and use these proxies to utilize hardware + * implementations. (You can see this inside the SampleRunners.) + */ + + // Configuring the Hastlayer shell. Which flavor should we use? If you're unsure then you'll need + // the Client flavor: This will let you connect to a remote Hastlayer service to run the software + // to hardware transformation. + var hastlayerConfiguration = new HastlayerConfiguration { Flavor = HastlayerFlavor.Developer }; + + // Initializing a Hastlayer shell. Since this is non-trivial to do you can cache this shell object + // while the program runs and re-use it continuously. No need to always wrap it into a using() like + // here, just make sure to Dispose() it before the program terminates. + using (var hastlayer = await Hastlayer.Create(hastlayerConfiguration)) + { + // Hooking into an event of Hastlayer so some execution information can be made visible on the + // console. + hastlayer.ExecutedOnHardware += (sender, e) => + { + Console.WriteLine( + "Executing " + + e.MemberFullName + + " on hardware took " + + e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds + + " milliseconds (net) " + + e.HardwareExecutionInformation.FullExecutionTimeMilliseconds + + " milliseconds (all together)"); + }; + + + // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. + var devices = await hastlayer.GetSupportedDevices(); + // Let's just use the first one that is available. However you might want to use a specific + // device, not just any first one. + var configuration = new HardwareGenerationConfiguration(devices.First().Name); + + // If you're running Hastlayer in the Client flavor, you also need to configure some credentials + // here: + var remoteClientConfiguration = configuration.RemoteClientConfiguration(); + remoteClientConfiguration.AppName = "TestApp"; + remoteClientConfiguration.AppSecret = "appsecret"; + if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && + remoteClientConfiguration.AppSecret == "appsecret") + { + throw new InvalidOperationException( + "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); + } + + + // Letting the configuration of samples run. Check out those methods too! + switch (Configuration.SampleToRun) + { + case Sample.Posit8_0_Calculator: + Posit8_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_1_Calculator: + Posit8_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_2_Calculator: + Posit8_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_3_Calculator: + Posit8_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_4_Calculator: + Posit8_4_CalculatorSampleRunner.Configure(configuration); + break; + //case Sample.Posit8_0_AdvancedCalculator: + // Posit8_0_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_1_AdvancedCalculator: + // Posit8_1_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_2_AdvancedCalculator: + // Posit8_2_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_3_AdvancedCalculator: + // Posit8_3_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_4_AdvancedCalculator: + // Posit8_4_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_0_FusedCalculator: + // Posit8_0_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_1_FusedCalculator: + // Posit8_1_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_2_FusedCalculator: + // Posit8_2_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_3_FusedCalculator: + // Posit8_3_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit8_4_FusedCalculator: + // Posit8_4_FusedCalculatorSampleRunner.Configure(configuration); + // break; + case Sample.Posit16_0_Calculator: + Posit16_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_1_Calculator: + Posit16_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_2_Calculator: + Posit16_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_3_Calculator: + Posit16_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_4_Calculator: + Posit16_4_CalculatorSampleRunner.Configure(configuration); + break; + //case Sample.Posit16_0_AdvancedCalculator: + // Posit16_0_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_1_AdvancedCalculator: + // Posit16_1_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_2_AdvancedCalculator: + // Posit16_2_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_3_AdvancedCalculator: + // Posit16_3_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_4_AdvancedCalculator: + // Posit16_4_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_0_FusedCalculator: + // Posit16_0_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_1_FusedCalculator: + // Posit16_1_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_2_FusedCalculator: + // Posit16_2_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_3_FusedCalculator: + // Posit16_3_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit16_4_FusedCalculator: + // Posit16_4_FusedCalculatorSampleRunner.Configure(configuration); + // break; + case Sample.Posit32_0_Calculator: + Posit32_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_1_Calculator: + Posit32_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_2_Calculator: + Posit32_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_3_Calculator: + Posit32_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_4_Calculator: + Posit32_4_CalculatorSampleRunner.Configure(configuration); + break; + //case Sample.Posit32_0_AdvancedCalculator: + // Posit32_0_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_1_AdvancedCalculator: + // Posit32_1_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_2_AdvancedCalculator: + // Posit32_2_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_3_AdvancedCalculator: + // Posit32_3_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_4_AdvancedCalculator: + // Posit32_4_AdvancedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_0_FusedCalculator: + // Posit32_0_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_1_FusedCalculator: + // Posit32_1_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_2_FusedCalculator: + // Posit32_2_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_3_FusedCalculator: + // Posit32_3_FusedCalculatorSampleRunner.Configure(configuration); + // break; + //case Sample.Posit32_4_FusedCalculator: + // Posit32_4_FusedCalculatorSampleRunner.Configure(configuration); + // break; + + default: + break; + } + + // The generated VHDL code will contain debug-level information, though it will be slower to + // create. + configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; + + Console.WriteLine("Hardware generation starts."); + + // Generating hardware from the sample assembly with the given configuration. + var hardwareRepresentation = await hastlayer.GenerateHardware( + new[] + { + typeof(Posit8_0).Assembly, + typeof(Posit8_0_Calculator).Assembly, + }, + configuration); + + Console.WriteLine("Hardware generation finished."); + Console.WriteLine(); + + // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't + // cause any problems, but sometimes they can. + if (hardwareRepresentation.HardwareDescription.Warnings.Any()) + { + Console.WriteLine( + "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + + Environment.NewLine + + string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); + Console.WriteLine(); + } + + if (!string.IsNullOrEmpty(Configuration.VhdlOutputFilePath)) + { + Console.WriteLine("Writing VHDL source to file."); + + await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); + + Console.WriteLine("VHDL source written to file."); + Console.WriteLine(); + } + + Console.WriteLine("Starting hardware execution."); + + // Running samples. + switch (Configuration.SampleToRun) + { + case Sample.Posit8_0_Calculator: + await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_1_Calculator: + await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_2_Calculator: + await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_3_Calculator: + await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_4_Calculator: + await Posit8_4_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + //case Sample.Posit8_0_AdvancedCalculator: + // await Posit8_0_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_1_AdvancedCalculator: + // await Posit8_1_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_2_AdvancedCalculator: + // await Posit8_2_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_3_AdvancedCalculator: + // await Posit8_3_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_4_AdvancedCalculator: + // await Posit8_4_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_0_FusedCalculator: + // await Posit8_0_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_1_FusedCalculator: + // await Posit8_1_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_2_FusedCalculator: + // await Posit8_2_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_3_FusedCalculator: + // await Posit8_3_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit8_4_FusedCalculator: + // await Posit8_4_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + case Sample.Posit16_0_Calculator: + await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_1_Calculator: + await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_2_Calculator: + await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_3_Calculator: + await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_4_Calculator: + await Posit16_4_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + //case Sample.Posit16_0_AdvancedCalculator: + // await Posit16_0_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_1_AdvancedCalculator: + // await Posit16_1_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_2_AdvancedCalculator: + // await Posit16_2_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_3_AdvancedCalculator: + // await Posit16_3_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_4_AdvancedCalculator: + // await Posit16_4_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_0_FusedCalculator: + // await Posit16_0_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_1_FusedCalculator: + // await Posit16_1_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_2_FusedCalculator: + // await Posit16_2_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_3_FusedCalculator: + // await Posit16_3_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit16_4_FusedCalculator: + // await Posit16_4_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + case Sample.Posit32_0_Calculator: + await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_1_Calculator: + await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_2_Calculator: + await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_3_Calculator: + await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_4_Calculator: + await Posit32_4_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + //case Sample.Posit32_0_AdvancedCalculator: + // await Posit32_0_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_1_AdvancedCalculator: + // await Posit32_1_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_2_AdvancedCalculator: + // await Posit32_2_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_3_AdvancedCalculator: + // await Posit32_3_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_4_AdvancedCalculator: + // await Posit32_4_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_0_FusedCalculator: + // await Posit32_0_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_1_FusedCalculator: + // await Posit32_1_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_2_FusedCalculator: + // await Posit32_2_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_3_FusedCalculator: + // await Posit32_3_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + //case Sample.Posit32_4_FusedCalculator: + // await Posit32_4_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + // break; + + default: + break; + } + } + }).Wait(); + + Console.WriteLine("Press any key to exit."); + Console.ReadKey(); + } + } +} diff --git a/Hast.Samples.Posit/Program.tt b/Hast.Samples.Posit/Program.tt new file mode 100644 index 000000000..21bdb19d4 --- /dev/null +++ b/Hast.Samples.Posit/Program.tt @@ -0,0 +1,166 @@ +<#@ template debug="true" hostSpecific="true" #> +<#@ output extension=".cs" #> + +<# + var positSizes = new byte[] {8,16,32,64}; + var calculatorTypes = new string[] {"Calculator", "AdvancedCalculator", "FusedCalculator"}; + +#> +using Hast.Layer; +using Hast.Transformer.Vhdl.Abstractions.Configuration; +using Lombiq.Arithmetics; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace Hast.Samples.Posit +{ + internal static class Configuration + { + public static string VhdlOutputFilePath = @"Hast_IP.vhd"; + + public static Sample SampleToRun = Sample.Posit32_2_AdvancedCalculator; + } + + class Program + { + static void Main(string[] args) + { + Task.Run(async () => + { + /* + * On a high level these are the steps to use Hastlayer: + * 1. Create the Hastlayer shell. + * 2. Configure hardware generation and generate FPGA hardware representation of the given .NET code. + * 3. Generate proxies for hardware-transformed types and use these proxies to utilize hardware + * implementations. (You can see this inside the SampleRunners.) + */ + + // Configuring the Hastlayer shell. Which flavor should we use? If you're unsure then you'll need + // the Client flavor: This will let you connect to a remote Hastlayer service to run the software + // to hardware transformation. + var hastlayerConfiguration = new HastlayerConfiguration { Flavor = HastlayerFlavor.Developer }; + + // Initializing a Hastlayer shell. Since this is non-trivial to do you can cache this shell object + // while the program runs and re-use it continuously. No need to always wrap it into a using() like + // here, just make sure to Dispose() it before the program terminates. + using (var hastlayer = await Hastlayer.Create(hastlayerConfiguration)) + { + // Hooking into an event of Hastlayer so some execution information can be made visible on the + // console. + hastlayer.ExecutedOnHardware += (sender, e) => + { + Console.WriteLine( + "Executing " + + e.MemberFullName + + " on hardware took " + + e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds + + " milliseconds (net) " + + e.HardwareExecutionInformation.FullExecutionTimeMilliseconds + + " milliseconds (all together)"); + }; + + + // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. + var devices = await hastlayer.GetSupportedDevices(); + // Let's just use the first one that is available. However you might want to use a specific + // device, not just any first one. + var configuration = new HardwareGenerationConfiguration(devices.First().Name); + + // If you're running Hastlayer in the Client flavor, you also need to configure some credentials + // here: + var remoteClientConfiguration = configuration.RemoteClientConfiguration(); + remoteClientConfiguration.AppName = "TestApp"; + remoteClientConfiguration.AppSecret = "appsecret"; + if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && + remoteClientConfiguration.AppSecret == "appsecret") + { + throw new InvalidOperationException( + "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); + } + + + // Letting the configuration of samples run. Check out those methods too! + switch (Configuration.SampleToRun) + { + <#for (var positSize = 0; positSize<=2; positSize++){ + for (var type = 0; type<=2; type++){ + for (var exponentSize = 0; exponentSize<=4; exponentSize++){#> + <# var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; #> + case Sample.<#=positCalculatorName#>: + <#=positCalculatorName#>SampleRunner.Configure(configuration); + break; + <# } + } + } #> + default: + break; + } + + // The generated VHDL code will contain debug-level information, though it will be slower to + // create. + configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; + + Console.WriteLine("Hardware generation starts."); + + // Generating hardware from the sample assembly with the given configuration. + var hardwareRepresentation = await hastlayer.GenerateHardware( + new[] + { + typeof(Posit8_0).Assembly, + }, + configuration); + + Console.WriteLine("Hardware generation finished."); + Console.WriteLine(); + + // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't + // cause any problems, but sometimes they can. + if (hardwareRepresentation.HardwareDescription.Warnings.Any()) + { + Console.WriteLine( + "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + + Environment.NewLine + + string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); + Console.WriteLine(); + } + + if (!string.IsNullOrEmpty(Configuration.VhdlOutputFilePath)) + { + Console.WriteLine("Writing VHDL source to file."); + + await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); + + Console.WriteLine("VHDL source written to file."); + Console.WriteLine(); + } + + Console.WriteLine("Starting hardware execution."); + + // Running samples. + switch (Configuration.SampleToRun) + { + <#for (var positSize = 0; positSize<=2; positSize++){ + for (var type = 0; type<=2; type++){ + for (var exponentSize = 0; exponentSize<=4; exponentSize++){#> + <# var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; #> + case Sample.<#=positCalculatorName#>: + await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); + break; + <# } + } + } #> + default: + break; + } + } + }).Wait(); + + Console.WriteLine("Press any key to exit."); + Console.ReadKey(); + } + } +} +<# + +#> \ No newline at end of file diff --git a/Hast.Samples.Posit/Properties/AssemblyInfo.cs b/Hast.Samples.Posit/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..b31e8f7b9 --- /dev/null +++ b/Hast.Samples.Posit/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Hast.Samples.Posit")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Hast.Samples.Posit")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d60c14f3-05fd-44bf-b109-7a3f4113cc46")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Hast.Samples.Posit/Sample.cs b/Hast.Samples.Posit/Sample.cs new file mode 100644 index 000000000..189138070 --- /dev/null +++ b/Hast.Samples.Posit/Sample.cs @@ -0,0 +1,55 @@ + + + +namespace Hast.Samples.Posit +{ + internal enum Sample + { + Posit8_0_Calculator, + Posit8_1_Calculator, + Posit8_2_Calculator, + Posit8_3_Calculator, + Posit8_4_Calculator, + //Posit8_0_AdvancedCalculator, + //Posit8_1_AdvancedCalculator, + //Posit8_2_AdvancedCalculator, + //Posit8_3_AdvancedCalculator, + //Posit8_4_AdvancedCalculator, + //Posit8_0_FusedCalculator, + //Posit8_1_FusedCalculator, + //Posit8_2_FusedCalculator, + //Posit8_3_FusedCalculator, + //Posit8_4_FusedCalculator, + Posit16_0_Calculator, + Posit16_1_Calculator, + Posit16_2_Calculator, + Posit16_3_Calculator, + Posit16_4_Calculator, + //Posit16_0_AdvancedCalculator, + //Posit16_1_AdvancedCalculator, + //Posit16_2_AdvancedCalculator, + //Posit16_3_AdvancedCalculator, + //Posit16_4_AdvancedCalculator, + //Posit16_0_FusedCalculator, + //Posit16_1_FusedCalculator, + //Posit16_2_FusedCalculator, + //Posit16_3_FusedCalculator, + //Posit16_4_FusedCalculator, + Posit32_0_Calculator, + Posit32_1_Calculator, + Posit32_2_Calculator, + Posit32_3_Calculator, + Posit32_4_Calculator + // Posit32_0_AdvancedCalculator, + // Posit32_1_AdvancedCalculator, + // Posit32_2_AdvancedCalculator, + // Posit32_3_AdvancedCalculator, + // Posit32_4_AdvancedCalculator, + // Posit32_0_FusedCalculator, + // Posit32_1_FusedCalculator, + // Posit32_2_FusedCalculator, + // Posit32_3_FusedCalculator, + + //Posit32_4_FusedCalculator + } +} diff --git a/Hast.Samples.Posit/Sample.tt b/Hast.Samples.Posit/Sample.tt new file mode 100644 index 000000000..7e15b1b7e --- /dev/null +++ b/Hast.Samples.Posit/Sample.tt @@ -0,0 +1,37 @@ +<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> +<#@ output extension=".cs" #> + +<# + var positSizes = new byte[] {8,16,32,64}; + var calculatorTypes = new string[] {"Calculator", "AdvancedCalculator", "FusedCalculator"}; + List calculatorNames = new List(); + + for (var positSize = 2; positSize >= 0; positSize--){ + for (var type = 2; type>=0; type--){ + for (var exponentSize = 4; exponentSize>=0; exponentSize--){ + var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; + calculatorNames.Add(positCalculatorName); + } + } + } +#> + +namespace Hast.Samples.Posit +{ + internal enum Sample + { + <# + var last = calculatorNames.First(); + calculatorNames.RemoveAt(0); + calculatorNames.Reverse(); + foreach (var name in calculatorNames) + { #> + <#=name#>, + <# } #> + <#=last#> + } +} diff --git a/Hastlayer.SDK.sln b/Hastlayer.SDK.sln index 7353aab4b..eedb74973 100644 --- a/Hastlayer.SDK.sln +++ b/Hastlayer.SDK.sln @@ -110,6 +110,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lombiq.Arithmetics", "Lombi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hast.Algorithms.Tests", "Hast.Algorithms\Hast.Algorithms.Tests\Hast.Algorithms.Tests.csproj", "{EB781B61-C535-4A9B-A039-E28B56ACCB64}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hast.Samples.Posit", "Hast.Samples.Posit\Hast.Samples.Posit.csproj", "{D60C14F3-05FD-44BF-B109-7A3F4113CC46}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -260,6 +262,10 @@ Global {EB781B61-C535-4A9B-A039-E28B56ACCB64}.Debug|Any CPU.Build.0 = Debug|Any CPU {EB781B61-C535-4A9B-A039-E28B56ACCB64}.Release|Any CPU.ActiveCfg = Release|Any CPU {EB781B61-C535-4A9B-A039-E28B56ACCB64}.Release|Any CPU.Build.0 = Release|Any CPU + {D60C14F3-05FD-44BF-B109-7A3F4113CC46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D60C14F3-05FD-44BF-B109-7A3F4113CC46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D60C14F3-05FD-44BF-B109-7A3F4113CC46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D60C14F3-05FD-44BF-B109-7A3F4113CC46}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -297,6 +303,7 @@ Global {0DD555F3-B472-422B-8CFB-63CB494CBB5A} = {765A6EB0-438E-422C-ACDD-13544EFE6467} {CBAC105A-E50F-4202-A647-CFD674E6C0F8} = {765A6EB0-438E-422C-ACDD-13544EFE6467} {E77C13E5-DA6A-4A9D-9601-B17BDBFE50BD} = {701D5B71-9A6F-47A4-A3C2-3714AF8B4115} + {D60C14F3-05FD-44BF-B109-7A3F4113CC46} = {765A6EB0-438E-422C-ACDD-13544EFE6467} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5C7FC230-A7A8-4206-A533-E41B96D6E222} From f0ccc3c99f67ea0512791c1ecb14ae2f2c0b234c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Wed, 16 Dec 2020 14:59:07 +0100 Subject: [PATCH 05/43] Updating merged Lombiq.Arithmetics submodule --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index ffb0e61c2..1b26a1438 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit ffb0e61c2cff7364cc23d514748a9fb88d64161c +Subproject commit 1b26a1438b18212d841c1564f8aa508fa6d1ebe0 From 7af5f8f76df2eaaadcadba9814bb5dd9dcf7ec1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Tue, 29 Dec 2020 16:35:06 +0100 Subject: [PATCH 06/43] Targeting .netcoreapp3.1 --- Hast.Core | 2 +- Hast.Samples.Posit/Hast.Samples.Posit.csproj | 225 +----------------- Hast.Samples.Posit/Program.cs | 2 - .../Posit32AdvancedCalculator.cs | 3 +- 4 files changed, 13 insertions(+), 219 deletions(-) diff --git a/Hast.Core b/Hast.Core index 9413e84f6..2fd44072d 160000 --- a/Hast.Core +++ b/Hast.Core @@ -1 +1 @@ -Subproject commit 9413e84f6783c3304506f43ca2e0d3f7b2303a0d +Subproject commit 2fd44072dbbe08ec72d02481d94d16ebe113cc76 diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj index 82308b1ce..3d99ab90e 100644 --- a/Hast.Samples.Posit/Hast.Samples.Posit.csproj +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -1,219 +1,16 @@ - - - + - Debug - AnyCPU - {D60C14F3-05FD-44BF-B109-7A3F4113CC46} + netcoreapp3.1 Exe - Hast.Samples.Posit - Hast.Samples.Posit - v4.6.1 - 512 - true - true + false - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - PositCalculator.tt - - - PositCalculatorSampleRunner.tt - - - True - True - PositCalculator.tt - - - True - True - PositCalculatorSampleRunner.tt - - - True - True - Program.tt - - - - True - True - Sample.tt - - - - - - - - - TextTemplatingFileGenerator - PositCalculator.cs - - - TextTemplatingFileGenerator - PositCalculatorSampleRunner.cs - - - TextTemplatingFileGenerator - Program.cs - - - TextTemplatingFileGenerator - Sample.cs - - - - - - - - {9dee53c2-83ac-4048-adab-2993faffe4ea} - Hast.Transformer.Abstractions - - - {0D13986F-FA33-4898-A0DE-C173764663F0} - Hast.Transformer.Vhdl.Abstractions - - - {6EE5898A-E450-4A1D-80AF-AD27A8BD5E69} - Hast.Common - - - {0D77AFBA-1B4D-4083-B35C-3309251039B7} - Hast.Layer - - - {86d47be0-994e-48ba-b68c-1f0bcab67f02} - Hast.Remote.Client - - - {6cfe69a4-04a3-4427-818c-c0832d197a62} - Lombiq.Arithmetics - - - {cb4a8cf6-7dd4-4493-b5cd-e5e51ee5c73b} - Hast.Samples.SampleAssembly - - - + + + + + + + - - \ No newline at end of file + diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index 1f1dd9401..f0a367d15 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -1,5 +1,3 @@ - - using Hast.Layer; using Hast.Transformer.Vhdl.Abstractions.Configuration; using Lombiq.Arithmetics; diff --git a/Samples/Hast.Samples.SampleAssembly/Posit32AdvancedCalculator.cs b/Samples/Hast.Samples.SampleAssembly/Posit32AdvancedCalculator.cs index 2c3385202..e08ca5ffd 100644 --- a/Samples/Hast.Samples.SampleAssembly/Posit32AdvancedCalculator.cs +++ b/Samples/Hast.Samples.SampleAssembly/Posit32AdvancedCalculator.cs @@ -1,5 +1,4 @@ -using Hast.Layer; -using Hast.Synthesis.Abstractions; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; From 9266048731069a3501305347662a4ea5c0657341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Tue, 29 Dec 2020 16:39:13 +0100 Subject: [PATCH 07/43] Targeting .netcoreapp3.1 --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index 1b26a1438..8d8cccef4 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit 1b26a1438b18212d841c1564f8aa508fa6d1ebe0 +Subproject commit 8d8cccef4d7150dcdea670c349c733e710922316 From 21ea9a1fce5f1992bf24435f8713d0dfe375497c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lmos=20Szab=C3=B3?= Date: Wed, 13 Jan 2021 19:04:26 +0100 Subject: [PATCH 08/43] Starting to adjust to SimpleMemory changes --- Hast.Samples.Posit/Posit16_0_Calculator.cs | 51 ++-- .../Posit16_0_CalculatorSampleRunner.cs | 150 +++++------ Hast.Samples.Posit/PositCalculator.tt | 45 +++- .../PositCalculatorSampleRunner.tt | 244 +++++++++--------- .../Hast.Samples.SampleAssembly/Loopback.cs | 7 +- 5 files changed, 269 insertions(+), 228 deletions(-) diff --git a/Hast.Samples.Posit/Posit16_0_Calculator.cs b/Hast.Samples.Posit/Posit16_0_Calculator.cs index 8936b53e7..0f687e9ef 100644 --- a/Hast.Samples.Posit/Posit16_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_0_Calculator.cs @@ -1,8 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -21,7 +22,7 @@ public class Posit16_0_Calculator public const int CalculatePowerOfReal_InputInt32Index = 0; public const int CalculatePowerOfReal_InputPosit32Index = 1; public const int CalculatePowerOfReal_OutputPosit32Index = 0; - + public const int MaxDegreeOfParallelism = 5; @@ -109,70 +110,76 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit16_0_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit16_0_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit16_0_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); - memory.WriteInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + memory.WriteInt32(Posit16_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); - return memory.ReadInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + return memory.ReadInt32(Posit16_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit16_0_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal(this Posit16_0_Calculator positCalculator, int number, float real) { var memory = new SimpleMemory(2); - memory.WriteInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_0(real).PositBits); + memory.WriteInt32(Posit16_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32(Posit16_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_0(real).PositBits); positCalculator.CalculatePowerOfReal(memory); - return (float)new Posit16_0((ushort)memory.ReadUInt32( Posit16_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + return (float)new Posit16_0((ushort)memory.ReadUInt32(Posit16_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_0_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_0_Calculator positCalculator, int[] numbers) { - if (numbers.Length != Posit16_0_Calculator.MaxDegreeOfParallelism) + if (numbers.Length != Posit16_0_Calculator.MaxDegreeOfParallelism) { throw new ArgumentException( "Provide as many numbers as the degree of parallelism of Posit16_0_Calculator is (" + Posit16_0_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit16_0_Calculator.MaxDegreeOfParallelism); + var memory = new SimpleMemory(Posit16_0_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { - memory.WriteInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + memory.WriteInt32(Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); } positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - var results = new int[ Posit16_0_Calculator.MaxDegreeOfParallelism]; + var results = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < numbers.Length; i++) { - results[i] = memory.ReadInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + results[i] = memory.ReadInt32(Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); } return results; } - public static float AddPositsInArray(this Posit16_0_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray(this Posit16_0_Calculator positCalculator, uint[] positArray) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = new SimpleMemory(positArray.Length + 1); - memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + memory.WriteUInt32(Posit16_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint)positArray.Length); - for (var i = 0; i < positArray.Length; i++) + for (var i = 0; i < positArray.Length; i++) { - memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + memory.WriteUInt32(Posit16_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); } positCalculator.AddPositsInArray(memory); - return (float)new Posit32(memory.ReadUInt32( Posit16_0_Calculator.AddPositsInArray_OutputPosit32Index), true); + return (float)new Posit32(memory.ReadUInt32(Posit16_0_Calculator.AddPositsInArray_OutputPosit32Index), true); } } } diff --git a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs index 2195039b4..f646daaf3 100644 --- a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -10,108 +10,108 @@ namespace Hast.Samples.Posit { - internal class Posit16_0_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit16_0_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_0_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - + positCalculator.CalculatePowerOfReal(10000, (float)1.015625); - var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - var Posit16_0Array = new uint[100000]; - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; - else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; - } + var Posit16_0Array = new uint[100000]; - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; + else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; + } - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit16_0_Calculator(); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); + } + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_0_Calculator(); - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - sw.Stop(); + Console.WriteLine(); - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + sw = Stopwatch.StartNew(); - Console.WriteLine(); + var powerOfReal = positCalculator.CalculatePowerOfReal(10000, (float)1.015625); - var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } + sw.Stop(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); - Console.WriteLine(); + var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - var Posit16_0Array = new uint[100000]; + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + sw.Stop(); - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; - else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; - } + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - positCalculator.AddPositsInArray( Posit16_0Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_0Array); - sw.Stop(); + Console.WriteLine(); - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + var Posit16_0Array = new uint[100000]; - Console.WriteLine(); - } - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; + else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray(Posit16_0Array); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + } + } } diff --git a/Hast.Samples.Posit/PositCalculator.tt b/Hast.Samples.Posit/PositCalculator.tt index d62b68378..69b189f42 100644 --- a/Hast.Samples.Posit/PositCalculator.tt +++ b/Hast.Samples.Posit/PositCalculator.tt @@ -1,4 +1,4 @@ -<#@ template debug="true" hostSpecific="true" #> +<#@ template debug="true" hostSpecific="true" #> <#@ output extension=".cs" #> <#@ include file="PositCalculatorTemplateFileManager.ttinclude" #> @@ -135,9 +135,15 @@ namespace Hast.Samples.Posit public static class <#=className#>CalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this <#= className#> positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this <#= className#> positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( <#= className#>.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -145,9 +151,16 @@ namespace Hast.Samples.Posit return memory.ReadInt32( <#= className#>.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this <#= className#> positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this <#= className#> positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( <#= className#>.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( <#= className#>.CalculatePowerOfReal_InputPosit32Index, new <#=positName#>(real).PositBits); @@ -157,7 +170,11 @@ namespace Hast.Samples.Posit return (float)new <#=positName#>((<#= underLyingStructureName[i]#>)memory.ReadUInt32( <#= className#>.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this <#= className#> positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this <#= className#> positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != <#= className#>.MaxDegreeOfParallelism) { @@ -166,7 +183,9 @@ namespace Hast.Samples.Posit <#= className#>.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( <#= className#>.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(<#= className#>.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, <#= className#>.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -185,9 +204,15 @@ namespace Hast.Samples.Posit return results; } - public static float AddPositsInArray(this <#= className#> positCalculator, uint[] positArray) + public static float AddPositsInArray( + this <#= className#> positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( <#= className#>.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); @@ -210,4 +235,4 @@ namespace Hast.Samples.Posit manager.Process(); -#> \ No newline at end of file +#> diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt index 1935f4b17..c00d12094 100644 --- a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt @@ -1,4 +1,4 @@ -<#@ template debug="true" hostSpecific="true" #> +<#@ template debug="true" hostSpecific="true" #> <#@ output extension=".cs" #> <#@ include file="PositCalculatorTemplateFileManager.ttinclude" #> @@ -10,31 +10,31 @@ var manager = TemplateFileManager.Create(this); #> <# - var positSizes = new byte[] {8,16,32,64}; - var underLyingStructureName= new string[] {"byte", "ushort","uint","ulong","BitMask"}; + var positSizes = new byte[] {8,16,32,64}; + var underLyingStructureName= new string[] {"byte", "ushort","uint","ulong","BitMask"}; for (var i = 0; i<3; i++){ - for(var MaximumExponentSize = 0; MaximumExponentSize <=4; MaximumExponentSize++){ - var positName = "Posit"+positSizes[i] + "_" + MaximumExponentSize; - var calculatorName = positName + "_Calculator"; - var className = calculatorName + "SampleRunner"; - var quireSize = 1; - while (quireSize < (4*positSizes[i]-8)*(1<=0 ;n--) - { - maxScaleFactor = Math.Pow((1 << (1 << MaximumExponentSize)), positSizes[i]-2-MaximumExponentSize-n) + (1<= pIntMax) break; - } - manager.StartNewFile(className + ".cs"); + for(var MaximumExponentSize = 0; MaximumExponentSize <=4; MaximumExponentSize++){ + var positName = "Posit"+positSizes[i] + "_" + MaximumExponentSize; + var calculatorName = positName + "_Calculator"; + var className = calculatorName + "SampleRunner"; + var quireSize = 1; + while (quireSize < (4*positSizes[i]-8)*(1<=0 ;n--) + { + maxScaleFactor = Math.Pow((1 << (1 << MaximumExponentSize)), positSizes[i]-2-MaximumExponentSize-n) + (1<= pIntMax) break; + } + manager.StartNewFile(className + ".cs"); #> using System; using System.Collections.Generic; @@ -48,130 +48,136 @@ using Lombiq.Arithmetics; namespace Hast.Samples.Posit { - internal class <#=className#> - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType<<#=calculatorName#>>(); - } + internal class <#=className#> + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType<<#=calculatorName#>>(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new <#=calculatorName#>()); + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new <#=calculatorName#>()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); <# if (i == 0) { - #> - positCalculator.CalculatePowerOfReal( 5, (float)0.5); - + #> + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + <# } else { #> - positCalculator.CalculatePowerOfReal( <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, (float)1.015625); - <# } #> - + positCalculator.CalculatePowerOfReal( <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + <# } #> - var numbers = new int[<#=calculatorName#>.MaxDegreeOfParallelism]; - for (int i = 0; i < <#=calculatorName#>.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } + var numbers = new int[<#=calculatorName#>.MaxDegreeOfParallelism]; + for (int i = 0; i < <#=calculatorName#>.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var <#=positName#>Array = new uint[100000]; + var <#=positName#>Array = new uint[100000]; - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) <#=positName#>Array[i] = new <#=positName#>((float)0.25 * 2 * i).PositBits; - else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) <#=positName#>Array[i] = new <#=positName#>((float)0.25 * 2 * i).PositBits; + else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; + } - var positsInArraySum = positCalculator.AddPositsInArray(<#=positName#>Array); - } + var positsInArraySum = positCalculator.AddPositsInArray(<#=positName#>Array); + } - public static void RunSoftwareBenchmarks() - { - var positCalculator = new <#=calculatorName#>(); + public static void RunSoftwareBenchmarks() + { + var positCalculator = new <#=calculatorName#>(); - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); + Console.WriteLine(); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + <# if (i == 0) { - #> + #> - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); <# } else { #> - var powerOfReal = positCalculator.CalculatePowerOfReal( <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, (float)1.015625); - <# } #> - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[<#=calculatorName#>.MaxDegreeOfParallelism]; - for (int i = 0; i < <#=calculatorName#>.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var <#=positName#>Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) <#=positName#>Array[i] = new <#=positName#>((float)0.25 * 2 * i).PositBits; - else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( <#=positName#>Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( <#=positName#>Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + var powerOfReal = positCalculator.CalculatePowerOfReal( + <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + <# } #> + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[<#=calculatorName#>.MaxDegreeOfParallelism]; + for (int i = 0; i < <#=calculatorName#>.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var <#=positName#>Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) <#=positName#>Array[i] = new <#=positName#>((float)0.25 * 2 * i).PositBits; + else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( <#=positName#>Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( <#=positName#>Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } <# - } + } }#> <# manager.Process(); -#> \ No newline at end of file +#> diff --git a/Samples/Hast.Samples.SampleAssembly/Loopback.cs b/Samples/Hast.Samples.SampleAssembly/Loopback.cs index 73135796e..a33510d44 100644 --- a/Samples/Hast.Samples.SampleAssembly/Loopback.cs +++ b/Samples/Hast.Samples.SampleAssembly/Loopback.cs @@ -1,4 +1,4 @@ -using Hast.Layer; +using Hast.Layer; using Hast.Synthesis.Abstractions; using Hast.Transformer.Abstractions.SimpleMemory; @@ -21,7 +21,10 @@ public virtual void Run(SimpleMemory memory) memory.WriteInt32(Run_InputOutputInt32Index, memory.ReadInt32(Run_InputOutputInt32Index) + 1); } - public int Run(int input, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) + public int Run( + int input, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(1) From 041b58734042401cca7e7dd6f3bdd7ae832716f6 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 18 Mar 2021 18:36:37 +0100 Subject: [PATCH 09/43] Updating Lombiq.Arithmetics with SDK-style project fixes to be able to compile T4 templates --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index 8d8cccef4..ac6399de3 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit 8d8cccef4d7150dcdea670c349c733e710922316 +Subproject commit ac6399de34632e822374fe83eb43bbbc53c145bd From 76d4f5694f23b209ac4bf07f1d8d3f5111e5c9a7 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 18 Mar 2021 18:38:23 +0100 Subject: [PATCH 10/43] Updating Host.Samples.Posit.csproj with T4 template generator declarations --- Hast.Samples.Posit/Hast.Samples.Posit.csproj | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj index 3d99ab90e..b67439d0c 100644 --- a/Hast.Samples.Posit/Hast.Samples.Posit.csproj +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -13,4 +13,23 @@ + + + TextTemplatingFileGenerator + PositCalculator.cs + + + TextTemplatingFileGenerator + + + + + + + + True + True + PositCalculator.tt + + From 920d7f672d3250dfa06d6c4eaf738c256e5f7892 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 18 Mar 2021 18:39:29 +0100 Subject: [PATCH 11/43] Untabifying PositCalculatorTemplateFileManager.ttinclude --- Hast.Samples.Posit/PositCalculator.cs | 22 +- ...sitCalculatorTemplateFileManager.ttinclude | 1080 ++++++++--------- 2 files changed, 541 insertions(+), 561 deletions(-) diff --git a/Hast.Samples.Posit/PositCalculator.cs b/Hast.Samples.Posit/PositCalculator.cs index 3079bb6b3..f4a2e8e1d 100644 --- a/Hast.Samples.Posit/PositCalculator.cs +++ b/Hast.Samples.Posit/PositCalculator.cs @@ -1,21 +1 @@ - - - - -// Generated helper templates -// Generated items -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_4_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_3_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_2_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_1_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_0_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_4_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_3_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_2_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_1_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_0_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_4_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_3_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_2_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_1_Calculator.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_0_Calculator.cs +ErrorGeneratingOutput \ No newline at end of file diff --git a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude index 9a50f6c3e..158ddc5f0 100644 --- a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude +++ b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude @@ -1,4 +1,4 @@ -<#@ assembly name="System.Core" #> +<#@ assembly name="System.Core" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.Data.Entity" #> <#@ assembly name="System.Xml" #> @@ -76,7 +76,7 @@ void WriteToBuildPane (string message){ typeof( SVsOutputWindow ) ) as IVsOutputWindow; Guid generalPaneGuid = Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.BuildOutputPane_guid; - // P.S. There's also the GUID_OutWindowDebugPane available. + // P.S. There's also the GUID_OutWindowDebugPane available. IVsOutputWindowPane generalPane; outWindow.GetPane( ref generalPaneGuid , out generalPane ); generalPane.OutputString( message ); @@ -90,12 +90,12 @@ Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.BuildOutputPane_guid; /// R. Leupold public class TemplateFileManager { - private EnvDTE.ProjectItem templateProjectItem; + private EnvDTE.ProjectItem templateProjectItem; private Action checkOutAction; private Action> projectSyncAction; - private EnvDTE.DTE dte; - private List templatePlaceholderList = new List(); - + private EnvDTE.DTE dte; + private List templatePlaceholderList = new List(); + /// /// Creates files with VS sync /// @@ -127,11 +127,11 @@ public class TemplateFileManager { throw new ArgumentNullException("textTransformation"); } - + _textTransformation = DynamicTextTransformation2.Create(textTransformation); _generationEnvironment = _textTransformation.GenerationEnvironment; - - var hostServiceProvider = _textTransformation.Host.AsIServiceProvider(); + + var hostServiceProvider = _textTransformation.Host.AsIServiceProvider(); if (hostServiceProvider == null) { throw new ArgumentNullException("Could not obtain hostServiceProvider"); @@ -142,43 +142,43 @@ public class TemplateFileManager { throw new ArgumentNullException("Could not obtain DTE from host"); } - - this.templateProjectItem = dte.Solution.FindProjectItem(_textTransformation.Host.TemplateFile); - this.CanOverrideExistingFile = true; - this.IsAutoIndentEnabled = false; - this.Encoding = System.Text.Encoding.UTF8; + + this.templateProjectItem = dte.Solution.FindProjectItem(_textTransformation.Host.TemplateFile); + this.CanOverrideExistingFile = true; + this.IsAutoIndentEnabled = false; + this.Encoding = System.Text.Encoding.UTF8; checkOutAction = fileName => dte.SourceControl.CheckOutItem(fileName); projectSyncAction = keepFileNames => ProjectSync(templateProjectItem, keepFileNames); } - /// - /// If set to false, existing files are not overwritten - /// - /// - public bool CanOverrideExistingFile { get; set; } - - /// - /// If set to true, output files (c#, vb) are formatted based on the vs settings. - /// - /// - public bool IsAutoIndentEnabled { get; set; } - - /// - /// Defines Encoding format for generated output file. (Default UTF8) - /// - /// - public System.Text.Encoding Encoding { get; set; } - + /// + /// If set to false, existing files are not overwritten + /// + /// + public bool CanOverrideExistingFile { get; set; } + + /// + /// If set to true, output files (c#, vb) are formatted based on the vs settings. + /// + /// + public bool IsAutoIndentEnabled { get; set; } + + /// + /// Defines Encoding format for generated output file. (Default UTF8) + /// + /// + public System.Text.Encoding Encoding { get; set; } + /// /// Marks the end of the last file if there was one, and starts a new /// and marks this point in generation as a new file. /// - /// Filename - /// Name of the target project for the new file. - /// Name of the target folder for the new file. - /// File property settings in vs for the new File + /// Filename + /// Name of the target project for the new file. + /// Name of the target folder for the new file. + /// File property settings in vs for the new File public void StartNewFile(string name - , string projectName = "", string folderName = "", FileProperties fileProperties = null) + , string projectName = "", string folderName = "", FileProperties fileProperties = null) { if (String.IsNullOrWhiteSpace(name) == true) { @@ -186,12 +186,12 @@ public class TemplateFileManager } CurrentBlock = new Block - { - Name = name, - ProjectName = projectName, - FolderName = folderName, - FileProperties = fileProperties ?? new FileProperties() - }; + { + Name = name, + ProjectName = projectName, + FolderName = folderName, + FileProperties = fileProperties ?? new FileProperties() + }; } public void StartFooter() @@ -220,14 +220,14 @@ public class TemplateFileManager currentBlock = null; } - + /// /// Produce the template output files. /// public virtual IEnumerable Process(bool split = true) { - var list = new List(); - + var list = new List(); + if (split) { EndBlock(); @@ -238,178 +238,178 @@ public class TemplateFileManager foreach (var block in files) { - var outputPath = VSHelper.GetOutputPath(dte, block, Path.GetDirectoryName(_textTransformation.Host.TemplateFile)); + var outputPath = VSHelper.GetOutputPath(dte, block, Path.GetDirectoryName(_textTransformation.Host.TemplateFile)); var fileName = Path.Combine(outputPath, block.Name); var content = this.ReplaceParameter(headerText, block) + - _generationEnvironment.ToString(block.Start, block.Length) + - footerText; + _generationEnvironment.ToString(block.Start, block.Length) + + footerText; var file = new OutputFile - { - FileName = fileName, - ProjectName = block.ProjectName, - FolderName = block.FolderName, - FileProperties = block.FileProperties, - Content = content - }; - + { + FileName = fileName, + ProjectName = block.ProjectName, + FolderName = block.FolderName, + FileProperties = block.FileProperties, + Content = content + }; + CreateFile(file); _generationEnvironment.Remove(block.Start, block.Length); - - list.Add(file); + + list.Add(file); + } + } + + projectSyncAction.EndInvoke(projectSyncAction.BeginInvoke(list, null, null)); + this.CleanUpTemplatePlaceholders(); + var items = VSHelper.GetOutputFilesAsProjectItems(this.dte, list); + this.WriteVsProperties(items, list); + + if (this.IsAutoIndentEnabled == true && split == true) + { + this.FormatProjectItems(items); + } + + this.WriteLog(list); + + return list; + } + + private void FormatProjectItems(IEnumerable items) + { + foreach (var item in items) + { + this._textTransformation.WriteLine( + VSHelper.ExecuteVsCommand(this.dte, item, "Edit.FormatDocument")); //, "Edit.RemoveAndSort")); + this._textTransformation.WriteLine("//-> " + item.Name); + } + } + + private void WriteVsProperties(IEnumerable items, IEnumerable outputFiles) + { + foreach (var file in outputFiles) + { + var item = items.Where(p => p.Name == Path.GetFileName(file.FileName)).FirstOrDefault(); + if (item == null) continue; + + if (String.IsNullOrEmpty(file.FileProperties.CustomTool) == false) + { + VSHelper.SetPropertyValue(item, "CustomTool", file.FileProperties.CustomTool); + } + + if (String.IsNullOrEmpty(file.FileProperties.BuildActionString) == false) + { + VSHelper.SetPropertyValue(item, "ItemType", file.FileProperties.BuildActionString); + } + } + } + + private string ReplaceParameter(string text, Block block) + { + if (String.IsNullOrEmpty(text) == false) + { + text = text.Replace("$filename$", block.Name); + } + + + foreach (var item in block.FileProperties.TemplateParameter.AsEnumerable()) + { + text = text.Replace(item.Key, item.Value); + } + + return text; + } + + /// + /// Write log to the default output file. + /// + /// + private void WriteLog(IEnumerable list) + { + this._textTransformation.WriteLine("// Generated helper templates"); + foreach (var item in templatePlaceholderList) + { + this._textTransformation.WriteLine("// " + this.GetDirectorySolutionRelative(item)); + } + + this._textTransformation.WriteLine("// Generated items"); + foreach (var item in list) + { + this._textTransformation.WriteLine("// " + this.GetDirectorySolutionRelative(item.FileName)); + } + } + + /// + /// Removes old template placeholders from the solution. + /// + private void CleanUpTemplatePlaceholders() + { + string[] activeTemplateFullNames = this.templatePlaceholderList.ToArray(); + string[] allHelperTemplateFullNames = VSHelper.GetAllSolutionItems(this.dte) + .Where(p => p.Name == VSHelper.GetTemplatePlaceholderName(this.templateProjectItem)) + .Select(p => VSHelper.GetProjectItemFullPath(p)) + .ToArray(); + + var delta = allHelperTemplateFullNames.Except(activeTemplateFullNames).ToArray(); + + var dirtyHelperTemplates = VSHelper.GetAllSolutionItems(this.dte) + .Where(p => delta.Contains(VSHelper.GetProjectItemFullPath(p))); + + foreach (ProjectItem item in dirtyHelperTemplates) + { + if (item.ProjectItems != null) + { + foreach (ProjectItem subItem in item.ProjectItems) + { + subItem.Remove(); + } } + + item.Remove(); } - - projectSyncAction.EndInvoke(projectSyncAction.BeginInvoke(list, null, null)); - this.CleanUpTemplatePlaceholders(); - var items = VSHelper.GetOutputFilesAsProjectItems(this.dte, list); - this.WriteVsProperties(items, list); - - if (this.IsAutoIndentEnabled == true && split == true) - { - this.FormatProjectItems(items); - } - - this.WriteLog(list); - - return list; } - - private void FormatProjectItems(IEnumerable items) - { - foreach (var item in items) - { - this._textTransformation.WriteLine( - VSHelper.ExecuteVsCommand(this.dte, item, "Edit.FormatDocument")); //, "Edit.RemoveAndSort")); - this._textTransformation.WriteLine("//-> " + item.Name); - } - } - - private void WriteVsProperties(IEnumerable items, IEnumerable outputFiles) - { - foreach (var file in outputFiles) - { - var item = items.Where(p => p.Name == Path.GetFileName(file.FileName)).FirstOrDefault(); - if (item == null) continue; - - if (String.IsNullOrEmpty(file.FileProperties.CustomTool) == false) - { - VSHelper.SetPropertyValue(item, "CustomTool", file.FileProperties.CustomTool); - } - - if (String.IsNullOrEmpty(file.FileProperties.BuildActionString) == false) - { - VSHelper.SetPropertyValue(item, "ItemType", file.FileProperties.BuildActionString); - } - } - } - - private string ReplaceParameter(string text, Block block) - { - if (String.IsNullOrEmpty(text) == false) - { - text = text.Replace("$filename$", block.Name); - } - - - foreach (var item in block.FileProperties.TemplateParameter.AsEnumerable()) - { - text = text.Replace(item.Key, item.Value); - } - - return text; - } - - /// - /// Write log to the default output file. - /// - /// - private void WriteLog(IEnumerable list) - { - this._textTransformation.WriteLine("// Generated helper templates"); - foreach (var item in templatePlaceholderList) - { - this._textTransformation.WriteLine("// " + this.GetDirectorySolutionRelative(item)); - } - - this._textTransformation.WriteLine("// Generated items"); - foreach (var item in list) - { - this._textTransformation.WriteLine("// " + this.GetDirectorySolutionRelative(item.FileName)); - } - } - - /// - /// Removes old template placeholders from the solution. - /// - private void CleanUpTemplatePlaceholders() - { - string[] activeTemplateFullNames = this.templatePlaceholderList.ToArray(); - string[] allHelperTemplateFullNames = VSHelper.GetAllSolutionItems(this.dte) - .Where(p => p.Name == VSHelper.GetTemplatePlaceholderName(this.templateProjectItem)) - .Select(p => VSHelper.GetProjectItemFullPath(p)) - .ToArray(); - - var delta = allHelperTemplateFullNames.Except(activeTemplateFullNames).ToArray(); - - var dirtyHelperTemplates = VSHelper.GetAllSolutionItems(this.dte) - .Where(p => delta.Contains(VSHelper.GetProjectItemFullPath(p))); - - foreach (ProjectItem item in dirtyHelperTemplates) - { - if (item.ProjectItems != null) - { - foreach (ProjectItem subItem in item.ProjectItems) - { - subItem.Remove(); - } - } - - item.Remove(); - } - } - - /// - /// Gets a list of helper templates from the log. - /// - /// List of generated helper templates. - private string[] GetPreviousTemplatePlaceholdersFromLog() - { - string path = Path.GetDirectoryName(this._textTransformation.Host.ResolvePath(this._textTransformation.Host.TemplateFile)); - string file1 = Path.GetFileNameWithoutExtension(this._textTransformation.Host.TemplateFile) + ".txt"; - string contentPrevious = File.ReadAllText(Path.Combine(path, file1)); - - var result = contentPrevious + + /// + /// Gets a list of helper templates from the log. + /// + /// List of generated helper templates. + private string[] GetPreviousTemplatePlaceholdersFromLog() + { + string path = Path.GetDirectoryName(this._textTransformation.Host.ResolvePath(this._textTransformation.Host.TemplateFile)); + string file1 = Path.GetFileNameWithoutExtension(this._textTransformation.Host.TemplateFile) + ".txt"; + string contentPrevious = File.ReadAllText(Path.Combine(path, file1)); + + var result = contentPrevious .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x.Split(new[] { "=>" }, StringSplitOptions.RemoveEmptyEntries).First()) .Select(x => Regex.Replace(x, "//", String.Empty).Trim()) - .Where(x => x.EndsWith(VSHelper.GetTemplatePlaceholderName(this.templateProjectItem))) - .ToArray(); - - return result; - } - - private string GetDirectorySolutionRelative(string fullName) - { - int slnPos = fullName.IndexOf(Path.GetFileNameWithoutExtension(this.dte.Solution.FileName)); - if (slnPos < 0) - { - slnPos = 0; - } - - return fullName.Substring(slnPos); - } - + .Where(x => x.EndsWith(VSHelper.GetTemplatePlaceholderName(this.templateProjectItem))) + .ToArray(); + + return result; + } + + private string GetDirectorySolutionRelative(string fullName) + { + int slnPos = fullName.IndexOf(Path.GetFileNameWithoutExtension(this.dte.Solution.FileName)); + if (slnPos < 0) + { + slnPos = 0; + } + + return fullName.Substring(slnPos); + } + protected virtual void CreateFile(OutputFile file) { - if (this.CanOverrideExistingFile == false && File.Exists(file.FileName) == true) - { - return; - } - + if (this.CanOverrideExistingFile == false && File.Exists(file.FileName) == true) + { + return; + } + if (IsFileContentDifferent(file)) { - CheckoutFileIfRequired(file.FileName); + CheckoutFileIfRequired(file.FileName); File.WriteAllText(file.FileName, file.Content, this.Encoding); } } @@ -437,38 +437,38 @@ public class TemplateFileManager currentBlock = value; } } - - private void ProjectSync(EnvDTE.ProjectItem templateProjectItem, IEnumerable keepFileNames) - { - var groupedFileNames = from f in keepFileNames - group f by new { f.ProjectName, f.FolderName } - into l - select new { - ProjectName = l.Key.ProjectName, - FolderName = l.Key.FolderName, - FirstItem = l.First(), - OutputFiles = l - }; - - this.templatePlaceholderList.Clear(); - - foreach (var item in groupedFileNames) - { - EnvDTE.ProjectItem pi = VSHelper.GetTemplateProjectItem(templateProjectItem.DTE, item.FirstItem, templateProjectItem); - ProjectSyncPart(pi, item.OutputFiles); - - if (pi.Name.EndsWith("txt4")) - this.templatePlaceholderList.Add(VSHelper.GetProjectItemFullPath(pi)); - } - - // clean up - bool hasDefaultItems = groupedFileNames.Where(f => String.IsNullOrEmpty(f.ProjectName) && String.IsNullOrEmpty(f.FolderName)).Count() > 0; - if (hasDefaultItems == false) - { - ProjectSyncPart(templateProjectItem, new List()); - } - } - + + private void ProjectSync(EnvDTE.ProjectItem templateProjectItem, IEnumerable keepFileNames) + { + var groupedFileNames = from f in keepFileNames + group f by new { f.ProjectName, f.FolderName } + into l + select new { + ProjectName = l.Key.ProjectName, + FolderName = l.Key.FolderName, + FirstItem = l.First(), + OutputFiles = l + }; + + this.templatePlaceholderList.Clear(); + + foreach (var item in groupedFileNames) + { + EnvDTE.ProjectItem pi = VSHelper.GetTemplateProjectItem(templateProjectItem.DTE, item.FirstItem, templateProjectItem); + ProjectSyncPart(pi, item.OutputFiles); + + if (pi.Name.EndsWith("txt4")) + this.templatePlaceholderList.Add(VSHelper.GetProjectItemFullPath(pi)); + } + + // clean up + bool hasDefaultItems = groupedFileNames.Where(f => String.IsNullOrEmpty(f.ProjectName) && String.IsNullOrEmpty(f.FolderName)).Count() > 0; + if (hasDefaultItems == false) + { + ProjectSyncPart(templateProjectItem, new List()); + } + } + private static void ProjectSyncPart(EnvDTE.ProjectItem templateProjectItem, IEnumerable keepFileNames) { var keepFileNameSet = new HashSet(keepFileNames); @@ -483,7 +483,7 @@ public class TemplateFileManager // Remove unused items from the project foreach (var pair in projectFiles) { - bool isNotFound = keepFileNames.Where(f=>f.FileName == pair.Key).Count() == 0; + bool isNotFound = keepFileNames.Where(f=>f.FileName == pair.Key).Count() == 0; if (isNotFound == true && !(Path.GetFileNameWithoutExtension(pair.Key) + ".").StartsWith(originalOutput + ".")) { @@ -760,331 +760,331 @@ public sealed class Block { public String Name; public int Start, Length; - public string ProjectName { get; set; } - public string FolderName { get; set; } - public FileProperties FileProperties { get; set; } + public string ProjectName { get; set; } + public string FolderName { get; set; } + public FileProperties FileProperties { get; set; } } public class ParamTextTemplate { - private ITextTemplatingEngineHost Host { get; set; } - - private ParamTextTemplate(ITextTemplatingEngineHost host) - { - this.Host = host; - } - - public static ParamTextTemplate Create(ITextTemplatingEngineHost host) - { - return new ParamTextTemplate(host); - } - - public static TextTemplatingSession GetSessionObject() - { - return new TextTemplatingSession(); - } - - public string TransformText(string templateName, TextTemplatingSession session) - { - return this.GetTemplateContent(templateName, session); - } - - public string GetTemplateContent(string templateName, TextTemplatingSession session) - { - string fullName = this.Host.ResolvePath(templateName); - string templateContent = File.ReadAllText(fullName); - - var sessionHost = this.Host as ITextTemplatingSessionHost; - sessionHost.Session = session; - - Engine engine = new Engine(); - return engine.ProcessTemplate(templateContent, this.Host); - } + private ITextTemplatingEngineHost Host { get; set; } + + private ParamTextTemplate(ITextTemplatingEngineHost host) + { + this.Host = host; + } + + public static ParamTextTemplate Create(ITextTemplatingEngineHost host) + { + return new ParamTextTemplate(host); + } + + public static TextTemplatingSession GetSessionObject() + { + return new TextTemplatingSession(); + } + + public string TransformText(string templateName, TextTemplatingSession session) + { + return this.GetTemplateContent(templateName, session); + } + + public string GetTemplateContent(string templateName, TextTemplatingSession session) + { + string fullName = this.Host.ResolvePath(templateName); + string templateContent = File.ReadAllText(fullName); + + var sessionHost = this.Host as ITextTemplatingSessionHost; + sessionHost.Session = session; + + Engine engine = new Engine(); + return engine.ProcessTemplate(templateContent, this.Host); + } } public class VSHelper { - /// - /// Execute Visual Studio commands against the project item. - /// - /// The current project item. - /// The vs command as string. - /// An error message if the command fails. - public static string ExecuteVsCommand(EnvDTE.DTE dte, EnvDTE.ProjectItem item, params string[] command) - { - if (item == null) - { - throw new ArgumentNullException("item"); - } - - string error = String.Empty; - - try - { - EnvDTE.Window window = item.Open(); - window.Activate(); - - foreach (var cmd in command) - { - if (String.IsNullOrWhiteSpace(cmd) == true) - { - continue; - } - - EnvDTE80.DTE2 dte2 = dte as EnvDTE80.DTE2; - dte2.ExecuteCommand(cmd, String.Empty); - } - - item.Save(); - window.Visible = false; - // window.Close(); // Ends VS, but not the tab :( - } - catch (Exception ex) - { - error = String.Format("Error processing file {0} {1}", item.Name, ex.Message); - } - - return error; - } - - /// - /// Sets a property value for the vs project item. - /// - public static void SetPropertyValue(EnvDTE.ProjectItem item, string propertyName, object value) - { - EnvDTE.Property property = item.Properties.Item(propertyName); - if (property == null) - { - throw new ArgumentException(String.Format("The property {0} was not found.", propertyName)); - } - else - { - property.Value = value; - } - } - - public static IEnumerable GetOutputFilesAsProjectItems(EnvDTE.DTE dte, IEnumerable outputFiles) - { - var fileNames = (from o in outputFiles - select Path.GetFileName(o.FileName)).ToArray(); - - return VSHelper.GetAllSolutionItems(dte).Where(f => fileNames.Contains(f.Name)); - } - - public static string GetOutputPath(EnvDTE.DTE dte, Block block, string defaultPath) - { - if (String.IsNullOrEmpty(block.ProjectName) == true && String.IsNullOrEmpty(block.FolderName) == true) - { - return defaultPath; - } - - EnvDTE.Project prj = null; - EnvDTE.ProjectItem item = null; - - if (String.IsNullOrEmpty(block.ProjectName) == false) - { - prj = GetProject(dte, block.ProjectName); - } - - if (String.IsNullOrEmpty(block.FolderName) == true && prj != null) - { - return Path.GetDirectoryName(prj.FullName); - } - else if (prj != null && String.IsNullOrEmpty(block.FolderName) == false) - { - item = GetAllProjectItemsRecursive(prj.ProjectItems).Where(i=>i.Name == block.FolderName).First(); - } - else if (String.IsNullOrEmpty(block.FolderName) == false) - { - item = GetAllProjectItemsRecursive( - dte.ActiveDocument.ProjectItem.ContainingProject.ProjectItems). - Where(i=>i.Name == block.FolderName).First(); - } - - if (item != null) - { - return GetProjectItemFullPath(item); - } - - return defaultPath; - } - public static string GetTemplatePlaceholderName(EnvDTE.ProjectItem item) - { - return String.Format("{0}.txt4", Path.GetFileNameWithoutExtension(item.Name)); - } - - public static EnvDTE.ProjectItem GetTemplateProjectItem(EnvDTE.DTE dte, OutputFile file, EnvDTE.ProjectItem defaultItem) - { - if (String.IsNullOrEmpty(file.ProjectName) == true && String.IsNullOrEmpty(file.FolderName) == true) - { - return defaultItem; - } - - string templatePlaceholder = GetTemplatePlaceholderName(defaultItem); - string itemPath = Path.GetDirectoryName(file.FileName); - string fullName = Path.Combine(itemPath, templatePlaceholder); - EnvDTE.Project prj = null; - EnvDTE.ProjectItem item = null; - - if (String.IsNullOrEmpty(file.ProjectName) == false) - { - prj = GetProject(dte, file.ProjectName); - } - - if (String.IsNullOrEmpty(file.FolderName) == true && prj != null) - { - return FindProjectItem(prj.ProjectItems, fullName, true); - } - else if (prj != null && String.IsNullOrEmpty(file.FolderName) == false) - { - item = GetAllProjectItemsRecursive(prj.ProjectItems).Where(i=>i.Name == file.FolderName).First(); - } - else if (String.IsNullOrEmpty(file.FolderName) == false) - { - item = GetAllProjectItemsRecursive( - dte.ActiveDocument.ProjectItem.ContainingProject.ProjectItems). - Where(i=>i.Name == file.FolderName).First(); - } - - if (item != null) - { - return FindProjectItem(item.ProjectItems, fullName, true); - } - - return defaultItem; - } - - private static EnvDTE.ProjectItem FindProjectItem(EnvDTE.ProjectItems items, string fullName, bool canCreateIfNotExists) - { - EnvDTE.ProjectItem item = (from i in items.Cast() - where i.Name == Path.GetFileName(fullName) - select i).FirstOrDefault(); - if (item == null) - { - File.CreateText(fullName); - item = items.AddFromFile(fullName); - } - - return item; - } - - public static EnvDTE.Project GetProject(EnvDTE.DTE dte, string projectName) - { - return GetAllProjects(dte).Where(p=>p.Name == projectName).First(); - } - - public static IEnumerable GetAllProjects(EnvDTE.DTE dte) - { - List projectList = new List(); - - var folders = dte.Solution.Projects.Cast().Where(p=>p.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder); - - foreach (EnvDTE.Project folder in folders) - { - if (folder.ProjectItems == null) continue; - - foreach (EnvDTE.ProjectItem item in folder.ProjectItems) - { - if (item.Object is EnvDTE.Project) - projectList.Add(item.Object as EnvDTE.Project); - } - } - - var projects = dte.Solution.Projects.Cast().Where(p=>p.Kind != EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder); - - if (projects.Count() > 0) - projectList.AddRange(projects); - - return projectList; - } - - public static EnvDTE.ProjectItem GetProjectItemWithName(EnvDTE.ProjectItems items, string itemName) - { - return GetAllProjectItemsRecursive(items).Cast().Where(i=>i.Name == itemName).First(); - } - - public static string GetProjectItemFullPath(EnvDTE.ProjectItem item) - { - return item.Properties.Item("FullPath").Value.ToString(); - } - - public static IEnumerable GetAllSolutionItems(EnvDTE.DTE dte) - { - List itemList = new List(); - - foreach (Project item in GetAllProjects(dte)) - { - if (item == null || item.ProjectItems == null) continue; - - itemList.AddRange(GetAllProjectItemsRecursive(item.ProjectItems)); - } - - return itemList; - } - - public static IEnumerable GetAllProjectItemsRecursive(EnvDTE.ProjectItems projectItems) - { - foreach (EnvDTE.ProjectItem projectItem in projectItems) - { - if (projectItem.ProjectItems == null) continue; - - foreach (EnvDTE.ProjectItem subItem in GetAllProjectItemsRecursive(projectItem.ProjectItems)) - { - yield return subItem; - } - - - yield return projectItem; - } - } + /// + /// Execute Visual Studio commands against the project item. + /// + /// The current project item. + /// The vs command as string. + /// An error message if the command fails. + public static string ExecuteVsCommand(EnvDTE.DTE dte, EnvDTE.ProjectItem item, params string[] command) + { + if (item == null) + { + throw new ArgumentNullException("item"); + } + + string error = String.Empty; + + try + { + EnvDTE.Window window = item.Open(); + window.Activate(); + + foreach (var cmd in command) + { + if (String.IsNullOrWhiteSpace(cmd) == true) + { + continue; + } + + EnvDTE80.DTE2 dte2 = dte as EnvDTE80.DTE2; + dte2.ExecuteCommand(cmd, String.Empty); + } + + item.Save(); + window.Visible = false; + // window.Close(); // Ends VS, but not the tab :( + } + catch (Exception ex) + { + error = String.Format("Error processing file {0} {1}", item.Name, ex.Message); + } + + return error; + } + + /// + /// Sets a property value for the vs project item. + /// + public static void SetPropertyValue(EnvDTE.ProjectItem item, string propertyName, object value) + { + EnvDTE.Property property = item.Properties.Item(propertyName); + if (property == null) + { + throw new ArgumentException(String.Format("The property {0} was not found.", propertyName)); + } + else + { + property.Value = value; + } + } + + public static IEnumerable GetOutputFilesAsProjectItems(EnvDTE.DTE dte, IEnumerable outputFiles) + { + var fileNames = (from o in outputFiles + select Path.GetFileName(o.FileName)).ToArray(); + + return VSHelper.GetAllSolutionItems(dte).Where(f => fileNames.Contains(f.Name)); + } + + public static string GetOutputPath(EnvDTE.DTE dte, Block block, string defaultPath) + { + if (String.IsNullOrEmpty(block.ProjectName) == true && String.IsNullOrEmpty(block.FolderName) == true) + { + return defaultPath; + } + + EnvDTE.Project prj = null; + EnvDTE.ProjectItem item = null; + + if (String.IsNullOrEmpty(block.ProjectName) == false) + { + prj = GetProject(dte, block.ProjectName); + } + + if (String.IsNullOrEmpty(block.FolderName) == true && prj != null) + { + return Path.GetDirectoryName(prj.FullName); + } + else if (prj != null && String.IsNullOrEmpty(block.FolderName) == false) + { + item = GetAllProjectItemsRecursive(prj.ProjectItems).Where(i=>i.Name == block.FolderName).First(); + } + else if (String.IsNullOrEmpty(block.FolderName) == false) + { + item = GetAllProjectItemsRecursive( + dte.ActiveDocument.ProjectItem.ContainingProject.ProjectItems). + Where(i=>i.Name == block.FolderName).First(); + } + + if (item != null) + { + return GetProjectItemFullPath(item); + } + + return defaultPath; + } + public static string GetTemplatePlaceholderName(EnvDTE.ProjectItem item) + { + return String.Format("{0}.txt4", Path.GetFileNameWithoutExtension(item.Name)); + } + + public static EnvDTE.ProjectItem GetTemplateProjectItem(EnvDTE.DTE dte, OutputFile file, EnvDTE.ProjectItem defaultItem) + { + if (String.IsNullOrEmpty(file.ProjectName) == true && String.IsNullOrEmpty(file.FolderName) == true) + { + return defaultItem; + } + + string templatePlaceholder = GetTemplatePlaceholderName(defaultItem); + string itemPath = Path.GetDirectoryName(file.FileName); + string fullName = Path.Combine(itemPath, templatePlaceholder); + EnvDTE.Project prj = null; + EnvDTE.ProjectItem item = null; + + if (String.IsNullOrEmpty(file.ProjectName) == false) + { + prj = GetProject(dte, file.ProjectName); + } + + if (String.IsNullOrEmpty(file.FolderName) == true && prj != null) + { + return FindProjectItem(prj.ProjectItems, fullName, true); + } + else if (prj != null && String.IsNullOrEmpty(file.FolderName) == false) + { + item = GetAllProjectItemsRecursive(prj.ProjectItems).Where(i=>i.Name == file.FolderName).First(); + } + else if (String.IsNullOrEmpty(file.FolderName) == false) + { + item = GetAllProjectItemsRecursive( + dte.ActiveDocument.ProjectItem.ContainingProject.ProjectItems). + Where(i=>i.Name == file.FolderName).First(); + } + + if (item != null) + { + return FindProjectItem(item.ProjectItems, fullName, true); + } + + return defaultItem; + } + + private static EnvDTE.ProjectItem FindProjectItem(EnvDTE.ProjectItems items, string fullName, bool canCreateIfNotExists) + { + EnvDTE.ProjectItem item = (from i in items.Cast() + where i.Name == Path.GetFileName(fullName) + select i).FirstOrDefault(); + if (item == null) + { + File.CreateText(fullName); + item = items.AddFromFile(fullName); + } + + return item; + } + + public static EnvDTE.Project GetProject(EnvDTE.DTE dte, string projectName) + { + return GetAllProjects(dte).Where(p=>p.Name == projectName).First(); + } + + public static IEnumerable GetAllProjects(EnvDTE.DTE dte) + { + List projectList = new List(); + + var folders = dte.Solution.Projects.Cast().Where(p=>p.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder); + + foreach (EnvDTE.Project folder in folders) + { + if (folder.ProjectItems == null) continue; + + foreach (EnvDTE.ProjectItem item in folder.ProjectItems) + { + if (item.Object is EnvDTE.Project) + projectList.Add(item.Object as EnvDTE.Project); + } + } + + var projects = dte.Solution.Projects.Cast().Where(p=>p.Kind != EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder); + + if (projects.Count() > 0) + projectList.AddRange(projects); + + return projectList; + } + + public static EnvDTE.ProjectItem GetProjectItemWithName(EnvDTE.ProjectItems items, string itemName) + { + return GetAllProjectItemsRecursive(items).Cast().Where(i=>i.Name == itemName).First(); + } + + public static string GetProjectItemFullPath(EnvDTE.ProjectItem item) + { + return item.Properties.Item("FullPath").Value.ToString(); + } + + public static IEnumerable GetAllSolutionItems(EnvDTE.DTE dte) + { + List itemList = new List(); + + foreach (Project item in GetAllProjects(dte)) + { + if (item == null || item.ProjectItems == null) continue; + + itemList.AddRange(GetAllProjectItemsRecursive(item.ProjectItems)); + } + + return itemList; + } + + public static IEnumerable GetAllProjectItemsRecursive(EnvDTE.ProjectItems projectItems) + { + foreach (EnvDTE.ProjectItem projectItem in projectItems) + { + if (projectItem.ProjectItems == null) continue; + + foreach (EnvDTE.ProjectItem subItem in GetAllProjectItemsRecursive(projectItem.ProjectItems)) + { + yield return subItem; + } + + + yield return projectItem; + } + } } public sealed class OutputFile { - public OutputFile() - { - this.FileProperties = new FileProperties - { - CustomTool = String.Empty, - BuildAction = BuildAction.None - }; - } - - public string FileName { get; set; } - public string ProjectName { get; set; } - public string FolderName { get; set; } - public string Content { get; set; } - public FileProperties FileProperties { get; set; } + public OutputFile() + { + this.FileProperties = new FileProperties + { + CustomTool = String.Empty, + BuildAction = BuildAction.None + }; + } + + public string FileName { get; set; } + public string ProjectName { get; set; } + public string FolderName { get; set; } + public string Content { get; set; } + public FileProperties FileProperties { get; set; } } public class BuildAction { - public const string None = "None"; - public const string Compile = "Compile"; - public const string Content = "Content"; - public const string EmbeddedResource = "EmbeddedResource"; - public const string EntityDeploy = "EntityDeploy"; + public const string None = "None"; + public const string Compile = "Compile"; + public const string Content = "Content"; + public const string EmbeddedResource = "EmbeddedResource"; + public const string EntityDeploy = "EntityDeploy"; } public sealed class FileProperties { - public FileProperties () - { - this.TemplateParameter = new Dictionary(); - } - - public string CustomTool { get; set; } - public string BuildAction { get; set; } - public Dictionary TemplateParameter { get; set; } - - internal string BuildActionString - { - get - { - return this.BuildAction; - } - } + public FileProperties () + { + this.TemplateParameter = new Dictionary(); + } + + public string CustomTool { get; set; } + public string BuildAction { get; set; } + public Dictionary TemplateParameter { get; set; } + + internal string BuildActionString + { + get + { + return this.BuildAction; + } + } } -#> \ No newline at end of file +#> From 3edccb68d0746a65c30ccf7ade2d26c71388b82f Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 18 Mar 2021 18:44:54 +0100 Subject: [PATCH 12/43] Unifying assembly and namespace reference syntax in PositCalculatorTemplateFileManager.ttinclude --- .../PositCalculatorTemplateFileManager.ttinclude | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude index 158ddc5f0..e8cb75ac6 100644 --- a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude +++ b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude @@ -2,13 +2,13 @@ <#@ assembly name="System.Data" #> <#@ assembly name="System.Data.Entity" #> <#@ assembly name="System.Xml" #> -<#@ assembly name="System.Xml.Linq"#> +<#@ assembly name="System.Xml.Linq" #> <#@ assembly name="System.Windows.Forms" #> -<#@ assembly name="EnvDTE"#> +<#@ assembly name="EnvDTE" #> <#@ assembly name="EnvDTE80" #> -<#@ assembly name="Microsoft.VisualStudio.Shell.15.0"#> -<#@ assembly name="Microsoft.VisualStudio.Shell.Interop"#> -<#@ import namespace="Microsoft.VisualStudio.Shell.Interop"#> +<#@ assembly name="Microsoft.VisualStudio.Shell.15.0" #> +<#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #> +<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #> <#@ import namespace="System" #> <#@ import namespace="System.Data" #> <#@ import namespace="System.Data.Objects" #> @@ -23,8 +23,8 @@ <#@ import namespace="System.Reflection" #> <#@ import namespace="System.CodeDom" #> <#@ import namespace="System.CodeDom.Compiler" #> -<#@ import namespace="Microsoft.CSharp"#> -<#@ import namespace="System.Text"#> +<#@ import namespace="Microsoft.CSharp" #> +<#@ import namespace="System.Text" #> <#@ import namespace="EnvDTE" #> <#@ import namespace="Microsoft.VisualStudio.TextTemplating" #> <#+ From 62fce9740a1db7613c61cb812a8a782f994bde09 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 18 Mar 2021 18:51:38 +0100 Subject: [PATCH 13/43] Adding missing Microsoft.VisualStudio.Shell.Framework assembly reference to PositCalculatorTemplateFileManager.ttinclude --- Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude | 1 + 1 file changed, 1 insertion(+) diff --git a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude index e8cb75ac6..52bf2c259 100644 --- a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude +++ b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude @@ -7,6 +7,7 @@ <#@ assembly name="EnvDTE" #> <#@ assembly name="EnvDTE80" #> <#@ assembly name="Microsoft.VisualStudio.Shell.15.0" #> +<#@ assembly name="Microsoft.VisualStudio.Shell.Framework" #> <#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #> <#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #> <#@ import namespace="System" #> From 85179c7d9be0cfeb7600c1383059212c0263084d Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 18 Mar 2021 18:53:42 +0100 Subject: [PATCH 14/43] Posit Samples: Recompiling Posit Calculators and Posit Calculator Sample Runners --- Hast.Samples.Posit/Posit16_0_Calculator.cs | 70 +++--- .../Posit16_0_CalculatorSampleRunner.cs | 46 ++-- Hast.Samples.Posit/Posit16_1_Calculator.cs | 41 +++- .../Posit16_1_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit16_2_Calculator.cs | 41 +++- .../Posit16_2_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit16_3_Calculator.cs | 41 +++- .../Posit16_3_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit16_4_Calculator.cs | 41 +++- .../Posit16_4_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit32_0_Calculator.cs | 41 +++- .../Posit32_0_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit32_1_Calculator.cs | 41 +++- .../Posit32_1_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit32_2_Calculator.cs | 41 +++- .../Posit32_2_CalculatorSampleRunner.cs | 55 +++-- Hast.Samples.Posit/Posit32_3_Calculator.cs | 41 +++- .../Posit32_3_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit32_4_Calculator.cs | 41 +++- .../Posit32_4_CalculatorSampleRunner.cs | 208 +++++++++--------- Hast.Samples.Posit/Posit8_0_Calculator.cs | 41 +++- .../Posit8_0_CalculatorSampleRunner.cs | 154 ++++++------- Hast.Samples.Posit/Posit8_1_Calculator.cs | 41 +++- .../Posit8_1_CalculatorSampleRunner.cs | 154 ++++++------- Hast.Samples.Posit/Posit8_2_Calculator.cs | 41 +++- .../Posit8_2_CalculatorSampleRunner.cs | 154 ++++++------- Hast.Samples.Posit/Posit8_3_Calculator.cs | 41 +++- .../Posit8_3_CalculatorSampleRunner.cs | 154 ++++++------- Hast.Samples.Posit/Posit8_4_Calculator.cs | 41 +++- .../Posit8_4_CalculatorSampleRunner.cs | 154 ++++++------- 30 files changed, 1811 insertions(+), 1368 deletions(-) diff --git a/Hast.Samples.Posit/Posit16_0_Calculator.cs b/Hast.Samples.Posit/Posit16_0_Calculator.cs index 0f687e9ef..6ab032ae1 100644 --- a/Hast.Samples.Posit/Posit16_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_0_Calculator.cs @@ -1,9 +1,8 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -22,7 +21,7 @@ public class Posit16_0_Calculator public const int CalculatePowerOfReal_InputInt32Index = 0; public const int CalculatePowerOfReal_InputPosit32Index = 1; public const int CalculatePowerOfReal_OutputPosit32Index = 0; - + public const int MaxDegreeOfParallelism = 5; @@ -111,75 +110,94 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit16_0_CalculatorCalculatorExtensions { public static int CalculateIntegerSumUpToNumber( - this Posit16_0_Calculator positCalculator, + this Posit16_0_Calculator positCalculator, int number, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); - memory.WriteInt32(Posit16_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + memory.WriteInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); - return memory.ReadInt32(Posit16_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + return memory.ReadInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit16_0_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit16_0_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); - memory.WriteInt32(Posit16_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32(Posit16_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_0(real).PositBits); + memory.WriteInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_0(real).PositBits); positCalculator.CalculatePowerOfReal(memory); - return (float)new Posit16_0((ushort)memory.ReadUInt32(Posit16_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + return (float)new Posit16_0((ushort)memory.ReadUInt32( Posit16_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_0_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16_0_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - if (numbers.Length != Posit16_0_Calculator.MaxDegreeOfParallelism) + if (numbers.Length != Posit16_0_Calculator.MaxDegreeOfParallelism) { throw new ArgumentException( "Provide as many numbers as the degree of parallelism of Posit16_0_Calculator is (" + Posit16_0_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory(Posit16_0_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16_0_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16_0_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { - memory.WriteInt32(Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + memory.WriteInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); } positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - var results = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; + var results = new int[ Posit16_0_Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < numbers.Length; i++) { - results[i] = memory.ReadInt32(Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + results[i] = memory.ReadInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); } return results; } - public static float AddPositsInArray(this Posit16_0_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit16_0_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); - memory.WriteUInt32(Posit16_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint)positArray.Length); + memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - for (var i = 0; i < positArray.Length; i++) + for (var i = 0; i < positArray.Length; i++) { - memory.WriteUInt32(Posit16_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); } positCalculator.AddPositsInArray(memory); - return (float)new Posit32(memory.ReadUInt32(Posit16_0_Calculator.AddPositsInArray_OutputPosit32Index), true); + return (float)new Posit32(memory.ReadUInt32( Posit16_0_Calculator.AddPositsInArray_OutputPosit32Index), true); } } } diff --git a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs index f646daaf3..af6f0ea35 100644 --- a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -26,17 +26,15 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - - positCalculator.CalculatePowerOfReal(10000, (float)1.015625); - - + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_0Array = new uint[100000]; @@ -58,7 +56,10 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); @@ -66,11 +67,15 @@ public static void RunSoftwareBenchmarks() Console.WriteLine(); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal(10000, (float)1.015625); - + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); Console.WriteLine("Result of power of real number: " + powerOfReal); @@ -84,9 +89,12 @@ public static void RunSoftwareBenchmarks() numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); @@ -98,20 +106,18 @@ public static void RunSoftwareBenchmarks() for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; - else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; + else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit16_0Array); + positCalculator.AddPositsInArray( Posit16_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); - } - } + }} } diff --git a/Hast.Samples.Posit/Posit16_1_Calculator.cs b/Hast.Samples.Posit/Posit16_1_Calculator.cs index 2f8c20195..df27b8eee 100644 --- a/Hast.Samples.Posit/Posit16_1_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_1_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit16_1_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit16_1_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit16_1_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit16_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit16_1_Calculator posi return memory.ReadInt32( Posit16_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit16_1_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit16_1_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit16_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit16_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_1(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit16_1_Calculator positCalcul return (float)new Posit16_1((ushort)memory.ReadUInt32( Posit16_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_1_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16_1_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit16_1_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_1_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit16_1_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16_1_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16_1_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit16_1_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit16_1_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit16_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs index df48c250c..57b1c7b13 100644 --- a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit16_1_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit16_1_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_1_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit16_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; + else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_1Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_1_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; + else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_1_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - - var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit16_1Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; - else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_1Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit16_1_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit16_1Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; - else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit16_1Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_1Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit16_2_Calculator.cs b/Hast.Samples.Posit/Posit16_2_Calculator.cs index 71a8bdc49..f59a39fc2 100644 --- a/Hast.Samples.Posit/Posit16_2_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_2_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit16_2_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit16_2_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit16_2_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit16_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit16_2_Calculator posi return memory.ReadInt32( Posit16_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit16_2_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit16_2_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit16_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit16_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_2(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit16_2_Calculator positCalcul return (float)new Posit16_2((ushort)memory.ReadUInt32( Posit16_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_2_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16_2_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit16_2_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_2_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit16_2_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16_2_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16_2_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit16_2_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit16_2_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit16_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs index ff7ce6ac2..67eb43487 100644 --- a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit16_2_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit16_2_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_2_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit16_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; + else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_2Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_2_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_2Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; + else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_2_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - - var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit16_2Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; - else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_2Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit16_2_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit16_2Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; - else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit16_2Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_2Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit16_3_Calculator.cs b/Hast.Samples.Posit/Posit16_3_Calculator.cs index c986d1a98..53e9f4d48 100644 --- a/Hast.Samples.Posit/Posit16_3_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_3_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit16_3_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit16_3_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit16_3_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit16_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit16_3_Calculator posi return memory.ReadInt32( Posit16_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit16_3_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit16_3_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit16_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit16_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_3(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit16_3_Calculator positCalcul return (float)new Posit16_3((ushort)memory.ReadUInt32( Posit16_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_3_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16_3_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit16_3_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_3_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit16_3_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16_3_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16_3_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit16_3_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit16_3_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit16_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs index f57a0c466..57ee8ae8b 100644 --- a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit16_3_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit16_3_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_3_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit16_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; + else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_3Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_3_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; + else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_3_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - - var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit16_3Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; - else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_3Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit16_3_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit16_3Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; - else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit16_3Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_3Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit16_4_Calculator.cs b/Hast.Samples.Posit/Posit16_4_Calculator.cs index 6ab3ed9b0..9575a1cdc 100644 --- a/Hast.Samples.Posit/Posit16_4_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_4_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit16_4_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit16_4_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit16_4_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit16_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit16_4_Calculator posi return memory.ReadInt32( Posit16_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit16_4_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit16_4_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit16_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit16_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_4(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit16_4_Calculator positCalcul return (float)new Posit16_4((ushort)memory.ReadUInt32( Posit16_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_4_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16_4_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit16_4_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit16_4_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit16_4_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16_4_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16_4_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit16_4_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit16_4_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit16_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs index 117719576..360904718 100644 --- a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit16_4_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit16_4_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_4_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit16_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; + else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_4Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit16_4_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit16_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; + else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit16_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_4_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - - var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit16_4Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; - else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_4Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit16_4_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 10000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit16_4Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; - else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit16_4Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_4Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit32_0_Calculator.cs b/Hast.Samples.Posit/Posit32_0_Calculator.cs index 8c74d4599..faa826dbc 100644 --- a/Hast.Samples.Posit/Posit32_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_0_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit32_0_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit32_0_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit32_0_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit32_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit32_0_Calculator posi return memory.ReadInt32( Posit32_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit32_0_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit32_0_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit32_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit32_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_0(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit32_0_Calculator positCalcul return (float)new Posit32_0((uint)memory.ReadUInt32( Posit32_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_0_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit32_0_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit32_0_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_0_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit32_0_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit32_0_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32_0_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit32_0_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit32_0_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit32_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs index 607a16701..18d94e44d 100644 --- a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit32_0_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit32_0_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_0_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit32_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; + else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_0Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_0_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_0Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; + else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_0_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - - var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit32_0Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; - else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_0Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit32_0_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit32_0Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; - else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit32_0Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_0Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit32_1_Calculator.cs b/Hast.Samples.Posit/Posit32_1_Calculator.cs index b646ce70d..24b29b39e 100644 --- a/Hast.Samples.Posit/Posit32_1_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_1_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit32_1_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit32_1_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit32_1_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit32_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit32_1_Calculator posi return memory.ReadInt32( Posit32_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit32_1_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit32_1_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit32_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit32_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_1(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit32_1_Calculator positCalcul return (float)new Posit32_1((uint)memory.ReadUInt32( Posit32_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_1_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit32_1_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit32_1_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_1_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit32_1_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit32_1_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32_1_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit32_1_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit32_1_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit32_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs index e30bda029..fad23c06f 100644 --- a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit32_1_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit32_1_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_1_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit32_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; + else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_1Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_1_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_1Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; + else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_1_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - - var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit32_1Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; - else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_1Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit32_1_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit32_1Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; - else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit32_1Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_1Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit32_2_Calculator.cs b/Hast.Samples.Posit/Posit32_2_Calculator.cs index c7e623792..fade5b7dd 100644 --- a/Hast.Samples.Posit/Posit32_2_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_2_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit32_2_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit32_2_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit32_2_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit32_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit32_2_Calculator posi return memory.ReadInt32( Posit32_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit32_2_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit32_2_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit32_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit32_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_2(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit32_2_Calculator positCalcul return (float)new Posit32_2((uint)memory.ReadUInt32( Posit32_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_2_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit32_2_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit32_2_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_2_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit32_2_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit32_2_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32_2_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit32_2_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit32_2_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit32_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs index 6b0c23a2d..41243ca3c 100644 --- a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs @@ -1,8 +1,12 @@ -using Hast.Layer; -using Lombiq.Arithmetics; -using System; +using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; +using System.Text; using System.Threading.Tasks; +using Hast.Layer; +using Hast.Samples.SampleAssembly; +using Lombiq.Arithmetics; namespace Hast.Samples.Posit { @@ -20,19 +24,17 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_2_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal(1000, (float)1.015625); - + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var numbers = new int[Posit32_2_Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < Posit32_2_Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_2Array = new uint[100000]; @@ -54,7 +56,10 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); @@ -62,10 +67,15 @@ public static void RunSoftwareBenchmarks() Console.WriteLine(); + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal(1000, (float)1.015625); - + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); Console.WriteLine("Result of power of real number: " + powerOfReal); @@ -79,9 +89,12 @@ public static void RunSoftwareBenchmarks() numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); @@ -93,20 +106,18 @@ public static void RunSoftwareBenchmarks() for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_2Array[i] = new Posit32_2((float)0.25 * 2 * i).PositBits; - else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32_2Array[i] = new Posit32_2((float)0.25 * 2 * i).PositBits; + else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit32_2Array); + positCalculator.AddPositsInArray( Posit32_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_2Array); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); - } - } + }} } diff --git a/Hast.Samples.Posit/Posit32_3_Calculator.cs b/Hast.Samples.Posit/Posit32_3_Calculator.cs index 90f03e00a..fd59c7b62 100644 --- a/Hast.Samples.Posit/Posit32_3_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_3_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit32_3_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit32_3_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit32_3_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit32_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit32_3_Calculator posi return memory.ReadInt32( Posit32_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit32_3_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit32_3_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit32_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit32_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_3(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit32_3_Calculator positCalcul return (float)new Posit32_3((uint)memory.ReadUInt32( Posit32_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_3_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit32_3_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit32_3_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_3_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit32_3_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit32_3_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32_3_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit32_3_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit32_3_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit32_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs index d4453aad0..bb2de3ed6 100644 --- a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit32_3_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit32_3_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_3_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit32_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; + else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_3Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_3_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_3Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; + else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_3_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - - var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit32_3Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; - else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_3Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit32_3_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit32_3Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; - else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit32_3Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_3Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit32_4_Calculator.cs b/Hast.Samples.Posit/Posit32_4_Calculator.cs index 46856fe91..1060e8252 100644 --- a/Hast.Samples.Posit/Posit32_4_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_4_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit32_4_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit32_4_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit32_4_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit32_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit32_4_Calculator posi return memory.ReadInt32( Posit32_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit32_4_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit32_4_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit32_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit32_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_4(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit32_4_Calculator positCalcul return (float)new Posit32_4((uint)memory.ReadUInt32( Posit32_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_4_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit32_4_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit32_4_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit32_4_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit32_4_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit32_4_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32_4_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit32_4_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit32_4_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit32_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs index e62f128ae..c55b7af40 100644 --- a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs @@ -10,108 +10,114 @@ namespace Hast.Samples.Posit { - internal class Posit32_4_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit32_4_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); + + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_4_Calculator()); + + + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + + + var Posit32_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; + else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; + } + + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_4Array); + } + + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit32_4_Calculator(); + + + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + + sw.Stop(); + + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); + + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + + Console.WriteLine(); + + var Posit32_4Array = new uint[100000]; + + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; + else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; + } + + positCalculator.AddPositsInArray( Posit32_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_4_Calculator()); - - - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - - - positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - - var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - - - var Posit32_4Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; - else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; - } - - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_4Array); - } - - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit32_4_Calculator(); - - - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); - - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - - var powerOfReal = positCalculator.CalculatePowerOfReal( 100000, (float)1.015625); - - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); - - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - - var Posit32_4Array = new uint[100000]; - - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; - else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; - } - - positCalculator.AddPositsInArray( Posit32_4Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_4Array); - sw.Stop(); - - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit8_0_Calculator.cs b/Hast.Samples.Posit/Posit8_0_Calculator.cs index 67f6f7434..c2f2fc20a 100644 --- a/Hast.Samples.Posit/Posit8_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_0_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit8_0_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit8_0_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit8_0_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit8_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit8_0_Calculator posit return memory.ReadInt32( Posit8_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit8_0_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit8_0_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit8_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit8_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_0(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit8_0_Calculator positCalcula return (float)new Posit8_0((byte)memory.ReadUInt32( Posit8_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_0_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8_0_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit8_0_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_0_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit8_0_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8_0_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8_0_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit8_0_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit8_0_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit8_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs index 7894d54bd..07acb314f 100644 --- a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs @@ -10,110 +10,112 @@ namespace Hast.Samples.Posit { - internal class Posit8_0_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit8_0_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_0_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - - positCalculator.CalculatePowerOfReal( 5, (float)0.5); - + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_0Array = new uint[100000]; + var Posit8_0Array = new uint[100000]; - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; - else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; + else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; + } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_0Array); - } + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_0Array); + } - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit8_0_Calculator(); + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_0_Calculator(); - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); + Console.WriteLine(); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } + Console.WriteLine(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); + var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var Posit8_0Array = new uint[100000]; + Console.WriteLine(); - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; - else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; - } + var Posit8_0Array = new uint[100000]; - positCalculator.AddPositsInArray( Posit8_0Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_0Array); - sw.Stop(); + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; + else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; + } - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.AddPositsInArray( Posit8_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit8_1_Calculator.cs b/Hast.Samples.Posit/Posit8_1_Calculator.cs index 2df896183..71bf16f9a 100644 --- a/Hast.Samples.Posit/Posit8_1_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_1_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit8_1_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit8_1_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit8_1_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit8_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit8_1_Calculator posit return memory.ReadInt32( Posit8_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit8_1_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit8_1_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit8_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit8_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_1(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit8_1_Calculator positCalcula return (float)new Posit8_1((byte)memory.ReadUInt32( Posit8_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_1_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8_1_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit8_1_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_1_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit8_1_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8_1_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8_1_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit8_1_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit8_1_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit8_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs index c9bf8e2b9..dffa962a2 100644 --- a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs @@ -10,110 +10,112 @@ namespace Hast.Samples.Posit { - internal class Posit8_1_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit8_1_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_1_Calculator()); + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_1_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - - positCalculator.CalculatePowerOfReal( 5, (float)0.5); - + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_1Array = new uint[100000]; + var Posit8_1Array = new uint[100000]; - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; - else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; + else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; + } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_1Array); - } + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_1Array); + } - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit8_1_Calculator(); + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_1_Calculator(); - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); + Console.WriteLine(); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } + Console.WriteLine(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); + var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var Posit8_1Array = new uint[100000]; + Console.WriteLine(); - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; - else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; - } + var Posit8_1Array = new uint[100000]; - positCalculator.AddPositsInArray( Posit8_1Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_1Array); - sw.Stop(); + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; + else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; + } - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.AddPositsInArray( Posit8_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit8_2_Calculator.cs b/Hast.Samples.Posit/Posit8_2_Calculator.cs index 0bb112036..3c2cacae1 100644 --- a/Hast.Samples.Posit/Posit8_2_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_2_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit8_2_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit8_2_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit8_2_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit8_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit8_2_Calculator posit return memory.ReadInt32( Posit8_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit8_2_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit8_2_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit8_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit8_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_2(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit8_2_Calculator positCalcula return (float)new Posit8_2((byte)memory.ReadUInt32( Posit8_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_2_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8_2_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit8_2_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_2_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit8_2_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8_2_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8_2_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit8_2_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit8_2_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit8_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs index f32121cbc..18299d8da 100644 --- a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs @@ -10,110 +10,112 @@ namespace Hast.Samples.Posit { - internal class Posit8_2_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit8_2_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_2_Calculator()); + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_2_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - - positCalculator.CalculatePowerOfReal( 5, (float)0.5); - + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_2Array = new uint[100000]; + var Posit8_2Array = new uint[100000]; - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; - else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; + else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; + } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_2Array); - } + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_2Array); + } - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit8_2_Calculator(); + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_2_Calculator(); - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); + Console.WriteLine(); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } + Console.WriteLine(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); + var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var Posit8_2Array = new uint[100000]; + Console.WriteLine(); - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; - else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; - } + var Posit8_2Array = new uint[100000]; - positCalculator.AddPositsInArray( Posit8_2Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_2Array); - sw.Stop(); + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; + else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; + } - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.AddPositsInArray( Posit8_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit8_3_Calculator.cs b/Hast.Samples.Posit/Posit8_3_Calculator.cs index 156c64e93..86b545b46 100644 --- a/Hast.Samples.Posit/Posit8_3_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_3_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit8_3_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit8_3_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit8_3_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit8_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit8_3_Calculator posit return memory.ReadInt32( Posit8_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit8_3_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit8_3_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit8_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit8_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_3(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit8_3_Calculator positCalcula return (float)new Posit8_3((byte)memory.ReadUInt32( Posit8_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_3_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8_3_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit8_3_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_3_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit8_3_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8_3_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8_3_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit8_3_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit8_3_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit8_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs index fc69d595e..5dd9b568d 100644 --- a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs @@ -10,110 +10,112 @@ namespace Hast.Samples.Posit { - internal class Posit8_3_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit8_3_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_3_Calculator()); + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_3_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - - positCalculator.CalculatePowerOfReal( 5, (float)0.5); - + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_3Array = new uint[100000]; + var Posit8_3Array = new uint[100000]; - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; - else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; + else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; + } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_3Array); - } + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_3Array); + } - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit8_3_Calculator(); + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_3_Calculator(); - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); + Console.WriteLine(); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } + Console.WriteLine(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); + var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var Posit8_3Array = new uint[100000]; + Console.WriteLine(); - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; - else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; - } + var Posit8_3Array = new uint[100000]; - positCalculator.AddPositsInArray( Posit8_3Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_3Array); - sw.Stop(); + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; + else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; + } - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.AddPositsInArray( Posit8_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } diff --git a/Hast.Samples.Posit/Posit8_4_Calculator.cs b/Hast.Samples.Posit/Posit8_4_Calculator.cs index 530815fb7..a371eb461 100644 --- a/Hast.Samples.Posit/Posit8_4_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_4_Calculator.cs @@ -109,9 +109,15 @@ public virtual void AddPositsInArray(SimpleMemory memory) public static class Posit8_4_CalculatorCalculatorExtensions { - public static int CalculateIntegerSumUpToNumber(this Posit8_4_Calculator positCalculator, int number) + public static int CalculateIntegerSumUpToNumber( + this Posit8_4_Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(1); + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); memory.WriteInt32( Posit8_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); @@ -119,9 +125,16 @@ public static int CalculateIntegerSumUpToNumber(this Posit8_4_Calculator posit return memory.ReadInt32( Posit8_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); } - public static float CalculatePowerOfReal(this Posit8_4_Calculator positCalculator, int number, float real) + public static float CalculatePowerOfReal( + this Posit8_4_Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory(2); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); memory.WriteInt32( Posit8_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); memory.WriteUInt32( Posit8_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_4(real).PositBits); @@ -131,7 +144,11 @@ public static float CalculatePowerOfReal(this Posit8_4_Calculator positCalcula return (float)new Posit8_4((byte)memory.ReadUInt32( Posit8_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_4_Calculator positCalculator, int[] numbers) + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8_4_Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { if (numbers.Length != Posit8_4_Calculator.MaxDegreeOfParallelism) { @@ -140,7 +157,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this Posit8_4_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = new SimpleMemory( Posit8_4_Calculator.MaxDegreeOfParallelism); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8_4_Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8_4_Calculator.MaxDegreeOfParallelism)); for (int i = 0; i < numbers.Length; i++) { @@ -159,9 +178,15 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers(this return results; } - public static float AddPositsInArray(this Posit8_4_Calculator positCalculator, uint[] positArray) + public static float AddPositsInArray( + this Posit8_4_Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) { - var memory = new SimpleMemory( positArray.Length + 1); + var memory = var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); memory.WriteUInt32( Posit8_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); diff --git a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs index 87d14b80d..d99deab37 100644 --- a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs @@ -10,110 +10,112 @@ namespace Hast.Samples.Posit { - internal class Posit8_4_CalculatorSampleRunner - { - public static void Configure(HardwareGenerationConfiguration configuration) - { - configuration.AddHardwareEntryPointType(); - } + internal class Posit8_4_CalculatorSampleRunner + { + public static void Configure(HardwareGenerationConfiguration configuration) + { + configuration.AddHardwareEntryPointType(); + } - public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) - { - RunSoftwareBenchmarks(); + public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) + { + RunSoftwareBenchmarks(); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_4_Calculator()); + var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_4_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - - positCalculator.CalculatePowerOfReal( 5, (float)0.5); - + positCalculator.CalculatePowerOfReal( 5, (float)0.5); + + var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } - - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_4Array = new uint[100000]; + var Posit8_4Array = new uint[100000]; - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; - else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; - } + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; + else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; + } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_4Array); - } + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_4Array); + } - public static void RunSoftwareBenchmarks() - { - var positCalculator = new Posit8_4_Calculator(); + public static void RunSoftwareBenchmarks() + { + var positCalculator = new Posit8_4_Calculator(); - // Not to run the benchmark below the first time, because JIT compiling can affect it. - positCalculator.CalculateIntegerSumUpToNumber(100000); - var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000); - sw.Stop(); + // Not to run the benchmark below the first time, because JIT compiling can affect it. + positCalculator.CalculateIntegerSumUpToNumber(100000); + var sw = Stopwatch.StartNew(); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + 100000, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine("Result of counting up to 100000: " + integerSumUpToNumber); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - Console.WriteLine(); + Console.WriteLine(); - positCalculator.CalculatePowerOfReal(100000, (float)1.0001); - sw = Stopwatch.StartNew(); - + positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5); - sw.Stop(); - - Console.WriteLine("Result of power of real number: " + powerOfReal); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of power of real number: " + powerOfReal); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) - { - numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); - } + Console.WriteLine(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers); - sw.Stop(); + var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) + { + numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); + } - Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + numbers, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); + Console.WriteLine("Result of counting up to ~100000 parallelized: " + string.Join(", ", integerSumsUpToNumbers)); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); - var Posit8_4Array = new uint[100000]; + Console.WriteLine(); - for (var i = 0; i < 100000; i++) - { - if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; - else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; - } + var Posit8_4Array = new uint[100000]; - positCalculator.AddPositsInArray( Posit8_4Array); - sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_4Array); - sw.Stop(); + for (var i = 0; i < 100000; i++) + { + if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; + else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; + } - Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); - Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + positCalculator.AddPositsInArray( Posit8_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw = Stopwatch.StartNew(); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + sw.Stop(); - Console.WriteLine(); - } - } + Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); + Console.WriteLine("Elapsed: " + sw.ElapsedMilliseconds + "ms"); + Console.WriteLine(); + }} } From f9e249eccfc0ac1147450e4c39a04f13638b4034 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 18 Mar 2021 19:15:16 +0100 Subject: [PATCH 15/43] Fixing PositCalculator template syntax errors --- Hast.Samples.Posit/Posit16_0_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit16_1_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit16_2_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit16_3_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit16_4_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit32_0_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit32_1_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit32_2_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit32_3_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit32_4_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit8_0_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit8_1_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit8_2_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit8_3_Calculator.cs | 11 +++++------ Hast.Samples.Posit/Posit8_4_Calculator.cs | 11 +++++------ Hast.Samples.Posit/PositCalculator.tt | 11 +++++------ 16 files changed, 80 insertions(+), 96 deletions(-) diff --git a/Hast.Samples.Posit/Posit16_0_Calculator.cs b/Hast.Samples.Posit/Posit16_0_Calculator.cs index 6ab032ae1..8bef724e8 100644 --- a/Hast.Samples.Posit/Posit16_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_0_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit16_0_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit16_0_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_0_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit16_0_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit16_1_Calculator.cs b/Hast.Samples.Posit/Posit16_1_Calculator.cs index df27b8eee..143c555c8 100644 --- a/Hast.Samples.Posit/Posit16_1_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_1_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit16_1_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit16_1_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_1_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit16_1_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit16_2_Calculator.cs b/Hast.Samples.Posit/Posit16_2_Calculator.cs index f59a39fc2..92da29ad6 100644 --- a/Hast.Samples.Posit/Posit16_2_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_2_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit16_2_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit16_2_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_2_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit16_2_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit16_3_Calculator.cs b/Hast.Samples.Posit/Posit16_3_Calculator.cs index 53e9f4d48..cf31cac2f 100644 --- a/Hast.Samples.Posit/Posit16_3_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_3_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit16_3_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit16_3_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_3_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit16_3_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit16_4_Calculator.cs b/Hast.Samples.Posit/Posit16_4_Calculator.cs index 9575a1cdc..726d7edb1 100644 --- a/Hast.Samples.Posit/Posit16_4_Calculator.cs +++ b/Hast.Samples.Posit/Posit16_4_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit16_4_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit16_4_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_4_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit16_4_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit32_0_Calculator.cs b/Hast.Samples.Posit/Posit32_0_Calculator.cs index faa826dbc..84e2a75fd 100644 --- a/Hast.Samples.Posit/Posit32_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_0_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit32_0_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit32_0_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_0_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit32_0_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit32_1_Calculator.cs b/Hast.Samples.Posit/Posit32_1_Calculator.cs index 24b29b39e..980d4640c 100644 --- a/Hast.Samples.Posit/Posit32_1_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_1_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit32_1_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit32_1_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_1_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit32_1_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit32_2_Calculator.cs b/Hast.Samples.Posit/Posit32_2_Calculator.cs index fade5b7dd..7376f5959 100644 --- a/Hast.Samples.Posit/Posit32_2_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_2_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit32_2_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit32_2_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_2_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit32_2_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit32_3_Calculator.cs b/Hast.Samples.Posit/Posit32_3_Calculator.cs index fd59c7b62..2425445f6 100644 --- a/Hast.Samples.Posit/Posit32_3_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_3_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit32_3_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit32_3_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_3_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit32_3_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit32_4_Calculator.cs b/Hast.Samples.Posit/Posit32_4_Calculator.cs index 1060e8252..9cdcadb3d 100644 --- a/Hast.Samples.Posit/Posit32_4_Calculator.cs +++ b/Hast.Samples.Posit/Posit32_4_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit32_4_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit32_4_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_4_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit32_4_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit8_0_Calculator.cs b/Hast.Samples.Posit/Posit8_0_Calculator.cs index c2f2fc20a..092b9f133 100644 --- a/Hast.Samples.Posit/Posit8_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_0_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit8_0_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit8_0_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_0_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit8_0_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit8_1_Calculator.cs b/Hast.Samples.Posit/Posit8_1_Calculator.cs index 71bf16f9a..95f4999de 100644 --- a/Hast.Samples.Posit/Posit8_1_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_1_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit8_1_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit8_1_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_1_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit8_1_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit8_2_Calculator.cs b/Hast.Samples.Posit/Posit8_2_Calculator.cs index 3c2cacae1..c677b61f6 100644 --- a/Hast.Samples.Posit/Posit8_2_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_2_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit8_2_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit8_2_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_2_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit8_2_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit8_3_Calculator.cs b/Hast.Samples.Posit/Posit8_3_Calculator.cs index 86b545b46..6ca05c142 100644 --- a/Hast.Samples.Posit/Posit8_3_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_3_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit8_3_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit8_3_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_3_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit8_3_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/Posit8_4_Calculator.cs b/Hast.Samples.Posit/Posit8_4_Calculator.cs index a371eb461..d8a87ebaa 100644 --- a/Hast.Samples.Posit/Posit8_4_Calculator.cs +++ b/Hast.Samples.Posit/Posit8_4_Calculator.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -132,7 +131,7 @@ public static float CalculatePowerOfReal( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -157,9 +156,9 @@ public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( Posit8_4_Calculator.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(Posit8_4_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_4_Calculator.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, Posit8_4_Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -184,7 +183,7 @@ public static float AddPositsInArray( IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); diff --git a/Hast.Samples.Posit/PositCalculator.tt b/Hast.Samples.Posit/PositCalculator.tt index 69b189f42..6ef14af3d 100644 --- a/Hast.Samples.Posit/PositCalculator.tt +++ b/Hast.Samples.Posit/PositCalculator.tt @@ -26,9 +26,8 @@ for (var i = 0; i<3; i++){ #> using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Hast.Layer; using Hast.Transformer.Abstractions.SimpleMemory; using Lombiq.Arithmetics; @@ -158,7 +157,7 @@ namespace Hast.Samples.Posit IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); @@ -183,9 +182,9 @@ namespace Hast.Samples.Posit <#= className#>.MaxDegreeOfParallelism + ")"); } - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(<#= className#>.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, <#= className#>.MaxDegreeOfParallelism)); + : hastlayer.CreateMemory(configuration, <#= className#>.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { @@ -210,7 +209,7 @@ namespace Hast.Samples.Posit IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - var memory = var memory = hastlayer is null + var memory = hastlayer is null ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); From 0e903ae3257eb94677d7c524e2c911f4840297a6 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 21 Mar 2021 13:13:44 +0100 Subject: [PATCH 16/43] Updating Lombiq.Arithmetics with T4 compilation issues fixed --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index ac6399de3..6947150fc 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit ac6399de34632e822374fe83eb43bbbc53c145bd +Subproject commit 6947150fc3f90ffc2243cb7e26a137c897be2551 From f0f6db56cd9bcdcc9661d0646fba1552dd2b7594 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 21 Mar 2021 13:18:21 +0100 Subject: [PATCH 17/43] Temporary (?) workaround to be able transform Posit.tt as T4 templates currently can't find PackageReference assemblies automatically --- Hast.Samples.Posit/Hast.Samples.Posit.csproj | 4 ++++ .../PositCalculatorTemplateFileManager.ttinclude | 1 + 2 files changed, 5 insertions(+) diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj index b67439d0c..a2a367475 100644 --- a/Hast.Samples.Posit/Hast.Samples.Posit.csproj +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -3,6 +3,7 @@ netcoreapp3.1 Exe false + true @@ -22,6 +23,9 @@ TextTemplatingFileGenerator + + + diff --git a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude index 52bf2c259..e0f597b04 100644 --- a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude +++ b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude @@ -9,6 +9,7 @@ <#@ assembly name="Microsoft.VisualStudio.Shell.15.0" #> <#@ assembly name="Microsoft.VisualStudio.Shell.Framework" #> <#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #> +<#@ assembly name="D:\DEV\Hastlayer\Hastlayer-SDK\Lombiq.Arithmetics\bin\Debug\netstandard2.0\Microsoft.VisualStudio.Validation.dll" #> <#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #> <#@ import namespace="System" #> <#@ import namespace="System.Data" #> From 3e5fce58f00c48e25d491471893fa1570c83320a Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 21 Mar 2021 13:19:39 +0100 Subject: [PATCH 18/43] Untabifying Sample.tt and setting csproj to auto-compile it --- Hast.Samples.Posit/Hast.Samples.Posit.csproj | 9 ++ Hast.Samples.Posit/Sample.cs | 92 ++++++++++---------- Hast.Samples.Posit/Sample.tt | 30 +++---- 3 files changed, 70 insertions(+), 61 deletions(-) diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj index a2a367475..208019d4e 100644 --- a/Hast.Samples.Posit/Hast.Samples.Posit.csproj +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -22,6 +22,10 @@ TextTemplatingFileGenerator + + TextTemplatingFileGenerator + Sample.cs + @@ -35,5 +39,10 @@ True PositCalculator.tt + + True + True + Sample.tt + diff --git a/Hast.Samples.Posit/Sample.cs b/Hast.Samples.Posit/Sample.cs index 189138070..83fbaa626 100644 --- a/Hast.Samples.Posit/Sample.cs +++ b/Hast.Samples.Posit/Sample.cs @@ -1,55 +1,55 @@ - + namespace Hast.Samples.Posit { internal enum Sample { - Posit8_0_Calculator, - Posit8_1_Calculator, - Posit8_2_Calculator, - Posit8_3_Calculator, - Posit8_4_Calculator, - //Posit8_0_AdvancedCalculator, - //Posit8_1_AdvancedCalculator, - //Posit8_2_AdvancedCalculator, - //Posit8_3_AdvancedCalculator, - //Posit8_4_AdvancedCalculator, - //Posit8_0_FusedCalculator, - //Posit8_1_FusedCalculator, - //Posit8_2_FusedCalculator, - //Posit8_3_FusedCalculator, - //Posit8_4_FusedCalculator, - Posit16_0_Calculator, - Posit16_1_Calculator, - Posit16_2_Calculator, - Posit16_3_Calculator, - Posit16_4_Calculator, - //Posit16_0_AdvancedCalculator, - //Posit16_1_AdvancedCalculator, - //Posit16_2_AdvancedCalculator, - //Posit16_3_AdvancedCalculator, - //Posit16_4_AdvancedCalculator, - //Posit16_0_FusedCalculator, - //Posit16_1_FusedCalculator, - //Posit16_2_FusedCalculator, - //Posit16_3_FusedCalculator, - //Posit16_4_FusedCalculator, - Posit32_0_Calculator, - Posit32_1_Calculator, - Posit32_2_Calculator, - Posit32_3_Calculator, - Posit32_4_Calculator - // Posit32_0_AdvancedCalculator, - // Posit32_1_AdvancedCalculator, - // Posit32_2_AdvancedCalculator, - // Posit32_3_AdvancedCalculator, - // Posit32_4_AdvancedCalculator, - // Posit32_0_FusedCalculator, - // Posit32_1_FusedCalculator, - // Posit32_2_FusedCalculator, - // Posit32_3_FusedCalculator, + Posit8_0_Calculator, + Posit8_1_Calculator, + Posit8_2_Calculator, + Posit8_3_Calculator, + Posit8_4_Calculator, + Posit8_0_AdvancedCalculator, + Posit8_1_AdvancedCalculator, + Posit8_2_AdvancedCalculator, + Posit8_3_AdvancedCalculator, + Posit8_4_AdvancedCalculator, + Posit8_0_FusedCalculator, + Posit8_1_FusedCalculator, + Posit8_2_FusedCalculator, + Posit8_3_FusedCalculator, + Posit8_4_FusedCalculator, + Posit16_0_Calculator, + Posit16_1_Calculator, + Posit16_2_Calculator, + Posit16_3_Calculator, + Posit16_4_Calculator, + Posit16_0_AdvancedCalculator, + Posit16_1_AdvancedCalculator, + Posit16_2_AdvancedCalculator, + Posit16_3_AdvancedCalculator, + Posit16_4_AdvancedCalculator, + Posit16_0_FusedCalculator, + Posit16_1_FusedCalculator, + Posit16_2_FusedCalculator, + Posit16_3_FusedCalculator, + Posit16_4_FusedCalculator, + Posit32_0_Calculator, + Posit32_1_Calculator, + Posit32_2_Calculator, + Posit32_3_Calculator, + Posit32_4_Calculator, + Posit32_0_AdvancedCalculator, + Posit32_1_AdvancedCalculator, + Posit32_2_AdvancedCalculator, + Posit32_3_AdvancedCalculator, + Posit32_4_AdvancedCalculator, + Posit32_0_FusedCalculator, + Posit32_1_FusedCalculator, + Posit32_2_FusedCalculator, + Posit32_3_FusedCalculator, - //Posit32_4_FusedCalculator + Posit32_4_FusedCalculator } } diff --git a/Hast.Samples.Posit/Sample.tt b/Hast.Samples.Posit/Sample.tt index 7e15b1b7e..a65aa513c 100644 --- a/Hast.Samples.Posit/Sample.tt +++ b/Hast.Samples.Posit/Sample.tt @@ -1,4 +1,4 @@ -<#@ template debug="false" hostspecific="false" language="C#" #> +<#@ template debug="false" hostspecific="false" language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> @@ -6,15 +6,15 @@ <#@ output extension=".cs" #> <# - var positSizes = new byte[] {8,16,32,64}; - var calculatorTypes = new string[] {"Calculator", "AdvancedCalculator", "FusedCalculator"}; - List calculatorNames = new List(); + var positSizes = new byte[] {8,16,32,64}; + var calculatorTypes = new string[] {"Calculator", "AdvancedCalculator", "FusedCalculator"}; + List calculatorNames = new List(); - for (var positSize = 2; positSize >= 0; positSize--){ - for (var type = 2; type>=0; type--){ - for (var exponentSize = 4; exponentSize>=0; exponentSize--){ - var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; - calculatorNames.Add(positCalculatorName); + for (var positSize = 2; positSize >= 0; positSize--){ + for (var type = 2; type>=0; type--){ + for (var exponentSize = 4; exponentSize>=0; exponentSize--){ + var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; + calculatorNames.Add(positCalculatorName); } } } @@ -25,13 +25,13 @@ namespace Hast.Samples.Posit internal enum Sample { <# - var last = calculatorNames.First(); - calculatorNames.RemoveAt(0); - calculatorNames.Reverse(); - foreach (var name in calculatorNames) + var last = calculatorNames.First(); + calculatorNames.RemoveAt(0); + calculatorNames.Reverse(); + foreach (var name in calculatorNames) { #> - <#=name#>, + <#=name#>, <# } #> - <#=last#> + <#=last#> } } From db3338de2b7d6c68ae8df29fcf62bd422bb3d9de Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 21 Mar 2021 13:30:26 +0100 Subject: [PATCH 19/43] Recompiling PositCalculatorSampleRunner.tt --- Hast.Samples.Posit/Hast.Samples.Posit.csproj | 6 ++++ .../Posit16_0_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit16_1_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit16_2_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit16_3_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit16_4_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit32_0_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit32_1_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit32_2_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit32_3_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit32_4_CalculatorSampleRunner.cs | 22 ++++++------- .../Posit8_0_CalculatorSampleRunner.cs | 14 ++++---- .../Posit8_1_CalculatorSampleRunner.cs | 14 ++++---- .../Posit8_2_CalculatorSampleRunner.cs | 14 ++++---- .../Posit8_3_CalculatorSampleRunner.cs | 14 ++++---- .../Posit8_4_CalculatorSampleRunner.cs | 14 ++++---- Hast.Samples.Posit/PositCalculator.cs | 22 ++++++++++++- .../PositCalculatorSampleRunner.cs | 32 +++++++++---------- .../PositCalculatorSampleRunner.tt | 24 +++++++------- 19 files changed, 200 insertions(+), 174 deletions(-) diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj index 208019d4e..7545cb2a7 100644 --- a/Hast.Samples.Posit/Hast.Samples.Posit.csproj +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -21,6 +21,7 @@ TextTemplatingFileGenerator + PositCalculatorSampleRunner.cs TextTemplatingFileGenerator @@ -39,6 +40,11 @@ True PositCalculator.tt + + True + True + PositCalculatorSampleRunner.tt + True True diff --git a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs index af6f0ea35..7cb9ea353 100644 --- a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_0_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_0Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); + positCalculator.AddPositsInArray(Posit16_0Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs index 57b1c7b13..145e92fc2 100644 --- a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_1_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_1Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_1Array); + positCalculator.AddPositsInArray(Posit16_1Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs index 67eb43487..c64579036 100644 --- a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_2_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_2Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_2Array); + positCalculator.AddPositsInArray(Posit16_2Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs index 57ee8ae8b..52aecbece 100644 --- a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_3_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_3Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_3Array); + positCalculator.AddPositsInArray(Posit16_3Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs index 360904718..e1c72fb4e 100644 --- a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_4_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_4Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_4Array); + positCalculator.AddPositsInArray(Posit16_4Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs index 18d94e44d..873317467 100644 --- a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_0_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_0Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_0Array); + positCalculator.AddPositsInArray(Posit32_0Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs index fad23c06f..fa4dd6dae 100644 --- a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_1_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_1Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_1Array); + positCalculator.AddPositsInArray(Posit32_1Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs index 41243ca3c..46ba1cf4c 100644 --- a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_2_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_2Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_2Array); + positCalculator.AddPositsInArray(Posit32_2Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs index bb2de3ed6..37c3d1100 100644 --- a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_3_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_3Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_3Array); + positCalculator.AddPositsInArray(Posit32_3Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs index c55b7af40..a0e3fa890 100644 --- a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_4_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_4Array = new uint[100000]; @@ -45,7 +45,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_4Array); + positCalculator.AddPositsInArray(Posit32_4Array); } public static void RunSoftwareBenchmarks() @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs index 07acb314f..e17abdd4c 100644 --- a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_0_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_0Array = new uint[100000]; @@ -46,7 +46,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_0Array); + positCalculator.AddPositsInArray(Posit8_0Array); } public static void RunSoftwareBenchmarks() @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs index dffa962a2..7aa190baa 100644 --- a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_1_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_1Array = new uint[100000]; @@ -46,7 +46,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_1Array); + positCalculator.AddPositsInArray(Posit8_1Array); } public static void RunSoftwareBenchmarks() @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs index 18299d8da..8dff7b882 100644 --- a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_2_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_2Array = new uint[100000]; @@ -46,7 +46,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_2Array); + positCalculator.AddPositsInArray(Posit8_2Array); } public static void RunSoftwareBenchmarks() @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs index 5dd9b568d..b9cc7253e 100644 --- a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_3_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_3Array = new uint[100000]; @@ -46,7 +46,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_3Array); + positCalculator.AddPositsInArray(Posit8_3Array); } public static void RunSoftwareBenchmarks() @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs index d99deab37..2b8c6547b 100644 --- a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs @@ -24,7 +24,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_4_Calculator()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_4Array = new uint[100000]; @@ -46,7 +46,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_4Array); + positCalculator.AddPositsInArray(Posit8_4Array); } public static void RunSoftwareBenchmarks() @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/PositCalculator.cs b/Hast.Samples.Posit/PositCalculator.cs index f4a2e8e1d..f3da1500f 100644 --- a/Hast.Samples.Posit/PositCalculator.cs +++ b/Hast.Samples.Posit/PositCalculator.cs @@ -1 +1,21 @@ -ErrorGeneratingOutput \ No newline at end of file + + + + +// Generated helper templates +// Generated items +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_4_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_3_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_2_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_1_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_0_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_4_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_3_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_2_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_1_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_0_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_4_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_3_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_2_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_1_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_0_Calculator.cs diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.cs b/Hast.Samples.Posit/PositCalculatorSampleRunner.cs index 8994f7ce2..5d656c2fd 100644 --- a/Hast.Samples.Posit/PositCalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.cs @@ -1,21 +1,21 @@ - + // Generated helper templates // Generated items -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_4_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_3_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_2_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_1_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit32_0_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_4_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_3_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_2_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_1_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit16_0_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_4_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_3_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_2_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_1_CalculatorSampleRunner.cs -// E:\Lombiq\hastlayer-sdk-latest\Hast.Samples.Posit\Posit8_0_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_4_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_3_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_2_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_1_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_0_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_4_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_3_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_2_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_1_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_0_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_4_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_3_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_2_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_1_CalculatorSampleRunner.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_0_CalculatorSampleRunner.cs diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt index c00d12094..7fbc10a99 100644 --- a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt @@ -62,7 +62,7 @@ namespace Hast.Samples.Posit var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new <#=calculatorName#>()); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); <# if (i == 0) { @@ -79,7 +79,7 @@ namespace Hast.Samples.Posit numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var <#=positName#>Array = new uint[100000]; @@ -90,7 +90,7 @@ namespace Hast.Samples.Posit else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(<#=positName#>Array); + positCalculator.AddPositsInArray(<#=positName#>Array); } public static void RunSoftwareBenchmarks() @@ -101,7 +101,7 @@ namespace Hast.Samples.Posit // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( + positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -119,13 +119,13 @@ namespace Hast.Samples.Posit { #> - var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); <# } else { #> - var powerOfReal = positCalculator.CalculatePowerOfReal( - <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.CalculatePowerOfReal( + <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); <# } #> sw.Stop(); @@ -143,7 +143,7 @@ namespace Hast.Samples.Posit positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -164,7 +164,7 @@ namespace Hast.Samples.Posit positCalculator.AddPositsInArray( <#=positName#>Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( <#=positName#>Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( <#=positName#>Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); From 8d1bd32bd92e3e7de35bbbadcf101ac1dddd0efe Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 21 Mar 2021 16:45:26 +0100 Subject: [PATCH 20/43] Fixing Sample.tt + code styling improvements --- Hast.Samples.Posit/Sample.cs | 97 ++++++++++++++++++------------------ Hast.Samples.Posit/Sample.tt | 37 +++++++------- 2 files changed, 65 insertions(+), 69 deletions(-) diff --git a/Hast.Samples.Posit/Sample.cs b/Hast.Samples.Posit/Sample.cs index 83fbaa626..e8d5f2efd 100644 --- a/Hast.Samples.Posit/Sample.cs +++ b/Hast.Samples.Posit/Sample.cs @@ -1,55 +1,54 @@ - - - namespace Hast.Samples.Posit { internal enum Sample { - Posit8_0_Calculator, - Posit8_1_Calculator, - Posit8_2_Calculator, - Posit8_3_Calculator, - Posit8_4_Calculator, - Posit8_0_AdvancedCalculator, - Posit8_1_AdvancedCalculator, - Posit8_2_AdvancedCalculator, - Posit8_3_AdvancedCalculator, - Posit8_4_AdvancedCalculator, - Posit8_0_FusedCalculator, - Posit8_1_FusedCalculator, - Posit8_2_FusedCalculator, - Posit8_3_FusedCalculator, - Posit8_4_FusedCalculator, - Posit16_0_Calculator, - Posit16_1_Calculator, - Posit16_2_Calculator, - Posit16_3_Calculator, - Posit16_4_Calculator, - Posit16_0_AdvancedCalculator, - Posit16_1_AdvancedCalculator, - Posit16_2_AdvancedCalculator, - Posit16_3_AdvancedCalculator, - Posit16_4_AdvancedCalculator, - Posit16_0_FusedCalculator, - Posit16_1_FusedCalculator, - Posit16_2_FusedCalculator, - Posit16_3_FusedCalculator, - Posit16_4_FusedCalculator, - Posit32_0_Calculator, - Posit32_1_Calculator, - Posit32_2_Calculator, - Posit32_3_Calculator, - Posit32_4_Calculator, - Posit32_0_AdvancedCalculator, - Posit32_1_AdvancedCalculator, - Posit32_2_AdvancedCalculator, - Posit32_3_AdvancedCalculator, - Posit32_4_AdvancedCalculator, - Posit32_0_FusedCalculator, - Posit32_1_FusedCalculator, - Posit32_2_FusedCalculator, - Posit32_3_FusedCalculator, - - Posit32_4_FusedCalculator + Posit8_0_Calculator, + Posit8_0_AdvancedCalculator, + Posit8_0_FusedCalculator, + Posit8_1_Calculator, + Posit8_1_AdvancedCalculator, + Posit8_1_FusedCalculator, + Posit8_2_Calculator, + Posit8_2_AdvancedCalculator, + Posit8_2_FusedCalculator, + Posit8_3_Calculator, + Posit8_3_AdvancedCalculator, + Posit8_3_FusedCalculator, + Posit16_0_Calculator, + Posit16_0_AdvancedCalculator, + Posit16_0_FusedCalculator, + Posit16_1_Calculator, + Posit16_1_AdvancedCalculator, + Posit16_1_FusedCalculator, + Posit16_2_Calculator, + Posit16_2_AdvancedCalculator, + Posit16_2_FusedCalculator, + Posit16_3_Calculator, + Posit16_3_AdvancedCalculator, + Posit16_3_FusedCalculator, + Posit32_0_Calculator, + Posit32_0_AdvancedCalculator, + Posit32_0_FusedCalculator, + Posit32_1_Calculator, + Posit32_1_AdvancedCalculator, + Posit32_1_FusedCalculator, + Posit32_2_Calculator, + Posit32_2_AdvancedCalculator, + Posit32_2_FusedCalculator, + Posit32_3_Calculator, + Posit32_3_AdvancedCalculator, + Posit32_3_FusedCalculator, + Posit64_0_Calculator, + Posit64_0_AdvancedCalculator, + Posit64_0_FusedCalculator, + Posit64_1_Calculator, + Posit64_1_AdvancedCalculator, + Posit64_1_FusedCalculator, + Posit64_2_Calculator, + Posit64_2_AdvancedCalculator, + Posit64_2_FusedCalculator, + Posit64_3_Calculator, + Posit64_3_AdvancedCalculator, + Posit64_3_FusedCalculator, } } diff --git a/Hast.Samples.Posit/Sample.tt b/Hast.Samples.Posit/Sample.tt index a65aa513c..2b4d46275 100644 --- a/Hast.Samples.Posit/Sample.tt +++ b/Hast.Samples.Posit/Sample.tt @@ -4,34 +4,31 @@ <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".cs" #> - <# - var positSizes = new byte[] {8,16,32,64}; - var calculatorTypes = new string[] {"Calculator", "AdvancedCalculator", "FusedCalculator"}; - List calculatorNames = new List(); + var positSizes = new byte[] { 8, 16, 32, 64 }; + var calculatorTypes = new string[] { "Calculator", "AdvancedCalculator", "FusedCalculator" }; + var exponentSizes = Enumerable.Range(0, 4); + + List calculatorNames = new List(); - for (var positSize = 2; positSize >= 0; positSize--){ - for (var type = 2; type>=0; type--){ - for (var exponentSize = 4; exponentSize>=0; exponentSize--){ - var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; - calculatorNames.Add(positCalculatorName); - } + foreach (var positSize in positSizes) + { + foreach (var exponentSize in exponentSizes) + { + foreach (var calculatorType in calculatorTypes) + { + calculatorNames.Add($"Posit{positSize}_{exponentSize}_{calculatorType}"); } + } } -#> - +#> namespace Hast.Samples.Posit { internal enum Sample { <# - var last = calculatorNames.First(); - calculatorNames.RemoveAt(0); - calculatorNames.Reverse(); foreach (var name in calculatorNames) - { #> - <#=name#>, - <# } #> - <#=last#> - } + {#> + <#= name #>, + <#}#>} } From cb7e01b4824e07477dd20dbc8631a8a355151ac0 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 21 Mar 2021 17:07:17 +0100 Subject: [PATCH 21/43] Fixing syntax and structure in Program.tt, but VHDL source ouput is disabled --- Hast.Samples.Posit/Hast.Samples.Posit.csproj | 9 + Hast.Samples.Posit/Program.cs | 370 +++++-------------- Hast.Samples.Posit/Program.tt | 314 ++++++++-------- 3 files changed, 257 insertions(+), 436 deletions(-) diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj index 7545cb2a7..a59605e2d 100644 --- a/Hast.Samples.Posit/Hast.Samples.Posit.csproj +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -23,6 +23,10 @@ TextTemplatingFileGenerator PositCalculatorSampleRunner.cs + + TextTemplatingFileGenerator + Program.cs + TextTemplatingFileGenerator Sample.cs @@ -45,6 +49,11 @@ True PositCalculatorSampleRunner.tt + + True + True + Program.tt + True True diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index f0a367d15..a7116cc45 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -11,7 +11,7 @@ internal static class Configuration { public static string VhdlOutputFilePath = @"Hast_IP.vhd"; - public static Sample SampleToRun = Sample.Posit32_2_Calculator; + public static Sample SampleToRun = Sample.Posit32_0_Calculator; } class Program @@ -21,12 +21,12 @@ static void Main(string[] args) Task.Run(async () => { /* - * On a high level these are the steps to use Hastlayer: - * 1. Create the Hastlayer shell. - * 2. Configure hardware generation and generate FPGA hardware representation of the given .NET code. - * 3. Generate proxies for hardware-transformed types and use these proxies to utilize hardware - * implementations. (You can see this inside the SampleRunners.) - */ + * On a high level these are the steps to use Hastlayer: + * 1. Create the Hastlayer shell. + * 2. Configure hardware generation and generate FPGA hardware representation of the given .NET code. + * 3. Generate proxies for hardware-transformed types and use these proxies to utilize hardware + * implementations. (You can see this inside the SampleRunners.) + */ // Configuring the Hastlayer shell. Which flavor should we use? If you're unsure then you'll need // the Client flavor: This will let you connect to a remote Hastlayer service to run the software @@ -36,7 +36,7 @@ static void Main(string[] args) // Initializing a Hastlayer shell. Since this is non-trivial to do you can cache this shell object // while the program runs and re-use it continuously. No need to always wrap it into a using() like // here, just make sure to Dispose() it before the program terminates. - using (var hastlayer = await Hastlayer.Create(hastlayerConfiguration)) + using (var hastlayer = Hastlayer.Create(hastlayerConfiguration)) { // Hooking into an event of Hastlayer so some execution information can be made visible on the // console. @@ -54,10 +54,10 @@ static void Main(string[] args) // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. - var devices = await hastlayer.GetSupportedDevices(); + var devices = hastlayer.GetSupportedDevices(); // Let's just use the first one that is available. However you might want to use a specific // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(devices.First().Name); + var configuration = new HardwareGenerationConfiguration(devices.First().Name, null); // If you're running Hastlayer in the Client flavor, you also need to configure some credentials // here: @@ -75,142 +75,43 @@ static void Main(string[] args) // Letting the configuration of samples run. Check out those methods too! switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - Posit8_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_1_Calculator: - Posit8_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_2_Calculator: - Posit8_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_3_Calculator: - Posit8_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_4_Calculator: - Posit8_4_CalculatorSampleRunner.Configure(configuration); - break; - //case Sample.Posit8_0_AdvancedCalculator: - // Posit8_0_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_1_AdvancedCalculator: - // Posit8_1_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_2_AdvancedCalculator: - // Posit8_2_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_3_AdvancedCalculator: - // Posit8_3_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_4_AdvancedCalculator: - // Posit8_4_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_0_FusedCalculator: - // Posit8_0_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_1_FusedCalculator: - // Posit8_1_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_2_FusedCalculator: - // Posit8_2_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_3_FusedCalculator: - // Posit8_3_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit8_4_FusedCalculator: - // Posit8_4_FusedCalculatorSampleRunner.Configure(configuration); - // break; - case Sample.Posit16_0_Calculator: - Posit16_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_1_Calculator: - Posit16_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_2_Calculator: - Posit16_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_3_Calculator: - Posit16_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_4_Calculator: - Posit16_4_CalculatorSampleRunner.Configure(configuration); - break; - //case Sample.Posit16_0_AdvancedCalculator: - // Posit16_0_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_1_AdvancedCalculator: - // Posit16_1_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_2_AdvancedCalculator: - // Posit16_2_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_3_AdvancedCalculator: - // Posit16_3_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_4_AdvancedCalculator: - // Posit16_4_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_0_FusedCalculator: - // Posit16_0_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_1_FusedCalculator: - // Posit16_1_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_2_FusedCalculator: - // Posit16_2_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_3_FusedCalculator: - // Posit16_3_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit16_4_FusedCalculator: - // Posit16_4_FusedCalculatorSampleRunner.Configure(configuration); - // break; - case Sample.Posit32_0_Calculator: - Posit32_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_1_Calculator: - Posit32_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_2_Calculator: - Posit32_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_3_Calculator: - Posit32_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_4_Calculator: - Posit32_4_CalculatorSampleRunner.Configure(configuration); - break; - //case Sample.Posit32_0_AdvancedCalculator: - // Posit32_0_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_1_AdvancedCalculator: - // Posit32_1_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_2_AdvancedCalculator: - // Posit32_2_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_3_AdvancedCalculator: - // Posit32_3_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_4_AdvancedCalculator: - // Posit32_4_AdvancedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_0_FusedCalculator: - // Posit32_0_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_1_FusedCalculator: - // Posit32_1_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_2_FusedCalculator: - // Posit32_2_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_3_FusedCalculator: - // Posit32_3_FusedCalculatorSampleRunner.Configure(configuration); - // break; - //case Sample.Posit32_4_FusedCalculator: - // Posit32_4_FusedCalculatorSampleRunner.Configure(configuration); - // break; - + case Sample.Posit8_0_Calculator: + Posit8_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_1_Calculator: + Posit8_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_2_Calculator: + Posit8_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_3_Calculator: + Posit8_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_0_Calculator: + Posit16_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_1_Calculator: + Posit16_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_2_Calculator: + Posit16_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_3_Calculator: + Posit16_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_0_Calculator: + Posit32_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_1_Calculator: + Posit32_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_2_Calculator: + Posit32_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_3_Calculator: + Posit32_3_CalculatorSampleRunner.Configure(configuration); + break; + default: break; } @@ -226,7 +127,6 @@ static void Main(string[] args) new[] { typeof(Posit8_0).Assembly, - typeof(Posit8_0_Calculator).Assembly, }, configuration); @@ -248,7 +148,8 @@ static void Main(string[] args) { Console.WriteLine("Writing VHDL source to file."); - await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); + // TODO: Re-enable VHDL source update. + //await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); Console.WriteLine("VHDL source written to file."); Console.WriteLine(); @@ -259,142 +160,43 @@ static void Main(string[] args) // Running samples. switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_1_Calculator: - await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_2_Calculator: - await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_3_Calculator: - await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_4_Calculator: - await Posit8_4_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - //case Sample.Posit8_0_AdvancedCalculator: - // await Posit8_0_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_1_AdvancedCalculator: - // await Posit8_1_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_2_AdvancedCalculator: - // await Posit8_2_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_3_AdvancedCalculator: - // await Posit8_3_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_4_AdvancedCalculator: - // await Posit8_4_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_0_FusedCalculator: - // await Posit8_0_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_1_FusedCalculator: - // await Posit8_1_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_2_FusedCalculator: - // await Posit8_2_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_3_FusedCalculator: - // await Posit8_3_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit8_4_FusedCalculator: - // await Posit8_4_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - case Sample.Posit16_0_Calculator: - await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_1_Calculator: - await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_2_Calculator: - await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_3_Calculator: - await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_4_Calculator: - await Posit16_4_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - //case Sample.Posit16_0_AdvancedCalculator: - // await Posit16_0_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_1_AdvancedCalculator: - // await Posit16_1_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_2_AdvancedCalculator: - // await Posit16_2_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_3_AdvancedCalculator: - // await Posit16_3_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_4_AdvancedCalculator: - // await Posit16_4_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_0_FusedCalculator: - // await Posit16_0_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_1_FusedCalculator: - // await Posit16_1_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_2_FusedCalculator: - // await Posit16_2_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_3_FusedCalculator: - // await Posit16_3_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit16_4_FusedCalculator: - // await Posit16_4_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - case Sample.Posit32_0_Calculator: - await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_1_Calculator: - await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_2_Calculator: - await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_3_Calculator: - await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_4_Calculator: - await Posit32_4_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - //case Sample.Posit32_0_AdvancedCalculator: - // await Posit32_0_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_1_AdvancedCalculator: - // await Posit32_1_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_2_AdvancedCalculator: - // await Posit32_2_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_3_AdvancedCalculator: - // await Posit32_3_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_4_AdvancedCalculator: - // await Posit32_4_AdvancedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_0_FusedCalculator: - // await Posit32_0_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_1_FusedCalculator: - // await Posit32_1_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_2_FusedCalculator: - // await Posit32_2_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_3_FusedCalculator: - // await Posit32_3_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - //case Sample.Posit32_4_FusedCalculator: - // await Posit32_4_FusedCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - // break; - + case Sample.Posit8_0_Calculator: + await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_1_Calculator: + await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_2_Calculator: + await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_3_Calculator: + await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_0_Calculator: + await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_1_Calculator: + await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_2_Calculator: + await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_3_Calculator: + await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_0_Calculator: + await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_1_Calculator: + await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_2_Calculator: + await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_3_Calculator: + await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + default: break; } diff --git a/Hast.Samples.Posit/Program.tt b/Hast.Samples.Posit/Program.tt index 21bdb19d4..9fb67d0b4 100644 --- a/Hast.Samples.Posit/Program.tt +++ b/Hast.Samples.Posit/Program.tt @@ -1,11 +1,14 @@ -<#@ template debug="true" hostSpecific="true" #> +<#@ template debug="true" hostspecific="false" language="C#" #> +<#@ assembly name="System.Core" #> +<#@ import namespace="System.Linq" #> +<#@ import namespace="System.Text" #> +<#@ import namespace="System.Collections.Generic" #> <#@ output extension=".cs" #> - <# - var positSizes = new byte[] {8,16,32,64}; - var calculatorTypes = new string[] {"Calculator", "AdvancedCalculator", "FusedCalculator"}; - -#> + var positSizes = new byte[] { 8, 16, 32 }; + var calculatorTypes = new string[] { "Calculator" }; + var exponentSizes = Enumerable.Range(0, 4); +#> using Hast.Layer; using Hast.Transformer.Vhdl.Abstractions.Configuration; using Lombiq.Arithmetics; @@ -15,152 +18,159 @@ using System.Threading.Tasks; namespace Hast.Samples.Posit { - internal static class Configuration - { - public static string VhdlOutputFilePath = @"Hast_IP.vhd"; - - public static Sample SampleToRun = Sample.Posit32_2_AdvancedCalculator; - } - - class Program - { - static void Main(string[] args) - { - Task.Run(async () => - { - /* - * On a high level these are the steps to use Hastlayer: - * 1. Create the Hastlayer shell. - * 2. Configure hardware generation and generate FPGA hardware representation of the given .NET code. - * 3. Generate proxies for hardware-transformed types and use these proxies to utilize hardware - * implementations. (You can see this inside the SampleRunners.) - */ - - // Configuring the Hastlayer shell. Which flavor should we use? If you're unsure then you'll need - // the Client flavor: This will let you connect to a remote Hastlayer service to run the software - // to hardware transformation. - var hastlayerConfiguration = new HastlayerConfiguration { Flavor = HastlayerFlavor.Developer }; - - // Initializing a Hastlayer shell. Since this is non-trivial to do you can cache this shell object - // while the program runs and re-use it continuously. No need to always wrap it into a using() like - // here, just make sure to Dispose() it before the program terminates. - using (var hastlayer = await Hastlayer.Create(hastlayerConfiguration)) - { - // Hooking into an event of Hastlayer so some execution information can be made visible on the - // console. - hastlayer.ExecutedOnHardware += (sender, e) => - { - Console.WriteLine( - "Executing " + - e.MemberFullName + - " on hardware took " + - e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds + - " milliseconds (net) " + - e.HardwareExecutionInformation.FullExecutionTimeMilliseconds + - " milliseconds (all together)"); - }; - - - // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. - var devices = await hastlayer.GetSupportedDevices(); - // Let's just use the first one that is available. However you might want to use a specific - // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(devices.First().Name); - - // If you're running Hastlayer in the Client flavor, you also need to configure some credentials - // here: - var remoteClientConfiguration = configuration.RemoteClientConfiguration(); - remoteClientConfiguration.AppName = "TestApp"; - remoteClientConfiguration.AppSecret = "appsecret"; - if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && - remoteClientConfiguration.AppSecret == "appsecret") - { - throw new InvalidOperationException( - "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); - } - - - // Letting the configuration of samples run. Check out those methods too! - switch (Configuration.SampleToRun) - { - <#for (var positSize = 0; positSize<=2; positSize++){ - for (var type = 0; type<=2; type++){ - for (var exponentSize = 0; exponentSize<=4; exponentSize++){#> - <# var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; #> - case Sample.<#=positCalculatorName#>: - <#=positCalculatorName#>SampleRunner.Configure(configuration); - break; - <# } - } - } #> - default: - break; - } - - // The generated VHDL code will contain debug-level information, though it will be slower to - // create. - configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; - - Console.WriteLine("Hardware generation starts."); - - // Generating hardware from the sample assembly with the given configuration. - var hardwareRepresentation = await hastlayer.GenerateHardware( - new[] - { - typeof(Posit8_0).Assembly, - }, - configuration); - - Console.WriteLine("Hardware generation finished."); - Console.WriteLine(); - - // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't - // cause any problems, but sometimes they can. - if (hardwareRepresentation.HardwareDescription.Warnings.Any()) - { - Console.WriteLine( - "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + - Environment.NewLine + - string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); - Console.WriteLine(); - } - - if (!string.IsNullOrEmpty(Configuration.VhdlOutputFilePath)) - { - Console.WriteLine("Writing VHDL source to file."); - - await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); - - Console.WriteLine("VHDL source written to file."); - Console.WriteLine(); - } - - Console.WriteLine("Starting hardware execution."); - - // Running samples. - switch (Configuration.SampleToRun) - { - <#for (var positSize = 0; positSize<=2; positSize++){ - for (var type = 0; type<=2; type++){ - for (var exponentSize = 0; exponentSize<=4; exponentSize++){#> - <# var positCalculatorName = "Posit" + positSizes[positSize] + "_" + exponentSize + "_" + calculatorTypes[type]; #> - case Sample.<#=positCalculatorName#>: - await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); - break; - <# } - } - } #> - default: - break; - } - } - }).Wait(); - - Console.WriteLine("Press any key to exit."); - Console.ReadKey(); - } - } + internal static class Configuration + { + public static string VhdlOutputFilePath = @"Hast_IP.vhd"; + + public static Sample SampleToRun = Sample.Posit32_0_Calculator; + } + + class Program + { + static void Main(string[] args) + { + Task.Run(async () => + { + /* + * On a high level these are the steps to use Hastlayer: + * 1. Create the Hastlayer shell. + * 2. Configure hardware generation and generate FPGA hardware representation of the given .NET code. + * 3. Generate proxies for hardware-transformed types and use these proxies to utilize hardware + * implementations. (You can see this inside the SampleRunners.) + */ + + // Configuring the Hastlayer shell. Which flavor should we use? If you're unsure then you'll need + // the Client flavor: This will let you connect to a remote Hastlayer service to run the software + // to hardware transformation. + var hastlayerConfiguration = new HastlayerConfiguration { Flavor = HastlayerFlavor.Developer }; + + // Initializing a Hastlayer shell. Since this is non-trivial to do you can cache this shell object + // while the program runs and re-use it continuously. No need to always wrap it into a using() like + // here, just make sure to Dispose() it before the program terminates. + using (var hastlayer = Hastlayer.Create(hastlayerConfiguration)) + { + // Hooking into an event of Hastlayer so some execution information can be made visible on the + // console. + hastlayer.ExecutedOnHardware += (sender, e) => + { + Console.WriteLine( + "Executing " + + e.MemberFullName + + " on hardware took " + + e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds + + " milliseconds (net) " + + e.HardwareExecutionInformation.FullExecutionTimeMilliseconds + + " milliseconds (all together)"); + }; + + + // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. + var devices = hastlayer.GetSupportedDevices(); + // Let's just use the first one that is available. However you might want to use a specific + // device, not just any first one. + var configuration = new HardwareGenerationConfiguration(devices.First().Name, null); + + // If you're running Hastlayer in the Client flavor, you also need to configure some credentials + // here: + var remoteClientConfiguration = configuration.RemoteClientConfiguration(); + remoteClientConfiguration.AppName = "TestApp"; + remoteClientConfiguration.AppSecret = "appsecret"; + if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && + remoteClientConfiguration.AppSecret == "appsecret") + { + throw new InvalidOperationException( + "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); + } + + + // Letting the configuration of samples run. Check out those methods too! + switch (Configuration.SampleToRun) + { + <# foreach (var positSize in positSizes) + { + foreach (var exponentSize in exponentSizes) + { + foreach (var calculatorType in calculatorTypes) + { + var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> + case Sample.<#=positCalculatorName#>: + <#=positCalculatorName#>SampleRunner.Configure(configuration); + break; + <#} + } + }#> + default: + break; + } + + // The generated VHDL code will contain debug-level information, though it will be slower to + // create. + configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; + + Console.WriteLine("Hardware generation starts."); + + // Generating hardware from the sample assembly with the given configuration. + var hardwareRepresentation = await hastlayer.GenerateHardware( + new[] + { + typeof(Posit8_0).Assembly, + }, + configuration); + + Console.WriteLine("Hardware generation finished."); + Console.WriteLine(); + + // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't + // cause any problems, but sometimes they can. + if (hardwareRepresentation.HardwareDescription.Warnings.Any()) + { + Console.WriteLine( + "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + + Environment.NewLine + + string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); + Console.WriteLine(); + } + + if (!string.IsNullOrEmpty(Configuration.VhdlOutputFilePath)) + { + Console.WriteLine("Writing VHDL source to file."); + + // TODO: Re-enable VHDL source update. + //await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); + + Console.WriteLine("VHDL source written to file."); + Console.WriteLine(); + } + + Console.WriteLine("Starting hardware execution."); + + // Running samples. + switch (Configuration.SampleToRun) + { + <# foreach (var positSize in positSizes) + { + foreach (var exponentSize in exponentSizes) + { + foreach (var calculatorType in calculatorTypes) + { + var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> + case Sample.<#=positCalculatorName#>: + await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); + break; + <#} + } + }#> + default: + break; + } + } + }).Wait(); + + Console.WriteLine("Press any key to exit."); + Console.ReadKey(); + } + } } <# -#> \ No newline at end of file +#> From 1011d981ed67dd5406df4fa1aab3a2cc5703e0c3 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 21 Mar 2021 17:35:06 +0100 Subject: [PATCH 22/43] Reverting temporary changes in PositCalculatorSampleRunner.tt and fixing RunSoftwareBenchmarks function signature --- .../Posit16_0_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit16_1_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit16_2_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit16_3_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit16_4_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit32_0_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit32_1_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit32_2_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit32_3_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit32_4_CalculatorSampleRunner.cs | 26 ++++++++--------- .../Posit8_0_CalculatorSampleRunner.cs | 18 ++++++------ .../Posit8_1_CalculatorSampleRunner.cs | 18 ++++++------ .../Posit8_2_CalculatorSampleRunner.cs | 18 ++++++------ .../Posit8_3_CalculatorSampleRunner.cs | 18 ++++++------ .../Posit8_4_CalculatorSampleRunner.cs | 18 ++++++------ .../PositCalculatorSampleRunner.tt | 28 +++++++++---------- 16 files changed, 189 insertions(+), 189 deletions(-) diff --git a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs index 7cb9ea353..5709d3c7c 100644 --- a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_0_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_0Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit16_0Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit16_0_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit16_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs index 145e92fc2..72f9bd9dc 100644 --- a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_1_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_1Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit16_1Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_1Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit16_1_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit16_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs index c64579036..5d9d113a5 100644 --- a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_2_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_2Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit16_2Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_2Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit16_2_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit16_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs index 52aecbece..0a359480d 100644 --- a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_3_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_3Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit16_3Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_3Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit16_3_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit16_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs index e1c72fb4e..72f6ade44 100644 --- a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_4_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit16_4Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit16_4Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16_4Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit16_4_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 10000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 10000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit16_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit16_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs index 873317467..144b51062 100644 --- a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_0_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_0Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit32_0Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_0Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit32_0_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit32_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs index fa4dd6dae..e632226dd 100644 --- a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_1_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_1Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit32_1Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_1Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit32_1_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit32_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs index 46ba1cf4c..4e3d47488 100644 --- a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_2_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_2Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit32_2Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_2Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit32_2_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit32_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs index 37c3d1100..38a4d6265 100644 --- a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_3_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_3Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit32_3Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_3Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit32_3_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit32_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs index a0e3fa890..12bac0c66 100644 --- a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_4_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -34,7 +34,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit32_4Array = new uint[100000]; @@ -45,10 +45,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit32_4Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32_4Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit32_4_Calculator(); @@ -56,7 +56,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -70,11 +70,11 @@ public static void RunSoftwareBenchmarks() positCalculator.CalculatePowerOfReal(100000, (float)1.0001, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( - 100000, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + 100000, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); @@ -91,7 +91,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -112,7 +112,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit32_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit32_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs index e17abdd4c..589adec0e 100644 --- a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_0_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_0Array = new uint[100000]; @@ -46,10 +46,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit8_0Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_0Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit8_0_Calculator(); @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit8_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs index 7aa190baa..9b862156f 100644 --- a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_1_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_1Array = new uint[100000]; @@ -46,10 +46,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit8_1Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_1Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit8_1_Calculator(); @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit8_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs index 8dff7b882..39f25ff98 100644 --- a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_2_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_2Array = new uint[100000]; @@ -46,10 +46,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit8_2Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_2Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit8_2_Calculator(); @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit8_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs index b9cc7253e..72aa95907 100644 --- a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_3_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_3Array = new uint[100000]; @@ -46,10 +46,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit8_3Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_3Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit8_3_Calculator(); @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit8_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs index 2b8c6547b..8d5909442 100644 --- a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs @@ -19,12 +19,12 @@ public static void Configure(HardwareGenerationConfiguration configuration) public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_4_Calculator()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 5, (float)0.5); @@ -35,7 +35,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var Posit8_4Array = new uint[100000]; @@ -46,10 +46,10 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(Posit8_4Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8_4Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new Posit8_4_Calculator(); @@ -57,7 +57,7 @@ public static void RunSoftwareBenchmarks() // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -72,7 +72,7 @@ public static void RunSoftwareBenchmarks() sw = Stopwatch.StartNew(); - positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); sw.Stop(); @@ -89,7 +89,7 @@ public static void RunSoftwareBenchmarks() positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -110,7 +110,7 @@ public static void RunSoftwareBenchmarks() positCalculator.AddPositsInArray( Posit8_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( Posit8_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt index 7fbc10a99..65eb419d5 100644 --- a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt @@ -57,12 +57,12 @@ namespace Hast.Samples.Posit public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - RunSoftwareBenchmarks(); + RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new <#=calculatorName#>()); - positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); <# if (i == 0) { @@ -79,7 +79,7 @@ namespace Hast.Samples.Posit numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); var <#=positName#>Array = new uint[100000]; @@ -90,10 +90,10 @@ namespace Hast.Samples.Posit else <#=positName#>Array[i] = new <#=positName#>((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray(<#=positName#>Array); + var positsInArraySum = positCalculator.AddPositsInArray(<#=positName#>Array); } - public static void RunSoftwareBenchmarks() + public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { var positCalculator = new <#=calculatorName#>(); @@ -101,7 +101,7 @@ namespace Hast.Samples.Posit // Not to run the benchmark below the first time, because JIT compiling can affect it. positCalculator.CalculateIntegerSumUpToNumber(100000); var sw = Stopwatch.StartNew(); - positCalculator.CalculateIntegerSumUpToNumber( + var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber( 100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -119,13 +119,13 @@ namespace Hast.Samples.Posit { #> - positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); + var powerOfReal = positCalculator.CalculatePowerOfReal( 5, (float)0.5, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration ); <# } else { #> - positCalculator.CalculatePowerOfReal( - <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, - (float)1.015625, - hastlayer, - hardwareRepresentation.HardwareGenerationConfiguration); + var powerOfReal = positCalculator.CalculatePowerOfReal( + <#=(int) Math.Pow(10,Math.Log(positSizes[i], 2)) #>, + (float)1.015625, + hastlayer, + hardwareRepresentation.HardwareGenerationConfiguration); <# } #> sw.Stop(); @@ -143,7 +143,7 @@ namespace Hast.Samples.Posit positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( + var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers( numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -164,7 +164,7 @@ namespace Hast.Samples.Posit positCalculator.AddPositsInArray( <#=positName#>Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - positCalculator.AddPositsInArray( <#=positName#>Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( <#=positName#>Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); From 7f51ec1a5671b3beb07851d9ccadba2b660b3d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Mon, 22 Mar 2021 02:50:46 +0100 Subject: [PATCH 23/43] Fixing Posit sample hardware generation (just in the generated code) --- Hast.Samples.Posit/Program.cs | 165 ++++++++++++++++------------------ 1 file changed, 77 insertions(+), 88 deletions(-) diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index a7116cc45..7643c07a3 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -9,9 +9,8 @@ namespace Hast.Samples.Posit { internal static class Configuration { - public static string VhdlOutputFilePath = @"Hast_IP.vhd"; - public static Sample SampleToRun = Sample.Posit32_0_Calculator; + public static string HardwareFrameworkPath = "HardwareFramework"; } class Program @@ -57,7 +56,7 @@ static void Main(string[] args) var devices = hastlayer.GetSupportedDevices(); // Let's just use the first one that is available. However you might want to use a specific // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(devices.First().Name, null); + var configuration = new HardwareGenerationConfiguration(devices.First().Name, Configuration.HardwareFrameworkPath); // If you're running Hastlayer in the Client flavor, you also need to configure some credentials // here: @@ -75,43 +74,43 @@ static void Main(string[] args) // Letting the configuration of samples run. Check out those methods too! switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - Posit8_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_1_Calculator: - Posit8_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_2_Calculator: - Posit8_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_3_Calculator: - Posit8_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_0_Calculator: - Posit16_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_1_Calculator: - Posit16_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_2_Calculator: - Posit16_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_3_Calculator: - Posit16_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_0_Calculator: - Posit32_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_1_Calculator: - Posit32_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_2_Calculator: - Posit32_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_3_Calculator: - Posit32_3_CalculatorSampleRunner.Configure(configuration); - break; - + case Sample.Posit8_0_Calculator: + Posit8_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_1_Calculator: + Posit8_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_2_Calculator: + Posit8_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_3_Calculator: + Posit8_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_0_Calculator: + Posit16_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_1_Calculator: + Posit16_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_2_Calculator: + Posit16_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_3_Calculator: + Posit16_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_0_Calculator: + Posit32_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_1_Calculator: + Posit32_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_2_Calculator: + Posit32_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_3_Calculator: + Posit32_3_CalculatorSampleRunner.Configure(configuration); + break; + default: break; } @@ -127,6 +126,7 @@ static void Main(string[] args) new[] { typeof(Posit8_0).Assembly, + typeof(Posit8_0_Calculator).Assembly, }, configuration); @@ -144,59 +144,48 @@ static void Main(string[] args) Console.WriteLine(); } - if (!string.IsNullOrEmpty(Configuration.VhdlOutputFilePath)) - { - Console.WriteLine("Writing VHDL source to file."); - - // TODO: Re-enable VHDL source update. - //await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); - - Console.WriteLine("VHDL source written to file."); - Console.WriteLine(); - } - Console.WriteLine("Starting hardware execution."); // Running samples. switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_1_Calculator: - await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_2_Calculator: - await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_3_Calculator: - await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_0_Calculator: - await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_1_Calculator: - await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_2_Calculator: - await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_3_Calculator: - await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_0_Calculator: - await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_1_Calculator: - await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_2_Calculator: - await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_3_Calculator: - await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - + case Sample.Posit8_0_Calculator: + await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_1_Calculator: + await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_2_Calculator: + await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_3_Calculator: + await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_0_Calculator: + await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_1_Calculator: + await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_2_Calculator: + await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_3_Calculator: + await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_0_Calculator: + await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_1_Calculator: + await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_2_Calculator: + await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_3_Calculator: + await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + default: break; } From 8ee540163ecfe8a65dca292cb5b3910c51f81925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Mon, 22 Mar 2021 02:54:46 +0100 Subject: [PATCH 24/43] Adding device name config for Posit samples --- Hast.Samples.Posit/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index 7643c07a3..1925a3335 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -9,6 +9,7 @@ namespace Hast.Samples.Posit { internal static class Configuration { + public static string DeviceName = "Nexys A7"; public static Sample SampleToRun = Sample.Posit32_0_Calculator; public static string HardwareFrameworkPath = "HardwareFramework"; } @@ -56,7 +57,7 @@ static void Main(string[] args) var devices = hastlayer.GetSupportedDevices(); // Let's just use the first one that is available. However you might want to use a specific // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(devices.First().Name, Configuration.HardwareFrameworkPath); + var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); // If you're running Hastlayer in the Client flavor, you also need to configure some credentials // here: From f65646aaf6871ca7c2de4f30bd51ea05da356317 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 22 Mar 2021 10:38:52 +0100 Subject: [PATCH 25/43] Updating Program.tt and compiling it to adapt to Hastlayer API changes --- Hast.Samples.Posit/Program.cs | 148 +++++++++++++++++----------------- Hast.Samples.Posit/Program.tt | 18 +---- 2 files changed, 78 insertions(+), 88 deletions(-) diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index 1925a3335..1095c00c5 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -75,43 +75,43 @@ static void Main(string[] args) // Letting the configuration of samples run. Check out those methods too! switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - Posit8_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_1_Calculator: - Posit8_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_2_Calculator: - Posit8_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_3_Calculator: - Posit8_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_0_Calculator: - Posit16_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_1_Calculator: - Posit16_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_2_Calculator: - Posit16_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_3_Calculator: - Posit16_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_0_Calculator: - Posit32_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_1_Calculator: - Posit32_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_2_Calculator: - Posit32_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_3_Calculator: - Posit32_3_CalculatorSampleRunner.Configure(configuration); - break; - + case Sample.Posit8_0_Calculator: + Posit8_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_1_Calculator: + Posit8_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_2_Calculator: + Posit8_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_3_Calculator: + Posit8_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_0_Calculator: + Posit16_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_1_Calculator: + Posit16_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_2_Calculator: + Posit16_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_3_Calculator: + Posit16_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_0_Calculator: + Posit32_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_1_Calculator: + Posit32_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_2_Calculator: + Posit32_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_3_Calculator: + Posit32_3_CalculatorSampleRunner.Configure(configuration); + break; + default: break; } @@ -150,43 +150,43 @@ static void Main(string[] args) // Running samples. switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_1_Calculator: - await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_2_Calculator: - await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_3_Calculator: - await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_0_Calculator: - await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_1_Calculator: - await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_2_Calculator: - await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_3_Calculator: - await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_0_Calculator: - await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_1_Calculator: - await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_2_Calculator: - await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_3_Calculator: - await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - + case Sample.Posit8_0_Calculator: + await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_1_Calculator: + await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_2_Calculator: + await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_3_Calculator: + await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_0_Calculator: + await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_1_Calculator: + await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_2_Calculator: + await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_3_Calculator: + await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_0_Calculator: + await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_1_Calculator: + await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_2_Calculator: + await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_3_Calculator: + await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + default: break; } diff --git a/Hast.Samples.Posit/Program.tt b/Hast.Samples.Posit/Program.tt index 9fb67d0b4..c61f5cb0f 100644 --- a/Hast.Samples.Posit/Program.tt +++ b/Hast.Samples.Posit/Program.tt @@ -20,9 +20,9 @@ namespace Hast.Samples.Posit { internal static class Configuration { - public static string VhdlOutputFilePath = @"Hast_IP.vhd"; - + public static string DeviceName = "Nexys A7"; public static Sample SampleToRun = Sample.Posit32_0_Calculator; + public static string HardwareFrameworkPath = "HardwareFramework"; } class Program @@ -68,7 +68,7 @@ namespace Hast.Samples.Posit var devices = hastlayer.GetSupportedDevices(); // Let's just use the first one that is available. However you might want to use a specific // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(devices.First().Name, null); + var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); // If you're running Hastlayer in the Client flavor, you also need to configure some credentials // here: @@ -114,6 +114,7 @@ namespace Hast.Samples.Posit new[] { typeof(Posit8_0).Assembly, + typeof(Posit8_0_Calculator).Assembly, }, configuration); @@ -131,17 +132,6 @@ namespace Hast.Samples.Posit Console.WriteLine(); } - if (!string.IsNullOrEmpty(Configuration.VhdlOutputFilePath)) - { - Console.WriteLine("Writing VHDL source to file."); - - // TODO: Re-enable VHDL source update. - //await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath); - - Console.WriteLine("VHDL source written to file."); - Console.WriteLine(); - } - Console.WriteLine("Starting hardware execution."); // Running samples. From 1f9185e170f50fcff3b00d47711a406b7bd81b92 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 22 Mar 2021 10:50:11 +0100 Subject: [PATCH 26/43] Updating Program.tt to correct code formatting in Program.cs --- Hast.Samples.Posit/Program.cs | 146 +++++++++++++++++----------------- Hast.Samples.Posit/Program.tt | 52 ++++++------ 2 files changed, 98 insertions(+), 100 deletions(-) diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index 1095c00c5..66d00aab7 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -75,43 +75,42 @@ static void Main(string[] args) // Letting the configuration of samples run. Check out those methods too! switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - Posit8_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_1_Calculator: - Posit8_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_2_Calculator: - Posit8_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_3_Calculator: - Posit8_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_0_Calculator: - Posit16_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_1_Calculator: - Posit16_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_2_Calculator: - Posit16_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_3_Calculator: - Posit16_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_0_Calculator: - Posit32_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_1_Calculator: - Posit32_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_2_Calculator: - Posit32_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_3_Calculator: - Posit32_3_CalculatorSampleRunner.Configure(configuration); - break; - + case Sample.Posit8_0_Calculator: + Posit8_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_1_Calculator: + Posit8_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_2_Calculator: + Posit8_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_3_Calculator: + Posit8_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_0_Calculator: + Posit16_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_1_Calculator: + Posit16_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_2_Calculator: + Posit16_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_3_Calculator: + Posit16_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_0_Calculator: + Posit32_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_1_Calculator: + Posit32_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_2_Calculator: + Posit32_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_3_Calculator: + Posit32_3_CalculatorSampleRunner.Configure(configuration); + break; default: break; } @@ -150,43 +149,42 @@ static void Main(string[] args) // Running samples. switch (Configuration.SampleToRun) { - case Sample.Posit8_0_Calculator: - await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_1_Calculator: - await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_2_Calculator: - await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_3_Calculator: - await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_0_Calculator: - await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_1_Calculator: - await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_2_Calculator: - await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_3_Calculator: - await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_0_Calculator: - await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_1_Calculator: - await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_2_Calculator: - await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_3_Calculator: - await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - + case Sample.Posit8_0_Calculator: + await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_1_Calculator: + await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_2_Calculator: + await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_3_Calculator: + await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_0_Calculator: + await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_1_Calculator: + await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_2_Calculator: + await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_3_Calculator: + await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_0_Calculator: + await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_1_Calculator: + await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_2_Calculator: + await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_3_Calculator: + await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; default: break; } diff --git a/Hast.Samples.Posit/Program.tt b/Hast.Samples.Posit/Program.tt index c61f5cb0f..19c08ca88 100644 --- a/Hast.Samples.Posit/Program.tt +++ b/Hast.Samples.Posit/Program.tt @@ -86,19 +86,19 @@ namespace Hast.Samples.Posit // Letting the configuration of samples run. Check out those methods too! switch (Configuration.SampleToRun) { - <# foreach (var positSize in positSizes) - { - foreach (var exponentSize in exponentSizes) - { - foreach (var calculatorType in calculatorTypes) - { - var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> - case Sample.<#=positCalculatorName#>: - <#=positCalculatorName#>SampleRunner.Configure(configuration); - break; - <#} - } - }#> +<#foreach (var positSize in positSizes) +{ + foreach (var exponentSize in exponentSizes) + { + foreach (var calculatorType in calculatorTypes) + { + var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> + case Sample.<#=positCalculatorName#>: + <#=positCalculatorName#>SampleRunner.Configure(configuration); + break; +<# } + } +}#> default: break; } @@ -137,19 +137,19 @@ namespace Hast.Samples.Posit // Running samples. switch (Configuration.SampleToRun) { - <# foreach (var positSize in positSizes) - { - foreach (var exponentSize in exponentSizes) - { - foreach (var calculatorType in calculatorTypes) - { - var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> - case Sample.<#=positCalculatorName#>: - await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); - break; - <#} - } - }#> +<#foreach (var positSize in positSizes) +{ + foreach (var exponentSize in exponentSizes) + { + foreach (var calculatorType in calculatorTypes) + { + var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> + case Sample.<#=positCalculatorName#>: + await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); + break; +<# } + } +}#> default: break; } From 0dd0246673dfc80a99af0dae3561b5a434dbd4f4 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 22 Mar 2021 10:57:45 +0100 Subject: [PATCH 27/43] Fixing code analysis warnings in Program --- Hast.Samples.Posit/Program.cs | 286 +++++++++++++++++----------------- Hast.Samples.Posit/Program.tt | 154 +++++++++--------- 2 files changed, 212 insertions(+), 228 deletions(-) diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index 66d00aab7..c6ccfb3d2 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -14,9 +14,9 @@ internal static class Configuration public static string HardwareFrameworkPath = "HardwareFramework"; } - class Program + internal class Program { - static void Main(string[] args) + private static void Main() { Task.Run(async () => { @@ -36,158 +36,150 @@ static void Main(string[] args) // Initializing a Hastlayer shell. Since this is non-trivial to do you can cache this shell object // while the program runs and re-use it continuously. No need to always wrap it into a using() like // here, just make sure to Dispose() it before the program terminates. - using (var hastlayer = Hastlayer.Create(hastlayerConfiguration)) + using var hastlayer = Hastlayer.Create(hastlayerConfiguration); + + // Hooking into an event of Hastlayer so some execution information can be made visible on the + // console. + hastlayer.ExecutedOnHardware += (sender, e) => + Console.WriteLine(@$"Executing {e.MemberFullName} on hardware took +{e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds} milliseconds (net) +{e.HardwareExecutionInformation.FullExecutionTimeMilliseconds} milliseconds (all together)."); + + + // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. + var devices = hastlayer.GetSupportedDevices(); + // Let's just use the first one that is available. However you might want to use a specific + // device, not just any first one. + var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); + + // If you're running Hastlayer in the Client flavor, you also need to configure some credentials + // here: + var remoteClientConfiguration = configuration.RemoteClientConfiguration(); + remoteClientConfiguration.AppName = "TestApp"; + remoteClientConfiguration.AppSecret = "appsecret"; + if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && + remoteClientConfiguration.AppSecret == "appsecret") { - // Hooking into an event of Hastlayer so some execution information can be made visible on the - // console. - hastlayer.ExecutedOnHardware += (sender, e) => - { - Console.WriteLine( - "Executing " + - e.MemberFullName + - " on hardware took " + - e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds + - " milliseconds (net) " + - e.HardwareExecutionInformation.FullExecutionTimeMilliseconds + - " milliseconds (all together)"); - }; - - - // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. - var devices = hastlayer.GetSupportedDevices(); - // Let's just use the first one that is available. However you might want to use a specific - // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); - - // If you're running Hastlayer in the Client flavor, you also need to configure some credentials - // here: - var remoteClientConfiguration = configuration.RemoteClientConfiguration(); - remoteClientConfiguration.AppName = "TestApp"; - remoteClientConfiguration.AppSecret = "appsecret"; - if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && - remoteClientConfiguration.AppSecret == "appsecret") - { - throw new InvalidOperationException( - "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); - } + throw new InvalidOperationException( + "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); + } - // Letting the configuration of samples run. Check out those methods too! - switch (Configuration.SampleToRun) - { - case Sample.Posit8_0_Calculator: - Posit8_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_1_Calculator: - Posit8_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_2_Calculator: - Posit8_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit8_3_Calculator: - Posit8_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_0_Calculator: - Posit16_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_1_Calculator: - Posit16_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_2_Calculator: - Posit16_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit16_3_Calculator: - Posit16_3_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_0_Calculator: - Posit32_0_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_1_Calculator: - Posit32_1_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_2_Calculator: - Posit32_2_CalculatorSampleRunner.Configure(configuration); - break; - case Sample.Posit32_3_Calculator: - Posit32_3_CalculatorSampleRunner.Configure(configuration); - break; - default: - break; - } - - // The generated VHDL code will contain debug-level information, though it will be slower to - // create. - configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; - - Console.WriteLine("Hardware generation starts."); - - // Generating hardware from the sample assembly with the given configuration. - var hardwareRepresentation = await hastlayer.GenerateHardware( - new[] - { - typeof(Posit8_0).Assembly, - typeof(Posit8_0_Calculator).Assembly, - }, - configuration); - - Console.WriteLine("Hardware generation finished."); - Console.WriteLine(); + // Letting the configuration of samples run. Check out those methods too! + switch (Configuration.SampleToRun) + { + case Sample.Posit8_0_Calculator: + Posit8_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_1_Calculator: + Posit8_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_2_Calculator: + Posit8_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit8_3_Calculator: + Posit8_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_0_Calculator: + Posit16_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_1_Calculator: + Posit16_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_2_Calculator: + Posit16_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit16_3_Calculator: + Posit16_3_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_0_Calculator: + Posit32_0_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_1_Calculator: + Posit32_1_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_2_Calculator: + Posit32_2_CalculatorSampleRunner.Configure(configuration); + break; + case Sample.Posit32_3_Calculator: + Posit32_3_CalculatorSampleRunner.Configure(configuration); + break; + default: + break; + } - // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't - // cause any problems, but sometimes they can. - if (hardwareRepresentation.HardwareDescription.Warnings.Any()) - { - Console.WriteLine( - "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + - Environment.NewLine + - string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); - Console.WriteLine(); - } + // The generated VHDL code will contain debug-level information, though it will be slower to + // create. + configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; - Console.WriteLine("Starting hardware execution."); + Console.WriteLine("Hardware generation starts."); - // Running samples. - switch (Configuration.SampleToRun) + // Generating hardware from the sample assembly with the given configuration. + var hardwareRepresentation = await hastlayer.GenerateHardware( + new[] { - case Sample.Posit8_0_Calculator: - await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_1_Calculator: - await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_2_Calculator: - await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit8_3_Calculator: - await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_0_Calculator: - await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_1_Calculator: - await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_2_Calculator: - await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit16_3_Calculator: - await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_0_Calculator: - await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_1_Calculator: - await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_2_Calculator: - await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - case Sample.Posit32_3_Calculator: - await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); - break; - default: - break; - } + typeof(Posit8_0).Assembly, + typeof(Posit8_0_Calculator).Assembly, + }, + configuration); + + Console.WriteLine("Hardware generation finished."); + Console.WriteLine(); + + // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't + // cause any problems, but sometimes they can. + if (hardwareRepresentation.HardwareDescription.Warnings.Any()) + { + Console.WriteLine( + "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + + Environment.NewLine + + string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); + Console.WriteLine(); + } + + Console.WriteLine("Starting hardware execution."); + + // Running samples. + switch (Configuration.SampleToRun) + { + case Sample.Posit8_0_Calculator: + await Posit8_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_1_Calculator: + await Posit8_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_2_Calculator: + await Posit8_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit8_3_Calculator: + await Posit8_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_0_Calculator: + await Posit16_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_1_Calculator: + await Posit16_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_2_Calculator: + await Posit16_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit16_3_Calculator: + await Posit16_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_0_Calculator: + await Posit32_0_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_1_Calculator: + await Posit32_1_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_2_Calculator: + await Posit32_2_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + case Sample.Posit32_3_Calculator: + await Posit32_3_CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation); + break; + default: + break; } }).Wait(); diff --git a/Hast.Samples.Posit/Program.tt b/Hast.Samples.Posit/Program.tt index 19c08ca88..2af9cd7e9 100644 --- a/Hast.Samples.Posit/Program.tt +++ b/Hast.Samples.Posit/Program.tt @@ -25,9 +25,9 @@ namespace Hast.Samples.Posit public static string HardwareFrameworkPath = "HardwareFramework"; } - class Program + internal class Program { - static void Main(string[] args) + private static void Main() { Task.Run(async () => { @@ -47,45 +47,38 @@ namespace Hast.Samples.Posit // Initializing a Hastlayer shell. Since this is non-trivial to do you can cache this shell object // while the program runs and re-use it continuously. No need to always wrap it into a using() like // here, just make sure to Dispose() it before the program terminates. - using (var hastlayer = Hastlayer.Create(hastlayerConfiguration)) + using var hastlayer = Hastlayer.Create(hastlayerConfiguration); + + // Hooking into an event of Hastlayer so some execution information can be made visible on the + // console. + hastlayer.ExecutedOnHardware += (sender, e) => + Console.WriteLine(@$"Executing {e.MemberFullName} on hardware took +{e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds} milliseconds (net) +{e.HardwareExecutionInformation.FullExecutionTimeMilliseconds} milliseconds (all together)."); + + + // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. + var devices = hastlayer.GetSupportedDevices(); + // Let's just use the first one that is available. However you might want to use a specific + // device, not just any first one. + var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); + + // If you're running Hastlayer in the Client flavor, you also need to configure some credentials + // here: + var remoteClientConfiguration = configuration.RemoteClientConfiguration(); + remoteClientConfiguration.AppName = "TestApp"; + remoteClientConfiguration.AppSecret = "appsecret"; + if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && + remoteClientConfiguration.AppSecret == "appsecret") { - // Hooking into an event of Hastlayer so some execution information can be made visible on the - // console. - hastlayer.ExecutedOnHardware += (sender, e) => - { - Console.WriteLine( - "Executing " + - e.MemberFullName + - " on hardware took " + - e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds + - " milliseconds (net) " + - e.HardwareExecutionInformation.FullExecutionTimeMilliseconds + - " milliseconds (all together)"); - }; - - - // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. - var devices = hastlayer.GetSupportedDevices(); - // Let's just use the first one that is available. However you might want to use a specific - // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); - - // If you're running Hastlayer in the Client flavor, you also need to configure some credentials - // here: - var remoteClientConfiguration = configuration.RemoteClientConfiguration(); - remoteClientConfiguration.AppName = "TestApp"; - remoteClientConfiguration.AppSecret = "appsecret"; - if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && - remoteClientConfiguration.AppSecret == "appsecret") - { - throw new InvalidOperationException( - "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); - } + throw new InvalidOperationException( + "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); + } - // Letting the configuration of samples run. Check out those methods too! - switch (Configuration.SampleToRun) - { + // Letting the configuration of samples run. Check out those methods too! + switch (Configuration.SampleToRun) + { <#foreach (var positSize in positSizes) { foreach (var exponentSize in exponentSizes) @@ -93,50 +86,50 @@ namespace Hast.Samples.Posit foreach (var calculatorType in calculatorTypes) { var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> - case Sample.<#=positCalculatorName#>: - <#=positCalculatorName#>SampleRunner.Configure(configuration); - break; + case Sample.<#=positCalculatorName#>: + <#=positCalculatorName#>SampleRunner.Configure(configuration); + break; <# } } }#> - default: - break; - } - - // The generated VHDL code will contain debug-level information, though it will be slower to - // create. - configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; - - Console.WriteLine("Hardware generation starts."); - - // Generating hardware from the sample assembly with the given configuration. - var hardwareRepresentation = await hastlayer.GenerateHardware( - new[] - { - typeof(Posit8_0).Assembly, - typeof(Posit8_0_Calculator).Assembly, - }, - configuration); - - Console.WriteLine("Hardware generation finished."); - Console.WriteLine(); + default: + break; + } - // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't - // cause any problems, but sometimes they can. - if (hardwareRepresentation.HardwareDescription.Warnings.Any()) - { - Console.WriteLine( - "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + - Environment.NewLine + - string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); - Console.WriteLine(); - } + // The generated VHDL code will contain debug-level information, though it will be slower to + // create. + configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; - Console.WriteLine("Starting hardware execution."); + Console.WriteLine("Hardware generation starts."); - // Running samples. - switch (Configuration.SampleToRun) + // Generating hardware from the sample assembly with the given configuration. + var hardwareRepresentation = await hastlayer.GenerateHardware( + new[] { + typeof(Posit8_0).Assembly, + typeof(Posit8_0_Calculator).Assembly, + }, + configuration); + + Console.WriteLine("Hardware generation finished."); + Console.WriteLine(); + + // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't + // cause any problems, but sometimes they can. + if (hardwareRepresentation.HardwareDescription.Warnings.Any()) + { + Console.WriteLine( + "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + + Environment.NewLine + + string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); + Console.WriteLine(); + } + + Console.WriteLine("Starting hardware execution."); + + // Running samples. + switch (Configuration.SampleToRun) + { <#foreach (var positSize in positSizes) { foreach (var exponentSize in exponentSizes) @@ -144,15 +137,14 @@ namespace Hast.Samples.Posit foreach (var calculatorType in calculatorTypes) { var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> - case Sample.<#=positCalculatorName#>: - await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); - break; + case Sample.<#=positCalculatorName#>: + await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); + break; <# } } }#> - default: - break; - } + default: + break; } }).Wait(); From 91f2695a270e1bd471b727bffe8704af876b7116 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 22 Mar 2021 11:19:20 +0100 Subject: [PATCH 28/43] Code styling in Program --- Hast.Samples.Posit/Program.cs | 24 ++++++++++++++---------- Hast.Samples.Posit/Program.tt | 24 ++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index c6ccfb3d2..987daad9b 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -50,7 +50,9 @@ private static void Main() var devices = hastlayer.GetSupportedDevices(); // Let's just use the first one that is available. However you might want to use a specific // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); + var configuration = new HardwareGenerationConfiguration( + Configuration.DeviceName, + Configuration.HardwareFrameworkPath); // If you're running Hastlayer in the Client flavor, you also need to configure some credentials // here: @@ -60,8 +62,8 @@ private static void Main() if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && remoteClientConfiguration.AppSecret == "appsecret") { - throw new InvalidOperationException( - "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); + throw new InvalidOperationException("You haven't changed the default remote credentials!" + + " Write to crew@hastlayer.com to receive access if you don't have yet."); } @@ -110,7 +112,8 @@ private static void Main() // The generated VHDL code will contain debug-level information, though it will be slower to // create. - configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; + configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = + VhdlGenerationConfiguration.Debug; Console.WriteLine("Hardware generation starts."); @@ -123,18 +126,19 @@ private static void Main() }, configuration); - Console.WriteLine("Hardware generation finished."); - Console.WriteLine(); + Console.WriteLine($"Hardware generation finished.{Environment.NewLine}"); // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't // cause any problems, but sometimes they can. if (hardwareRepresentation.HardwareDescription.Warnings.Any()) { Console.WriteLine( - "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + - Environment.NewLine + - string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); - Console.WriteLine(); + "There were the following transformation warnings, which may hint on issues " + + $"that can cause the hardware implementation to produce incorrect results:{Environment.NewLine}" + + string.Join( + Environment.NewLine, + hardwareRepresentation.HardwareDescription.Warnings.Select(warning => $"* {warning}")) + + Environment.NewLine); } Console.WriteLine("Starting hardware execution."); diff --git a/Hast.Samples.Posit/Program.tt b/Hast.Samples.Posit/Program.tt index 2af9cd7e9..fe7e83259 100644 --- a/Hast.Samples.Posit/Program.tt +++ b/Hast.Samples.Posit/Program.tt @@ -61,7 +61,9 @@ namespace Hast.Samples.Posit var devices = hastlayer.GetSupportedDevices(); // Let's just use the first one that is available. However you might want to use a specific // device, not just any first one. - var configuration = new HardwareGenerationConfiguration(Configuration.DeviceName, Configuration.HardwareFrameworkPath); + var configuration = new HardwareGenerationConfiguration( + Configuration.DeviceName, + Configuration.HardwareFrameworkPath); // If you're running Hastlayer in the Client flavor, you also need to configure some credentials // here: @@ -71,8 +73,8 @@ namespace Hast.Samples.Posit if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client && remoteClientConfiguration.AppSecret == "appsecret") { - throw new InvalidOperationException( - "You haven't changed the default remote credentials! Write to crew@hastlayer.com to receive access if you don't have yet."); + throw new InvalidOperationException("You haven't changed the default remote credentials!" + + " Write to crew@hastlayer.com to receive access if you don't have yet."); } @@ -98,7 +100,8 @@ namespace Hast.Samples.Posit // The generated VHDL code will contain debug-level information, though it will be slower to // create. - configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug; + configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = + VhdlGenerationConfiguration.Debug; Console.WriteLine("Hardware generation starts."); @@ -111,18 +114,19 @@ namespace Hast.Samples.Posit }, configuration); - Console.WriteLine("Hardware generation finished."); - Console.WriteLine(); + Console.WriteLine($"Hardware generation finished.{Environment.NewLine}"); // Be sure to check out transformation warnings. Most of the time the issues noticed shouldn't // cause any problems, but sometimes they can. if (hardwareRepresentation.HardwareDescription.Warnings.Any()) { Console.WriteLine( - "There were the following transformation warnings, which may hint on issues that can cause the hardware implementation to produce incorrect results:" + - Environment.NewLine + - string.Join(Environment.NewLine, hardwareRepresentation.HardwareDescription.Warnings.Select(warning => "* " + warning.ToString()))); - Console.WriteLine(); + "There were the following transformation warnings, which may hint on issues " + + $"that can cause the hardware implementation to produce incorrect results:{Environment.NewLine}" + + string.Join( + Environment.NewLine, + hardwareRepresentation.HardwareDescription.Warnings.Select(warning => $"* {warning}")) + + Environment.NewLine); } Console.WriteLine("Starting hardware execution."); From f914f46bbc0be9142ac182d39e490ff005730fc6 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 29 Oct 2021 14:17:25 +0200 Subject: [PATCH 29/43] Updating Hast.Core and Lombiq.Arithmetics with their superrepo dev commits merged into this issue branch --- Hast.Core | 2 +- Lombiq.Arithmetics | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Hast.Core b/Hast.Core index 2fd44072d..3c583a004 160000 --- a/Hast.Core +++ b/Hast.Core @@ -1 +1 @@ -Subproject commit 2fd44072dbbe08ec72d02481d94d16ebe113cc76 +Subproject commit 3c583a00459df2e626a51192c0dae3a47571d024 diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index 6947150fc..99c50bea8 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit 6947150fc3f90ffc2243cb7e26a137c897be2551 +Subproject commit 99c50bea8c605955c94fb5f6cf5a16fb4d249a43 From 13279062aa970f3916911010bbc8757da220808d Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 29 Oct 2021 14:22:06 +0200 Subject: [PATCH 30/43] Updating Lombiq.Arithmetics with Lombiq.Arithmetics.Tests package version update --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index 99c50bea8..e936d823a 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit 99c50bea8c605955c94fb5f6cf5a16fb4d249a43 +Subproject commit e936d823ac657703c3c97e1f880e1c5c12cacb3c From 74d090bf47adfc8269b19a1e119dc257b3a26ebc Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 29 Oct 2021 14:35:58 +0200 Subject: [PATCH 31/43] Updating Hast.Core with latest dev merged into the issue branch --- Hast.Core | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hast.Core b/Hast.Core index 3c583a004..7e035ca48 160000 --- a/Hast.Core +++ b/Hast.Core @@ -1 +1 @@ -Subproject commit 3c583a00459df2e626a51192c0dae3a47571d024 +Subproject commit 7e035ca482b47d35295dc82567d3cd1940b311e0 From ebf9c511d4bf3920af9974644fd3446d1fa44103 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 15 Dec 2021 16:21:44 +0100 Subject: [PATCH 32/43] Updating Lombiq.Arithmetics with PositHelper.tt/cs code styling --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index e936d823a..eb8fce128 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit e936d823ac657703c3c97e1f880e1c5c12cacb3c +Subproject commit eb8fce128b1879b9c7dbb10255a1153fcec3ad55 From 424e07506b169406fd88aafbcc26db373d97d63f Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 15 Dec 2021 18:38:05 +0100 Subject: [PATCH 33/43] Updating Lombiq.Arithmetics with FixedSizePositTests corrected --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index eb8fce128..f4596ff22 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit eb8fce128b1879b9c7dbb10255a1153fcec3ad55 +Subproject commit f4596ff221d09f43ebf9ee521e39a755c8218114 From 3a1271ac2b9b163e0d7b4312cec5489c37b6434c Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 7 Jan 2022 16:50:17 +0100 Subject: [PATCH 34/43] Updating Hast.Core and Lombiq.Arithmetics after merge from dev --- Hast.Core | 2 +- Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude | 2 +- Lombiq.Arithmetics | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Hast.Core b/Hast.Core index 7e035ca48..6454ae2e5 160000 --- a/Hast.Core +++ b/Hast.Core @@ -1 +1 @@ -Subproject commit 7e035ca482b47d35295dc82567d3cd1940b311e0 +Subproject commit 6454ae2e5d1db298b93a4909c5d8ce077e4dcb1c diff --git a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude index e0f597b04..6f6700e66 100644 --- a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude +++ b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude @@ -9,7 +9,7 @@ <#@ assembly name="Microsoft.VisualStudio.Shell.15.0" #> <#@ assembly name="Microsoft.VisualStudio.Shell.Framework" #> <#@ assembly name="Microsoft.VisualStudio.Shell.Interop" #> -<#@ assembly name="D:\DEV\Hastlayer\Hastlayer-SDK\Lombiq.Arithmetics\bin\Debug\netstandard2.0\Microsoft.VisualStudio.Validation.dll" #> +<#@ assembly name="Microsoft.VisualStudio.Validation.dll" #> <#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #> <#@ import namespace="System" #> <#@ import namespace="System.Data" #> diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index f4596ff22..d7e034958 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit f4596ff221d09f43ebf9ee521e39a755c8218114 +Subproject commit d7e0349581b9d5d666bc6f5493eb646f0c01acc9 From 1d612a59f6e65a4afc23abadab83d591ac1407f9 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 7 Jan 2022 17:23:03 +0100 Subject: [PATCH 35/43] Hast.Samples.Posit: Upgrading Microsoft.VisualStudio.Validation to version 16.9.32 to avoid dependency version conflict --- Hast.Samples.Posit/Hast.Samples.Posit.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hast.Samples.Posit/Hast.Samples.Posit.csproj b/Hast.Samples.Posit/Hast.Samples.Posit.csproj index a59605e2d..d7a7933c6 100644 --- a/Hast.Samples.Posit/Hast.Samples.Posit.csproj +++ b/Hast.Samples.Posit/Hast.Samples.Posit.csproj @@ -33,7 +33,7 @@ - + From 0593db56215df9ce4c8410ef31a6b1c3c462016c Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 10 Jan 2022 15:20:26 +0100 Subject: [PATCH 36/43] Posit samples: Adapting Program.tt and PositCalculatorSampleRunner.tt to Hastlayer API changes --- Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs | 2 +- Hast.Samples.Posit/PositCalculatorSampleRunner.tt | 2 +- Hast.Samples.Posit/Program.cs | 8 ++++---- Hast.Samples.Posit/Program.tt | 8 ++++---- 18 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs index 5709d3c7c..61ceac3bb 100644 --- a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_0_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs index 72f9bd9dc..27c424ede 100644 --- a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_1_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_1_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs index 5d9d113a5..b2660c050 100644 --- a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_2_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_2_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs index 0a359480d..400d90fd7 100644 --- a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_3_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_3_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs index 72f6ade44..9327701c6 100644 --- a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit16_4_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_4_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs index 144b51062..b5456ec25 100644 --- a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_0_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs index e632226dd..898426c37 100644 --- a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_1_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_1_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs index 4e3d47488..d622e76ed 100644 --- a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_2_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_2_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs index 38a4d6265..1a11c351f 100644 --- a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_3_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_3_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs index 12bac0c66..f748c9b9b 100644 --- a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit32_4_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_4_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs index 589adec0e..ff2655a26 100644 --- a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_0_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs index 9b862156f..4982d33cd 100644 --- a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_1_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_1_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs index 39f25ff98..214145e89 100644 --- a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_2_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_2_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs index 72aa95907..63de617a3 100644 --- a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_3_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_3_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs index 8d5909442..78e1d8627 100644 --- a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs @@ -21,7 +21,7 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new Posit8_4_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_4_Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt index 65eb419d5..3c3230e69 100644 --- a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt @@ -59,7 +59,7 @@ namespace Hast.Samples.Posit { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxy(hardwareRepresentation, new <#=calculatorName#>()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new <#=calculatorName#>()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); diff --git a/Hast.Samples.Posit/Program.cs b/Hast.Samples.Posit/Program.cs index 987daad9b..f8e213f7a 100644 --- a/Hast.Samples.Posit/Program.cs +++ b/Hast.Samples.Posit/Program.cs @@ -41,9 +41,9 @@ private static void Main() // Hooking into an event of Hastlayer so some execution information can be made visible on the // console. hastlayer.ExecutedOnHardware += (sender, e) => - Console.WriteLine(@$"Executing {e.MemberFullName} on hardware took -{e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds} milliseconds (net) -{e.HardwareExecutionInformation.FullExecutionTimeMilliseconds} milliseconds (all together)."); + Console.WriteLine(@$"Executing {e.Arguments.MemberFullName} on hardware took +{e.Arguments.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds} milliseconds (net) +{e.Arguments.HardwareExecutionInformation.FullExecutionTimeMilliseconds} milliseconds (all together)."); // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. @@ -118,7 +118,7 @@ private static void Main() Console.WriteLine("Hardware generation starts."); // Generating hardware from the sample assembly with the given configuration. - var hardwareRepresentation = await hastlayer.GenerateHardware( + var hardwareRepresentation = await hastlayer.GenerateHardwareAsync( new[] { typeof(Posit8_0).Assembly, diff --git a/Hast.Samples.Posit/Program.tt b/Hast.Samples.Posit/Program.tt index fe7e83259..1294be1b7 100644 --- a/Hast.Samples.Posit/Program.tt +++ b/Hast.Samples.Posit/Program.tt @@ -52,9 +52,9 @@ namespace Hast.Samples.Posit // Hooking into an event of Hastlayer so some execution information can be made visible on the // console. hastlayer.ExecutedOnHardware += (sender, e) => - Console.WriteLine(@$"Executing {e.MemberFullName} on hardware took -{e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds} milliseconds (net) -{e.HardwareExecutionInformation.FullExecutionTimeMilliseconds} milliseconds (all together)."); + Console.WriteLine(@$"Executing {e.Arguments.MemberFullName} on hardware took +{e.Arguments.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds} milliseconds (net) +{e.Arguments.HardwareExecutionInformation.FullExecutionTimeMilliseconds} milliseconds (all together)."); // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for. @@ -106,7 +106,7 @@ namespace Hast.Samples.Posit Console.WriteLine("Hardware generation starts."); // Generating hardware from the sample assembly with the given configuration. - var hardwareRepresentation = await hastlayer.GenerateHardware( + var hardwareRepresentation = await hastlayer.GenerateHardwareAsync( new[] { typeof(Posit8_0).Assembly, From 808d98f3d9de449b1ef7d6b7ea678c17719189dd Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 10 Jan 2022 17:19:47 +0100 Subject: [PATCH 37/43] Updating Lombiq.Arithmetics with some of the failing tests fixed + fixing VS freeze during T4 compilation --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index d7e034958..c0def6b43 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit d7e0349581b9d5d666bc6f5493eb646f0c01acc9 +Subproject commit c0def6b4364ad575f9a75ff11058e470005213a4 From 69be216e92c14e7152e89b27a1ea7abf9e4fa4ce Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 10 Jan 2022 17:21:15 +0100 Subject: [PATCH 38/43] Fixing PositCalculatorTemplateFileManager.ttinclude so that saving its template file doesn't freeze VS https://github.com/aspnet/Tooling/issues/399#issuecomment-205398593 --- Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude index 6f6700e66..6292508e8 100644 --- a/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude +++ b/Hast.Samples.Posit/PositCalculatorTemplateFileManager.ttinclude @@ -262,7 +262,7 @@ public class TemplateFileManager } } - projectSyncAction.EndInvoke(projectSyncAction.BeginInvoke(list, null, null)); + projectSyncAction(list); this.CleanUpTemplatePlaceholders(); var items = VSHelper.GetOutputFilesAsProjectItems(this.dte, list); this.WriteVsProperties(items, list); From 0c5392ee20912ddb61f1806e6459f02bff2b2f31 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 10 Jan 2022 17:33:41 +0100 Subject: [PATCH 39/43] Updating Lombiq.Arithmetics with the remaining tests fixed --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index c0def6b43..031332288 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit c0def6b4364ad575f9a75ff11058e470005213a4 +Subproject commit 031332288758121c13e1a6d00ba993998b163927 From 6c1ab83c39297c33f3a13fb1ff5e9bcc309bd28f Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 10 Jan 2022 18:23:36 +0100 Subject: [PATCH 40/43] Fixing indentation in PositCalculator.tt and PositCalculatorSampleRunner.tt --- Hast.Samples.Posit/PositCalculator.tt | 2 +- Hast.Samples.Posit/PositCalculatorSampleRunner.tt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Hast.Samples.Posit/PositCalculator.tt b/Hast.Samples.Posit/PositCalculator.tt index 6ef14af3d..997649441 100644 --- a/Hast.Samples.Posit/PositCalculator.tt +++ b/Hast.Samples.Posit/PositCalculator.tt @@ -20,7 +20,7 @@ for (var i = 0; i<3; i++){ var quireSize = 1; while (quireSize < (4*positSizes[i]-8)*(1< diff --git a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt index 3c3230e69..7c51064d8 100644 --- a/Hast.Samples.Posit/PositCalculatorSampleRunner.tt +++ b/Hast.Samples.Posit/PositCalculatorSampleRunner.tt @@ -21,7 +21,7 @@ for (var i = 0; i<3; i++){ var quireSize = 1; while (quireSize < (4*positSizes[i]-8)*(1< Date: Tue, 11 Jan 2022 15:37:45 +0100 Subject: [PATCH 41/43] Updating Lombiq.Arithmetics with code styling improvements and adapting dependent classes according new templated posit class/file names --- Hast.Samples.Posit/Posit16E0Calculator.cs | 203 ++++++++++++++++++ ....cs => Posit16E0CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit16E1Calculator.cs | 203 ++++++++++++++++++ ....cs => Posit16E1CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit16E2Calculator.cs | 203 ++++++++++++++++++ ....cs => Posit16E2CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit16E3Calculator.cs | 203 ++++++++++++++++++ ....cs => Posit16E3CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit16E4Calculator.cs | 203 ++++++++++++++++++ ....cs => Posit16E4CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit16_0_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit16_1_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit16_2_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit16_3_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit16_4_Calculator.cs | 203 ------------------ ...0_Calculator.cs => Posit32E0Calculator.cs} | 56 ++--- ....cs => Posit32E0CalculatorSampleRunner.cs} | 34 +-- ...3_Calculator.cs => Posit32E1Calculator.cs} | 56 ++--- ....cs => Posit32E1CalculatorSampleRunner.cs} | 34 +-- ...1_Calculator.cs => Posit32E2Calculator.cs} | 56 ++--- ....cs => Posit32E2CalculatorSampleRunner.cs} | 34 +-- ...2_Calculator.cs => Posit32E3Calculator.cs} | 56 ++--- ....cs => Posit32E3CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit32E4Calculator.cs | 203 ++++++++++++++++++ ....cs => Posit32E4CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit32_0_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit32_1_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit32_2_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit32_3_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit32_4_Calculator.cs | 203 ------------------ Hast.Samples.Posit/Posit8E0Calculator.cs | 203 ++++++++++++++++++ ...r.cs => Posit8E0CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit8E1Calculator.cs | 203 ++++++++++++++++++ ...r.cs => Posit8E1CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit8E2Calculator.cs | 203 ++++++++++++++++++ ...r.cs => Posit8E2CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit8E3Calculator.cs | 203 ++++++++++++++++++ ...r.cs => Posit8E3CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit8E4Calculator.cs | 203 ++++++++++++++++++ ...r.cs => Posit8E4CalculatorSampleRunner.cs} | 34 +-- Hast.Samples.Posit/Posit8_4_Calculator.cs | 203 ------------------ Hast.Samples.Posit/PositCalculator.cs | 30 +-- Hast.Samples.Posit/PositCalculator.tt | 4 +- .../PositCalculatorSampleRunner.cs | 30 +-- .../PositCalculatorSampleRunner.tt | 6 +- Hast.Samples.Posit/Program.cs | 102 ++++----- Hast.Samples.Posit/Program.tt | 10 +- Hast.Samples.Posit/Sample.cs | 96 ++++----- Hast.Samples.Posit/Sample.tt | 2 +- Lombiq.Arithmetics | 2 +- 50 files changed, 2741 insertions(+), 2741 deletions(-) create mode 100644 Hast.Samples.Posit/Posit16E0Calculator.cs rename Hast.Samples.Posit/{Posit16_0_CalculatorSampleRunner.cs => Posit16E0CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit16E1Calculator.cs rename Hast.Samples.Posit/{Posit16_1_CalculatorSampleRunner.cs => Posit16E1CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit16E2Calculator.cs rename Hast.Samples.Posit/{Posit16_2_CalculatorSampleRunner.cs => Posit16E2CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit16E3Calculator.cs rename Hast.Samples.Posit/{Posit16_3_CalculatorSampleRunner.cs => Posit16E3CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit16E4Calculator.cs rename Hast.Samples.Posit/{Posit16_4_CalculatorSampleRunner.cs => Posit16E4CalculatorSampleRunner.cs} (77%) delete mode 100644 Hast.Samples.Posit/Posit16_0_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit16_1_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit16_2_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit16_3_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit16_4_Calculator.cs rename Hast.Samples.Posit/{Posit8_0_Calculator.cs => Posit32E0Calculator.cs} (72%) rename Hast.Samples.Posit/{Posit32_0_CalculatorSampleRunner.cs => Posit32E0CalculatorSampleRunner.cs} (77%) rename Hast.Samples.Posit/{Posit8_3_Calculator.cs => Posit32E1Calculator.cs} (73%) rename Hast.Samples.Posit/{Posit32_1_CalculatorSampleRunner.cs => Posit32E1CalculatorSampleRunner.cs} (77%) rename Hast.Samples.Posit/{Posit8_1_Calculator.cs => Posit32E2Calculator.cs} (72%) rename Hast.Samples.Posit/{Posit32_2_CalculatorSampleRunner.cs => Posit32E2CalculatorSampleRunner.cs} (77%) rename Hast.Samples.Posit/{Posit8_2_Calculator.cs => Posit32E3Calculator.cs} (72%) rename Hast.Samples.Posit/{Posit32_3_CalculatorSampleRunner.cs => Posit32E3CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit32E4Calculator.cs rename Hast.Samples.Posit/{Posit32_4_CalculatorSampleRunner.cs => Posit32E4CalculatorSampleRunner.cs} (77%) delete mode 100644 Hast.Samples.Posit/Posit32_0_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit32_1_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit32_2_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit32_3_Calculator.cs delete mode 100644 Hast.Samples.Posit/Posit32_4_Calculator.cs create mode 100644 Hast.Samples.Posit/Posit8E0Calculator.cs rename Hast.Samples.Posit/{Posit8_0_CalculatorSampleRunner.cs => Posit8E0CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit8E1Calculator.cs rename Hast.Samples.Posit/{Posit8_1_CalculatorSampleRunner.cs => Posit8E1CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit8E2Calculator.cs rename Hast.Samples.Posit/{Posit8_2_CalculatorSampleRunner.cs => Posit8E2CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit8E3Calculator.cs rename Hast.Samples.Posit/{Posit8_3_CalculatorSampleRunner.cs => Posit8E3CalculatorSampleRunner.cs} (77%) create mode 100644 Hast.Samples.Posit/Posit8E4Calculator.cs rename Hast.Samples.Posit/{Posit8_4_CalculatorSampleRunner.cs => Posit8E4CalculatorSampleRunner.cs} (77%) delete mode 100644 Hast.Samples.Posit/Posit8_4_Calculator.cs diff --git a/Hast.Samples.Posit/Posit16E0Calculator.cs b/Hast.Samples.Posit/Posit16E0Calculator.cs new file mode 100644 index 000000000..72fa7d8fa --- /dev/null +++ b/Hast.Samples.Posit/Posit16E0Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16E0Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16E0((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16E0(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16E0(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16E0((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16E0((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16E0CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit16E0Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit16E0Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16E0Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit16E0Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit16E0Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16E0Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16E0(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16E0((ushort)memory.ReadUInt32( Posit16E0Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16E0Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit16E0Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16E0Calculator is (" + + Posit16E0Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16E0Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16E0Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16E0Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16E0Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16E0Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit16E0Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit16E0Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16E0Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16E0Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16E0CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit16E0CalculatorSampleRunner.cs index 61ceac3bb..4615dd962 100644 --- a/Hast.Samples.Posit/Posit16_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16E0CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit16_0_CalculatorSampleRunner + internal class Posit16E0CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16E0Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E0Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E0Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit16_0Array = new uint[100000]; + var Posit16E0Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; - else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E0Array[i] = new Posit16E0((float)0.25 * 2 * i).PositBits; + else Posit16E0Array[i] = new Posit16E0((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_0Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16E0Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit16_0_Calculator(); + var positCalculator = new Posit16E0Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit16_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_0_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E0Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E0Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit16_0Array = new uint[100000]; + var Posit16E0Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_0Array[i] = new Posit16_0((float)0.25 * 2 * i).PositBits; - else Posit16_0Array[i] = new Posit16_0((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E0Array[i] = new Posit16E0((float)0.25 * 2 * i).PositBits; + else Posit16E0Array[i] = new Posit16E0((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit16_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16E0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16E0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16E1Calculator.cs b/Hast.Samples.Posit/Posit16E1Calculator.cs new file mode 100644 index 000000000..bb8077398 --- /dev/null +++ b/Hast.Samples.Posit/Posit16E1Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16E1Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16E1((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16E1(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16E1(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16E1((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16E1((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16E1CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit16E1Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit16E1Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16E1Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit16E1Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit16E1Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16E1Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16E1(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16E1((ushort)memory.ReadUInt32( Posit16E1Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16E1Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit16E1Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16E1Calculator is (" + + Posit16E1Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16E1Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16E1Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16E1Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16E1Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16E1Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit16E1Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit16E1Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16E1Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16E1Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16E1CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit16E1CalculatorSampleRunner.cs index 27c424ede..b044c4938 100644 --- a/Hast.Samples.Posit/Posit16_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16E1CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit16_1_CalculatorSampleRunner + internal class Posit16E1CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_1_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16E1Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E1Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E1Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit16_1Array = new uint[100000]; + var Posit16E1Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; - else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E1Array[i] = new Posit16E1((float)0.25 * 2 * i).PositBits; + else Posit16E1Array[i] = new Posit16E1((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_1Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16E1Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit16_1_Calculator(); + var positCalculator = new Posit16E1Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit16_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_1_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E1Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E1Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit16_1Array = new uint[100000]; + var Posit16E1Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_1Array[i] = new Posit16_1((float)0.25 * 2 * i).PositBits; - else Posit16_1Array[i] = new Posit16_1((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E1Array[i] = new Posit16E1((float)0.25 * 2 * i).PositBits; + else Posit16E1Array[i] = new Posit16E1((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit16_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16E1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16E1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16E2Calculator.cs b/Hast.Samples.Posit/Posit16E2Calculator.cs new file mode 100644 index 000000000..416da047e --- /dev/null +++ b/Hast.Samples.Posit/Posit16E2Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16E2Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16E2((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16E2(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16E2(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16E2((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16E2((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16E2CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit16E2Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit16E2Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16E2Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit16E2Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit16E2Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16E2Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16E2(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16E2((ushort)memory.ReadUInt32( Posit16E2Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16E2Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit16E2Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16E2Calculator is (" + + Posit16E2Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16E2Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16E2Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16E2Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16E2Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16E2Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit16E2Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit16E2Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16E2Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16E2Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16E2CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit16E2CalculatorSampleRunner.cs index b2660c050..ec06540a4 100644 --- a/Hast.Samples.Posit/Posit16_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16E2CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit16_2_CalculatorSampleRunner + internal class Posit16E2CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_2_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16E2Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E2Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E2Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit16_2Array = new uint[100000]; + var Posit16E2Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; - else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E2Array[i] = new Posit16E2((float)0.25 * 2 * i).PositBits; + else Posit16E2Array[i] = new Posit16E2((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_2Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16E2Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit16_2_Calculator(); + var positCalculator = new Posit16E2Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit16_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_2_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E2Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E2Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit16_2Array = new uint[100000]; + var Posit16E2Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_2Array[i] = new Posit16_2((float)0.25 * 2 * i).PositBits; - else Posit16_2Array[i] = new Posit16_2((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E2Array[i] = new Posit16E2((float)0.25 * 2 * i).PositBits; + else Posit16E2Array[i] = new Posit16E2((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit16_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16E2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16E2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16E3Calculator.cs b/Hast.Samples.Posit/Posit16E3Calculator.cs new file mode 100644 index 000000000..c9d1559b9 --- /dev/null +++ b/Hast.Samples.Posit/Posit16E3Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16E3Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16E3((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16E3(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16E3(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16E3((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16E3((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16E3CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit16E3Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit16E3Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16E3Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit16E3Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit16E3Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16E3Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16E3(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16E3((ushort)memory.ReadUInt32( Posit16E3Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16E3Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit16E3Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16E3Calculator is (" + + Posit16E3Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16E3Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16E3Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16E3Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16E3Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16E3Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit16E3Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit16E3Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16E3Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16E3Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16E3CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit16E3CalculatorSampleRunner.cs index 400d90fd7..e34544e10 100644 --- a/Hast.Samples.Posit/Posit16_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16E3CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit16_3_CalculatorSampleRunner + internal class Posit16E3CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_3_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16E3Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E3Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E3Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit16_3Array = new uint[100000]; + var Posit16E3Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; - else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E3Array[i] = new Posit16E3((float)0.25 * 2 * i).PositBits; + else Posit16E3Array[i] = new Posit16E3((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_3Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16E3Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit16_3_Calculator(); + var positCalculator = new Posit16E3Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit16_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_3_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E3Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E3Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit16_3Array = new uint[100000]; + var Posit16E3Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_3Array[i] = new Posit16_3((float)0.25 * 2 * i).PositBits; - else Posit16_3Array[i] = new Posit16_3((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E3Array[i] = new Posit16E3((float)0.25 * 2 * i).PositBits; + else Posit16E3Array[i] = new Posit16E3((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit16_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16E3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16E3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16E4Calculator.cs b/Hast.Samples.Posit/Posit16E4Calculator.cs new file mode 100644 index 000000000..8b09ac783 --- /dev/null +++ b/Hast.Samples.Posit/Posit16E4Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit16E4Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit16E4((ushort)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit16E4(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit16E4(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit16E4((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit16E4((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit16E4CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit16E4Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit16E4Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit16E4Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit16E4Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit16E4Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit16E4Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16E4(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit16E4((ushort)memory.ReadUInt32( Posit16E4Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit16E4Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit16E4Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit16E4Calculator is (" + + Posit16E4Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit16E4Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit16E4Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit16E4Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit16E4Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit16E4Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit16E4Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit16E4Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit16E4Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit16E4Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit16E4CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit16E4CalculatorSampleRunner.cs index 9327701c6..c31fb8e7a 100644 --- a/Hast.Samples.Posit/Posit16_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit16E4CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit16_4_CalculatorSampleRunner + internal class Posit16E4CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16_4_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit16E4Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 10000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E4Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E4Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit16_4Array = new uint[100000]; + var Posit16E4Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; - else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E4Array[i] = new Posit16E4((float)0.25 * 2 * i).PositBits; + else Posit16E4Array[i] = new Posit16E4((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit16_4Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit16E4Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit16_4_Calculator(); + var positCalculator = new Posit16E4Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit16_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit16_4_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit16E4Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit16E4Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit16_4Array = new uint[100000]; + var Posit16E4Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit16_4Array[i] = new Posit16_4((float)0.25 * 2 * i).PositBits; - else Posit16_4Array[i] = new Posit16_4((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit16E4Array[i] = new Posit16E4((float)0.25 * 2 * i).PositBits; + else Posit16E4Array[i] = new Posit16E4((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit16_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit16E4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit16_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit16E4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit16_0_Calculator.cs b/Hast.Samples.Posit/Posit16_0_Calculator.cs deleted file mode 100644 index 8bef724e8..000000000 --- a/Hast.Samples.Posit/Posit16_0_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit16_0_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit16_0((ushort)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit16_0(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit16_0(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit16_0((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit16_0((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit16_0_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit16_0_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit16_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit16_0_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit16_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_0(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit16_0((ushort)memory.ReadUInt32( Posit16_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit16_0_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit16_0_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit16_0_Calculator is (" + - Posit16_0_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit16_0_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_0_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit16_0_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit16_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit16_0_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit16_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit16_0_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit16_1_Calculator.cs b/Hast.Samples.Posit/Posit16_1_Calculator.cs deleted file mode 100644 index 143c555c8..000000000 --- a/Hast.Samples.Posit/Posit16_1_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit16_1_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit16_1((ushort)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit16_1(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit16_1(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit16_1((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit16_1((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit16_1_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit16_1_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit16_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit16_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit16_1_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit16_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit16_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_1(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit16_1((ushort)memory.ReadUInt32( Posit16_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit16_1_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit16_1_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit16_1_Calculator is (" + - Posit16_1_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit16_1_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_1_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit16_1_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit16_1_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit16_1_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit16_1_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit16_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit16_1_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit16_1_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit16_2_Calculator.cs b/Hast.Samples.Posit/Posit16_2_Calculator.cs deleted file mode 100644 index 92da29ad6..000000000 --- a/Hast.Samples.Posit/Posit16_2_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit16_2_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit16_2((ushort)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit16_2(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit16_2(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit16_2((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit16_2((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit16_2_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit16_2_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit16_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit16_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit16_2_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit16_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit16_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_2(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit16_2((ushort)memory.ReadUInt32( Posit16_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit16_2_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit16_2_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit16_2_Calculator is (" + - Posit16_2_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit16_2_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_2_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit16_2_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit16_2_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit16_2_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit16_2_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit16_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit16_2_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit16_2_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit16_3_Calculator.cs b/Hast.Samples.Posit/Posit16_3_Calculator.cs deleted file mode 100644 index cf31cac2f..000000000 --- a/Hast.Samples.Posit/Posit16_3_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit16_3_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit16_3((ushort)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit16_3(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit16_3(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit16_3((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit16_3((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit16_3_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit16_3_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit16_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit16_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit16_3_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit16_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit16_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_3(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit16_3((ushort)memory.ReadUInt32( Posit16_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit16_3_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit16_3_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit16_3_Calculator is (" + - Posit16_3_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit16_3_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_3_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit16_3_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit16_3_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit16_3_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit16_3_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit16_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit16_3_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit16_3_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit16_4_Calculator.cs b/Hast.Samples.Posit/Posit16_4_Calculator.cs deleted file mode 100644 index 726d7edb1..000000000 --- a/Hast.Samples.Posit/Posit16_4_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit16_4_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit16_4((ushort)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (ushort)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit16_4(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit16_4(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit16_4((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit16_4((ushort)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit16_4_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit16_4_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit16_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit16_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit16_4_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit16_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit16_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit16_4(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit16_4((ushort)memory.ReadUInt32( Posit16_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit16_4_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit16_4_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit16_4_Calculator is (" + - Posit16_4_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit16_4_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit16_4_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit16_4_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit16_4_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit16_4_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit16_4_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit16_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit16_4_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit16_4_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit8_0_Calculator.cs b/Hast.Samples.Posit/Posit32E0Calculator.cs similarity index 72% rename from Hast.Samples.Posit/Posit8_0_Calculator.cs rename to Hast.Samples.Posit/Posit32E0Calculator.cs index 092b9f133..ea6835d7c 100644 --- a/Hast.Samples.Posit/Posit8_0_Calculator.cs +++ b/Hast.Samples.Posit/Posit32E0Calculator.cs @@ -8,7 +8,7 @@ namespace Hast.Samples.Posit { - public class Posit8_0_Calculator + public class Posit32E0Calculator { public const int CalculateLargeIntegerSum_InputInt32Index = 0; public const int CalculateLargeIntegerSum_OutputInt32Index = 0; @@ -28,7 +28,7 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) { var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - var a = new Posit8_0((byte)1); + var a = new Posit32E0((uint)1); var b = a; for (uint i = 1; i < number; i++) @@ -43,9 +43,9 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) public virtual void CalculatePowerOfReal(SimpleMemory memory) { var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - var a = new Posit8_0(positToMultiply, true); + var a = new Posit32E0(positToMultiply, true); var b = a; for (uint i = 0; i < number; i++) @@ -70,7 +70,7 @@ public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memo tasks[i] = Task.Factory.StartNew( upToNumberObject => { - var a = new Posit8_0(1); + var a = new Posit32E0(1); var b = a; for (int j = 1; j < (int)upToNumberObject; j++) @@ -94,11 +94,11 @@ public virtual void AddPositsInArray(SimpleMemory memory) { uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - var result = new Posit8_0((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + var result = new Posit32E0((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); for (int i = 1; i < numberCount; i++) { - result += new Posit8_0((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + result += new Posit32E0((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); } memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); @@ -106,10 +106,10 @@ public virtual void AddPositsInArray(SimpleMemory memory) } - public static class Posit8_0_CalculatorCalculatorExtensions + public static class Posit32E0CalculatorCalculatorExtensions { public static int CalculateIntegerSumUpToNumber( - this Posit8_0_Calculator positCalculator, + this Posit32E0Calculator positCalculator, int number, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -118,14 +118,14 @@ public static int CalculateIntegerSumUpToNumber( ? SimpleMemory.CreateSoftwareMemory(1) : hastlayer.CreateMemory(configuration, 1); - memory.WriteInt32( Posit8_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + memory.WriteInt32( Posit32E0Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); - return memory.ReadInt32( Posit8_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + return memory.ReadInt32( Posit32E0Calculator.CalculateLargeIntegerSum_OutputInt32Index); } public static float CalculatePowerOfReal( - this Posit8_0_Calculator positCalculator, + this Posit32E0Calculator positCalculator, int number, float real, IHastlayer hastlayer = null, @@ -135,50 +135,50 @@ public static float CalculatePowerOfReal( ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); - memory.WriteInt32( Posit8_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit8_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_0(real).PositBits); + memory.WriteInt32( Posit32E0Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32E0Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32E0(real).PositBits); positCalculator.CalculatePowerOfReal(memory); - return (float)new Posit8_0((byte)memory.ReadUInt32( Posit8_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + return (float)new Posit32E0((uint)memory.ReadUInt32( Posit32E0Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit8_0_Calculator positCalculator, + this Posit32E0Calculator positCalculator, int[] numbers, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - if (numbers.Length != Posit8_0_Calculator.MaxDegreeOfParallelism) + if (numbers.Length != Posit32E0Calculator.MaxDegreeOfParallelism) { throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit8_0_Calculator is (" + - Posit8_0_Calculator.MaxDegreeOfParallelism + ")"); + "Provide as many numbers as the degree of parallelism of Posit32E0Calculator is (" + + Posit32E0Calculator.MaxDegreeOfParallelism + ")"); } var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit8_0_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_0_Calculator.MaxDegreeOfParallelism); + ? SimpleMemory.CreateSoftwareMemory(Posit32E0Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32E0Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { - memory.WriteInt32( Posit8_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + memory.WriteInt32( Posit32E0Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); } positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - var results = new int[ Posit8_0_Calculator.MaxDegreeOfParallelism]; + var results = new int[ Posit32E0Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < numbers.Length; i++) { - results[i] = memory.ReadInt32( Posit8_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + results[i] = memory.ReadInt32( Posit32E0Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); } return results; } public static float AddPositsInArray( - this Posit8_0_Calculator positCalculator, + this Posit32E0Calculator positCalculator, uint[] positArray, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -187,16 +187,16 @@ public static float AddPositsInArray( ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); - memory.WriteUInt32( Posit8_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + memory.WriteUInt32( Posit32E0Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); for (var i = 0; i < positArray.Length; i++) { - memory.WriteUInt32( Posit8_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + memory.WriteUInt32( Posit32E0Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); } positCalculator.AddPositsInArray(memory); - return (float)new Posit32(memory.ReadUInt32( Posit8_0_Calculator.AddPositsInArray_OutputPosit32Index), true); + return (float)new Posit32(memory.ReadUInt32( Posit32E0Calculator.AddPositsInArray_OutputPosit32Index), true); } } } diff --git a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32E0CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit32E0CalculatorSampleRunner.cs index b5456ec25..0846fb012 100644 --- a/Hast.Samples.Posit/Posit32_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32E0CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit32_0_CalculatorSampleRunner + internal class Posit32E0CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32E0Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E0Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E0Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit32_0Array = new uint[100000]; + var Posit32E0Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; - else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E0Array[i] = new Posit32E0((float)0.25 * 2 * i).PositBits; + else Posit32E0Array[i] = new Posit32E0((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_0Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32E0Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit32_0_Calculator(); + var positCalculator = new Posit32E0Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit32_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_0_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E0Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E0Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit32_0Array = new uint[100000]; + var Posit32E0Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_0Array[i] = new Posit32_0((float)0.25 * 2 * i).PositBits; - else Posit32_0Array[i] = new Posit32_0((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E0Array[i] = new Posit32E0((float)0.25 * 2 * i).PositBits; + else Posit32E0Array[i] = new Posit32E0((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit32_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32E0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32E0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_3_Calculator.cs b/Hast.Samples.Posit/Posit32E1Calculator.cs similarity index 73% rename from Hast.Samples.Posit/Posit8_3_Calculator.cs rename to Hast.Samples.Posit/Posit32E1Calculator.cs index 6ca05c142..29845812e 100644 --- a/Hast.Samples.Posit/Posit8_3_Calculator.cs +++ b/Hast.Samples.Posit/Posit32E1Calculator.cs @@ -8,7 +8,7 @@ namespace Hast.Samples.Posit { - public class Posit8_3_Calculator + public class Posit32E1Calculator { public const int CalculateLargeIntegerSum_InputInt32Index = 0; public const int CalculateLargeIntegerSum_OutputInt32Index = 0; @@ -28,7 +28,7 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) { var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - var a = new Posit8_3((byte)1); + var a = new Posit32E1((uint)1); var b = a; for (uint i = 1; i < number; i++) @@ -43,9 +43,9 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) public virtual void CalculatePowerOfReal(SimpleMemory memory) { var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - var a = new Posit8_3(positToMultiply, true); + var a = new Posit32E1(positToMultiply, true); var b = a; for (uint i = 0; i < number; i++) @@ -70,7 +70,7 @@ public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memo tasks[i] = Task.Factory.StartNew( upToNumberObject => { - var a = new Posit8_3(1); + var a = new Posit32E1(1); var b = a; for (int j = 1; j < (int)upToNumberObject; j++) @@ -94,11 +94,11 @@ public virtual void AddPositsInArray(SimpleMemory memory) { uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - var result = new Posit8_3((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + var result = new Posit32E1((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); for (int i = 1; i < numberCount; i++) { - result += new Posit8_3((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + result += new Posit32E1((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); } memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); @@ -106,10 +106,10 @@ public virtual void AddPositsInArray(SimpleMemory memory) } - public static class Posit8_3_CalculatorCalculatorExtensions + public static class Posit32E1CalculatorCalculatorExtensions { public static int CalculateIntegerSumUpToNumber( - this Posit8_3_Calculator positCalculator, + this Posit32E1Calculator positCalculator, int number, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -118,14 +118,14 @@ public static int CalculateIntegerSumUpToNumber( ? SimpleMemory.CreateSoftwareMemory(1) : hastlayer.CreateMemory(configuration, 1); - memory.WriteInt32( Posit8_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + memory.WriteInt32( Posit32E1Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); - return memory.ReadInt32( Posit8_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + return memory.ReadInt32( Posit32E1Calculator.CalculateLargeIntegerSum_OutputInt32Index); } public static float CalculatePowerOfReal( - this Posit8_3_Calculator positCalculator, + this Posit32E1Calculator positCalculator, int number, float real, IHastlayer hastlayer = null, @@ -135,50 +135,50 @@ public static float CalculatePowerOfReal( ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); - memory.WriteInt32( Posit8_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit8_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_3(real).PositBits); + memory.WriteInt32( Posit32E1Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32E1Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32E1(real).PositBits); positCalculator.CalculatePowerOfReal(memory); - return (float)new Posit8_3((byte)memory.ReadUInt32( Posit8_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + return (float)new Posit32E1((uint)memory.ReadUInt32( Posit32E1Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit8_3_Calculator positCalculator, + this Posit32E1Calculator positCalculator, int[] numbers, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - if (numbers.Length != Posit8_3_Calculator.MaxDegreeOfParallelism) + if (numbers.Length != Posit32E1Calculator.MaxDegreeOfParallelism) { throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit8_3_Calculator is (" + - Posit8_3_Calculator.MaxDegreeOfParallelism + ")"); + "Provide as many numbers as the degree of parallelism of Posit32E1Calculator is (" + + Posit32E1Calculator.MaxDegreeOfParallelism + ")"); } var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit8_3_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_3_Calculator.MaxDegreeOfParallelism); + ? SimpleMemory.CreateSoftwareMemory(Posit32E1Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32E1Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { - memory.WriteInt32( Posit8_3_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + memory.WriteInt32( Posit32E1Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); } positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - var results = new int[ Posit8_3_Calculator.MaxDegreeOfParallelism]; + var results = new int[ Posit32E1Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < numbers.Length; i++) { - results[i] = memory.ReadInt32( Posit8_3_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + results[i] = memory.ReadInt32( Posit32E1Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); } return results; } public static float AddPositsInArray( - this Posit8_3_Calculator positCalculator, + this Posit32E1Calculator positCalculator, uint[] positArray, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -187,16 +187,16 @@ public static float AddPositsInArray( ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); - memory.WriteUInt32( Posit8_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + memory.WriteUInt32( Posit32E1Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); for (var i = 0; i < positArray.Length; i++) { - memory.WriteUInt32( Posit8_3_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + memory.WriteUInt32( Posit32E1Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); } positCalculator.AddPositsInArray(memory); - return (float)new Posit32(memory.ReadUInt32( Posit8_3_Calculator.AddPositsInArray_OutputPosit32Index), true); + return (float)new Posit32(memory.ReadUInt32( Posit32E1Calculator.AddPositsInArray_OutputPosit32Index), true); } } } diff --git a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32E1CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit32E1CalculatorSampleRunner.cs index 898426c37..7442fe11e 100644 --- a/Hast.Samples.Posit/Posit32_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32E1CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit32_1_CalculatorSampleRunner + internal class Posit32E1CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_1_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32E1Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E1Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E1Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit32_1Array = new uint[100000]; + var Posit32E1Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; - else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E1Array[i] = new Posit32E1((float)0.25 * 2 * i).PositBits; + else Posit32E1Array[i] = new Posit32E1((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_1Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32E1Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit32_1_Calculator(); + var positCalculator = new Posit32E1Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit32_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_1_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E1Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E1Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit32_1Array = new uint[100000]; + var Posit32E1Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_1Array[i] = new Posit32_1((float)0.25 * 2 * i).PositBits; - else Posit32_1Array[i] = new Posit32_1((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E1Array[i] = new Posit32E1((float)0.25 * 2 * i).PositBits; + else Posit32E1Array[i] = new Posit32E1((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit32_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32E1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32E1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_1_Calculator.cs b/Hast.Samples.Posit/Posit32E2Calculator.cs similarity index 72% rename from Hast.Samples.Posit/Posit8_1_Calculator.cs rename to Hast.Samples.Posit/Posit32E2Calculator.cs index 95f4999de..a6c334d66 100644 --- a/Hast.Samples.Posit/Posit8_1_Calculator.cs +++ b/Hast.Samples.Posit/Posit32E2Calculator.cs @@ -8,7 +8,7 @@ namespace Hast.Samples.Posit { - public class Posit8_1_Calculator + public class Posit32E2Calculator { public const int CalculateLargeIntegerSum_InputInt32Index = 0; public const int CalculateLargeIntegerSum_OutputInt32Index = 0; @@ -28,7 +28,7 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) { var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - var a = new Posit8_1((byte)1); + var a = new Posit32E2((uint)1); var b = a; for (uint i = 1; i < number; i++) @@ -43,9 +43,9 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) public virtual void CalculatePowerOfReal(SimpleMemory memory) { var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - var a = new Posit8_1(positToMultiply, true); + var a = new Posit32E2(positToMultiply, true); var b = a; for (uint i = 0; i < number; i++) @@ -70,7 +70,7 @@ public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memo tasks[i] = Task.Factory.StartNew( upToNumberObject => { - var a = new Posit8_1(1); + var a = new Posit32E2(1); var b = a; for (int j = 1; j < (int)upToNumberObject; j++) @@ -94,11 +94,11 @@ public virtual void AddPositsInArray(SimpleMemory memory) { uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - var result = new Posit8_1((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + var result = new Posit32E2((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); for (int i = 1; i < numberCount; i++) { - result += new Posit8_1((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + result += new Posit32E2((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); } memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); @@ -106,10 +106,10 @@ public virtual void AddPositsInArray(SimpleMemory memory) } - public static class Posit8_1_CalculatorCalculatorExtensions + public static class Posit32E2CalculatorCalculatorExtensions { public static int CalculateIntegerSumUpToNumber( - this Posit8_1_Calculator positCalculator, + this Posit32E2Calculator positCalculator, int number, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -118,14 +118,14 @@ public static int CalculateIntegerSumUpToNumber( ? SimpleMemory.CreateSoftwareMemory(1) : hastlayer.CreateMemory(configuration, 1); - memory.WriteInt32( Posit8_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + memory.WriteInt32( Posit32E2Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); - return memory.ReadInt32( Posit8_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + return memory.ReadInt32( Posit32E2Calculator.CalculateLargeIntegerSum_OutputInt32Index); } public static float CalculatePowerOfReal( - this Posit8_1_Calculator positCalculator, + this Posit32E2Calculator positCalculator, int number, float real, IHastlayer hastlayer = null, @@ -135,50 +135,50 @@ public static float CalculatePowerOfReal( ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); - memory.WriteInt32( Posit8_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit8_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_1(real).PositBits); + memory.WriteInt32( Posit32E2Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32E2Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32E2(real).PositBits); positCalculator.CalculatePowerOfReal(memory); - return (float)new Posit8_1((byte)memory.ReadUInt32( Posit8_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + return (float)new Posit32E2((uint)memory.ReadUInt32( Posit32E2Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit8_1_Calculator positCalculator, + this Posit32E2Calculator positCalculator, int[] numbers, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - if (numbers.Length != Posit8_1_Calculator.MaxDegreeOfParallelism) + if (numbers.Length != Posit32E2Calculator.MaxDegreeOfParallelism) { throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit8_1_Calculator is (" + - Posit8_1_Calculator.MaxDegreeOfParallelism + ")"); + "Provide as many numbers as the degree of parallelism of Posit32E2Calculator is (" + + Posit32E2Calculator.MaxDegreeOfParallelism + ")"); } var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit8_1_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_1_Calculator.MaxDegreeOfParallelism); + ? SimpleMemory.CreateSoftwareMemory(Posit32E2Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32E2Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { - memory.WriteInt32( Posit8_1_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + memory.WriteInt32( Posit32E2Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); } positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - var results = new int[ Posit8_1_Calculator.MaxDegreeOfParallelism]; + var results = new int[ Posit32E2Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < numbers.Length; i++) { - results[i] = memory.ReadInt32( Posit8_1_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + results[i] = memory.ReadInt32( Posit32E2Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); } return results; } public static float AddPositsInArray( - this Posit8_1_Calculator positCalculator, + this Posit32E2Calculator positCalculator, uint[] positArray, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -187,16 +187,16 @@ public static float AddPositsInArray( ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); - memory.WriteUInt32( Posit8_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + memory.WriteUInt32( Posit32E2Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); for (var i = 0; i < positArray.Length; i++) { - memory.WriteUInt32( Posit8_1_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + memory.WriteUInt32( Posit32E2Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); } positCalculator.AddPositsInArray(memory); - return (float)new Posit32(memory.ReadUInt32( Posit8_1_Calculator.AddPositsInArray_OutputPosit32Index), true); + return (float)new Posit32(memory.ReadUInt32( Posit32E2Calculator.AddPositsInArray_OutputPosit32Index), true); } } } diff --git a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32E2CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit32E2CalculatorSampleRunner.cs index d622e76ed..27131ecea 100644 --- a/Hast.Samples.Posit/Posit32_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32E2CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit32_2_CalculatorSampleRunner + internal class Posit32E2CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_2_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32E2Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit32_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_2_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E2Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E2Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit32_2Array = new uint[100000]; + var Posit32E2Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_2Array[i] = new Posit32_2((float)0.25 * 2 * i).PositBits; - else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E2Array[i] = new Posit32E2((float)0.25 * 2 * i).PositBits; + else Posit32E2Array[i] = new Posit32E2((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_2Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32E2Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit32_2_Calculator(); + var positCalculator = new Posit32E2Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit32_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_2_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E2Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E2Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit32_2Array = new uint[100000]; + var Posit32E2Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_2Array[i] = new Posit32_2((float)0.25 * 2 * i).PositBits; - else Posit32_2Array[i] = new Posit32_2((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E2Array[i] = new Posit32E2((float)0.25 * 2 * i).PositBits; + else Posit32E2Array[i] = new Posit32E2((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit32_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32E2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32E2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_2_Calculator.cs b/Hast.Samples.Posit/Posit32E3Calculator.cs similarity index 72% rename from Hast.Samples.Posit/Posit8_2_Calculator.cs rename to Hast.Samples.Posit/Posit32E3Calculator.cs index c677b61f6..33d2b0e83 100644 --- a/Hast.Samples.Posit/Posit8_2_Calculator.cs +++ b/Hast.Samples.Posit/Posit32E3Calculator.cs @@ -8,7 +8,7 @@ namespace Hast.Samples.Posit { - public class Posit8_2_Calculator + public class Posit32E3Calculator { public const int CalculateLargeIntegerSum_InputInt32Index = 0; public const int CalculateLargeIntegerSum_OutputInt32Index = 0; @@ -28,7 +28,7 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) { var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - var a = new Posit8_2((byte)1); + var a = new Posit32E3((uint)1); var b = a; for (uint i = 1; i < number; i++) @@ -43,9 +43,9 @@ public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) public virtual void CalculatePowerOfReal(SimpleMemory memory) { var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - var a = new Posit8_2(positToMultiply, true); + var a = new Posit32E3(positToMultiply, true); var b = a; for (uint i = 0; i < number; i++) @@ -70,7 +70,7 @@ public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memo tasks[i] = Task.Factory.StartNew( upToNumberObject => { - var a = new Posit8_2(1); + var a = new Posit32E3(1); var b = a; for (int j = 1; j < (int)upToNumberObject; j++) @@ -94,11 +94,11 @@ public virtual void AddPositsInArray(SimpleMemory memory) { uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - var result = new Posit8_2((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + var result = new Posit32E3((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); for (int i = 1; i < numberCount; i++) { - result += new Posit8_2((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + result += new Posit32E3((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); } memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); @@ -106,10 +106,10 @@ public virtual void AddPositsInArray(SimpleMemory memory) } - public static class Posit8_2_CalculatorCalculatorExtensions + public static class Posit32E3CalculatorCalculatorExtensions { public static int CalculateIntegerSumUpToNumber( - this Posit8_2_Calculator positCalculator, + this Posit32E3Calculator positCalculator, int number, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -118,14 +118,14 @@ public static int CalculateIntegerSumUpToNumber( ? SimpleMemory.CreateSoftwareMemory(1) : hastlayer.CreateMemory(configuration, 1); - memory.WriteInt32( Posit8_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + memory.WriteInt32( Posit32E3Calculator.CalculateLargeIntegerSum_InputInt32Index, number); positCalculator.CalculateIntegerSumUpToNumber(memory); - return memory.ReadInt32( Posit8_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); + return memory.ReadInt32( Posit32E3Calculator.CalculateLargeIntegerSum_OutputInt32Index); } public static float CalculatePowerOfReal( - this Posit8_2_Calculator positCalculator, + this Posit32E3Calculator positCalculator, int number, float real, IHastlayer hastlayer = null, @@ -135,50 +135,50 @@ public static float CalculatePowerOfReal( ? SimpleMemory.CreateSoftwareMemory(2) : hastlayer.CreateMemory(configuration, 2); - memory.WriteInt32( Posit8_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit8_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_2(real).PositBits); + memory.WriteInt32( Posit32E3Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32E3Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32E3(real).PositBits); positCalculator.CalculatePowerOfReal(memory); - return (float)new Posit8_2((byte)memory.ReadUInt32( Posit8_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + return (float)new Posit32E3((uint)memory.ReadUInt32( Posit32E3Calculator.CalculatePowerOfReal_OutputPosit32Index), true); } public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit8_2_Calculator positCalculator, + this Posit32E3Calculator positCalculator, int[] numbers, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) { - if (numbers.Length != Posit8_2_Calculator.MaxDegreeOfParallelism) + if (numbers.Length != Posit32E3Calculator.MaxDegreeOfParallelism) { throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit8_2_Calculator is (" + - Posit8_2_Calculator.MaxDegreeOfParallelism + ")"); + "Provide as many numbers as the degree of parallelism of Posit32E3Calculator is (" + + Posit32E3Calculator.MaxDegreeOfParallelism + ")"); } var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit8_2_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_2_Calculator.MaxDegreeOfParallelism); + ? SimpleMemory.CreateSoftwareMemory(Posit32E3Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32E3Calculator.MaxDegreeOfParallelism); for (int i = 0; i < numbers.Length; i++) { - memory.WriteInt32( Posit8_2_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + memory.WriteInt32( Posit32E3Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); } positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - var results = new int[ Posit8_2_Calculator.MaxDegreeOfParallelism]; + var results = new int[ Posit32E3Calculator.MaxDegreeOfParallelism]; for (int i = 0; i < numbers.Length; i++) { - results[i] = memory.ReadInt32( Posit8_2_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + results[i] = memory.ReadInt32( Posit32E3Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); } return results; } public static float AddPositsInArray( - this Posit8_2_Calculator positCalculator, + this Posit32E3Calculator positCalculator, uint[] positArray, IHastlayer hastlayer = null, IHardwareGenerationConfiguration configuration = null) @@ -187,16 +187,16 @@ public static float AddPositsInArray( ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) : hastlayer.CreateMemory(configuration, positArray.Length + 1); - memory.WriteUInt32( Posit8_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + memory.WriteUInt32( Posit32E3Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); for (var i = 0; i < positArray.Length; i++) { - memory.WriteUInt32( Posit8_2_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + memory.WriteUInt32( Posit32E3Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); } positCalculator.AddPositsInArray(memory); - return (float)new Posit32(memory.ReadUInt32( Posit8_2_Calculator.AddPositsInArray_OutputPosit32Index), true); + return (float)new Posit32(memory.ReadUInt32( Posit32E3Calculator.AddPositsInArray_OutputPosit32Index), true); } } } diff --git a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32E3CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit32E3CalculatorSampleRunner.cs index 1a11c351f..23429354c 100644 --- a/Hast.Samples.Posit/Posit32_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32E3CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit32_3_CalculatorSampleRunner + internal class Posit32E3CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_3_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32E3Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E3Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E3Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit32_3Array = new uint[100000]; + var Posit32E3Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; - else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E3Array[i] = new Posit32E3((float)0.25 * 2 * i).PositBits; + else Posit32E3Array[i] = new Posit32E3((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_3Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32E3Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit32_3_Calculator(); + var positCalculator = new Posit32E3Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit32_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_3_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E3Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E3Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit32_3Array = new uint[100000]; + var Posit32E3Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_3Array[i] = new Posit32_3((float)0.25 * 2 * i).PositBits; - else Posit32_3Array[i] = new Posit32_3((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E3Array[i] = new Posit32E3((float)0.25 * 2 * i).PositBits; + else Posit32E3Array[i] = new Posit32E3((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit32_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32E3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32E3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32E4Calculator.cs b/Hast.Samples.Posit/Posit32E4Calculator.cs new file mode 100644 index 000000000..4c732daab --- /dev/null +++ b/Hast.Samples.Posit/Posit32E4Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit32E4Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit32E4((uint)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit32E4(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit32E4(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit32E4((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit32E4((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit32E4CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit32E4Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit32E4Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit32E4Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit32E4Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit32E4Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit32E4Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32E4(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit32E4((uint)memory.ReadUInt32( Posit32E4Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit32E4Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit32E4Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit32E4Calculator is (" + + Posit32E4Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit32E4Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit32E4Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit32E4Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit32E4Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit32E4Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit32E4Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit32E4Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit32E4Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit32E4Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit32E4CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit32E4CalculatorSampleRunner.cs index f748c9b9b..14fb28e7b 100644 --- a/Hast.Samples.Posit/Posit32_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit32E4CalculatorSampleRunner.cs @@ -10,26 +10,26 @@ namespace Hast.Samples.Posit { - internal class Posit32_4_CalculatorSampleRunner + internal class Posit32E4CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32_4_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit32E4Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); positCalculator.CalculatePowerOfReal( 100000, (float)1.015625, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E4Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E4Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -37,20 +37,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit32_4Array = new uint[100000]; + var Posit32E4Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; - else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E4Array[i] = new Posit32E4((float)0.25 * 2 * i).PositBits; + else Posit32E4Array[i] = new Posit32E4((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit32_4Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit32E4Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit32_4_Calculator(); + var positCalculator = new Posit32E4Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -83,8 +83,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit32_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit32_4_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit32E4Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit32E4Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -102,17 +102,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit32_4Array = new uint[100000]; + var Posit32E4Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit32_4Array[i] = new Posit32_4((float)0.25 * 2 * i).PositBits; - else Posit32_4Array[i] = new Posit32_4((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit32E4Array[i] = new Posit32E4((float)0.25 * 2 * i).PositBits; + else Posit32E4Array[i] = new Posit32E4((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit32_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit32E4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit32_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit32E4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit32_0_Calculator.cs b/Hast.Samples.Posit/Posit32_0_Calculator.cs deleted file mode 100644 index 84e2a75fd..000000000 --- a/Hast.Samples.Posit/Posit32_0_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit32_0_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit32_0((uint)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit32_0(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit32_0(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit32_0((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit32_0((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit32_0_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit32_0_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit32_0_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit32_0_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit32_0_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit32_0_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit32_0_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_0(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit32_0((uint)memory.ReadUInt32( Posit32_0_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit32_0_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit32_0_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit32_0_Calculator is (" + - Posit32_0_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit32_0_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_0_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit32_0_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit32_0_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit32_0_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit32_0_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit32_0_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit32_0_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit32_0_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit32_1_Calculator.cs b/Hast.Samples.Posit/Posit32_1_Calculator.cs deleted file mode 100644 index 980d4640c..000000000 --- a/Hast.Samples.Posit/Posit32_1_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit32_1_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit32_1((uint)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit32_1(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit32_1(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit32_1((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit32_1((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit32_1_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit32_1_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit32_1_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit32_1_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit32_1_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit32_1_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit32_1_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_1(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit32_1((uint)memory.ReadUInt32( Posit32_1_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit32_1_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit32_1_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit32_1_Calculator is (" + - Posit32_1_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit32_1_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_1_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit32_1_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit32_1_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit32_1_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit32_1_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit32_1_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit32_1_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit32_1_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit32_2_Calculator.cs b/Hast.Samples.Posit/Posit32_2_Calculator.cs deleted file mode 100644 index 7376f5959..000000000 --- a/Hast.Samples.Posit/Posit32_2_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit32_2_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit32_2((uint)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit32_2(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit32_2(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit32_2((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit32_2((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit32_2_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit32_2_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit32_2_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit32_2_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit32_2_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit32_2_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit32_2_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_2(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit32_2((uint)memory.ReadUInt32( Posit32_2_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit32_2_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit32_2_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit32_2_Calculator is (" + - Posit32_2_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit32_2_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_2_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit32_2_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit32_2_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit32_2_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit32_2_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit32_2_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit32_2_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit32_2_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit32_3_Calculator.cs b/Hast.Samples.Posit/Posit32_3_Calculator.cs deleted file mode 100644 index 2425445f6..000000000 --- a/Hast.Samples.Posit/Posit32_3_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit32_3_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit32_3((uint)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit32_3(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit32_3(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit32_3((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit32_3((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit32_3_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit32_3_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit32_3_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit32_3_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit32_3_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit32_3_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit32_3_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_3(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit32_3((uint)memory.ReadUInt32( Posit32_3_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit32_3_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit32_3_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit32_3_Calculator is (" + - Posit32_3_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit32_3_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_3_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit32_3_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit32_3_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit32_3_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit32_3_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit32_3_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit32_3_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit32_3_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit32_4_Calculator.cs b/Hast.Samples.Posit/Posit32_4_Calculator.cs deleted file mode 100644 index 9cdcadb3d..000000000 --- a/Hast.Samples.Posit/Posit32_4_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit32_4_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit32_4((uint)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (uint)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit32_4(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit32_4(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit32_4((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit32_4((uint)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit32_4_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit32_4_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit32_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit32_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit32_4_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit32_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit32_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit32_4(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit32_4((uint)memory.ReadUInt32( Posit32_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit32_4_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit32_4_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit32_4_Calculator is (" + - Posit32_4_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit32_4_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit32_4_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit32_4_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit32_4_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit32_4_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit32_4_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit32_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit32_4_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit32_4_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/Posit8E0Calculator.cs b/Hast.Samples.Posit/Posit8E0Calculator.cs new file mode 100644 index 000000000..077a3d0e6 --- /dev/null +++ b/Hast.Samples.Posit/Posit8E0Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8E0Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8E0((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8E0(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8E0(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8E0((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8E0((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8E0CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit8E0Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit8E0Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8E0Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit8E0Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit8E0Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8E0Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8E0(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8E0((byte)memory.ReadUInt32( Posit8E0Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8E0Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit8E0Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8E0Calculator is (" + + Posit8E0Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8E0Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8E0Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8E0Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8E0Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8E0Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit8E0Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit8E0Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8E0Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8E0Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8E0CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit8E0CalculatorSampleRunner.cs index ff2655a26..f075a2fdf 100644 --- a/Hast.Samples.Posit/Posit8_0_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8E0CalculatorSampleRunner.cs @@ -10,18 +10,18 @@ namespace Hast.Samples.Posit { - internal class Posit8_0_CalculatorSampleRunner + internal class Posit8E0CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_0_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8E0Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -29,8 +29,8 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw positCalculator.CalculatePowerOfReal( 5, (float)0.5); - var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E0Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E0Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -38,20 +38,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_0Array = new uint[100000]; + var Posit8E0Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; - else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E0Array[i] = new Posit8E0((float)0.25 * 2 * i).PositBits; + else Posit8E0Array[i] = new Posit8E0((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_0Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8E0Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit8_0_Calculator(); + var positCalculator = new Posit8E0Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -81,8 +81,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit8_0_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_0_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E0Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E0Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -100,17 +100,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit8_0Array = new uint[100000]; + var Posit8E0Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_0Array[i] = new Posit8_0((float)0.25 * 2 * i).PositBits; - else Posit8_0Array[i] = new Posit8_0((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E0Array[i] = new Posit8E0((float)0.25 * 2 * i).PositBits; + else Posit8E0Array[i] = new Posit8E0((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit8_0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8E0Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8E0Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8E1Calculator.cs b/Hast.Samples.Posit/Posit8E1Calculator.cs new file mode 100644 index 000000000..481acb27a --- /dev/null +++ b/Hast.Samples.Posit/Posit8E1Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8E1Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8E1((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8E1(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8E1(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8E1((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8E1((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8E1CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit8E1Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit8E1Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8E1Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit8E1Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit8E1Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8E1Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8E1(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8E1((byte)memory.ReadUInt32( Posit8E1Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8E1Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit8E1Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8E1Calculator is (" + + Posit8E1Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8E1Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8E1Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8E1Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8E1Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8E1Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit8E1Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit8E1Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8E1Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8E1Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8E1CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit8E1CalculatorSampleRunner.cs index 4982d33cd..12da1942e 100644 --- a/Hast.Samples.Posit/Posit8_1_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8E1CalculatorSampleRunner.cs @@ -10,18 +10,18 @@ namespace Hast.Samples.Posit { - internal class Posit8_1_CalculatorSampleRunner + internal class Posit8E1CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_1_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8E1Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -29,8 +29,8 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw positCalculator.CalculatePowerOfReal( 5, (float)0.5); - var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E1Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E1Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -38,20 +38,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_1Array = new uint[100000]; + var Posit8E1Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; - else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E1Array[i] = new Posit8E1((float)0.25 * 2 * i).PositBits; + else Posit8E1Array[i] = new Posit8E1((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_1Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8E1Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit8_1_Calculator(); + var positCalculator = new Posit8E1Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -81,8 +81,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit8_1_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_1_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E1Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E1Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -100,17 +100,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit8_1Array = new uint[100000]; + var Posit8E1Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_1Array[i] = new Posit8_1((float)0.25 * 2 * i).PositBits; - else Posit8_1Array[i] = new Posit8_1((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E1Array[i] = new Posit8E1((float)0.25 * 2 * i).PositBits; + else Posit8E1Array[i] = new Posit8E1((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit8_1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8E1Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8E1Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8E2Calculator.cs b/Hast.Samples.Posit/Posit8E2Calculator.cs new file mode 100644 index 000000000..dbbd438d4 --- /dev/null +++ b/Hast.Samples.Posit/Posit8E2Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8E2Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8E2((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8E2(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8E2(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8E2((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8E2((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8E2CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit8E2Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit8E2Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8E2Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit8E2Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit8E2Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8E2Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8E2(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8E2((byte)memory.ReadUInt32( Posit8E2Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8E2Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit8E2Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8E2Calculator is (" + + Posit8E2Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8E2Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8E2Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8E2Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8E2Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8E2Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit8E2Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit8E2Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8E2Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8E2Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8E2CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit8E2CalculatorSampleRunner.cs index 214145e89..fb00ff80a 100644 --- a/Hast.Samples.Posit/Posit8_2_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8E2CalculatorSampleRunner.cs @@ -10,18 +10,18 @@ namespace Hast.Samples.Posit { - internal class Posit8_2_CalculatorSampleRunner + internal class Posit8E2CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_2_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8E2Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -29,8 +29,8 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw positCalculator.CalculatePowerOfReal( 5, (float)0.5); - var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E2Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E2Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -38,20 +38,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_2Array = new uint[100000]; + var Posit8E2Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; - else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E2Array[i] = new Posit8E2((float)0.25 * 2 * i).PositBits; + else Posit8E2Array[i] = new Posit8E2((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_2Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8E2Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit8_2_Calculator(); + var positCalculator = new Posit8E2Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -81,8 +81,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit8_2_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_2_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E2Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E2Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -100,17 +100,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit8_2Array = new uint[100000]; + var Posit8E2Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_2Array[i] = new Posit8_2((float)0.25 * 2 * i).PositBits; - else Posit8_2Array[i] = new Posit8_2((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E2Array[i] = new Posit8E2((float)0.25 * 2 * i).PositBits; + else Posit8E2Array[i] = new Posit8E2((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit8_2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8E2Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8E2Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8E3Calculator.cs b/Hast.Samples.Posit/Posit8E3Calculator.cs new file mode 100644 index 000000000..c76aceede --- /dev/null +++ b/Hast.Samples.Posit/Posit8E3Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8E3Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8E3((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8E3(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8E3(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8E3((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8E3((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8E3CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit8E3Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit8E3Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8E3Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit8E3Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit8E3Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8E3Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8E3(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8E3((byte)memory.ReadUInt32( Posit8E3Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8E3Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit8E3Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8E3Calculator is (" + + Posit8E3Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8E3Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8E3Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8E3Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8E3Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8E3Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit8E3Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit8E3Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8E3Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8E3Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8E3CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit8E3CalculatorSampleRunner.cs index 63de617a3..3237db1f7 100644 --- a/Hast.Samples.Posit/Posit8_3_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8E3CalculatorSampleRunner.cs @@ -10,18 +10,18 @@ namespace Hast.Samples.Posit { - internal class Posit8_3_CalculatorSampleRunner + internal class Posit8E3CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_3_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8E3Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -29,8 +29,8 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw positCalculator.CalculatePowerOfReal( 5, (float)0.5); - var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E3Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E3Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -38,20 +38,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_3Array = new uint[100000]; + var Posit8E3Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; - else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E3Array[i] = new Posit8E3((float)0.25 * 2 * i).PositBits; + else Posit8E3Array[i] = new Posit8E3((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_3Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8E3Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit8_3_Calculator(); + var positCalculator = new Posit8E3Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -81,8 +81,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit8_3_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_3_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E3Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E3Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -100,17 +100,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit8_3Array = new uint[100000]; + var Posit8E3Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_3Array[i] = new Posit8_3((float)0.25 * 2 * i).PositBits; - else Posit8_3Array[i] = new Posit8_3((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E3Array[i] = new Posit8E3((float)0.25 * 2 * i).PositBits; + else Posit8E3Array[i] = new Posit8E3((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit8_3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8E3Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8E3Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8E4Calculator.cs b/Hast.Samples.Posit/Posit8E4Calculator.cs new file mode 100644 index 000000000..83357a167 --- /dev/null +++ b/Hast.Samples.Posit/Posit8E4Calculator.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Hast.Layer; +using Hast.Transformer.Abstractions.SimpleMemory; +using Lombiq.Arithmetics; + +namespace Hast.Samples.Posit +{ + + public class Posit8E4Calculator + { + public const int CalculateLargeIntegerSum_InputInt32Index = 0; + public const int CalculateLargeIntegerSum_OutputInt32Index = 0; + public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; + public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; + public const int AddPositsInArray_InputPosit32CountIndex = 0; + public const int AddPositsInArray_InputPosit32sStartIndex = 1; + public const int AddPositsInArray_OutputPosit32Index = 2; + public const int CalculatePowerOfReal_InputInt32Index = 0; + public const int CalculatePowerOfReal_InputPosit32Index = 1; + public const int CalculatePowerOfReal_OutputPosit32Index = 0; + + public const int MaxDegreeOfParallelism = 5; + + + public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) + { + var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); + + var a = new Posit8E4((byte)1); + var b = a; + + for (uint i = 1; i < number; i++) + { + a += b; + } + + var result = (int)a; + memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); + } + + public virtual void CalculatePowerOfReal(SimpleMemory memory) + { + var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); + var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); + + var a = new Posit8E4(positToMultiply, true); + var b = a; + + for (uint i = 0; i < number; i++) + { + a *= b; + } + + var result = a.PositBits; + memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); + } + + public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) + { + var numbers = new int[MaxDegreeOfParallelism]; + + var tasks = new Task[MaxDegreeOfParallelism]; + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); + + tasks[i] = Task.Factory.StartNew( + upToNumberObject => + { + var a = new Posit8E4(1); + var b = a; + + for (int j = 1; j < (int)upToNumberObject; j++) + { + a += b; + } + + return (int)a; + }, upToNumber); + } + + Task.WhenAll(tasks).Wait(); + + for (int i = 0; i < MaxDegreeOfParallelism; i++) + { + memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); + } + } + + public virtual void AddPositsInArray(SimpleMemory memory) + { + uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); + + var result = new Posit8E4((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); + + for (int i = 1; i < numberCount; i++) + { + result += new Posit8E4((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); + } + + memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); + } + } + + + public static class Posit8E4CalculatorCalculatorExtensions + { + public static int CalculateIntegerSumUpToNumber( + this Posit8E4Calculator positCalculator, + int number, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(1) + : hastlayer.CreateMemory(configuration, 1); + + memory.WriteInt32( Posit8E4Calculator.CalculateLargeIntegerSum_InputInt32Index, number); + positCalculator.CalculateIntegerSumUpToNumber(memory); + + return memory.ReadInt32( Posit8E4Calculator.CalculateLargeIntegerSum_OutputInt32Index); + } + + public static float CalculatePowerOfReal( + this Posit8E4Calculator positCalculator, + int number, + float real, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(2) + : hastlayer.CreateMemory(configuration, 2); + + memory.WriteInt32( Posit8E4Calculator.CalculatePowerOfReal_InputInt32Index, number); + memory.WriteUInt32( Posit8E4Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8E4(real).PositBits); + + positCalculator.CalculatePowerOfReal(memory); + + return (float)new Posit8E4((byte)memory.ReadUInt32( Posit8E4Calculator.CalculatePowerOfReal_OutputPosit32Index), true); + } + + public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( + this Posit8E4Calculator positCalculator, + int[] numbers, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + if (numbers.Length != Posit8E4Calculator.MaxDegreeOfParallelism) + { + throw new ArgumentException( + "Provide as many numbers as the degree of parallelism of Posit8E4Calculator is (" + + Posit8E4Calculator.MaxDegreeOfParallelism + ")"); + } + + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(Posit8E4Calculator.MaxDegreeOfParallelism) + : hastlayer.CreateMemory(configuration, Posit8E4Calculator.MaxDegreeOfParallelism); + + for (int i = 0; i < numbers.Length; i++) + { + memory.WriteInt32( Posit8E4Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); + } + + positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); + + var results = new int[ Posit8E4Calculator.MaxDegreeOfParallelism]; + + for (int i = 0; i < numbers.Length; i++) + { + results[i] = memory.ReadInt32( Posit8E4Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); + } + + return results; + } + + public static float AddPositsInArray( + this Posit8E4Calculator positCalculator, + uint[] positArray, + IHastlayer hastlayer = null, + IHardwareGenerationConfiguration configuration = null) + { + var memory = hastlayer is null + ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) + : hastlayer.CreateMemory(configuration, positArray.Length + 1); + + memory.WriteUInt32( Posit8E4Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); + + for (var i = 0; i < positArray.Length; i++) + { + memory.WriteUInt32( Posit8E4Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); + } + + positCalculator.AddPositsInArray(memory); + + return (float)new Posit32(memory.ReadUInt32( Posit8E4Calculator.AddPositsInArray_OutputPosit32Index), true); + } + } +} + diff --git a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs b/Hast.Samples.Posit/Posit8E4CalculatorSampleRunner.cs similarity index 77% rename from Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs rename to Hast.Samples.Posit/Posit8E4CalculatorSampleRunner.cs index 78e1d8627..a75548616 100644 --- a/Hast.Samples.Posit/Posit8_4_CalculatorSampleRunner.cs +++ b/Hast.Samples.Posit/Posit8E4CalculatorSampleRunner.cs @@ -10,18 +10,18 @@ namespace Hast.Samples.Posit { - internal class Posit8_4_CalculatorSampleRunner + internal class Posit8E4CalculatorSampleRunner { public static void Configure(HardwareGenerationConfiguration configuration) { - configuration.AddHardwareEntryPointType(); + configuration.AddHardwareEntryPointType(); } public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { RunSoftwareBenchmarks(hastlayer, hardwareRepresentation); - var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8_4_Calculator()); + var positCalculator = await hastlayer.GenerateProxyAsync(hardwareRepresentation, new Posit8E4Calculator()); var integerSumUpToNumber = positCalculator.CalculateIntegerSumUpToNumber(100000, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); @@ -29,8 +29,8 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw positCalculator.CalculatePowerOfReal( 5, (float)0.5); - var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E4Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E4Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -38,20 +38,20 @@ public static async Task Run(IHastlayer hastlayer, IHardwareRepresentation hardw var integerSumsUpToNumbers = positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(numbers, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); - var Posit8_4Array = new uint[100000]; + var Posit8E4Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; - else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E4Array[i] = new Posit8E4((float)0.25 * 2 * i).PositBits; + else Posit8E4Array[i] = new Posit8E4((float)0.25 * -2 * i).PositBits; } - var positsInArraySum = positCalculator.AddPositsInArray(Posit8_4Array); + var positsInArraySum = positCalculator.AddPositsInArray(Posit8E4Array); } public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresentation hardwareRepresentation) { - var positCalculator = new Posit8_4_Calculator(); + var positCalculator = new Posit8E4Calculator(); // Not to run the benchmark below the first time, because JIT compiling can affect it. @@ -81,8 +81,8 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var numbers = new int[Posit8_4_Calculator.MaxDegreeOfParallelism]; - for (int i = 0; i < Posit8_4_Calculator.MaxDegreeOfParallelism; i++) + var numbers = new int[Posit8E4Calculator.MaxDegreeOfParallelism]; + for (int i = 0; i < Posit8E4Calculator.MaxDegreeOfParallelism; i++) { numbers[i] = 100000 + (i % 2 == 0 ? -1 : 1); } @@ -100,17 +100,17 @@ public static void RunSoftwareBenchmarks(IHastlayer hastlayer, IHardwareRepresen Console.WriteLine(); - var Posit8_4Array = new uint[100000]; + var Posit8E4Array = new uint[100000]; for (var i = 0; i < 100000; i++) { - if (i % 2 == 0) Posit8_4Array[i] = new Posit8_4((float)0.25 * 2 * i).PositBits; - else Posit8_4Array[i] = new Posit8_4((float)0.25 * -2 * i).PositBits; + if (i % 2 == 0) Posit8E4Array[i] = new Posit8E4((float)0.25 * 2 * i).PositBits; + else Posit8E4Array[i] = new Posit8E4((float)0.25 * -2 * i).PositBits; } - positCalculator.AddPositsInArray( Posit8_4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + positCalculator.AddPositsInArray( Posit8E4Array,hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw = Stopwatch.StartNew(); - var positsInArraySum = positCalculator.AddPositsInArray( Posit8_4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); + var positsInArraySum = positCalculator.AddPositsInArray( Posit8E4Array, hastlayer, hardwareRepresentation.HardwareGenerationConfiguration); sw.Stop(); Console.WriteLine("Result of addition of posits in array: " + positsInArraySum); diff --git a/Hast.Samples.Posit/Posit8_4_Calculator.cs b/Hast.Samples.Posit/Posit8_4_Calculator.cs deleted file mode 100644 index d8a87ebaa..000000000 --- a/Hast.Samples.Posit/Posit8_4_Calculator.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Hast.Layer; -using Hast.Transformer.Abstractions.SimpleMemory; -using Lombiq.Arithmetics; - -namespace Hast.Samples.Posit -{ - - public class Posit8_4_Calculator - { - public const int CalculateLargeIntegerSum_InputInt32Index = 0; - public const int CalculateLargeIntegerSum_OutputInt32Index = 0; - public const int ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex = 0; - public const int ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex = 0; - public const int AddPositsInArray_InputPosit32CountIndex = 0; - public const int AddPositsInArray_InputPosit32sStartIndex = 1; - public const int AddPositsInArray_OutputPosit32Index = 2; - public const int CalculatePowerOfReal_InputInt32Index = 0; - public const int CalculatePowerOfReal_InputPosit32Index = 1; - public const int CalculatePowerOfReal_OutputPosit32Index = 0; - - public const int MaxDegreeOfParallelism = 5; - - - public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory) - { - var number = memory.ReadUInt32(CalculateLargeIntegerSum_InputInt32Index); - - var a = new Posit8_4((byte)1); - var b = a; - - for (uint i = 1; i < number; i++) - { - a += b; - } - - var result = (int)a; - memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, result); - } - - public virtual void CalculatePowerOfReal(SimpleMemory memory) - { - var number = memory.ReadInt32(CalculatePowerOfReal_InputInt32Index); - var positToMultiply = (byte)memory.ReadUInt32(CalculatePowerOfReal_InputPosit32Index); - - var a = new Posit8_4(positToMultiply, true); - var b = a; - - for (uint i = 0; i < number; i++) - { - a *= b; - } - - var result = a.PositBits; - memory.WriteUInt32(CalculatePowerOfReal_OutputPosit32Index, result); - } - - public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory) - { - var numbers = new int[MaxDegreeOfParallelism]; - - var tasks = new Task[MaxDegreeOfParallelism]; - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i); - - tasks[i] = Task.Factory.StartNew( - upToNumberObject => - { - var a = new Posit8_4(1); - var b = a; - - for (int j = 1; j < (int)upToNumberObject; j++) - { - a += b; - } - - return (int)a; - }, upToNumber); - } - - Task.WhenAll(tasks).Wait(); - - for (int i = 0; i < MaxDegreeOfParallelism; i++) - { - memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i, tasks[i].Result); - } - } - - public virtual void AddPositsInArray(SimpleMemory memory) - { - uint numberCount = memory.ReadUInt32(AddPositsInArray_InputPosit32CountIndex); - - var result = new Posit8_4((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex), true); - - for (int i = 1; i < numberCount; i++) - { - result += new Posit8_4((byte)memory.ReadUInt32(AddPositsInArray_InputPosit32sStartIndex + i), true); - } - - memory.WriteUInt32(AddPositsInArray_OutputPosit32Index, result.PositBits); - } - } - - - public static class Posit8_4_CalculatorCalculatorExtensions - { - public static int CalculateIntegerSumUpToNumber( - this Posit8_4_Calculator positCalculator, - int number, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(1) - : hastlayer.CreateMemory(configuration, 1); - - memory.WriteInt32( Posit8_4_Calculator.CalculateLargeIntegerSum_InputInt32Index, number); - positCalculator.CalculateIntegerSumUpToNumber(memory); - - return memory.ReadInt32( Posit8_4_Calculator.CalculateLargeIntegerSum_OutputInt32Index); - } - - public static float CalculatePowerOfReal( - this Posit8_4_Calculator positCalculator, - int number, - float real, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(2) - : hastlayer.CreateMemory(configuration, 2); - - memory.WriteInt32( Posit8_4_Calculator.CalculatePowerOfReal_InputInt32Index, number); - memory.WriteUInt32( Posit8_4_Calculator.CalculatePowerOfReal_InputPosit32Index, new Posit8_4(real).PositBits); - - positCalculator.CalculatePowerOfReal(memory); - - return (float)new Posit8_4((byte)memory.ReadUInt32( Posit8_4_Calculator.CalculatePowerOfReal_OutputPosit32Index), true); - } - - public static IEnumerable ParallelizedCalculateIntegerSumUpToNumbers( - this Posit8_4_Calculator positCalculator, - int[] numbers, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - if (numbers.Length != Posit8_4_Calculator.MaxDegreeOfParallelism) - { - throw new ArgumentException( - "Provide as many numbers as the degree of parallelism of Posit8_4_Calculator is (" + - Posit8_4_Calculator.MaxDegreeOfParallelism + ")"); - } - - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(Posit8_4_Calculator.MaxDegreeOfParallelism) - : hastlayer.CreateMemory(configuration, Posit8_4_Calculator.MaxDegreeOfParallelism); - - for (int i = 0; i < numbers.Length; i++) - { - memory.WriteInt32( Posit8_4_Calculator.ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]); - } - - positCalculator.ParallelizedCalculateIntegerSumUpToNumbers(memory); - - var results = new int[ Posit8_4_Calculator.MaxDegreeOfParallelism]; - - for (int i = 0; i < numbers.Length; i++) - { - results[i] = memory.ReadInt32( Posit8_4_Calculator.ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i); - } - - return results; - } - - public static float AddPositsInArray( - this Posit8_4_Calculator positCalculator, - uint[] positArray, - IHastlayer hastlayer = null, - IHardwareGenerationConfiguration configuration = null) - { - var memory = hastlayer is null - ? SimpleMemory.CreateSoftwareMemory(positArray.Length + 1) - : hastlayer.CreateMemory(configuration, positArray.Length + 1); - - memory.WriteUInt32( Posit8_4_Calculator.AddPositsInArray_InputPosit32CountIndex, (uint) positArray.Length); - - for (var i = 0; i < positArray.Length; i++) - { - memory.WriteUInt32( Posit8_4_Calculator.AddPositsInArray_InputPosit32sStartIndex + i, positArray[i]); - } - - positCalculator.AddPositsInArray(memory); - - return (float)new Posit32(memory.ReadUInt32( Posit8_4_Calculator.AddPositsInArray_OutputPosit32Index), true); - } - } -} - diff --git a/Hast.Samples.Posit/PositCalculator.cs b/Hast.Samples.Posit/PositCalculator.cs index f3da1500f..a9674679a 100644 --- a/Hast.Samples.Posit/PositCalculator.cs +++ b/Hast.Samples.Posit/PositCalculator.cs @@ -4,18 +4,18 @@ // Generated helper templates // Generated items -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_4_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_3_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_2_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_1_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32_0_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_4_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_3_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_2_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_1_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16_0_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_4_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_3_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_2_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_1_Calculator.cs -// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8_0_Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32E4Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32E3Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32E2Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32E1Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit32E0Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16E4Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16E3Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16E2Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16E1Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit16E0Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8E4Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8E3Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8E2Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8E1Calculator.cs +// D:\DEV\Hastlayer\Hastlayer-SDK\Hast.Samples.Posit\Posit8E0Calculator.cs diff --git a/Hast.Samples.Posit/PositCalculator.tt b/Hast.Samples.Posit/PositCalculator.tt index 997649441..a6b0ae695 100644 --- a/Hast.Samples.Posit/PositCalculator.tt +++ b/Hast.Samples.Posit/PositCalculator.tt @@ -15,8 +15,8 @@ var manager = TemplateFileManager.Create(this); for (var i = 0; i<3; i++){ for(var MaximumExponentSize = 0; MaximumExponentSize <= 4; MaximumExponentSize++){ - var positName = "Posit"+positSizes[i] + "_" + MaximumExponentSize; - var className = positName + "_Calculator"; + var positName = $"Posit{positSizes[i]}E{MaximumExponentSize}"; + var className = $"{positName}Calculator"; var quireSize = 1; while (quireSize < (4*positSizes[i]-8)*(1< + var positCalculatorName = $"Posit{positSize}E{exponentSize}{calculatorType}";#> case Sample.<#=positCalculatorName#>: <#=positCalculatorName#>SampleRunner.Configure(configuration); break; @@ -109,8 +109,8 @@ namespace Hast.Samples.Posit var hardwareRepresentation = await hastlayer.GenerateHardwareAsync( new[] { - typeof(Posit8_0).Assembly, - typeof(Posit8_0_Calculator).Assembly, + typeof(Posit8E0).Assembly, + typeof(Posit8E0Calculator).Assembly, }, configuration); @@ -140,7 +140,7 @@ namespace Hast.Samples.Posit { foreach (var calculatorType in calculatorTypes) { - var positCalculatorName = $"Posit{positSize}_{exponentSize}_{calculatorType}";#> + var positCalculatorName = $"Posit{positSize}E{exponentSize}{calculatorType}";#> case Sample.<#=positCalculatorName#>: await <#=positCalculatorName#>SampleRunner.Run(hastlayer, hardwareRepresentation); break; diff --git a/Hast.Samples.Posit/Sample.cs b/Hast.Samples.Posit/Sample.cs index e8d5f2efd..be5a28874 100644 --- a/Hast.Samples.Posit/Sample.cs +++ b/Hast.Samples.Posit/Sample.cs @@ -2,53 +2,53 @@ namespace Hast.Samples.Posit { internal enum Sample { - Posit8_0_Calculator, - Posit8_0_AdvancedCalculator, - Posit8_0_FusedCalculator, - Posit8_1_Calculator, - Posit8_1_AdvancedCalculator, - Posit8_1_FusedCalculator, - Posit8_2_Calculator, - Posit8_2_AdvancedCalculator, - Posit8_2_FusedCalculator, - Posit8_3_Calculator, - Posit8_3_AdvancedCalculator, - Posit8_3_FusedCalculator, - Posit16_0_Calculator, - Posit16_0_AdvancedCalculator, - Posit16_0_FusedCalculator, - Posit16_1_Calculator, - Posit16_1_AdvancedCalculator, - Posit16_1_FusedCalculator, - Posit16_2_Calculator, - Posit16_2_AdvancedCalculator, - Posit16_2_FusedCalculator, - Posit16_3_Calculator, - Posit16_3_AdvancedCalculator, - Posit16_3_FusedCalculator, - Posit32_0_Calculator, - Posit32_0_AdvancedCalculator, - Posit32_0_FusedCalculator, - Posit32_1_Calculator, - Posit32_1_AdvancedCalculator, - Posit32_1_FusedCalculator, - Posit32_2_Calculator, - Posit32_2_AdvancedCalculator, - Posit32_2_FusedCalculator, - Posit32_3_Calculator, - Posit32_3_AdvancedCalculator, - Posit32_3_FusedCalculator, - Posit64_0_Calculator, - Posit64_0_AdvancedCalculator, - Posit64_0_FusedCalculator, - Posit64_1_Calculator, - Posit64_1_AdvancedCalculator, - Posit64_1_FusedCalculator, - Posit64_2_Calculator, - Posit64_2_AdvancedCalculator, - Posit64_2_FusedCalculator, - Posit64_3_Calculator, - Posit64_3_AdvancedCalculator, - Posit64_3_FusedCalculator, + Posit8E0Calculator, + Posit8E0AdvancedCalculator, + Posit8E0FusedCalculator, + Posit8E1Calculator, + Posit8E1AdvancedCalculator, + Posit8E1FusedCalculator, + Posit8E2Calculator, + Posit8E2AdvancedCalculator, + Posit8E2FusedCalculator, + Posit8E3Calculator, + Posit8E3AdvancedCalculator, + Posit8E3FusedCalculator, + Posit16E0Calculator, + Posit16E0AdvancedCalculator, + Posit16E0FusedCalculator, + Posit16E1Calculator, + Posit16E1AdvancedCalculator, + Posit16E1FusedCalculator, + Posit16E2Calculator, + Posit16E2AdvancedCalculator, + Posit16E2FusedCalculator, + Posit16E3Calculator, + Posit16E3AdvancedCalculator, + Posit16E3FusedCalculator, + Posit32E0Calculator, + Posit32E0AdvancedCalculator, + Posit32E0FusedCalculator, + Posit32E1Calculator, + Posit32E1AdvancedCalculator, + Posit32E1FusedCalculator, + Posit32E2Calculator, + Posit32E2AdvancedCalculator, + Posit32E2FusedCalculator, + Posit32E3Calculator, + Posit32E3AdvancedCalculator, + Posit32E3FusedCalculator, + Posit64E0Calculator, + Posit64E0AdvancedCalculator, + Posit64E0FusedCalculator, + Posit64E1Calculator, + Posit64E1AdvancedCalculator, + Posit64E1FusedCalculator, + Posit64E2Calculator, + Posit64E2AdvancedCalculator, + Posit64E2FusedCalculator, + Posit64E3Calculator, + Posit64E3AdvancedCalculator, + Posit64E3FusedCalculator, } } diff --git a/Hast.Samples.Posit/Sample.tt b/Hast.Samples.Posit/Sample.tt index 2b4d46275..4c259e2e2 100644 --- a/Hast.Samples.Posit/Sample.tt +++ b/Hast.Samples.Posit/Sample.tt @@ -17,7 +17,7 @@ { foreach (var calculatorType in calculatorTypes) { - calculatorNames.Add($"Posit{positSize}_{exponentSize}_{calculatorType}"); + calculatorNames.Add($"Posit{positSize}E{exponentSize}{calculatorType}"); } } } diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index 031332288..7ab788231 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit 031332288758121c13e1a6d00ba993998b163927 +Subproject commit 7ab78823128be6b3a35632b798457e4783b1af2f From a71c95ffb117a5ab3e0ee658d4723fabd7c9b666 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 13 Jan 2022 20:38:52 +0100 Subject: [PATCH 42/43] Updating Lombiq.Arithmetics with PositTemplate code styling --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index 7ab788231..a040dfeb8 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit 7ab78823128be6b3a35632b798457e4783b1af2f +Subproject commit a040dfeb85bb19db04a4149a25799c454229e3d2 From e69245f7a89c3a917132e62e992bdeeeb0853863 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 21 Jan 2022 18:29:17 +0100 Subject: [PATCH 43/43] Updating Lombiq.Arithmetics with PositTemplate code styling improvements --- Lombiq.Arithmetics | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Arithmetics b/Lombiq.Arithmetics index a040dfeb8..e4aeb999f 160000 --- a/Lombiq.Arithmetics +++ b/Lombiq.Arithmetics @@ -1 +1 @@ -Subproject commit a040dfeb85bb19db04a4149a25799c454229e3d2 +Subproject commit e4aeb999f895cbb4e9646e64e2dc0cc85cfa6e91