views:

727

answers:

13

I have a quadcore processor and I would really like to take advantage of all those cores when I'm running quick simulations. The problem is I'm only familiar with the small Linux cluster we have in the lab and I'm using Vista at home.

What sort of things do I want to look into for multicore programming with C or Java? What is the lingo that I want to google?

Thanks for the help.

+12  A: 

The key word is "threading" - wouldn't work in a cluster, but it will be just fine in a single multicore machine (actually, on any kind of Windows, much better in general than spawning multiple processes -- Windows' processes are quite heavy-weight compared to Linux ones). Not quite that easy in C, very easy in Java -- for example, start here!

Alex Martelli
+1  A: 

You need to create multithreaded programs. Java supports multi-threading out of the box (though older JVMs ran all threads on one core). For C, you'll either need to use platform specific code to to create and manipulate threads (pthread* for Linux, CreateThread and company for Windows). Alternatively, you might want to do your threading from C++, where there are a fair number of libraries (e.g. Boost::threads) to make life a bit simpler and allow portable code.

If you want code that'll be portable across a single machine with multiple cores AND a cluster, you might look into MPI. It's really intended for the cluster situation, but has been ported to work on a single machine with multiple processors or multiple cores -- though it's not as efficient as code written specifically for a single machine.

Jerry Coffin
+1  A: 

So, that's a very broad question. You can experiment with multithreaded programming using many different programming languages including C or Java. If you wanted me to pick one for you, then I'd pick C. :)

You want to look into Windows threads, POSIX threads (or multithreading in Java, if that's language). You might want to try to find some problems to experiment with. I'd suggest trying out matrix multiplication; start with a sequential version and then try to improve the time using threads.

Also, OpenMP is available for Windows and offers a much different view of how to multithreaded programming.

BobbyShaftoe
+3  A: 

It depends on what your preferred language is to get the job done.

Besides the threading solutions, you may can also consider MPI as a possibility from Java and C --- as well as from Python or R or whatever you like. DeinoMPI appears to be popular on Windows, and OpenMPI just started with support for Windows too in the current release 1.3.3.

Dirk Eddelbuettel
I'd start with pthreads or OpenMP before MPI. Now, when you want to do distributed computations, MPI is the obvious choice.
BobbyShaftoe
I don't disagree. OpenMP and shared memory parallelism is a good option with recent compilers. It's just that invariably our problems get bigger and then you need to spread over more than one machine.
Dirk Eddelbuettel
+2  A: 

Check out this book: http://www.amazon.com/dp/0321349601/

Igor ostrovsky
+3  A: 

I think you should consider Clojure, too. It runs on the JVM and has good Java interoperability. As a Lisp, it's different from what you're used to with C and Java, so it might not be your cup of tea, but it's worth taking a look at the issues addressed by Clojure anyway, since the concepts are valuable regardless of what language you use. Check out this video, and then, if you're so inclined, the clojure site, which has links to some other good screencasts more specifically about Clojure in the upper right.

Anon
+1  A: 

A lot of people have talked about threading, which is one approach, but consider another way of doing it. What if you had several JVM's started up, connected to the network, and waiting for work to come their way? How would you program an application so that it could leverage all of those JVMs without knowing whether or not they are on the same CPU?

On a quadcore machine, you should be able to run 12 or more JVMs to process work. And if you approach the problem from this angle, scaling up to multiple computers is fairly simple, although you do have to consider higher network latencies when your communication is across a real network.

Michael Dillon
+3  A: 

You want Threads and Actors

Good point ... you can't google for it unless you know some keywords.

C: google pthread, short for Posix Thread, although the win32 native interface is non-posix, see Creating Threads on MSDN.

Java: See class Thread

Finally, you should read up a bit on functional programming, actor concurrency, and immutable objects. It turns out that managing concurrency in plain old shared memory is quite difficult, but message passing and functional programming can allow you to use styles that are inherently much safer and avoid concurrency problems. Java does allow you to do everything the hard way, where data is mutable shared memory and you desperately try to manually interlock shared state structures. But you can also use an advanced style from within java. Perhaps start with this JavaWorld article: Actors on the JVM.

DigitalRoss
+1  A: 

Even though you asked specifically for C or Java, Erlang isn't a bad choice of language if this is just a learning exercise

It allows you to do multiprocess style programming very very easily and it has a large set of libraries that let you dive in at just about any level you like.

It was built for distributed programming in a very pragmatic way. If you are comfortable with java, the transition shouldn't be too difficult.

If you are interested, I would recommend the book, "Programming Erlang" by Joe Armstrong.

(as a note: there are other languages designed to run on in highly parallel environments like Haskell. Erlang just tends to be more pragmatic than languages like Haskell which are rooted more in theory)

Paul
+2  A: 

Here is a good source of info on threading in C#.

Joe Internet
+1  A: 

If you want to do easy threading, such as parallel loops, I recommend check out .NET 4.0 Beta (C# in VS2010 Beta).

The book chapter Joe linked to is a very good one I use myself and highly recommend, but doesn't cover the new parallel extensions to the .NET framework.

Danny Varod
Now that the book has a new .NET 4.0 edition, it does cover the parallel extensions.
Danny Varod
A: 

yes , many threads , but if the threads are accessing the same position in the memory only one thread will execute,

we need multi memory cores

Arabcoder
A: 

By far the easiest way to do multicore programming on Windows is to use .NET 4 and C# or F#. Here is a simple example where a parallel program (from the shootout) is 7× shorter in F# than Java and just as fast.

.NET 4 provides a lot of new infrastructure for parallel programming and it is really easy to use.

Jon Harrop