• 0

How to make use of multi-core CPU?


Question

Hello. Nowadays CPU usually have 2 cores or even 4 cores. I want to know how can programmers write programs which can make use of power of multi-cores CPU? By threading? Or configuring the compiler to support multi-core?

Thanks~ :)

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Threading, instead of running a heavy CPU using task in the main thread, you run it in another thread and get it to report back.

Not everything can be muti-threaded, but some things can.

Link to comment
Share on other sites

  • 0
Creating parallelized programs is largely language dependent. What language are you going to use?

I am still a student in programming. I use Java most, but I am learning C# recently. :)

Link to comment
Share on other sites

  • 0
Threading, instead of running a heavy CPU using task in the main thread, you run it in another thread and get it to report back.

Not everything can be muti-threaded, but some things can.

It's a bit more complex than that. Simply running the intensive task in another thread accomplishes nothing, unless the main thread is also doing heavy work at the same time. Instead, you have to split the heavy work into smaller chunks, and then run each chunk in a separate thread and then assemble the result once they've all finished.

I am still a student in programming. I use Java most, but I am learning C# recently. :)

There are ways to take advantage of multiple processors without having to deal with low level threads, using parallelism libraries that abstract away the complexity. For .NET, you can play with the Parallel Extensions that are in beta, and will be integrated in .NET 4.

Microsoft also has a portal for parallel computing here.

Link to comment
Share on other sites

  • 0
I am still a student in programming. I use Java most, but I am learning C# recently. :)

Well, Sun has a tutorial for concurrent programming in Java. Basically, you?ll be manipulating a Thread object that contains some code you wrote.

As for C#, I haven?t dealt with parallelizing in that language. But just looking quickly at an example here, it looks like they?ve created methods that take care of parallelism for you (ie. foreach -> Parallel.ForEach). hdood?s link probably has more info.

Link to comment
Share on other sites

  • 0

Its quite easy to get a benefit from creating parallel programs but its actually a pretty complex problem. As The_Decryptor already it depends on whether the problem can actually be solved in a parallel way. Take image processing for example and negating an image. Each pixel can be thought of individually so for a 10x10 image, theoretically if you had 100 CPUs it be 100x faster than one CPU. You'd be getting super linear speed up.

It's not quite as easy to apply parallelism to other problems as some are inheritance not parallel. I think some games coming out are now designed to take advantage of multiple cores but I think right now the software has to catch up with the hardware. Unfortunately you cant just run a program on 4 cores instead of 1 core and watch it go 4 times as fast :p

Link to comment
Share on other sites

  • 0
Hello. Nowadays CPU usually have 2 cores or even 4 cores. I want to know how can programmers write programs which can make use of power of multi-cores CPU? By threading? Or configuring the compiler to support multi-core?

Thanks~ :)

You create threads, exactly like you would on a single-core system. The OS is responsible for scheduling them across the different CPUs if they're available. If your application is single-threaded, there's no way the OS can split it across different CPUs, but as long as you spawn different threads, the application will automatically scale across what's available in terms of hardware.

Creating threads is similar across programming languages, be it C, C++, Java, C#, Python, etc. Some of them have better tools for helping the mundane tasks associated with threading (maintaining a thread pool, for instance), and in C# there will soon be some form of automatic threading with the Parallel Extensions, but it's not going to replace manually spawning threads.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.