Esempio n. 1
0
        static void Main(string[] args)
        {
            Task.Run(async() =>
            {
                using (var hastlayer = await Hastlayer.Create())
                {
                    #region Configuration
                    var configuration = new HardwareGenerationConfiguration("Nexys4 DDR");

                    configuration.AddHardwareEntryPointType <ParallelAlgorithm>();

                    configuration.TransformerConfiguration().AddMemberInvocationInstanceCountConfiguration(
                        new MemberInvocationInstanceCountConfigurationForMethod <ParallelAlgorithm>(p => p.Run(null), 0)
                    {
                        MaxDegreeOfParallelism = ParallelAlgorithm.MaxDegreeOfParallelism
                    });

                    configuration.VhdlTransformerConfiguration().VhdlGenerationMode = VhdlGenerationMode.Debug;

                    hastlayer.ExecutedOnHardware += (sender, e) =>
                    {
                        Console.WriteLine(
                            "Executing " +
                            e.MemberFullName +
                            " on hardware took " +
                            e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds +
                            "ms (net) " +
                            e.HardwareExecutionInformation.FullExecutionTimeMilliseconds +
                            " milliseconds (all together)");
                    };
                    #endregion

                    #region HardwareGeneration
                    var hardwareRepresentation = await hastlayer.GenerateHardware(
                        new[]
                    {
                        typeof(Program).Assembly
                    },
                        configuration);

                    await hardwareRepresentation.HardwareDescription.WriteSource("Hast_IP.vhd");
                    #endregion

                    #region Execution
                    var parallelAlgorithm = await hastlayer.GenerateProxy(hardwareRepresentation, new ParallelAlgorithm());

                    var output1 = parallelAlgorithm.Run(234234);
                    var output2 = parallelAlgorithm.Run(123);
                    var output3 = parallelAlgorithm.Run(9999);

                    var sw        = System.Diagnostics.Stopwatch.StartNew();
                    var cpuOutput = new ParallelAlgorithm().Run(234234);
                    sw.Stop();
                    Console.WriteLine("On CPU it took " + sw.ElapsedMilliseconds + "ms.");
                    #endregion
                }
            }).Wait();

            Console.ReadKey();
        }
Esempio n. 2
0
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     // You can add complete types whose methods you'd like to invoke on the hardware from the outside like this.
     configuration.AddHardwareEntryPointType <PrimeCalculator>();
     // A not statically typed way of doing the same as above would be:
     //configuration.PublicHardwareMemberNamePrefixes.Add("Hast.Samples.SampleAssembly.PrimeCalculator");
     // Note that the bottom version can also be used to add multiple types from under a namespace.
 }
        public static void Configure(HardwareGenerationConfiguration configuration)
        {
            configuration.AddHardwareEntryPointType <ImageContrastModifier>();

            // ImageFilter is not parallelized, so not including it not to take away FPGA resources from
            // ImageContrastModifier.
            //configuration.AddHardwareEntryPointType<ImageFilter>();
        }
