Learn how to a build a cloud-first strategyRegister Now

x
Solved

How do I implement a Parallel For Loop in Delphi XE2?

Posted on 2011-10-29
Medium Priority
1,295 Views
I have a program structured as follows:
hits:=0;
for I:=0 to NumberOfIterations-1 do
begin
{perform some calculations dependent on random number generation to determine a value x}
if x>0 then hits:=hits+1;
end;{For Loop}
FailureProbability:=hits/NumberOfIterations;
The number of iterations is in the tens of millions. Can you show me how to implement a Parallel For loop in Delphi XE2 so that I can share the workload amongst all four processors on my computer?

0
Question by:riskassessor

LVL 8

Accepted Solution

lomo74 earned 2000 total points
ID: 37053219

``````program loop;

{\$APPTYPE CONSOLE}

const
NumberOfIterations = 30000000;

type
private
FIdx: Integer;
FHits: Cardinal;
protected
procedure Execute; override;
public
constructor Create(Idx: Integer); reintroduce;
end;

begin
FIdx := Idx;
FHits := 0;
inherited Create(False);
end;

var
i, x, start, finish: Integer;
begin
start := (NumberOfIterations div 4) * FIdx;
finish := start + (NumberOfIterations div 4) - 1;

for i := start to finish do begin
if x > 0 then
Inc(FHits);
end;
end;

var
hndarr: array[0..3] of THandle;
i: Integer;
FailureProbability: Extended;

begin
for i := 0 to 3 do begin
hndarr[i] := thrarr[i].Handle;
end;

WaitForMultipleObjects(4, @hndarr, True, INFINITE);

FailureProbability := Extended(thrarr[0].Hits + thrarr[1].Hits + thrarr[2].Hits + thrarr[3].Hits) / NumberOfIterations;

for i := 0 to 3 do
thrarr[i].Free;
end.
``````

maybe SetThreadAffinityMask can help too (you can force a thread to run on a specific processor), but I can't remember how to use it exactly and I haven't a compiler right now. BTW if the code contains any errors don't blame me I wrote it blindly with a notepad :-)
0

Author Closing Comment

ID: 37053275
Brilliant! Very impressive that you wrote it without access to a compiler. The only coding error was the attempted typecast to Extended near the end. All I had to do was delete the word Extended and everything worked. Thanks so much.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month20 days, 19 hours left to enroll