What is a parallel for loop, and how/when should it be used?

Noah Roth

I've been coding in C++ for years, and I've used threads in the past, but I'm just now starting to learn about multithreaded programming and how it actually works.

So far I'm doing okay with understanding the concepts, but one thing has me stumped.

  • What are parallel for loops, and how do they work?
  • Can any for loop be made parallel?
  • What are the use for them? Performance?
  • Other functionality?

I can't find anything online that explains it well enough for me to understand.

I code in C++, but I'm sure this question can apply to many different programming languages.

Thomas Matthews

What are parallel for loops, and how do they work?

A parallel for loop is a for loop in which the statements in the loop can be run in parallel: on separate cores, processors or threads.

Let us take a summing code:

unsigned int numbers[] = { 1, 2, 3, 4, 5, 6};
unsigned int sum = 0;
const unsigned int quantity = sizeof(numbers) / sizeof (numbers[0]);
for (unsigned int i = 0; i < quantity; ++i)
{
  sum = sum + numbers[i];
};

Calculating a sum does not depend on the order. The sum only cares that all numbers have been added.

The loop could be split into two loops that are executed by separate threads or processors:

// Even loop:
unsigned int even_sum = 0;
for (unsigned int e = 0; e < quantity; e += 2)
{
  even_sum += numbers[e];
}

// Odd summation loop:
unsigned int odd_sum = 0;
for (unsigned int odd = 1; odd < quantity; odd += 2)
{
  odd_sum += numbers[odd];
}

// Create the sum
sum = even_sum + odd_sum;

The even and odd summing loops are independent of each other. They do not access any of the same memory locations.

The summing for loop can be considered as a parallel for loop because its statements can be run by separate processes in parallel, such as separate CPU cores.

Somebody else can supply a more detailed definition, but this is the general example.

Edit 1:

Can any for loop be made parallel?

No, not any loop can be made parallel. Iterations of the loop must be independent from each other. That is, one cpu core should be able to run one iteration without any side effects to another cpu core running a different iteration.

What are the use for them?
Performance?

In general, the reason is for performance. However, the overhead of setting up the loop must be less than the execution time of the iteration. Also, there is overhead of waiting for the parallel execution to finish and join the results together.

Usually data moving and matrix operations are good candidates for parallelism. For example, moving a bitmap or applying a transformation to the bitmap. Huge quantities of data need all the help they can get.

Other functionality?

Yes, there are other possible uses of parallel for loops, such as updating more than one hardware device at the same time. However, the general case is for improving data processing performance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What should be a Parallel.For equivalent of this loop

From Dev

What should be used instead of "setListNavigationCallbacks"?

From Dev

What LayoutParams should be used in an AlertDialog?

From Dev

What size should be used for fgets?

From Dev

In Oracle, PARALLEL is extensively used. What are the differences among PARALLEL, PARALLEL(8), PARALLEL(a,8)?

From Dev

Should multi websocket connection used to achieve parallel communication?

From Dev

Should a range for loop be used instead of iterators on a vector?

From Dev

Should LINQ Expressions be used inside foreach loop or not?

From Java

What return type should be used for setTimeout in TypeScript?

From Dev

Android what is setLevel() and when should it be used?

From Dev

What SqlDbType should be used when creating a table?

From Java

What is std::decay and when it should be used?

From Java

What is std::move(), and when should it be used?

From Java

What are native methods in Java and where should they be used?

From Dev

what should be used New() or var in go?

From Dev

What charsets should be used for url encoding

From Dev

What is LoggingEventData ? How and where should it be used?

From Dev

What collation should be used for french language?

From Dev

What kind of network protocol should be used in this scenario?

From Dev

Static vars - what are they and when should they be used?

From Dev

What minimum'allows' should be used in iptables rules?

From Dev

is cron job what should be used in this case?

From Dev

What collation should be used for french language?

From Dev

What SqlDbType should be used when creating a table?

From Dev

What are form factories and when should they be used?

From Dev

If an XSD does not provide explicitly what encoding should be used, then what encoding should be used by default?

From Dev

What should be considered best practices for this IF loop?

From Java

Where should loop be used to generate dummy data for SwiftUI preview?

From Dev

What is the 'parallel' concept in Rich Hickey's transducers Strange Loop talk?

Related Related

  1. 1

    What should be a Parallel.For equivalent of this loop

  2. 2

    What should be used instead of "setListNavigationCallbacks"?

  3. 3

    What LayoutParams should be used in an AlertDialog?

  4. 4

    What size should be used for fgets?

  5. 5

    In Oracle, PARALLEL is extensively used. What are the differences among PARALLEL, PARALLEL(8), PARALLEL(a,8)?

  6. 6

    Should multi websocket connection used to achieve parallel communication?

  7. 7

    Should a range for loop be used instead of iterators on a vector?

  8. 8

    Should LINQ Expressions be used inside foreach loop or not?

  9. 9

    What return type should be used for setTimeout in TypeScript?

  10. 10

    Android what is setLevel() and when should it be used?

  11. 11

    What SqlDbType should be used when creating a table?

  12. 12

    What is std::decay and when it should be used?

  13. 13

    What is std::move(), and when should it be used?

  14. 14

    What are native methods in Java and where should they be used?

  15. 15

    what should be used New() or var in go?

  16. 16

    What charsets should be used for url encoding

  17. 17

    What is LoggingEventData ? How and where should it be used?

  18. 18

    What collation should be used for french language?

  19. 19

    What kind of network protocol should be used in this scenario?

  20. 20

    Static vars - what are they and when should they be used?

  21. 21

    What minimum'allows' should be used in iptables rules?

  22. 22

    is cron job what should be used in this case?

  23. 23

    What collation should be used for french language?

  24. 24

    What SqlDbType should be used when creating a table?

  25. 25

    What are form factories and when should they be used?

  26. 26

    If an XSD does not provide explicitly what encoding should be used, then what encoding should be used by default?

  27. 27

    What should be considered best practices for this IF loop?

  28. 28

    Where should loop be used to generate dummy data for SwiftUI preview?

  29. 29

    What is the 'parallel' concept in Rich Hickey's transducers Strange Loop talk?

HotTag

Archive