JDS
JDS

Reputation: 16978

Using multi-cores for increased parallel performance

I see the point of having, say, a quad core computer, is being to increase throughout. But I'm wondering at exactly what stage of normal day-to-day computing this is happening.

If I write C code to do a matrix multiplication (the "Hello World" of parallel computing) will the compiler on my system automatically know how to optimize this for all 4 cores? Or would I have to explicitly create multiple threads in my program to take advantage of all 4 cores?

Basically what I want to know is how much of a program is made parallel automatically versus how much a programmer can optimize by making multiple threads that the scheduler will send to the different cores.

Upvotes: 2

Views: 161

Answers (4)

Ira Baxter
Ira Baxter

Reputation: 95324

Pretty much if you write a program in any the languages you know about, it won't run in parallel.

To get parallelism in such languages, you have to resort either to built-in language features ("fork" or "do parallel") or to external libraries that somehow use threading.

The functional language guys claim one of the principal values is that there are no side effects, so in fact your entire functional program can be executed in parallell. I find it astonishing that the Haskell guys then proceed to provide you with (and as far as I can tell, insist you use) explicit parallelism primitives where you want it.

Upvotes: 1

rwong
rwong

Reputation: 6162

An increasing number of frameworks and language-integrated features are making declarative parallel computing easier. However, it will still require you to make some changes to your code. The amount of change can be small (a few lines) or large (complete rewrite), depending on the nature of the code and whether there is a good fit between your code and the framework.

There are ways to parallelize that does not require you to manage threads. Those are often termed task-parallelism, data-parallelism, actor, agent, etc.

Upvotes: 1

Loren Pechtel
Loren Pechtel

Reputation: 9083

The system does NOT know how to spread the work between cores. It's up to you to write your program to split the workload. This is only worthwhile when you have a lot of work to be done. That's one of the big things threads are for.

Upvotes: 1

nhahtdh
nhahtdh

Reputation: 56809

You have to create multiple threads yourself if you write in pure C, or use OpenMP (very appropriate for matrix multiplication; you can write code to split the work in a loop to multiple thread with OpenMP) to generate the threading code for you.

However, load balancing must be done by you.

A program must have multiple threads to be able to run on different cores.

Upvotes: 2

Related Questions