15

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.

Nope
  • 34,682
  • 42
  • 94
  • 119
  • 1
    Dulicate: http://stackoverflow.com/questions/363341/how-are-you-taking-advantage-of-multicore, http://stackoverflow.com/questions/1321615/what-are-multi-threading-dos-and-donts – S.Lott Oct 19 '09 at 20:44
  • 2
    Actually I read through both of those questions before posting and felt that they did not answer my specific question: What is the necessary lingo needed for me to get into multicore programming.... – Nope Oct 19 '09 at 20:50

15 Answers15

17

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
  • 854,459
  • 170
  • 1,222
  • 1,395
6

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
  • 143,651
  • 25
  • 248
  • 329
5

Check out this book: Java Concurrency in Practice

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Igor ostrovsky
  • 7,282
  • 2
  • 29
  • 28
3

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
  • 360,940
  • 56
  • 644
  • 725
  • I'd start with pthreads or OpenMP before MPI. Now, when you want to do distributed computations, MPI is the obvious choice. – BobbyShaftoe Oct 19 '09 at 20:45
  • 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 Oct 19 '09 at 21:55
3

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
  • 11,870
  • 3
  • 23
  • 19
2

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
  • 31,973
  • 6
  • 70
  • 106
2

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

Joe Internet
  • 571
  • 1
  • 3
  • 4
1

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
  • 476,176
  • 80
  • 629
  • 1,111
1

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
  • 28,337
  • 7
  • 52
  • 74
1

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
  • 61
  • 3
1

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
  • 17,324
  • 5
  • 69
  • 111
0

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

Mandrake
  • 363
  • 1
  • 3
  • 11
  • No all of them will execute. If they are all reading the position they will get the same answer. If one or more is a write the result is undefined unless you've bus-locked. – Olof Forshell Mar 05 '11 at 13:56
0

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.

J D
  • 48,105
  • 13
  • 171
  • 274
0

Multiple threads can exist in a single process. The threads that belong to the same process share the same memory area (can read from and write to the very same variables, and can interfere with one another). On the contrary, different processes live in different memory areas, and each of them has its own variables. In order to communicate, processes have to use other channels (files, pipes or sockets).

If you want to parallelize a computation, you're probably going to need multithreading, because you probably want the threads to cooperate on the same memory.

Speaking about performance, threads are faster to create and manage than processes (because the OS doesn't need to allocate a whole new virtual memory area), and inter-thread communication is usually faster than inter-process communication. But threads are harder to program. Threads can interfere with one another, and can write to each other's memory, but the way this happens is not always obvious (due to several factors, mainly instruction reordering and memory caching), and so you are going to need synchronization primitives to control access to your variables.

Taken from this answer.

Community
  • 1
  • 1
Revanth Kumar
  • 809
  • 12
  • 18
0

That you say "take advantage" sounds to me as something more than doing just any multi-threading. Simulations in my book are computation-intensive and in that respect the most efficient language is C. Some would say assembly but there are very few x86 assembly programmers who can beat a modern C compiler.

For the Windows NT engine (NT4, 2000, XP, Vista and 7) the mechanisms you should look into are threads, critical sections and I/O completion ports (iocp). Threads are nice but you need to be able to synchronize them among themselves and with I/O which is where cs's and iocps come in. To make sure your wringing every last bit of performance out of your code you need to profile, analyze, experiment/re-construct. Lots of fun but very time-consuming.

Olof Forshell
  • 3,169
  • 22
  • 28