C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-26-2009, 09:19 PM   #1
Registered User
 
Join Date: Jan 2009
Posts: 156
questions on multiple thread programming

I perhaps have to resort to multiple thread programming to reduce computational time. I am now studying how to use POSIX library.
I can access a cluster with 8 CPUs, each of which has one core. I was wondering how many threads to create would get approximately best time performance on my cluster? Is it the same number of CPUs? If I am using multiple thread library like POSIX, will the library take care of which thread run on which CPU or do I have to specify this in my code?

Thanks!
lehe is offline   Reply With Quote
Old 03-26-2009, 10:26 PM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
To get the best performance, create one thread per core. The library (or more specifically, the OS scheduler) will take care of which thread runs on which core. You don't need to worry about this.
bithub is offline   Reply With Quote
Old 03-27-2009, 12:06 AM   #3
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by bithub View Post
To get the best performance, create one thread per core. The library (or more specifically, the OS scheduler) will take care of which thread runs on which core. You don't need to worry about this.
I would say 1 worker thread per CPU

plus you can have several IO threads for example which will most of the time spend in waiting state while io operation is been completed
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-27-2009, 01:29 AM   #4
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
I would say 1 worker thread per CPU
Can you explain why you would use 1 thread per CPU instead of 1 thread per core? To software applications, there is no difference.

Quote:
plus you can have several IO threads for example which will most of the time spend in waiting state while io operation is been completed
Why would you create several threads to wait for IO? For the best performance, async IO is the best way to go.
bithub is offline   Reply With Quote
Old 03-27-2009, 01:44 AM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by bithub View Post
Can you explain why you would use 1 thread per CPU instead of 1 thread per core? To software applications, there is no difference.

Why would you create several threads to wait for IO? For the best performance, async IO is the best way to go.
by cpu I mean core

several IO? because you can have a logger thread writign logs to file,
db connection threads - requestion data from remote db server
sochet thread - processing some network connection etc...

it is too complicated to put all these task on one io thread
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-27-2009, 06:48 AM   #6
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
>> I can access a cluster with 8 CPUs, each of which has one core.
Multi-threading is for a single machine. For distributed-parallel programming you'll need something like MPI. Unless you're running a distributed OS providing SSI like openMosix.

gg
Codeplug is offline   Reply With Quote
Old 03-27-2009, 07:13 AM   #7
Registered User
 
Join Date: Jan 2009
Posts: 156
Thanks!
Yes I remember people called my server "cluster". But how to check if it is distributed system and running a distributed OS providing SSI? And which shall I use, multi-threading or distributed-parallel programming?

Here is the info of my server from uname:
Quote:
Linux 2.6.9-78.0.8.ELsmp #1 SMP Wed Nov 19 19:42:38 EST 2008 x86_64 x86_64 x86_64 GNU/Linux
And also info from cat /proc/cpuinfo
Quote:
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7321.81
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:

processor : 1
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 4
siblings : 2
core id : 4
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7315.15
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:

processor : 2
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 3
siblings : 2
core id : 3
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7315.02
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:

processor : 3
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 7
siblings : 2
core id : 7
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7315.10
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:

processor : 4
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7315.04
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:

processor : 5
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 4
siblings : 2
core id : 4
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7314.70
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:

processor : 6
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 3
siblings : 2
core id : 3
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7314.99
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:

processor : 7
vendor_id : GenuineIntel
cpu family : 15
model : 4
model name : Intel(R) Xeon(TM) MP CPU 3.66GHz
stepping : 1
cpu MHz : 3657.792
cache size : 1024 KB
physical id : 7
siblings : 2
core id : 7
cpu cores : 1
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx lm pni monitor ds_cpl est tm2 cid cx16 xtpr
bogomips : 7315.06
clflush size : 64
cache_alignment : 128
address sizes : 40 bits physical, 48 bits virtual
power management:
lehe is offline   Reply With Quote
Old 03-27-2009, 07:24 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
That is one machine for all intents and purposes.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 03-27-2009, 07:37 AM   #9
Registered User
 
Join Date: Jan 2009
Posts: 156
Hi Matsp,
Thanks!
From where did you tell it is not a distribution system? Does "cluster" only refer to distributed system?
So I should stick to multi-threading programming?
lehe is offline   Reply With Quote
Old 03-27-2009, 07:39 AM   #10
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by lehe View Post
Hi Matsp,
Thanks!
From where did you tell it is not a distribution system? Does "cluster" only refer to distributed system?
So I should stick to multi-threading programming?
A cluster/distributed system will not show that you have 8 cores in /proc/cpuinfo. It will only show the cores of THAT system, which will be one or two perhaps.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 03-27-2009, 07:42 AM   #11
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
The term "cluster" implies more than one physical machine (PC). You have a single machine with 8 cores, so Posix "multi-threading" (with pthreads or processes) is appropriate in this case.

>> I was wondering how many threads to create would get approximately best time performance on my cluster?
The answer can depend on what you're doing, but a typical answer is one thread per core - as bithub and vart mentioned.

>> will the library take care of which thread run on which CPU or do I have to specify this in my code?
The typical answer is that you let your OS/library take care of this for you.

gg
Codeplug is offline   Reply With Quote
Old 03-27-2009, 07:44 AM   #12
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Codeplug View Post
>> will the library take care of which thread run on which CPU or do I have to specify this in my code?
The typical answer is that you let your OS/library take care of this for you.
And unless you have SPECIFIC knowledge from understanding the problem nature, the OS in question and processors in question, it is nearly always a waste of time to try to improve on what the OS/library does.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multithreading (flag stopping a thread, ring buffer) volatile ShwangShwing C Programming 3 05-19-2009 07:27 AM
Thread confusion pyrolink C Programming 0 01-29-2006 09:42 PM
C++ Threading? draggy C++ Programming 5 08-16-2005 12:16 PM
[code] Win32 Thread Object Codeplug Windows Programming 0 06-03-2005 03:55 PM
injecting my own thread into the address space of another process - cannot call any f Andrew_5342 Windows Programming 12 05-17-2003 05:58 AM


All times are GMT -6. The time now is 12:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22