Why parallel processing takes more time than sequential processing ? | Sololearn: Learn to code for FREE!


Why parallel processing takes more time than sequential processing ?

I have a code, in that when you execute it in the parallel environment takes more time than in a sequential way. Here is the code https://code.sololearn.com/cCPep2DRYT3l/?ref=app [Code is just for reference, won't actually work on sololearn code playground ] -------------------------------------------------------------------------- Execution instruction - (Linux) For Parallel ~$g++ Bubble.cpp -fopenmp For Sequential ~$g++ Bubble.cpp Output: 1) For Parallel: Execution time is 26742 ms 2) For Sequential: Execution time is 14 ms [NOTE both gives correct output] -------------------------------------------------------------------------- Why is there a time delay? in parallel processing, it is expected that parallel processing will execute faster. but in actual practice, sequential is executing faster. What is the reason behind it?

9/29/2019 10:33:02 AM

Aaditya Deshpande

8 Answers

New Answer


Aaditya Deshpande I shelled out a whole 20 cents and ran your code on 16 cores on AWS, slightly modified: https://code.sololearn.com/c9nyyPizdP79/?ref=app sequential: 718 ms parallel: 218 ms So the parallel code is about 3 times as fast. I'm not sure how you are getting into half a minute territory then, whatever you are running on must really hate multithreaded code.


Some reasons could be 1. size of dataset is too small 2. frequent creation and destruction of parallel regions, which will affect threading 3. Blocking calls, like your parallel code can't do anything till variable "first" value is available, the implementation is more of of sequential implementation with overhead of behind the scene bookkeeping of parallelism. 4. How do you know the code is actually running on multicores? I don't know how to fix this? May be search for some tips and implementation on the net particularly on StackOverflow


Thank you all I got my answer 👍 Firstly my Data set was small changed it to 50000, also clock in c++ needs to reset, so I switched to chrono library Special thanks to Schindlabua 👍 for spending 20 cents, for me😇🙏


<Only experienced w/Java threads Not focusing on the code, threads are usually unpredictable since the OS determines which thread runs when and how it behaves. Sometimes they have to wait or are blocked by similar tasks (since many things are running in the background, we dont know what clashes or if a request is legal and the priority of a thread). more cores will be better since there is more "free space" i assume Also threads use the "memory pyramid" so a thread using a physical disc will run slower than one using the cpu (i dont think we have control over this). Some solutions to make it faster may be to set a high priority to your threads (im not sure if you can), synchronize threads (used mainly for files but may work), make the code more atomic, or use buffers. Threads are really only useful with gui programs, loading + displaying/using media/files concurrently, huge problems, doing thing in the background (saving to a file is the most popular) and problems that can be divided to smaller problems.


BroFarOps 「HAPPY TO HELP」 Morpheus Sonic HonFu


Schindlabua I think, threads here just splitting my array into a logical groups. here "first" variable is for even-odd selection of groups. threads are spawned inside a for loop and does not depend upon the previous thread. for example: 5 1 4 2 3 |5 1| |4 2| 3 //first even --step1 1 5 2 4 3 //after swap 1 |5 2| |4 3| //first odd --depends upon step 1 result 1 2 5 3 4 //after swap |1 2| |5 3| 4 //first even --depends upon step 2 result 1 2 3 5 4 //after swap 1 |2 3| |5 4| //first odd --depends upon step 3 result 1 2 3 4 5 --> Answer here dependencies are present but after the completion of inner for loop and that inner for loop gets executed in parallel. by comparing the number of iterations with sequential bubble sort we can say that this is a better approach for sorting, then why time required is more?


EDIT: Sorry I completely misread your code. You're not doing a classical bubblesort but you've altered it so the inner loop increases by 2 every iteration. It should be fine honestly, let me run some tests.