Re: Threading NOT working as expected
- From: Martijn Lievaart <m@xxxxxxxxxxxxxxxx>
- Date: Sat, 1 Mar 2008 18:09:32 +0100
On Sat, 01 Mar 2008 17:04:08 +0100, Peter J. Holzer wrote:
On 2008-02-26 16:39, Ted Zlatanov <tzz@xxxxxxxxxxxx> wrote:
On Mon, 25 Feb 2008 12:05:09 -0800 (PST) Ted <r.ted.byers@xxxxxxxxxx>[number of threads should equal number of CPU cores]
wrote:
This is incorrect. A modern single processor will perform well in a
multithreaded application. Just because a single thread will run
doesn't mean a single thread is doing work at any time.
Most of the time in a modern system is spent waiting for I/O and memory
access. There are some very special cases where the CPU is actually
tied up while the application runs, but memory and disk speeds have
fallen far behind CPU speeds so the CPU will usually be waiting for
something to happen. This is why modern CPUs have ridiculously large
L1 and L2 caches and many prefetching optimizations.
In a multithreaded setup, the CPU has a chance to run several threads
while memory and I/O fetches are happening.
Multithreading won't help for memory fetches. Firstly because CPUs don't
have a way to inform the OS of a slow memory access, and secondly
because the overhead of switching to a different thread would be much
too high for such a (relatively) short wait. There is one exception:
So-called multi-threading CPUs can keep the state of a fixed (and
usually low) number of thrads on the CPU and switch between them. But
these are really just multi-core CPUs which share some of their units.
You are completely right about I/O of course.
Not even. If all those I/Os are going to the same disk, you run the risk
of thrashing, and overall performance goes down instead of up.
Exactly the same behaviour can be seen with processes. Suppose you have a
bunch of files, together much larger than available memory. These files
are input to a program that handles one file and writes another output
file. You can do either:
for f in *; do program "$f" "$f.out"&; done; wait
or
for f in *; do program "$f" "$f.out"; done;
If the program is I/O bound, I expect the second version to be faster
than the first, although it depends on a lot of things.
So think, design, and profile, profile, profile.
M4
.
- Follow-Ups:
- Re: Threading NOT working as expected
- From: Ted Zlatanov
- Re: Threading NOT working as expected
- From: Peter J. Holzer
- Re: Threading NOT working as expected
- References:
- Re: Threading NOT working as expected
- From: Peter J. Holzer
- Re: Threading NOT working as expected
- Prev by Date: Re: Tecnique and/or Modules for Perl and HTML
- Next by Date: Re: Threading NOT working as expected
- Previous by thread: Re: Threading NOT working as expected
- Next by thread: Re: Threading NOT working as expected
- Index(es):
Relevant Pages
|