Re: Big Threading Problem
Unfortunately parallel sorting algorithms aren't really all that good. MergeSort is one that should be fairly parallelizable, though....
I'm suspicious of those four ReleaseMutex calls at the start of main(). First of all, I can't really see how a mutex would be necessary for a parallelized mergesort-----any parallel section should be working on disjoint data anyway. Second, releaseing a mutex before locking it is a bit strange.
This is enough to zero-initialize your array, incidentally:
Re: Big Threading Problem
Quote:
Originally Posted by
compuKidd
I need to do merge sort with threading but it seems to take more time with threading rather than no threading :(, and I have core quad!
Note that multithreading carries overhead so there's a threshold when the parallel algorithm starts beating the sequential one. N==100 may simply be too small. Try 1000, 10.000 or even 100.000.
Re: Big Threading Problem
Ok for sizes of 1,000->100,000 having one thread is better, but never 2 threads is better than 1 or 0 threads and never 3 is better than 2 and never 4 is better than 3 :( that's strange I thought threading is better than this.
if the overhead is that big why use it at all? I mean his assignment was to demonstrate that threading is better but it ended up worse :(
as for the mutex its just to keep the number of threads from exceeding a certain number.
any ideas how I can improve my results?
Re: Big Threading Problem
Quote:
Originally Posted by
compuKidd
<snip> that's strange I thought threading is better than this.</snip>
And what leads you to believe that the "problem" is with threading itself and not with your usage of it.
_uj brought up a critical point in dealing with threading. The overhead breaks down into three distinct domains.
1) Thread management overhead is the creation and destruction costs involved regardless of the work performed by the thread.
2) Resource contention overhead is anything that will block a thread outside of your control. For example if you are doing 100% compute bound work, and spin off more threads than there are processor units (CPU/Core/Ht), then you will likely see WORSE performance (or at least not better)
3) Synchronization overhead is anything your thread(s) are blocked explicitly waiting for another thread to complete some work so that data is available.
ANY implementation using multiple threads must address all three of these issues (ideally by emperical measurement).
Re: Big Threading Problem
Quote:
Originally Posted by
compuKidd
that's strange I thought threading is better than this.
Although multithreading carries an overhead, that may not be the major problem in this case.
I suggest you set the sorting aside for a while and concentrate on getting the actual multithreading going. Let one task/thread do something simple like just looping and printing something every second or so. Then add a task/thread and make sure it runs in parallel. Then add more. You should note that each added task up to 4 (because you got a quad) increases the throughtput. You should also note that the processor utilization increases with 25% for each task (on Windows you can easily watch this using the Task Manager).
Regarding the efficiency of multithreading, using one task per thread is often called coarse-grain multitasking. A more efficient way is to use fine-grain multitasking. In this model a pool of threads are kept running all the time and some manager assigns logical tasks to be processed by idle threads. A smart manager can balance the workload very efficiently and to a great extent reduce the overhead associated with multitasking. But this is nothing you program yourself. Support for this is in the pipeline both for Java and .NET. And for native C++ you got the Intel TBB library for example.
Re: Big Threading Problem
Quote:
Originally Posted by
_uj
But this is nothing you program yourself. Support for this is in the pipeline both for Java and .NET. .
While you are correct that this will be "baked-in" as of .NET 4.0 (aka VS-2010), people have had an opportunity to work with it for over 6 months:
http://www.microsoft.com/downloads/d...displaylang=en
Re: Big Threading Problem
Quote:
Originally Posted by
TheCPUWizard
The same is true for Java. It's planned for the upcoming Java 7 but available already,
http://www.infoq.com/news/2007/07/concurrency-java-se-7
And here's TBB for C++ programmers,
http://www.threadingbuildingblocks.org/