Results 1 to 6 of 6

Thread: vector parallel processing

  1. #1
    Join Date
    Aug 2010
    Location
    Karaganda, Kazakhstan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default vector parallel processing

    Hi!

    I am trying to implement some parallel processing of vector elements using all CPU cores. Is there an easy way to do this in Qt? I.e. some function that takes (for example) a functor and a vector as arguments and spawns threads as necessary.

    Of course, one could spawn all threads (one for each element) and start them all. Obviously, this will be not the most effective way in terms of performance and memory usage.

    P.S. I am trying to port calls of Parallel.ForEach of .NET Framework 4

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: vector parallel processing

    Try QtConcurrent

  3. #3
    Join Date
    Aug 2010
    Location
    Karaganda, Kazakhstan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector parallel processing

    Thank you! I was using wrong keywords for searching. Qt word is 'conrurrent', not 'parallel'.

    For now I implemented QtConcurrent::run though it's not the approach with the best performance. The strange part is that while concurrent version is somewhat faster than regular version in release build, in debug build it is many times slower. I was able to solve the problem only in this way:

    Qt Code:
    1. #ifdef _DEBUG
    2. for (int i = 0; i < Simulation->CompanyVector.size(); i++)
    3. if (!Simulation->CompanyVector[i]->IsBankrupt)
    4. MakeDecisionsForCompany(date, Simulation->CompanyVector[i]);
    5. #else
    6. vector<QFuture<void>> futureVector;
    7. for (int i = 0; i < Simulation->CompanyVector.size(); i++)
    8. if (!Simulation->CompanyVector[i]->IsBankrupt)
    9. futureVector.push_back(QtConcurrent::run(
    10. this,
    11. &CArtificialIntelligence::MakeDecisionsForCompany,
    12. date,
    13. Simulation->CompanyVector[i]));
    14. for (int i = 0; i < futureVector.size(); i++)
    15. futureVector[i].waitForFinished();
    16. #endif
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector parallel processing

    Because Qt debug code has many asserts especialy with vectors, lists etc.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: vector parallel processing

    Quote Originally Posted by casperbear View Post
    The strange part is that while concurrent version is somewhat faster than regular version in release build, in debug build it is many times slower.
    If you are using Linux or Unix or MacOS, I can suggest using Systemtap or DTrace for debugging. Performance impact is extremely low. However, these tools are not easy.

  6. #6
    Join Date
    Aug 2010
    Location
    Karaganda, Kazakhstan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector parallel processing

    Thank you for your suggestions!
    Unfortunately I use only Windows currently.

    BTW, I finished initial porting of my project from .NET/C# to Qt/C++. It's quite a straight port (I didn't add or remove any functionality of the simulation part), so I made some performance tests.
    Qt/C++ version is 1.33 times faster than .NET/C# version. Both versions used 2 available CPU cores.

    And memory consumption was reduced by several times!

    If you are interested you can download both versions here
    https://sourceforge.net/projects/ittycoon/

Similar Threads

  1. QThread - parallel
    By sgrant327 in forum Qt Programming
    Replies: 13
    Last Post: 13th April 2010, 14:48
  2. vector of vector and computes
    By mickey in forum General Programming
    Replies: 1
    Last Post: 15th May 2008, 12:47
  3. Parallel Interface
    By r00tz in forum Qt Programming
    Replies: 31
    Last Post: 19th November 2007, 12:50
  4. Parallel and serial I/O
    By Roberto in forum Qt Programming
    Replies: 3
    Last Post: 10th October 2007, 11:53
  5. vector of vector size
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th February 2007, 15:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.