using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace Azimuth.PerformanceTests { public partial class MainForm : Form { bool QRDecompostion { get; set; } int progress; public MainForm() { InitializeComponent(); } private delegate void ProgressIncrement(); private void StartTest_Click(object sender, EventArgs e) { Thread testThread = null; int workToBeDone = 0; progress = 0; if (QRDecompositionRadio.Checked) { testThread = RunQRTest(out workToBeDone); } else if (LinearLeastSquaresRadio.Checked) { testThread = RunLLSTest(out workToBeDone); } else if (BisectionRadio.Checked) { testThread = RunBisectionTest(out workToBeDone); } else { throw new Exception("Not yet implemented."); } progressBar.Minimum = 0; progressBar.Maximum = workToBeDone; var uiThread = new Thread(delegate() { var startTime = System.DateTime.Now; while (testThread.IsAlive) { progressBar.Invoke((MethodInvoker) delegate() { progressBar.Value = Math.Min(progressBar.Maximum, Math.Max(progressBar.Minimum, Thread.VolatileRead(ref progress))); progressLabel.Text = Thread.VolatileRead(ref progress).ToString() + "/" + workToBeDone.ToString(); }); Thread.Sleep(100); } System.TimeSpan Duration = System.DateTime.Now - startTime; string operationCount = (!Scalar.IsCountingFlops) ? "" : "\nOperation count: " + Scalar.FLOPs; System.Windows.Forms.MessageBox.Show("Done. Took " + Duration.TotalSeconds + " seconds." + operationCount); }); testThread.IsBackground = true; testThread.Start(); uiThread.IsBackground = true; uiThread.Start(); } private Azimuth.DenseLinearAlgebra.RectangularMatrix RandomMatrix(int size) { Random rand = new Random(); var matrix = new Azimuth.DenseLinearAlgebra.RectangularMatrix(size, size); for (int i = 0; i < matrix.RowCount; ++i) for (int j = 0; j < matrix.ColumnCount; ++j) { matrix[i, j] = rand.NextDouble() * 100000; } return matrix; } private Thread RunQRTest(out int doneValue) { doneValue = 350; progress = 0; var matrix = RandomMatrix(doneValue); var workerThread = new Thread(delegate() { Azimuth.DenseLinearAlgebra.QRDecomposition qr = new Azimuth.DenseLinearAlgebra.QRDecomposition(matrix, ref progress); }); return workerThread; } private Thread RunLLSTest(out int doneValue) { doneValue = 1050; progress = 0; var matrix = RandomMatrix(doneValue / 3); var workerThread = new Thread(delegate() { var lls = new Azimuth.DenseLinearAlgebra.LinearLeastSquares(matrix, ref progress); }); return workerThread; } private Thread RunBisectionTest(out int doneValue) { doneValue = 1024*1024; progress = 0; var workerThread = new Thread(delegate() { for (progress = 0; progress < 1024 * 1024; ++progress) { Azimuth.RootFinding.Bisection.FindRoot(x => (x - 4) * (x + 4) * (x - 3) * (x + 2) * (x + 5), new Interval(-10100, 45678)); } }); return workerThread; } } }