473,782 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threads not getting expected speedup

I'm trying to write some code which will split up a vector into two
halves and run a method on the objects in the vector using two seperate
threads. I was hoping to see a near linear speedup on an SMP machine,
but I'm finding that the code below takes almost exactly the same
amount of time as when I iterate through the vector, and don't use any
threads at all (using only one processor). I'm running this on a Dual
Athlon machine under Redhat 9, and when I look at the processors,
they're both maxed out. Thanks in advance for any help.

andy

SMPEvlautionWra pper processor1Threa d; // This Class extends
Thread
SMPEvlautionWra pper processor2Threa d; // and holds some other
vars.

Vector tempEvalPopulat ion1=new Vector();
Vector tempEvalPopulat ion2=new Vector();
for (int j=0;j<tempRetur nVector.size()/2;j++){

tempEvalPopulat ion1.add((Indiv idual)tempRetur nVector.element At(j));
}
for (int
j=tempReturnVec tor.size()/2;j<tempReturnV ector.size();j+ +){

tempEvalPopulat ion2.add((Indiv idual)tempRetur nVector.element At(j));
}

processor1Threa d= new SMPEvlautionWra pper();
processor2Threa d= new SMPEvlautionWra pper();

processor1Threa d.initVars("pro cessor
1",(Vector)temp EvalPopulation1 .clone(),(Evalu ationFunction)t empEval.copy()) ;
processor2Threa d.initVars("pro cessor
2",(Vector)temp EvalPopulation2 .clone(),(Evalu ationFunction)t empEval.copy()) ;
try{
processor1Threa d.start();
processor2Threa d.start();
}catch(Exceptio n ex){
ex.printStackTr ace();
}

while(true){
if
((processor1Thr ead.finished==t rue)&&(processo r2Thread.finish ed==true)){
break;
}
try{
Thread.sleep(10 );
}catch(Exceptio n ex){
ex.printStackTr ace();
}

}

Jul 17 '05 #1
8 2305
an***********@h otmail.com wrote:
I'm trying to write some code which will split up a vector into two
halves and run a method on the objects in the vector using two seperate
threads. I was hoping to see a near linear speedup on an SMP machine,
but I'm finding that the code below takes almost exactly the same
amount of time as when I iterate through the vector, and don't use any
threads at all (using only one processor). I'm running this on a Dual
Athlon machine under Redhat 9, and when I look at the processors,
they're both maxed out. Thanks in advance for any help.

andy

SMPEvlautionWra pper processor1Threa d; // This Class extends
Thread
SMPEvlautionWra pper processor2Threa d; // and holds some other
vars.

Vector tempEvalPopulat ion1=new Vector();
Vector tempEvalPopulat ion2=new Vector();
for (int j=0;j<tempRetur nVector.size()/2;j++){

tempEvalPopulat ion1.add((Indiv idual)tempRetur nVector.element At(j));
}
for (int
j=tempReturnVec tor.size()/2;j<tempReturnV ector.size();j+ +){

tempEvalPopulat ion2.add((Indiv idual)tempRetur nVector.element At(j));
}
It might be better to use the List.subList() to split the Vectors.

processor1Threa d= new SMPEvlautionWra pper();
processor2Threa d= new SMPEvlautionWra pper();

processor1Threa d.initVars("pro cessor
1",(Vector)temp EvalPopulation1 .clone(),(Evalu ationFunction)t empEval.copy()) ;
processor2Threa d.initVars("pro cessor
2",(Vector)temp EvalPopulation2 .clone(),(Evalu ationFunction)t empEval.copy()) ;
try{
processor1Threa d.start();
processor2Threa d.start();
}catch(Exceptio n ex){
ex.printStackTr ace();
}

while(true){
if
((processor1Thr ead.finished==t rue)&&(processo r2Thread.finish ed==true)){
break;
}
try{
Thread.sleep(10 );
}catch(Exceptio n ex){
ex.printStackTr ace();
}

}


This loop at the end here is probably a problem. What you really need
to do here is use Object.wait() and Object.notify() to synchronize your
threads. That would get rid of the polling.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

Also, avoid Vector's in favor of a simpler form of List. Do the
synchronization yourself at a much higher granularity.

The fact is that parallelization often turns into a mild form of
paralysis.

Jul 17 '05 #3
> > SMPEvlautionWra pper processor1Threa d; // This Class extends
Thread
SMPEvlautionWra pper processor2Threa d; // and holds some other

processor1Threa d= new SMPEvlautionWra pper();
processor2Threa d= new SMPEvlautionWra pper();

try{
processor1Threa d.start();
processor2Threa d.start();
}catch(Exceptio n ex){
ex.printStackTr ace();
}

while(true){
if
((processor1Thr ead.finished==t rue)&&(processo r2Thread.finish ed==true)){ break;
}
try{
Thread.sleep(10 );
}catch(Exceptio n ex){
ex.printStackTr ace();
}

}

This loop at the end here is probably a problem. What you really

need to do here is use Object.wait() and Object.notify() to synchronize your threads. That would get rid of the polling.

HTH,
Ray

--
XML is the programmer's duct tape.


Thanks alot for the help. I'm pretty new working with threads outside
of the simplest contexts. I tried substituting wait() on a dummy
variable for sleep() in the loop, and did get the speedup that I was
hoping for, i know this is the wrong way to be going about it (I get an
IllegalMonitorE xception). Can you suggest which object i should be
waiting on and which should be notifying? It also seems like if I
synchronize the entire method, both threads will operate on a single
processor. I'm having a hard time finding any good explinations in the
web literature.
Thanks.

Andy

Jul 17 '05 #4
I also probably should have mentioned that this entire method is itself
running from within a worker thread for a Swing GUI.
Thanks again

Andy

Jul 17 '05 #5
an***********@h otmail.com wrote:
SMPEvlautionWra pper processor1Threa d; // This Class extends
Thread
SMPEvlautionWra pper processor2Threa d; // and holds some other

processor1Threa d= new SMPEvlautionWra pper();
processor2Threa d= new SMPEvlautionWra pper();

try{
processor1Threa d.start();
processor2Threa d.start();
}catch(Exceptio n ex){
ex.printStackTr ace();
}

while(true){
if

((processor1Thr ead.finished==t rue)&&(processo r2Thread.finish ed==true)){
break;
}
try{
Thread.sleep(10 );
}catch(Exceptio n ex){
ex.printStackTr ace();
}

}


This loop at the end here is probably a problem. What you really


need
to do here is use Object.wait() and Object.notify() to synchronize


your
threads. That would get rid of the polling.

HTH,
Ray

--
XML is the programmer's duct tape.

Thanks alot for the help. I'm pretty new working with threads outside
of the simplest contexts. I tried substituting wait() on a dummy
variable for sleep() in the loop, and did get the speedup that I was
hoping for, i know this is the wrong way to be going about it (I get an
IllegalMonitorE xception). Can you suggest which object i should be
waiting on and which should be notifying? It also seems like if I
synchronize the entire method, both threads will operate on a single
processor. I'm having a hard time finding any good explinations in the
web literature.
Thanks.


I'd suggest that you make a new object for each thread. Make sure you
read the javadoc for Object.wait() and Object.notify() carefully.

The code might look something like this:

// Thread code
public void run()
{
// Do work
// ...

synchronized (sem)
{
sem.notifyAll() ;
}
}

// In the original thread
Object sem1 = new Object();
Object sem2 = new Object();
processor1Threa d.setSem(sem1);
processor2Threa d.setSem(sem2);
processor1Threa d.start();
processor2Threa d.start();

synchronized (sem1)
{
if (!processor1Thr ead.finished)
{
sem1.wait();
}
}

synchronized (sem2)
{
if (!processor2Thr ead.finished)
{
sem2.wait();
}
}

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #6

Raymond DeCampo wrote:
an***********@h otmail.com wrote:
SMPEvlautionWra pper processor1Threa d; // This Class extends
Thread
SMPEvlautionWra pper processor2Threa d; // and holds some other
processor1Threa d= new SMPEvlautionWra pper();
processor2Threa d= new SMPEvlautionWra pper();

try{
processor1Threa d.start();
processor2Threa d.start();
}catch(Exceptio n ex){
ex.printStackTr ace();
}

while(true){
if

((processor1Thr ead.finished==t rue)&&(processo r2Thread.finish ed==true)){
break;
}
try{
Thread.sleep(10 );
}catch(Exceptio n ex){
ex.printStackTr ace();
}

}
This loop at the end here is probably a problem. What you really


need
to do here is use Object.wait() and Object.notify() to synchronize


your
threads. That would get rid of the polling.

HTH,
Ray

--
XML is the programmer's duct tape.

Thanks alot for the help. I'm pretty new working with threads outside of the simplest contexts. I tried substituting wait() on a dummy
variable for sleep() in the loop, and did get the speedup that I was hoping for, i know this is the wrong way to be going about it (I get an IllegalMonitorE xception). Can you suggest which object i should be
waiting on and which should be notifying? It also seems like if I
synchronize the entire method, both threads will operate on a single processor. I'm having a hard time finding any good explinations in the web literature.
Thanks.


I'd suggest that you make a new object for each thread. Make sure

you read the javadoc for Object.wait() and Object.notify() carefully.

The code might look something like this:

// Thread code
public void run()
{
// Do work
// ...

synchronized (sem)
{
sem.notifyAll() ;
}
}

// In the original thread
Object sem1 = new Object();
Object sem2 = new Object();
processor1Threa d.setSem(sem1);
processor2Threa d.setSem(sem2);
processor1Threa d.start();
processor2Threa d.start();

synchronized (sem1)
{
if (!processor1Thr ead.finished)
{
sem1.wait();
}
}

synchronized (sem2)
{
if (!processor2Thr ead.finished)
{
sem2.wait();
}
}

HTH,
Ray

--
XML is the programmer's duct tape.


Thanks again for the help, I understand now how the synchronization
and the semaphores work. The code works fine now (without any
exceptions), but I'm back to not getting any speedup. All of the work
that's being done takes about 1 minute. When I run only one of the
threads (half of the work) It finishes in 30 sec. When I run both
threads, its back to 1 minute. I put some output into the threads, and
I can see that they are running concurrently, but each piece of work
within each thread seems to take almost exactly twice as long as when
only one thread is running. As well, I can see from the system monitor
that when both threads are running, only about 90% of the total cpus
are being used. I thought that this may be due to some kind of locks
on the variables that I was passing to the threads, so I initalized
them internally. Still no luck. I also tried running this code
seperately (not in the GUI worker thread that it was origionaly in).
What I did find though is that when I substitute a Thread.sleep(10 00)
for the work that was being done (a Monte-Carlo simulation) ,
Both threads will run in exactly the same time as one thread. I'd
planned on writing in some client/server code to run this on a cluster,
so I guess I'll just go with that, but I'd thought that the SMP threads
would be faster for just two processors.

Thanks again.

andy

Jul 17 '05 #7
Just figured it out...
I replaced the work being done with some arbitrary multiplications , and
the threads ran perfectly- on 100% of the cpus. I'm thinking that it
might have something to do with the random number generator that was
being accessed by the Monte-Carlo simulations in each thread.
thanks again,

andy

Jul 17 '05 #8

That was a very good guess. Random number generators have to serialize
access because they have state that gets updated. Try giving each
thread their own random number generator to keep for themselves (gen =
new Random()). This gives you more methods to call anyway.

I/O can cause the same problem. Big thread-specific buffers are the
answer to that.

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7452
by: Thomas Womack | last post by:
If I have a dual-processor hyperthreaded machine (so with four CPU contexts), will a python program distribute threads over all four logical processors? I ask because I'm fairly sure that this *does* happen using the threading extensions in MFC, and fairly sure that it *doesn't* when using Java, so I don't see that the result is obvious for python. Tom
9
1427
by: Klaus Neuner | last post by:
Hello, I wrote a program that does essentially the following: for rule in rules: for line in line_list: line = my_apply(rule, line) line_list contains the lines of some input text.
29
3233
by: Jeffrey Maitland | last post by:
Hello all, I am in the process of writing a multithreading program and what I was wondering is a sleep command in an executing function will affect the threads below it? Here is a basic example of what I mean. def main(): temp_var = True while temp_var == True: if
6
2484
by: Chaman Singh | last post by:
Hello, I wanted to use Threads on Sun 4 node SMP. In my program I use STL map and I noticed that I don't get any speedup from using threads and I suspect that it has something to do STL map. Can anybody throw some light on this issue ? csv
8
12949
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found that the object will not be garbage collected while the thread is running. Fair enough, the documented explanation is that the GC compresses objects on the heap and needs to update references, the references will be invalid for a couple of moments...
10
1682
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes, OS execute (All have same priority) Thread#1 may be other threads, Thread#2 may be other threads, Thread#3 may be other threads,
3
5974
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
6
2276
by: BogusException | last post by:
I'll ask a simple question: How can someone generate any number of threads from within a simple "for" ot Timer loop/sub (including indefinitely)? Dim i As Integer For i = 1 To 10 Dim t As New Thread(AddressOf DoSomethingSub) t.IsBackground = True t.Start()
20
541
by: greg.johnsen80 | last post by:
I have been struggling with the following problem: In my console application I have function1 calling function2 which in turn uses threads to do computationally intensive tasks. Some of the threads generate exceptions which cause the whole application to abort. How can I avoid termination and make function1 call function2 again in case of exceptions. Any ideas would be highly appreciated.
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.