One process open multiple threads on multiple CPUs
I am using the code below to open up multiple threads (20+).
However, it seems that all the threads are running on 1 CPU when I do have 4 cores(according to task manager).
I am guessing there is a limitation on where my threads can be run.
In other word, threads can only be open up on the CPU where the original process started.
Obviously something have gone wrong some where.
Anybody got some suggestions?
Thanks a lot in advance :)
Additional information on platform:
I am using Eclipse to code.
Have tried to run on both windows 7 and XP, none works.
Runnable runnable = new XThread();
Thread thread = new Thread(runnable);
thread.start();
public class XThread implements Runnable
{
public void run()
{
}
}
Re: One process open multiple threads on multiple CPUs
Quote:
I am guessing there is a limitation on where my threads can be run.
In other word, threads can only be open up on the CPU where the original process started.
Did you test your assumption? Write an SSCCE with several threads that work at full capacity for a particular duration and watch your CPU...based upon the code you posted (and please use the code tags) I'm not sure how you expect to see 20+ empty Runnable implementations to divide no work across your CPU's. And why 20+? That is a lot of overhead
Re: One process open multiple threads on multiple CPUs
Quote:
Originally Posted by
copeg
Did you test your assumption? Write an SSCCE with several threads that work at full capacity for a particular duration and watch your CPU...based upon the code you posted (and please use the code tags) I'm not sure how you expect to see 20+ empty Runnable implementations to divide no work across your CPU's. And why 20+? That is a lot of overhead
1. I know they are running cause I saw 1 CPU is at max capacity all the time, while all other CPUs are not doing anything.
2. Why 20+?
Cause I notice a significant improvement of running speed when I increase the number of threads.
The reason:
I am processing many small files (30% of my 10000+ files are less than 5k; some of my big files could be up to 1M).
Therefore, there is huge difference in terms of processing speed between the files(mainly due to the time it takes to load the file to my program).
I don't want to open up 3 threads and wait for the big files to finish before opening up the small files.
But this is not important, as if I open up 20+ threads, I should expect other CPUs to take some load.
Re: One process open multiple threads on multiple CPUs
Like I said, an SSCCE. You can describe what you are doing as much as you like - code better lays the foundation for understanding.
Threads have to be managed - gaining and releasing monitors with synchronization adds overhead...there is a sweet spot for number of threads, after which your improvement in optimization may actually take a hit - this is why I advise against 20+ threads. But I will leave it up to you decide what is optimal for your task at hand.
Try running the following and analyze your CPU's, you may have to do it a few times - playing with LENGTH and MAX_THREAD values so that the work gets tough enough to show up in the task manager.
Code java:
/**
* Thread demonstration. With the correct values for LENGTH and MAX_THREAD (OS dependent) one should see work distribute across all
* CPU's during the course of running this script.
*/
public class Test {
private static final int LENGTH = 1000000;
private static final int MAX_THREAD = 4;
private static class MyRunner implements Runnable{
public void run(){
for ( int i = 0; i < LENGTH; i++ ){
double d = Math.cos(Math.PI/1.23d);//do some random but semi-intesive work
for ( int j = 0; j < 20; j++ ){
d = Math.atan(d);
d /= Math.sin(d);
}
}
}
}
public static void main(String[] args){
Runnable[] runners = new Runnable[MAX_THREAD];
for ( int i = 0; i < MAX_THREAD; i++ ){
runners[i] = new MyRunner();
(new Thread(runners[i])).start();
}
}
}
With the right values of LENGTH and THREAD_COUNT, you should see the work distribute across all CPU's. The above is an SSCCE - if you consistently see your work being thrown onto a single CPU regardless of the number of threads or length of work performed, I would say this is support for your conclusion that your threads are running on a single CPU - and if this occurs post your OS.
Re: One process open multiple threads on multiple CPUs
First of all, thank you so much for the quick and detailed reply.
Really appreciate it.
And your code does occupy all 4 CPUs.
I will do more testing to see why my code doesn't.
Thanks,
Re: One process open multiple threads on multiple CPUs
I think I know what's going on now.
Each thread takes very short amount of time to process.
It is very hard to use up to full capacity.
However, in this case, wouldn't I benefit from having more threads than CPUs?
I understand switching between threads is expensive but having idle capacity should help right?
Why am I only seeing a 5% increase in performance when I am running 20 threads instead of 5?
Re: One process open multiple threads on multiple CPUs
Quote:
However, in this case, wouldn't I benefit from having more threads than CPUs?
I understand switching between threads is expensive but having idle capacity should help right?
Why am I only seeing a 5% increase in performance when I am running 20 threads instead of 5?
Why not try it both ways and see? And given you have provided no code, I won't guess at your last question.
For a situation such as this, I will typically create a thread queue: with several threads that pull their operations from a Queue containing Runnable implementations. If you've got 20+ separate things to do, these get placed into the queue for the threads to chew on. See http://en.wikipedia.org/wiki/Thread_pool_pattern