Class object inside or outside a parallel_for / parallel_for_each?

Wajih

I have been studying about parallel loops (C++11) and testing them with MS visual studio 2013. I am clear about them (lambdas esp.) which are pretty cool.

But my concern is that I have to call a function that performs a simple euclidean distance measure. Function by itself is clear, but I have to move the function to a class called EuclideanDistance and do the euclidean math inside the function Match(vectorA,vectorB) on two vectors which simply is some norm(...) calculation.And returns a floating point value.

Now how do I go about this inside a parallel_for/parallel_foreach loop? Do I create the class object inside the loop or keeping the class object outside the loop will cause inconsistencies? If I understood correctly about parallel loops the function over which it works is basically a clean copy for every thread launched. Does this happen in the case of class functions? My hunch is no! Unless I create an object inside the class as shown in the second code snippet.

e.g. For the sake of readability, I keep the code abbreviated.

vectorA; // Floating point array of 1024 entries.
concurrent_queue vectorQ; // each entry in the queue is a 1024 array
EuclideanDistance euclid;
parallel_for_each(begin,end,[&](auto item)
{
    auto distance = euclid.Match(vectorA,item);
});

Or this will be the right way of doing it?

parallel_for_each(begin,end,[&](auto item)
{
EuclideanDistance euclid;
    auto distance = euclid.Match(vectorA,item);
});

The whole class is nothing more than a single function.

    class EuclideanDistance
    {
public:
       float Match(vectorA,vectorB)
        {
           return norm(vectorA,vectorB); 
        }
    };

Any pitfalls would be highly appreciated!

mattnewport

You are correct that if you define your EuclideanDistance object outside the parallel_for_each lambda body, it will be shared across all the worker threads executing the parallel_for_each. This would be a problem if your Match() function had side effects affecting shared state in your EuclideanDistance object but in that case it is likely defining the object inside the lambda (which would give each execution of the loop body its own local instance) would have different results from defining it outside.

As long as any functions you call on the EuclideanDistance object have no side effects / do not modify shared state then you are fine using one object defined outside the lambda. If you were calling functions with side effects then you would need to do your own synchronization which would likely impact the performance gain of the parallel_for_each significantly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implementing a parallel_for_each function

From Dev

TBB::parallel_for creates too many class/body copies?

From Dev

TBB::parallel_for creates too many class/body copies?

From Dev

Usage of TBB parallel_for_each() with reduction to Atomic counters

From Dev

Simple example program that uses tbb::queueing mutex inside a tbb::parallel_for does not compile

From Dev

C++ parallel_for error

From Dev

PHP Define a new PDO object inside class (outside function)

From Dev

Parallel processing with Python class object

From Dev

Is there a boost function for parallel_for in C++?

From Dev

Reproducible results with parallel_for and random numbers possible?

From Dev

c++ error when use parallel_for

From Dev

Getting iterators within TBB parallel_for loop

From Dev

TBB Parallel_for count, increment variable inaccurate

From Dev

Access object outside of class

From Dev

Using a user-specified function in a parallel_for_each in C++AMP

From Dev

Using this operation -> inside and outside class

From Dev

Check selection inside or outside of a class

From Dev

semicolon inside and outside Promise object

From Dev

Variables inside and outside of object methods

From Dev

Difference in Use statement outside class / inside class

From Dev

Static class method returns object when called outside of classes, but returns empty object when called inside of different classes

From Dev

Concurrency::parallel_for (PPL) is creating too many threads

From Dev

Concurrency::parallel_for (PPL) is creating too many threads

From Dev

Does tbb::parallel_for always utilize the calling thread

From Dev

Overriding Object#equals(Object) outside the class

From Dev

Discriminate between callers inside and outside class hierarchy

From Dev

Builder Pattern inside vs outside class?

From Dev

Loading from database - inside or outside class?

From Dev

static_assert inside/outside class definition

Related Related

  1. 1

    Implementing a parallel_for_each function

  2. 2

    TBB::parallel_for creates too many class/body copies?

  3. 3

    TBB::parallel_for creates too many class/body copies?

  4. 4

    Usage of TBB parallel_for_each() with reduction to Atomic counters

  5. 5

    Simple example program that uses tbb::queueing mutex inside a tbb::parallel_for does not compile

  6. 6

    C++ parallel_for error

  7. 7

    PHP Define a new PDO object inside class (outside function)

  8. 8

    Parallel processing with Python class object

  9. 9

    Is there a boost function for parallel_for in C++?

  10. 10

    Reproducible results with parallel_for and random numbers possible?

  11. 11

    c++ error when use parallel_for

  12. 12

    Getting iterators within TBB parallel_for loop

  13. 13

    TBB Parallel_for count, increment variable inaccurate

  14. 14

    Access object outside of class

  15. 15

    Using a user-specified function in a parallel_for_each in C++AMP

  16. 16

    Using this operation -> inside and outside class

  17. 17

    Check selection inside or outside of a class

  18. 18

    semicolon inside and outside Promise object

  19. 19

    Variables inside and outside of object methods

  20. 20

    Difference in Use statement outside class / inside class

  21. 21

    Static class method returns object when called outside of classes, but returns empty object when called inside of different classes

  22. 22

    Concurrency::parallel_for (PPL) is creating too many threads

  23. 23

    Concurrency::parallel_for (PPL) is creating too many threads

  24. 24

    Does tbb::parallel_for always utilize the calling thread

  25. 25

    Overriding Object#equals(Object) outside the class

  26. 26

    Discriminate between callers inside and outside class hierarchy

  27. 27

    Builder Pattern inside vs outside class?

  28. 28

    Loading from database - inside or outside class?

  29. 29

    static_assert inside/outside class definition

HotTag

Archive