Ejemplo n.º 1
0
        public static bool IsPrimeNumber(this PrimeCalculator primeCalculator, uint number)
        {
            var memory = new SimpleMemory(1);

            memory.WriteUInt32(PrimeCalculator.IsPrimeNumber_InputUInt32Index, number);
            primeCalculator.IsPrimeNumber(memory);
            return(memory.ReadBoolean(PrimeCalculator.IsPrimeNumber_OutputBooleanIndex));
        }
Ejemplo n.º 2
0
        public static bool[] ArePrimeNumbers(this PrimeCalculator primeCalculator, uint[] numbers)
        {
            var memory = new SimpleMemory(numbers.Length + 1);

            memory.WriteUInt32(PrimeCalculator.ArePrimeNumbers_InputUInt32CountIndex, (uint)numbers.Length);

            for (int i = 0; i < numbers.Length; i++)
            {
                memory.WriteUInt32(PrimeCalculator.ArePrimeNumbers_InputUInt32sStartIndex + i, numbers[i]);
            }

            primeCalculator.ArePrimeNumbers(memory);


            var output = new bool[numbers.Length];

            for (int i = 0; i < numbers.Length; i++)
            {
                output[i] = memory.ReadBoolean(PrimeCalculator.ArePrimeNumbers_OutputUInt32sStartIndex + i);
            }
            return(output);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Task.Run(async() =>
            {
                using (var hastlayer = Hast.Xilinx.HastlayerFactory.Create())
                {
                    #region Configuration
                    hastlayer.ExecutedOnHardware += (sender, e) =>
                    {
                        Console.WriteLine(
                            "Executing " +
                            e.MemberFullName +
                            " on hardware took " +
                            e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds +
                            "ms (net) " +
                            e.HardwareExecutionInformation.FullExecutionTimeMilliseconds +
                            " milliseconds (all together)");
                    };


                    var hardwareConfiguration = new HardwareGenerationConfiguration();
                    hardwareConfiguration.PublicHardwareMemberNamePrefixes.Add("Hast.Samples.Psc.PrimeCalculator");
                    var hardwareRepresentation = await hastlayer.GenerateHardware(
                        new[]
                    {
                        typeof(Program).Assembly
                    },
                        hardwareConfiguration);

                    var stopwatch = new Stopwatch();
                    #endregion


                    var primeCalculator = await hastlayer
                                          .GenerateProxy(hardwareRepresentation, new PrimeCalculator());


                    #region Basic samples
                    var isPrime    = primeCalculator.IsPrimeNumber(15);
                    var isPrime2   = primeCalculator.IsPrimeNumber(13);
                    var arePrimes  = primeCalculator.ArePrimeNumbers(new uint[] { 15, 493, 2341, 99237 });
                    var arePrimes2 = primeCalculator.ArePrimeNumbers(new uint[] { 13, 493 });
                    Debugger.Break();
                    #endregion

                    #region Generating some big numbers
                    var numberCount = 4000;
                    var numbers     = new uint[numberCount];
                    for (uint i = (uint)(uint.MaxValue - numberCount); i < uint.MaxValue; i++)
                    {
                        numbers[i - (uint.MaxValue - numberCount)] = (uint)i;
                    }
                    #endregion

                    #region Crunching big numbers
                    stopwatch.Restart();
                    var cpuCalculator  = new PrimeCalculator();
                    var arePrimesOnCpu = cpuCalculator.ArePrimeNumbers(numbers.Take(20).ToArray());
                    StopAndWriteTime(stopwatch);
                    Debugger.Break();
                    stopwatch.Restart();
                    primeCalculator.ArePrimeNumbers(numbers.Take(20).ToArray());
                    StopAndWriteTime(stopwatch);
                    Debugger.Break();
                    stopwatch.Restart();
                    var arePrimes3 = primeCalculator.ArePrimeNumbers(numbers);
                    StopAndWriteTime(stopwatch);
                    Debugger.Break();
                    #endregion

                    #region Sequential launch
                    stopwatch.Restart();
                    var sequentialLaunchArePrimes = new bool[10][];
                    for (uint i = 0; i < 10; i++)
                    {
                        sequentialLaunchArePrimes[i] = primeCalculator.ArePrimeNumbers(numbers);
                    }
                    StopAndWriteTime(stopwatch);
                    Debugger.Break();
                    #endregion

                    #region Parallel launch
                    var taskScheduler = new QueuedTaskScheduler(2);
                    stopwatch.Restart();
                    var parallelLaunchedIsPrimeTasks = new List <Task <bool[]> >();
                    for (uint i = 0; i < 10; i++)
                    {
                        parallelLaunchedIsPrimeTasks.Add(Task.Factory.StartNew(() =>
                                                                               primeCalculator.ArePrimeNumbers(numbers),
                                                                               new CancellationToken(),
                                                                               TaskCreationOptions.None,
                                                                               taskScheduler));
                    }
                    var parallelLaunchedArePrimes = await Task.WhenAll(parallelLaunchedIsPrimeTasks);
                    StopAndWriteTime(stopwatch);
                    Debugger.Break();
                    #endregion

                    #region Looking at VHDL
                    File.WriteAllText(@"C:\HastlayerSample.vhd", ToVhdl(hardwareRepresentation.HardwareDescription));
                    Debugger.Break();
                    #endregion
                }
            }).Wait();
        }