Parallel For Loops

Parallel For Loops are a hassle-free way to supercharge your program with the Parallel Programming Library. The syntax is similar to the standard For loop, with the advantage of each iteration running on in a different task on the thread pool. This allows multiple iterations to run at the same time, taking advantage of the multi-core and hyper-threaded architecture common on laptops, desktops and mobile devices today.

Here is the replay of the Skill Sprint:

Here are the slides:

Update: I was chatting with Allen Bauer today and he mentioned that while you technically can use Queue and Synchronize from within a Parallel For loop, he wouldn’t recommend it because it will dramatically reduce the speed of the loop. It is still faster than a linear loop, but not as fast as it could be. I’ll leave these examples here, but keep that in mind when optimizing your parallel code.

Here is the syntax in Object Pascal. The stride is the first parameter is it is optional. It controls how the iterations are grouped when being sent to the CPUs. Min and Max are your usual start and stop range for the loop. The last parameter is an anonymous method that represents the code to be executed on each iterations. It takes an Index parameter that is either an Integer or Int64 that provides the value of the current iteration.

TParallel.For(Stride, Min, Max, procedure (Idx: Integer)
begin
  if IsPrime(Idx) then
  begin
    TInterlocked.Increment (Tot);
    TThread.Queue(TThread.CurrentThread, procedure
    begin
      Memo1.Lines.Add(Idx.ToString);
    end);
  end;
end);

Here is the C++ code syntax. It takes a event instead of an anonymous method.

// . . .
TParallel::For(NULL, Min, Max, MyIteratorEvent);
// . . . 

void __fastcall TFormThreading::MyIteratorEvent(TObject *Sender, int AIndex)
{
	if(IsPrime(AIndex)){
		TInterlocked::Increment(Tot);
	};
}

There are some great blog posts and videos on the Parallel Programming Library

Stephen Ball has a series of blog posts on the Parallel Programming Library, including a Quick Introduction and one on managing the thread pool. As Stephen points out, while you can customize the thread pool, that doesn’t alway mean you should. Malcolm Groves also has some great blog posts on PPL.

Danny Wind has a great Code Rage 9 session on the Parallel Programming Library (be sure to download his samples). David I. did the C++ version.

Allen Bauer, our Chief Scientist, also has a CodeRage 9 session on the PPL Architecture.

If you want to take advantage of the new C++11 Lambda syntax on Win64 and mobile, then check out this community article or watch this CodeRage 9 video snippet. Get Bruneau and David’s code for C++ and Parallel libraries.

Update: Keep in mind that a Parallel For loop isn’t always the best performance option, but it is really easy to use. Check out Stefan Glienke’s Stack Overflow answer for an alternative using the PPL TTask for even better performance.

This entry was posted in Android, Architecture, iOS, Mobile, Source Code. Bookmark the permalink.

7 Responses to Parallel For Loops

  1. Antonello says:

    hi, interesting post ..
    in firemonkey ios/android app, If I want to listen a TCP socket with tidTCPClient … Can I use a parallel technology ?

    Antonello

  2. Jim McKeeth says:

    Yes you can. You can even mix the PPL with traditional multi-threaded app development.

  3. Antonello says:

    thanks, I did some testing, but often doing refresh the interface, the application crach or freeze … where can I find some examples?

  4. Jim McKeeth says:

    Are you using TTask.Queue to TTask.Synchronize to post the UI updates back to the main UI thread? Queue is probably your best option. Check out Danny Wind’s samples. I don’t think they are server side specific, but you will see examples of Synchronize and Queue.

  5. Antonello says:

    thanks I will see the Danny examples

  6. Pingback: Deep Dive into Futures and the Parallel Programming Library | The Podcast at Delphi.org

  7. if am sending multiple emails in a loop, how can i use parallel so that my loop can be fast as for now i can only loop after the handshake with the mail server. i would love to let a different process handle handshake with the mail server as i proceed with the loop.

Leave a Reply