Exemplo n.º 1
0
        private void RunMainApplication(PrimeTable p)
        {
            MainWindow mw = new MainWindow(p);

            RedGate.Profiler.UserEvents.ProfilerEvent.SignalEvent("Main window created");
            Close();
            mw.Show();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generate a sample of random numbers and check them for primality.
        /// </summary>
        /// <returns>A ResultSet summarizing the results</returns>
        public static ResultSet Generate(ApplicationOptions options, PrimeTable primeTable)
        {
            IPrimalityTest backupAlgorithm;
            // Primes seen so far
            int primeCount = 0;

            // buffer for randomly generated bytes
            byte[] randomBuffer = new byte[sizeof(UInt64)];

            switch (options.Algorithm)
            {
            case PrimalityAlgorithm.BruteForce:
                backupAlgorithm = PrimalityAlgorithmFactory.CreateBruteForceAlgorithm(primeTable.MaxPrimeKnown);
                break;

            case PrimalityAlgorithm.MillerRabin:
                backupAlgorithm = PrimalityAlgorithmFactory.CreateMillerRabinAlgorithm();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            // The combined algorithm (pre-generated table followed by backupAlgorithm)
            IPrimalityTest lookupPlusAlgorithm = new PrecomputedTablePrimalityTest(backupAlgorithm, primeTable);

            // Begin timing
            DateTime now = DateTime.UtcNow;

            for (int i = 0; i < options.SampleSize; i++)
            {
                Rnd.NextBytes(randomBuffer);
                // Using modulo this way is bogus because it throws away the randomness from the upper bits,
                // but it's ok for this example, as we're merely generating load to demonstrate the profiler.
                ulong candidate = BitConverter.ToUInt64(randomBuffer, 0) % options.MaxRandom + 1; // +1 for non-zero

                if (lookupPlusAlgorithm.IsPrime(candidate))
                {
                    primeCount++;
                }
            }

            return(new ResultSet
            {
                Duration = DateTime.UtcNow - now,
                PrimeCount = primeCount,
                PrimePercentage = (double)primeCount / options.SampleSize,
                SampleSize = options.SampleSize,
                MaxRandom = options.MaxRandom,
                Algorithm = options.Algorithm.ToString()
            });
        }
Exemplo n.º 3
0
        internal MainWindow(PrimeTable primeTable) : this()
        {
            m_data = new ObservableCollection <ResultSet>();
            dataGrid1.ItemsSource = m_data;
            m_primeTable          = primeTable;
            m_options             = new ApplicationOptions
            {
                MaxPrime   = m_primeTable.MaxPrimeKnown,
                SampleSize = 500000,
                MaxRandom  = m_primeTable.MaxPrimeKnown * 2,
#if MILLER_RABIN
                Algorithm = PrimalityAlgorithm.MillerRabin
#else
                Algorithm = PrimalityAlgorithm.BruteForce
#endif
            };
            m_taskInProgress = false;
        }
Exemplo n.º 4
0
        private PrimeTable PrimeTableCreationTask()
        {
            PrimeTable primeTable = new PrimeTable();

            RedGate.Profiler.UserEvents.ProfilerEvent.SignalEvent("Beginning initialization");
            Thread.CurrentThread.Name = "ProgressUpdate";
            // Expiry time data
            DateTime expiry = DateTime.UtcNow.AddSeconds(ProgressBarTimeInSeconds);
            long     expiryIntervalInTicks = DateTime.MinValue.AddSeconds(ProgressBarTimeInSeconds).Ticks;

            var cTokenSource        = new CancellationTokenSource();
            var cToken              = cTokenSource.Token;
            var primeGenerationTask = Task.Factory.StartNew(() => primeTable.CreatePrimes(TableSize, cToken), cToken);
            // This is close enough to the correct interval
            var waitingTask = Task.Factory.StartNew(() => Thread.Sleep(TimeSpan.FromSeconds(ProgressBarTimeInSeconds)));
            // Schedule updates to the progress bar
            var dispatcherTimer = new DispatcherTimer(TimeSpan.FromSeconds(1),
                                                      DispatcherPriority.Normal,
                                                      (sender, e) => UpdateProgressBar(expiry, expiryIntervalInTicks),
                                                      Dispatcher);

            dispatcherTimer.Start();
            // Either the task will complete in time, or the waiting task will finish first
            int completedTaskIndex = Task.WaitAny(new[] { primeGenerationTask, waitingTask });

            dispatcherTimer.Stop();

            if (completedTaskIndex == 1)
            {
                // The task didn't complete in time, so cancel it
                cTokenSource.Cancel();
                // Wait for acknowledgement (or it may have completed)
                while (primeGenerationTask.Status != TaskStatus.Canceled && !primeGenerationTask.IsCompleted)
                {
                }
            }

            RedGate.Profiler.UserEvents.ProfilerEvent.SignalEvent("Initialization complete");
            return(primeTable);
        }