Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
Delphi 10.2 Tokyo Professional
|
Re: Geschwindigkeitsgewinn von Multithreading Berechnungen
22. Mai 2010, 14:06
Ok, vom Faktor 2 bin ich weit enffernt. ich poste mal meinen Code:
Der Thread:
Delphi-Quellcode:
type
TPointF = record
X,Y: Extended;
end;
TComplex = record
R: Extended;
I: Extended;
end;
TDataArray = Array of Array of Word;
TWorkDoneEvent = procedure(Sender: TThread; Data: TDataArray) of object;
TMandelbrotThread = class(TThread)
private
FSizeX,FSizeY: Integer;
FData: TDataArray;
FMatrix: Array of Array of TComplex;
FIterations: Integer;
FOnDataAvailable: TWorkDoneEvent;
procedure DoWorkDone;
protected
procedure Execute; override;
public
constructor Create(ASizeX, ASizeY: Integer; P1, P2: TPointF); reintroduce;
destructor Destroy; override;
procedure Init(P1,P2: TPointF);
property Iterations: Integer read FIterations write FIterations;
property OnDataAvailable: TWorkDoneEvent read FOnDataAvailable write FOnDataAvailable;
end;
implementation
constructor TMandelbrotThread.Create(ASizeX,ASizeY: Integer; P1,P2: TPointF);
var i: Integer;
begin
inherited Create(true);
FSizeX := ASizeX;
FSizeY := ASizeY;
SetLength(FData,FSizeY);
SetLength(FMatrix,FSizeY);
for i := 0 to FSizeY - 1 do
begin
SetLength(FData[i],FSizeX);
FillChar(FData[i][0],FSizeX*SizeOf(Word),0);
SetLength(FMatrix[i],FSizeX);
end;
Init(P1,P2);
end;
procedure TMandelbrotThread.Init(P1,P2: TPointF);
var i,j: Integer;
stepX,stepY: Extended;
begin
stepX := abs(P2.X-P1.X)/FSizeX;
stepY := abs(P2.Y-P1.Y)/FSizeY;
for i := 0 to FSizeY - 1 do
for j := 0 to FSizeX - 1 do
begin
FMatrix[i,j].R := P1.X+j*stepX;
FMatrix[i,j].I := P1.Y-i*stepY;
end;
end;
procedure TMandelbrotThread.Execute;
var i,j,k: Integer;
tmp, x: TComplex;
begin
for i := 0 to FSizeY - 1 do
for j := 0 to FSizeX - 1 do
begin
tmp := FMatrix[i,j];
for k := 1 to FIterations do
begin
if CAbs(tmp) > 2 then // CAbs = Betrag der Komplexen Zahl
begin
FData[i,j] := k;
break;
end;
x := CSqr(tmp); // CSqr = Komplexe Zahl "tmp" wird quadriert.
tmp.R := X.R + FMatrix[i,j].R;
tmp.I := X.I + FMatrix[i,j].I;
end;
end;
Synchronize(DoWorkDone);
end;
MainThread:
Delphi-Quellcode:
procedure TForm2.Button2Click(Sender: TObject);
var tmp: Array[0..1] of TMandelbrotThread;
i: Integer;
begin
// Linke hälfte des Bildes
tmp[0] := TMandelbrotThread.Create(500,1000,PointF(-2,2),PointF(0,-2));
tmp[0].FreeOnTerminate := true;
tmp[0].Iterations := 500;
tmp[0].OnDataAvailable := MandelbrotEvent1;
// SetThreadAffinityMask(tmp[0].Handle,1 shl 0);
// Rechte Hälfte
tmp[1] := TMandelbrotThread.Create(500,1000,PointF(0,2),PointF(2,-2));
tmp[1].FreeOnTerminate := true;
tmp[1].Iterations := 500;
tmp[1].OnDataAvailable := MandelbrotEvent2;
// SetThreadAffinityMask(tmp[1].Handle,1 shl 1);
// Start
tmp[0].Resume;
tmp[1].Resume;
end;
Michael "Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
|