No Gain with Boost Threading

Jan 12, 2009 at 1:47am
I have a program I've written using C++. The task it completes falls into the "embarrassingly parallel" realm, so I should be getting a pretty good performance gain using multi-threading over no threading.

However, I'm not, the program performs the same, if not worse than without threads, and I think I know why. In both cases, with and without threading, only one core is fully active. I believe that the affinity is allowing all X number of threads to run on one core, effectively negating any possible performance benefits from threading.

My question here is how do I modify the affinity so that half the threads are run on one core, and half on the other? Again, I'm using the boost thread library on
Windows.

Any help is greatly appreciated.
Jan 12, 2009 at 1:59am
Note that using threads - as you have already seen - isn't a guarantee that your program will run faster, on the contrary each thread you create has a rather large overhead and context switching between threads costs time. Often threading causes programs to actually run slower, a good example is Windows - even with multi-threading/core processor Windows doesn't necessarily run faster however it scales better i.e. runs more apps in parallell better.

That said, I don't think the problem is 'affinity', instead why don't you post a bit of your code so that we can see what you are doing. It could be any number of things that make your code not perform, often some accidental synchronization could cause the whole benefit to go away.

Personally I use threading mostly to have responsive apps, having one thread for the dialog and one or more threads for processing. The app is not 'faster' per se, instead it just looks faster since the user doesn't need to wait.

Jan 12, 2009 at 5:06am
Geez, I don't even know where to begin, I have more than 400 lines of code in several header/source files.

I'll show you how I tackled my threading, and we can go from there:

(Irrelevant code is removed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	
~~~~

boost::thread_group threadGroup;
threadGroup.create_thread(masterThread(0));

for(int i = 0; i < NTHREADS; i++)
{
    threadGroup.create_thread(threads(i));
}

threadGroup.join_all();

~~~~


The number of threads is set using a preprocessor directive:

#define NTHREADS [X]

Where [X] is the number of threads.

I have a loop set to create a number of worker threads, and a master thread that checks the state of the variables processed for a trigger event that ends the loop.

Note that no performance gain is seen between using two threads or two hundred. No matter what I do, only one core is used by the application, and no threading benefits are seen.

As I said, this is an embarrassingly parallel task, so there should be a noticeable performance gain if threading is executed correctly.
Last edited on Jan 12, 2009 at 5:09am
Jan 12, 2009 at 5:47am
Have you considered profiling to identify bottlenecks?
Jan 12, 2009 at 7:08pm
If your problem is parallel then by adding the threads you should see an almost factorial increase (cores above 2 aren't that efficient unfortunately).

But going from 1 to 2 threads (on a dual-core machine) should yield almost a 100% gain.

If your not experiencing the gain then something is obstructing the 2nd thread. Perhaps 1 thread is always in lock waiting for another thread to release an object?


Jan 19, 2009 at 9:06am
>>I have a loop set to create a number of worker threads, and a master thread that checks the state of the variables processed for a trigger event that ends the loop.<<

how does your thread function look like - roughly? Especially the synchronization part.
Last edited on Jan 19, 2009 at 9:11am
Topic archived. No new replies allowed.