unit ThSort;
{
  COSBI: Comprehensive Open Source Benchmarking Initiative
  Copyright (c) 2003 Van Smith
   
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  The current web address of the GNU General Public License is:
  http://www.gnu.org/licenses/gpl.html

  You can contact the authors of this software at:
  cosbi@vanshardware.com
  See www.vanshardware.com or www.cosbi.org for more contact details.
}
interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, COSBI_Common;

type
  TfrmThreads = class(TForm)
    StartBtn: TButton;
    edCount: TEdit;
    chkboxSort: TCheckBox;
    chkboxFib: TCheckBox;
    edN: TEdit;
    Label1: TLabel;
    Panel1: TPanel;
    memoResults: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure StartBtnClick(Sender: TObject);
    procedure RunThreads;
    procedure PiThreadDone(Sender: TObject);
    procedure FibThreadDone(Sender: TObject);
  private
    eFibStartTime : extended;
    eSortStartTime : extended;
    iThreadsRunning : integer;
    iCount : integer;
    iIterate :integer;
    n : integer;
    iNbrOfTests : integer;
    StopWatch : TStopWatch;
  public
  end;

var
  frmThreads: TfrmThreads;

implementation

uses SortThds;

{$R *.DFM}

//type
//  TSortArray =  array[0..300000] of Integer; //array[0..33000] of Integer;

//var
//  ArraysRandom: Boolean;
//  SortArray {BubbleSortArray, SelectionSortArray, QuickSortArray}: TSortArray;

const
  VERSION             = 'v0.11';
  MSG_NOTICE          = 'NOTICE: This test takes one to five minutes per multithreaded iteration.';
  TEST_STARTS         = '**** Test Starts ****';
  TEST_ENDS           = '**** Test Ends ****';
  P4CEL_TOTAL_TIME    = 115.33;
  P4CEL_PI_TIME       = 59.48;
  P4CEL_PI_TIME_2     = 115.33;
  P4CEL_FIB_TIME      = 55.89;
  P4CEL_FIB_TIME_2    = 111.72;
  DEFAULT_N           = 46;
  PI_DIGITS           = 15000;

{ TThreadSortForm }

procedure TfrmThreads.FormCreate(Sender: TObject);
begin
  Caption := Caption + ', Version ' + VERSION;
  StopWatch := TStopWatch.Create;
  StopWatch.Resolution := swresHigh;
  GetSystemInfo(memoResults, StopWatch);
  memoResults.Lines.Add(MSG_NOTICE);
  edN.Text := IntToStr(DEFAULT_N);
end;

procedure TfrmThreads.RunThreads;
begin
  n := StrToInt( edN.Text );
  if n < 1 then n := 1;
  if (chkboxSort.Checked) then begin
    StartBtn.Enabled := False;
    eSortStartTime := StopWatch.SplitTime;
    with TPiThread.Create( PI_DIGITS ) do begin
      inc( iThreadsRunning );
      OnTerminate := PiThreadDone;
    end;
  end;
  if (chkboxFib.Checked) then begin
    StartBtn.Enabled := False;
    eFibStartTime := StopWatch.SplitTime;
    with TFibThread.Create( n ) do begin
      inc( iThreadsRunning );
      OnTerminate := FibThreadDone;
    end;
  end;

//  with TSelectionSort.Create(SelectionSortArray) do
//    OnTerminate := ThreadDone;
//  with TSelectionSort.Create(QuickSortArray) do
//    OnTerminate := ThreadDone;
end;

procedure TfrmThreads.StartBtnClick(Sender: TObject);
begin
  iCount := StrToInt(edCount.Text);
  iIterate := iCount;
  iNbrOfTests := ( integer(chkboxFib.Checked) +  integer(chkboxSort.Checked) );

  if iNbrOfTests = 0 then begin
    showmessage( 'You need to select at least one thread.' );
    exit;
  end; // if

  iCount := iCount * iNbrOfTests;
  iThreadsRunning := 0;

  memoResults.Lines.Add( TEST_STARTS );
  memoResults.Lines.Add('Test, Score, Time(s)');
  memoResults.Lines.Add('====  =====  =======');

  StopWatch.StartTimer;
  RunThreads;

end; // procedure TfrmThreads.StartBtnClick(Sender: TObject);

procedure TfrmThreads.PiThreadDone(Sender: TObject);
var
  eTime    : extended;
  eAvgTime : extended;
  dScore   : double;
begin
  eTime := StopWatch.SplitTime - eSortStartTime;

  if iNbrOfTests = 1 then begin
    dScore := ( P4CEL_PI_TIME / eTime ) * 1000;
    memoResults.Lines.Add(Format('Pi(%d), %n, %n', [PI_DIGITS, dScore, eTime]));
  end else begin
    dScore := ( P4CEL_PI_TIME_2 / eTime ) * 1000;
    memoResults.Lines.Add(Format('Pi(%d), %n, %n', [PI_DIGITS, dScore, eTime]));
  end;

  Dec(iThreadsRunning);
  Dec(iCount);
  if iThreadsRunning = 0 then
  begin
    if iCount = 0 then
    begin
      eTime := StopWatch.StopTimer;
      if iNbrOfTests > 1 then begin
        eAvgTime := eTime / iIterate;
        dScore := ( P4CEL_TOTAL_TIME / eAvgTime ) * 1000;
        memoResults.Lines.Add(Format('2xThreads, %n, %n', [dScore, eAvgTime ]));
      end; // if
      if iIterate > 1 then begin
        memoResults.Lines.Add(Format('Total test time: %n s', [eTime]));
      end; // if
      memoResults.Lines.Add( TEST_ENDS );
      memoResults.Lines.Add( '' );
      StartBtn.Enabled := True;
    end
    else
    begin
      RunThreads;
    end;
  end;
end;

procedure TfrmThreads.FibThreadDone(Sender: TObject);
var
  eTime     : extended;
  eAvgTime  : extended;
  dScore    : double;
begin
  eTime := StopWatch.SplitTime - eFibStartTime;
  dScore := ( P4CEL_FIB_TIME / eTime ) * 1000;

  // we can only calculate scores for default n:
  if n = DEFAULT_N then begin

    if iNbrOfTests = 1 then begin
      dScore := ( P4CEL_FIB_TIME / eTime ) * 1000;
      memoResults.Lines.Add(Format('Fib(%d), %n, %n', [n, dScore, eTime]));
    end else begin
      dScore := ( P4CEL_FIB_TIME_2 / eTime ) * 1000;
      memoResults.Lines.Add(Format('Fib(%d), %n, %n', [n, dScore, eTime]));
    end; // if iNbrOfTests

  end else begin
    memoResults.Lines.Add(Format('Fib(%d) thread time (s), %n s', [n, eTime]));
  end; // if n = DEFAULT_N

  Dec(iThreadsRunning);
  Dec(iCount);

  if iThreadsRunning = 0 then
  begin
    if iCount = 0 then
    begin
      eTime := StopWatch.StopTimer;
      if iNbrOfTests > 1 then begin
        eAvgTime := eTime / iIterate;
        dScore := ( P4CEL_TOTAL_TIME / eAvgTime ) * 1000;
        memoResults.Lines.Add(Format('2xThreads, %n, %n', [dScore, eAvgTime ]));
      end; // if
      if iIterate > 1 then begin
        memoResults.Lines.Add(Format('Total test time: %n', [eTime]));
      end; // if
      memoResults.Lines.Add( TEST_ENDS );
      memoResults.Lines.Add( '' );
      StartBtn.Enabled := True;
    end
    else
    begin
      RunThreads;
    end;
  end;
end;

end.
