How does SMP work?

lotharamious

Member
Feb 17, 2006
61
0
0
Hey all,

For all of you code buffs out there, I was just wondering how a chunk of code (or whole program for that matter) can be coded for SMP.

Thanks
 

SamurAchzar

Platinum Member
Feb 15, 2006
2,422
3
76
From the perspective of a normal user application (i.e. anything which is not at the Kernel level), you just need to utilize multithreading.
The Operating System will schedule the different threads to run on different processors by itself.

 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
From the perspective of a normal user application (i.e. anything which is not at the Kernel level), you just need to utilize multithreading.
The Operating System will schedule the different threads to run on different processors by itself.
It's generally not trivial - spawning threads is easy, but dividing up work intelligently to maximize performance can be difficult. Correctly using locks for shared data can get very hard too, especially if you want very fine-grained locking to minimize contention.
 

BrownTown

Diamond Member
Dec 1, 2005
5,314
1
0
yeah, there are some things which are inherently parellel. For example and of the "divide and conquer" type algorthims.
 

SamurAchzar

Platinum Member
Feb 15, 2006
2,422
3
76
Originally posted by: CTho9305
It's generally not trivial - spawning threads is easy, but dividing up work intelligently to maximize performance can be difficult. Correctly using locks for shared data can get very hard too, especially if you want very fine-grained locking to minimize contention.

What I was referring to was the difference in SMP vs. uniprocessor systems from the user application perspective - there is none (perhaps thread processor affinity but that's neglible).
The sames rules of writing high quality multithreaded code apply in both cases.

That's opposed to the problems in kernel programming when more than one processor is present, especially problems with interrupt handling on multiple processors.

The other problem is that some kernels operate in a non-interruptible monolithic context (i.e. a context switch never occurs during kernel execution).
In such cases, on a uniprocessor system, the only way to interrupt the program flow (and cause possible synchronization problems) is by an interrupt request.
But what happens when you introduce another processor into the system? The kernel is no longer truely "monolithic" - the other processor might be executing the same code, even when your local processor has interrupt disabled.
It requires some adaption from kernel programmers.

Linux 2.6.x, IIRC, allows context switches in kernel mode, so some part of this problem is eliminated (there's less difference between execution on uni- and multiprocessor systems in that regard).

- All of that is written from the top of my head, I might be wrong on some aspects... Quite a while since I've written SMP-compatible driver code.

 

borealiss

Senior member
Jun 23, 2000
913
0
0
Originally posted by: lotharamious
Hey all,

For all of you code buffs out there, I was just wondering how a chunk of code (or whole program for that matter) can be coded for SMP.

Thanks

this depends on a lot of different factors. scheduling is a huge problem depending on how much performance you want vs. overhead involved when dealing with concurrency. like ctho9305 said, breaking up the task load using locks can be very tricky. it's a tradeoff between this overhead and the maximum concurrency, and how much performance you will gain out of smp + effort involved vs. programming a single-threaded application. it also depends on what types of algorithms you want to use. dynamic programming, heuristics, prediction, etc... prediction has its penalties too and may not be worth it. it really depends on your application at hand.