Esempio n. 4
0
        public static void Configure(HardwareGenerationConfiguration configuration)
        {
            configuration.AddHardwareEntryPointType <ParallelAlgorithm>();

            configuration.TransformerConfiguration().AddMemberInvocationInstanceCountConfiguration(
                new MemberInvocationInstanceCountConfigurationForMethod <ParallelAlgorithm>(p => p.Run(null), 0)
            {
                MaxDegreeOfParallelism = ParallelAlgorithm.MaxDegreeOfParallelism
            });
        }
        public static void Configure(HardwareGenerationConfiguration configuration)
        {
            configuration.AddHardwareEntryPointType <RecursiveAlgorithms>();

            configuration.TransformerConfiguration().AddMemberInvocationInstanceCountConfiguration(
                new MemberInvocationInstanceCountConfigurationForMethod <RecursiveAlgorithms>("Recursively")
            {
                // If we give these algorithms inputs causing a larger recursion depth then that will
                // cause runtime problems.
                MaxRecursionDepth = 20
            });
        }
        public static void Configure(HardwareGenerationConfiguration configuration)
        {
            configuration.AddHardwareEntryPointType <ParallelAlgorithm>();

            // Note that Hastlayer can figure out how many Tasks will be there to an extent (see comment in
            // ParallelAlgorithm) but if it can't, use a configuration like below:
            //configuration.TransformerConfiguration().AddMemberInvocationInstanceCountConfiguration(
            //    new MemberInvocationInstanceCountConfigurationForMethod<ParallelAlgorithm>(p => p.Run(null), 0)
            //    {
            //        MaxDegreeOfParallelism = ParallelAlgorithm.MaxDegreeOfParallelism
            //    });
        }
        public static void Configure(HardwareGenerationConfiguration configuration)
        {
            // You can add complete types whose methods you'd like to invoke on the hardware from the outside like this.
            configuration.AddHardwareEntryPointType <PrimeCalculator>();
            // A not statically typed way of doing the same as above would be:
            //configuration.PublicHardwareMemberNamePrefixes.Add("Hast.Samples.SampleAssembly.PrimeCalculator");
            // Note that the bottom version can also be used to add multiple types from under a namespace.

            configuration.TransformerConfiguration().AddMemberInvocationInstanceCountConfiguration(
                new MemberInvocationInstanceCountConfigurationForMethod <PrimeCalculator>(p => p.ParallelizedArePrimeNumbers(null), 0)
            {
                MaxDegreeOfParallelism = PrimeCalculator.MaxDegreeOfParallelism
            });
        }
        public static void Configure(HardwareGenerationConfiguration configuration)
        {
            configuration.AddHardwareEntryPointType <ImageContrastModifier>();

            configuration.TransformerConfiguration().AddMemberInvocationInstanceCountConfiguration(
                new MemberInvocationInstanceCountConfigurationForMethod <ImageContrastModifier>(i => i.ChangeContrast(null), 0)
            {
                MaxDegreeOfParallelism = ImageContrastModifier.MaxDegreeOfParallelism
            });

            // ImageFilter is not parallelized, so not including it not to take away FPGA resources from
            // ImageContrastModifier.
            //configuration.AddHardwareEntryPointType<ImageFilter>();
        }
Esempio n. 9
0
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.AddHardwareEntryPointType <MemoryTest>();
 }
