473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# Multithreading on dual machine problem

Hi all,

In my code I have a bottleneck which consists of a cpu-intensive loop of
size LENGTH. What I did was to divide that loop into two threads, Thread1
doing the 0 to LENGTH/2 part, and Thread2 doing the LENGTH/2 to LENGTH part.
There is no synchronization between threads, so they should work pretty
freely.

My machine has two processors (dual P3, Windows 2003 server). Now comes the
problem: when running the program, both threads are scheduled on the *same*
processor! I checked many things, for example:

1) Added a while(true); infinite loop, and it resulted in each thread be
scheduled to another processor (this was a test of course, I don't need an
infinite loop!).

2) Added the following code the loops in each thread:
for (int i = 0; l < 10000000; i++);
Just an empty loop that wastes CPU. Strangely enough, this somehow signals
to the scheduler that the threads are cpu-intensive and so each thread is
scheduled on different processor.

I would like to mention again that even without this wasteful loop each
thread is already 100% cpu-intensive, and so it is very strange that the
scheduler does not assign each thread to seperate processor.

As a last resort, I tried using a Win API call to SetThreadAffinityMask to
manually assign each thread to seperate processor. This to no avail, as my
request was ignored! (although the SetThreadAffinityMask function did not
return a fail status.) Again, strangely enough, when I use
SetThreadAffinityMask in the while(true) example above to ask the scheduler
to assign both threads to same processor, the scheduler does respect the
request and assigns both threads to same processor.

Finally, I have to mention that within the loops many memory accesses are
done (though no memory allocations). Is it possible that because of this the
scheduler places both threads on same processor to share L1 cache? (Though
after many years of parallel programming in C/C++ I have never encountered
such a strange behavior).

And if not, is it possible that the problem is because of a bug in .NET
Framework thread handling?

Thanks in advance for any reply that might shed some light on this mystery.

Nov 16 '05 #1
6 4581
David wrote:
Hi all,

In my code I have a bottleneck which consists of a cpu-intensive loop of
size LENGTH. What I did was to divide that loop into two threads, Thread1
doing the 0 to LENGTH/2 part, and Thread2 doing the LENGTH/2 to LENGTH part.
There is no synchronization between threads, so they should work pretty
freely.

My machine has two processors (dual P3, Windows 2003 server). Now comes the
problem: when running the program, both threads are scheduled on the *same*
processor! I checked many things, for example:


That doesn't sound odd at all. The point of threads is simply to allow
processes to run without blocking each other. The same amount of CPU
is utilized, it's just divvy'd up between the threads. If the first
processor has enough juice to run both threads, than so be it.
Nov 16 '05 #2


"John Bailo" wrote:
David wrote:
Hi all,

In my code I have a bottleneck which consists of a cpu-intensive loop of
size LENGTH. What I did was to divide that loop into two threads, Thread1
doing the 0 to LENGTH/2 part, and Thread2 doing the LENGTH/2 to LENGTH part.
There is no synchronization between threads, so they should work pretty
freely.

My machine has two processors (dual P3, Windows 2003 server). Now comes the
problem: when running the program, both threads are scheduled on the *same*
processor! I checked many things, for example:


That doesn't sound odd at all. The point of threads is simply to allow
processes to run without blocking each other. The same amount of CPU
is utilized, it's just divvy'd up between the threads. If the first
processor has enough juice to run both threads, than so be it.


Impossible. Each of the two threads if run alone would already consume 100%
cpu, so it is not possible that "the first processor has enough juice to run
both threads."
Nov 16 '05 #3
David wrote:
Impossible. Each of the two threads if run alone would already consume 100%
cpu, so it is not possible that "the first processor has enough juice to run
both threads."


A while loop that does an increment wouldn't use up a processor.

It's not cumulative.

It's using a little slice of CPU time/ /per/ /second/ with each
increment of the register.
Nov 16 '05 #4
-----Original Message-----
David wrote:
Impossible. Each of the two threads if run alone would already consume 100% cpu, so it is not possible that "the first processor has enough juice to run both threads."
A while loop that does an increment wouldn't use up a

processor.
It's not cumulative.

It's using a little slice of CPU time/ /per/ /second/ with eachincrement of the register.


What does the while loop have to do with the question?
Each of my 2 threads perform a cpu-intensive task (e.g.,
many mathematical calculations, etc) that take several
minutes to complete.

