multithreadprocs: added utility functions for blocks

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@938 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
mgaertner
2009-08-20 15:36:29 +00:00
parent 6b156e32d7
commit 387f91f5a1
2 changed files with 37 additions and 3 deletions

View File

@ -58,6 +58,8 @@ type
destructor Destroy; override;
function WaitForIndexRange(StartIndex, EndIndex: PtrInt): boolean;
function WaitForIndex(Index: PtrInt): boolean; inline;
procedure CalcBlock(Index, BlockSize, LoopLength: PtrInt;
out BlockStart, BlockEnd: PtrInt); inline;
property Index: PtrInt read FIndex;
property Group: TProcThreadGroup read FGroup;
property WaitingForIndexStart: PtrInt read FWaitingForIndexStart;
@ -171,10 +173,12 @@ type
StartIndex, EndIndex: PtrInt;
Data: Pointer = nil; MaxThreads: PtrInt = 0);
public
constructor Create;
destructor Destroy; override;
// for debugging only: the critical section is public:
procedure EnterPoolCriticalSection; inline;
procedure LeavePoolCriticalSection; inline;
public
constructor Create;
destructor Destroy; override;
procedure DoParallel(const AMethod: TMTMethod;
StartIndex, EndIndex: PtrInt;
@ -187,6 +191,10 @@ type
procedure DoParallelLocalProc(const LocalProc: Pointer;
StartIndex, EndIndex: PtrInt;
Data: Pointer = nil; MaxThreads: PtrInt = 0); // do not make this inline!
// utility functions for loops:
procedure CalcBlockSize(LoopLength: PtrInt;
out BlockCount, BlockSize: PtrInt; MinBlockSize: PtrInt = 0); inline;
public
property MaxThreadCount: PtrInt read FMaxThreadCount write SetMaxThreadCount;
property ThreadCount: PtrInt read FThreadCount;
@ -195,6 +203,7 @@ type
var
ProcThreadPool: TProcThreadPool = nil;
implementation
{ TMultiThreadProcItem }
@ -251,6 +260,15 @@ begin
Result:=WaitForIndexRange(Index,Index);
end;
procedure TMultiThreadProcItem.CalcBlock(Index, BlockSize, LoopLength: PtrInt;
out BlockStart, BlockEnd: PtrInt);
begin
BlockStart:=BlockSize*Index;
BlockEnd:=BlockStart+BlockSize;
if LoopLength<BlockEnd then BlockEnd:=LoopLength;
dec(BlockEnd);
end;
{ TProcThread }
procedure TProcThread.AddToList(var First: TProcThread;
@ -672,6 +690,21 @@ begin
Data,MaxThreads);
end;
procedure TProcThreadPool.CalcBlockSize(LoopLength: PtrInt; out BlockCount,
BlockSize: PtrInt; MinBlockSize: PtrInt);
begin
if LoopLength<=0 then begin
BlockCount:=0;
BlockSize:=1;
exit;
end;
// split work into equally sized blocks
BlockCount:=ProcThreadPool.MaxThreadCount;
BlockSize:=(LoopLength div BlockCount);
if (BlockSize<MinBlockSize) then BlockSize:=MinBlockSize;
BlockCount:=((LoopLength-1) div BlockSize)+1;
end;
procedure TProcThreadPool.DoParallelIntern(const AMethod: TMTMethod;
const AProc: TMTProcedure; const AFrame: Pointer;
StartIndex, EndIndex: PtrInt; Data: Pointer; MaxThreads: PtrInt);

View File

@ -14,7 +14,8 @@
**********************************************************************}
{
Abstract:
Utility functions using mtprocs.
For example a parallel sort.
}
unit MTPUtils;