Esempio n. 10
0
        static async Task MainTask(string[] args)
        {
            /*
             * 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)");
                };


                // A little helper for later.
                var argsList = (IList <string>)args;
                string GetArgument(string name)
                {
                    name = "-" + name;
                    return(args.Contains(name) ? args[argsList.IndexOf(name) + 1] : null);
                }

                // We need to set what kind of device (FPGA/FPGA board) to generate the hardware for.
                var devices = await hastlayer.GetSupportedDevices();

                if (devices == null || !devices.Any())
                {
                    throw new Exception("No devices are available!");
                }

                // Let's just use the first one that is available unless it's specified.
                if (string.IsNullOrEmpty(Configuration.DeviceName))
                {
                    Configuration.DeviceName = devices.First().Name;
                }
                var targetDeviceName = GetArgument("device") ?? Configuration.DeviceName;
                var selectedDevice   = devices.FirstOrDefault(device => device.Name == targetDeviceName);
                if (selectedDevice == null)
                {
                    throw new Exception($"Target device '{targetDeviceName}' not found!");
                }

                var configuration = new HardwareGenerationConfiguration(selectedDevice.Name);

                // If you're running Hastlayer in the Client flavor, you also need to configure some credentials:
                var remoteClientConfiguration = configuration.RemoteClientConfiguration();
                remoteClientConfiguration.AppName   = GetArgument("appname") ?? Configuration.AppName;
                remoteClientConfiguration.AppSecret = GetArgument("appsecret") ?? Configuration.AppSecret;
                if (hastlayerConfiguration.Flavor == HastlayerFlavor.Client &&
                    remoteClientConfiguration.AppSecret == "appsecret")
                {
                    throw new InvalidOperationException(
                              "You haven't changed the default remote credentials! Write to [email protected] to receive access if you don't have yet.");
                }

                // If the sample was selected in the command line use that, or otherwise the default.
                Configuration.SampleToRun = (Sample)Enum.Parse(typeof(Sample), GetArgument("sample") ?? Configuration.SampleToRun.ToString(), true);

                // Letting the configuration of samples run. Check out those methods too!
                switch (Configuration.SampleToRun)
                {
                case Sample.Fix64Calculator:
                    Fix64CalculatorSampleRunner.Configure(configuration);
                    break;

                case Sample.FSharpParallelAlgorithm:
                    FSharpParallelAlgorithmSampleRunner.Configure(configuration);
                    break;

                case Sample.GenomeMatcher:
                    GenomeMatcherSampleRunner.Configure(configuration);
                    break;

                case Sample.ParallelAlgorithm:
                    ParallelAlgorithmSampleRunner.Configure(configuration);
                    break;

                case Sample.ImageProcessingAlgorithms:
                    ImageProcessingAlgorithmsSampleRunner.Configure(configuration);
                    break;

                case Sample.Loopback:
                    LoopbackSampleRunner.Configure(configuration);
                    break;

                case Sample.MemoryTest:
                    MemoryTestSampleRunner.Configure(configuration);
                    break;

                case Sample.MonteCarloPiEstimator:
                    MonteCarloPiEstimatorSampleRunner.Configure(configuration);
                    break;

                case Sample.ObjectOrientedShowcase:
                    ObjectOrientedShowcaseSampleRunner.Configure(configuration);
                    break;

                case Sample.PrimeCalculator:
                    PrimeCalculatorSampleRunner.Configure(configuration);
                    break;

                case Sample.RecursiveAlgorithms:
                    RecursiveAlgorithmsSampleRunner.Configure(configuration);
                    break;

                case Sample.SimdCalculator:
                    SimdCalculatorSampleRunner.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[]
                {
                    // Selecting any type from the sample assembly here just to get its Assembly object.
                    typeof(PrimeCalculator).Assembly,
                    typeof(Fix64).Assembly,
                    typeof(FSharpSampleAssembly.FSharpParallelAlgorithmContainer).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.
                try
                {
                    switch (Configuration.SampleToRun)
                    {
                    case Sample.Fix64Calculator:
                        await Fix64CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.FSharpParallelAlgorithm:
                        await FSharpParallelAlgorithmSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.GenomeMatcher:
                        await GenomeMatcherSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.ParallelAlgorithm:
                        await ParallelAlgorithmSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.ImageProcessingAlgorithms:
                        await ImageProcessingAlgorithmsSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.Loopback:
                        await LoopbackSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.MemoryTest:
                        await MemoryTestSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.MonteCarloPiEstimator:
                        await MonteCarloPiEstimatorSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.ObjectOrientedShowcase:
                        await ObjectOrientedShowcaseSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.PrimeCalculator:
                        await PrimeCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.RecursiveAlgorithms:
                        await RecursiveAlgorithmsSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    case Sample.SimdCalculator:
                        await SimdCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation);

                        break;

                    default:
                        break;
                    }
                }
                catch (AggregateException ex) when(ex.InnerException is HardwareExecutionResultMismatchException)
                {
                    // If you set ProxyGenerationConfiguration.VerifyHardwareResults to true (when calling
                    // GenerateProxy()) then everything will be computed in software as well to check the hardware.
                    // You'll get such an exception if there is any mismatch. This shouldn't normally happen, but it's
                    // not impossible in corner cases.
                    var mismatches    = ((HardwareExecutionResultMismatchException)ex.InnerException).Mismatches;
                    var mismatchCount = mismatches.Count();

                    Console.WriteLine($"There {(mismatchCount == 1 ? "was a mismatch" : $"were {mismatchCount} mismatches")} between the software and hardware execution's results! Mismatch{(mismatchCount == 1 ? string.Empty : $"es")}:");
Esempio n. 11
0
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.AddHardwareEntryPointType <Fix64Calculator>();
 }
Esempio n. 12
0
        public async Task <IHastlayer> InitializeHastlayer(bool verifyOutput, bool randomSeedEnable)
        {
            _verifyOutput     = verifyOutput;
            _randomSeedEnable = randomSeedEnable;

            LogItFunction("Creating Hastlayer Factory...");

            var hastlayer = await Hastlayer.Create();

            hastlayer.ExecutedOnHardware += (sender, e) =>
            {
                LogItFunction("Hastlayer timer: " +
                              e.HardwareExecutionInformation.HardwareExecutionTimeMilliseconds + "ms (net) / " +
                              e.HardwareExecutionInformation.FullExecutionTimeMilliseconds + " ms (total)"
                              );
            };

            var configuration = new HardwareGenerationConfiguration((await hastlayer.GetSupportedDevices()).First().Name);

            configuration.VhdlTransformerConfiguration().VhdlGenerationConfiguration = VhdlGenerationConfiguration.Debug;
            configuration.EnableCaching = false;

            LogItFunction("Generating hardware...");

            if (_kpzTarget.HastlayerParallelizedAlgorithm())
            {
                configuration.AddHardwareEntryPointType <KpzKernelsParallelizedInterface>();
                configuration.TransformerConfiguration().AddAdditionalInlinableMethod <RandomMwc64X>(r => r.NextUInt32());
            }
            else if (_kpzTarget.HastlayerPlainAlgorithm())
            {
                configuration.AddHardwareEntryPointType <KpzKernelsInterface>();
            }
            else // if (kpzTarget == KpzTarget.PrngTest)
            {
                configuration.AddHardwareEntryPointType <PrngTestInterface>();
            }


            var hardwareRepresentation = await hastlayer.GenerateHardware(new[] {
                typeof(KpzKernelsParallelizedInterface).Assembly,
                typeof(RandomMwc64X).Assembly
            }, configuration);

            await hardwareRepresentation.HardwareDescription.WriteSource(VhdlOutputFilePath);

            LogItFunction("Generating proxy...");

            if (_kpzTarget.HastlayerOnFpga())
            {
                var proxyConf = new ProxyGenerationConfiguration
                {
                    VerifyHardwareResults = _verifyOutput
                };

                if (_kpzTarget == KpzTarget.Fpga)
                {
                    Kernels = await hastlayer.GenerateProxy(
                        hardwareRepresentation,
                        new KpzKernelsInterface(),
                        proxyConf);
                }
                else if (_kpzTarget == KpzTarget.FpgaParallelized)
                {
                    KernelsParallelized = await hastlayer.GenerateProxy(
                        hardwareRepresentation,
                        new KpzKernelsParallelizedInterface(),
                        proxyConf);
                }
                else //if(kpzTarget == KpzTarget.PrngTest)
                {
                    KernelsP = await hastlayer.GenerateProxy(
                        hardwareRepresentation,
                        new PrngTestInterface(),
                        proxyConf);
                }

                LogItFunction("FPGA target detected");
            }
            else //if (kpzTarget == KpzTarget.HastlayerSimulation())
            {
                Kernels             = new KpzKernelsInterface();
                KernelsParallelized = new KpzKernelsParallelizedInterface();
                LogItFunction("Simulation target detected");
            }

            if (_kpzTarget.HastlayerPlainAlgorithm())
            {
                LogItFunction("Running TestAdd...");
                uint resultFpga = Kernels.TestAddWrapper(4313, 123);
                uint resultCpu  = 4313 + 123;
                if (resultCpu == resultFpga)
                {
                    LogItFunction(string.Format("Success: {0} == {1}", resultFpga, resultCpu));
                }
                else
                {
                    LogItFunction(string.Format("Fail: {0} != {1}", resultFpga, resultCpu));
                }
            }

            if (_kpzTarget == KpzTarget.PrngTest)
            {
                LogItFunction("Running TestPrng...");

                var   kernelsCpu = new PrngTestInterface();
                ulong randomSeed = 0x37a92d76a96ef210UL;
                var   smCpu      = kernelsCpu.PushRandomSeed(randomSeed);
                var   smFpga     = KernelsP.PushRandomSeed(randomSeed);
                LogItFunction("PRNG results:");
                bool success = true;

                for (int PrngTestIndex = 0; PrngTestIndex < 10; PrngTestIndex++)
                {
                    uint prngCpuResult  = kernelsCpu.GetNextRandom(smCpu);
                    uint prngFpgaResult = KernelsP.GetNextRandom(smFpga);
                    if (prngCpuResult != prngFpgaResult)
                    {
                        success = false;
                    }
                    LogItFunction(String.Format("{0}, {1}", prngCpuResult, prngFpgaResult));
                }

                if (success)
                {
                    LogItFunction("TestPrng succeeded!");
                }
                else
                {
                    LogItFunction("TestPrng failed!");
                }
            }
            return(hastlayer);
        }
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.AddHardwareEntryPointType <ObjectOrientedShowcase>();
 }
Esempio n. 14
0
        static void Main(string[] args)
        {
            // Wrapping the whole program into Task.Run() is a workaround for async just to be able to run all this from
            // inside a console app.
            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 [email protected] 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.Fix64Calculator:
                        Fix64CalculatorSampleRunner.Configure(configuration);
                        break;

                    case Sample.GenomeMatcher:
                        GenomeMatcherSampleRunner.Configure(configuration);
                        break;

                    case Sample.ParallelAlgorithm:
                        ParallelAlgorithmSampleRunner.Configure(configuration);
                        break;

                    case Sample.ImageProcessingAlgorithms:
                        ImageProcessingAlgorithmsSampleRunner.Configure(configuration);
                        break;

                    case Sample.Loopback:
                        LoopbackSampleRunner.Configure(configuration);
                        break;

                    case Sample.MonteCarloAlgorithm:
                        MonteCarloAlgorithmSampleRunner.Configure(configuration);
                        break;

                    case Sample.ObjectOrientedShowcase:
                        ObjectOrientedShowcaseSampleRunner.Configure(configuration);
                        break;

                    case Sample.PrimeCalculator:
                        PrimeCalculatorSampleRunner.Configure(configuration);
                        break;

                    case Sample.RecursiveAlgorithms:
                        RecursiveAlgorithmsSampleRunner.Configure(configuration);
                        break;

                    case Sample.SimdCalculator:
                        SimdCalculatorSampleRunner.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[]
                    {
                        // Selecting any type from the sample assembly here just to get its Assembly object.
                        typeof(PrimeCalculator).Assembly,
                        typeof(Fix64).Assembly
                    },
                        configuration);

                    Console.WriteLine("Hardware generation finished, writing VHDL source to file.");

                    if (!string.IsNullOrEmpty(Configuration.VhdlOutputFilePath))
                    {
                        await hardwareRepresentation.HardwareDescription.WriteSource(Configuration.VhdlOutputFilePath);
                    }

                    Console.WriteLine("VHDL source written to file, starting hardware execution.");

                    // Running samples.
                    switch (Configuration.SampleToRun)
                    {
                    case Sample.Fix64Calculator:
                        await Fix64CalculatorSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.GenomeMatcher:
                        await GenomeMatcherSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.ParallelAlgorithm:
                        await ParallelAlgorithmSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.ImageProcessingAlgorithms:
                        await ImageProcessingAlgorithmsSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.Loopback:
                        await LoopbackSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.MonteCarloAlgorithm:
                        await MonteCarloAlgorithmSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.ObjectOrientedShowcase:
                        await ObjectOrientedShowcaseSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.PrimeCalculator:
                        await PrimeCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.RecursiveAlgorithms:
                        await RecursiveAlgorithmsSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    case Sample.SimdCalculator:
                        await SimdCalculatorSampleRunner.Run(hastlayer, hardwareRepresentation);
                        break;

                    default:
                        break;
                    }
                }
            }).Wait();

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.AddHardwareEntryPointType <MonteCarloPiEstimator>();
     configuration.TransformerConfiguration().AddAdditionalInlinableMethod <RandomXorshiftLfsr16>(p => p.NextUInt16());
 }
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.AddHardwareEntryPointType <Loopback>();
 }
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.AddHardwareEntryPointType <FSharpParallelAlgorithmContainer.FSharpParallelAlgorithm>();
 }
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.HardwareEntryPointMemberNamePrefixes.Add("Hast.Samples.SampleAssembly.MonteCarloAlgorithm");
 }
Esempio n. 19
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();
        }
Esempio n. 20
0
 public static void Configure(HardwareGenerationConfiguration configuration)
 {
     configuration.AddHardwareEntryPointType <GenomeMatcher>();
 }