When running 2 threads in parallel, each of them should be
assigned to another processor, which would result in
almost 50% reduction in total time spent (as there are no
synchronizatin points to delay the threads' calculations).
But instead, both are scheduled on same processor, not
taking benefit of the second processor.

P.S. While I appreciate your reply, I have to mention that
I am not new to parallelism (based on your replies, you
have assume that). I have spent many years on parallel
programming with C/C++, both on SMP machine and NUMA
supercomputers. My question is *NOT* about parallelism,
but about *.NET* handling of threads. Again, thanks in
advance for any reply that would shed light on why .NET
messes up the threads.
Nov 16 '05 #5

"David" <an*******@discussions.microsoft.com> wrote in message
news:1d****************************@phx.gbl...
-----Original Message-----
David wrote:
Impossible. Each of the two threads if run alone would already consume 100% cpu, so it is not possible that "the first processor has enough juice to run both threads."


A while loop that does an increment wouldn't use up a

processor.

It's not cumulative.

It's using a little slice of CPU time/ /per/ /second/

with each
increment of the register.


What does the while loop have to do with the question?
Each of my 2 threads perform a cpu-intensive task (e.g.,
many mathematical calculations, etc) that take several
minutes to complete.

When running 2 threads in parallel, each of them should be
assigned to another processor, which would result in
almost 50% reduction in total time spent (as there are no
synchronizatin points to delay the threads' calculations).
But instead, both are scheduled on same processor, not
taking benefit of the second processor.

P.S. While I appreciate your reply, I have to mention that
I am not new to parallelism (based on your replies, you
have assume that). I have spent many years on parallel
programming with C/C++, both on SMP machine and NUMA
supercomputers. My question is *NOT* about parallelism,
but about *.NET* handling of threads. Again, thanks in
advance for any reply that would shed light on why .NET
messes up the threads.


..NET doesn't messes up the threads, it doesn't even handle thread
scheduling, .NET is at the mercy of the OS scheduler for this just like any
other kind of application running on windows.
It's hard to tell what's going on without seeing any code, also we need more
info on the context of the application, things like the type of application
(console, windows...), number of threads and their priorities, the math
classes you are using (pure framework or other native) are a minimum to
start with.

All I can say for now, based on your problem description (the load is spread
over the available CPU's when executing a simple loop in parallel, but it's
serialized when calling into a math library ) is that the math library is
holding a lock effectively preventing the other thread to run in parallel.

Willy.

Nov 16 '05 #6
Did you try putting some of your code into the loop one at a time and
see which line causes the thread to go sequentially?
Nov 17 '05 #7

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

Similar topics

9
by: Tomer Ben-David | last post by:
Hi I have a big j2ee appliction that was so forth run on a single cpu machine. I have tested it on a dual cpu machine, its running much slower (about X3 times slower). I know that...
15
by: Mark Sisson | last post by:
Hey all. I've got a legacy COM dll that I'd like to use in my multithreaded C# app. Problem though is when I create several instances of the interop object and start them on their own threads...
0
by: Plymouth Acclaim | last post by:
Hi guys, We have a problem with Dual AMD64 Opteron/MySQL 4.0.18/Mandrake 10 for a very high volume site. We are evaluating the performance on our new server AMD64 and it seems it's slow compared...
10
by: gianguz | last post by:
The question is about the possible use of inlining to improve performance in a heavy multithreading environment (200+ threads). If we have to work with applications in which threads aren't I/O...
40
by: John Brawley | last post by:
Greetings, all. I have a program I'm trying to speed up by putting it on a new machine. The new machine is a Compaq W6000 2.0 GHz workstation with dual XEON processors. I've gained about 7x speed...
2
by: robert | last post by:
There is a strange freeze/crash only on dual core machines: I have a python app (Python 2.3.5 /Pythonwin build 203 / Windows) running with no stability problems on normal machines (Or a crash is...
1
by: P | last post by:
Hi, I'm working on server application. It use 8 ~ 16 multithread. The problem is when I put a breakpoint and when Visual Studio 2005 stop at that, sometimes IDE got stuck and cant' repaint...
3
by: Paul Sijben | last post by:
I am running a multi-threaded python application in a dual core intel running Ubuntu. I am using python 2.5.1 that I compiled myself. At random points I am getting segmentation faults (sometimes...
3
by: arunairs | last post by:
Hi, In a multi-threaded app on a dual core machine, is it possible to know on which cpu a particular thread is currently executing on? thanks, Arun
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.