Sunday, December 6, 2009

Parallel Number Crunching – Java vs. C & Grand Central Dispatch

I've been doing some parallel number crunching lately using Java 1.6 (1.6.0_17) on a MacPro (4,1) with a 2.93 GHz quad-core Nehalem processor. This weekend I decided to brush the dust off my C programming skills and re-implement the core of my Java code in C using Apple's Grand Central Dispatch (GCD) technology to see if I could get a dramatic speed boost. The results were both better and worse than expected.

The number crunching in question was the examination of data from a pseudo-random number generator (PRNG). Specifically, the PRNG output was interpreted as 30,000 triplets of 32-bit signed integers. The triplets were treated as points in 3-space, and the number crunching consisted of the search for every point coplanar with every possible plane those points could define. It's been done before, of course, and found weaknesses in PRNGs like multiplicative congruential generators. For my own edification, I wanted to look for the same weaknesses in some other PRNGs. I have yet to complete a single run, however, so those who've done it in the past either used better techniques than my brute-force approach, had more than my 23 GHz to work with, or were very patient. Maybe some even used worse techniques that were faster. I don't know. In any case, I've been giving the brute-force approach a shot.

My implementation identifies coplanarity as discussed in Wolfram MathWorld: by computing the determinant of a 4X4 matrix. If the determinant is zero, the point is coplanar. The determinant computation includes a lot of multiplies that can be precomputed, because the three points that define the plane don't change for each 30,000 point test, and my code (Java and C) dutifully precomputes the lot. [Some future version of the C code may be able to bring the CPU's vector processing unit, or the GPU's resources, to bear on this problem, but I've never tried vector processing, so that's a challenge I'll leave for another time.]

With that out of the way, here are the results for checking all 30,000 points for coplanarity with each of the first 5,999,600 planes. As you'd expect, lower test times are better.

LanguageEnvironmentTime
Java 1.6 (64-bit)Mac OS X command line811 secs.
Java 1.6 (64-bit)IntelliJ IDEA 7.0.5 console814 secs.
C & GCD (64-bit)Mac OS X command line658 secs.
C & GCD (64-bit)Xcode 3.2.1 debugger console657 secs.
C & GCD (64-bit)Xcode 3.2.1 debugger console with GuardMalloc 18 enabled1,814 secs.

More details: The C compiler was the one supplied with the current Xcode tools, GNU gdb 6.3.50-20050815 (Apple version gdb-1346) configured as "x86_64-apple-darwin". The Java runtime version was 1.6.0_17-b04-248-10M3025 and the virtual machine was "Java HotSpot(TM) 64-Bit Server VM" – in other words it was the current version of Java supplied by Apple for Mac OS X 10.6.2.

The results: Running on a quiescent machine, both the Java and C/GCD applications were able to push the CPU utilization to roughly 795% and keep it there at all times (the "Hyper-Threading" feature of the Nehalem cores enabled each of the four cores to act like two, so 800% was the theoretical maximum in this case). In general, Java was 23% slower than C/GCD. Personally, I'd expected Java to either closely approximate or even slightly exceed C/GCD's performance (because the Java just-in-time compiler can optimize and natively compile the code in a manner optimal to the specific hardware it finds itself running on at any given time, whereas the C compiler might make have to make more generalized assumptions), or for C/GCD to beat Java by many times. Neither outcome manifested, though the first was closest to the truth.

I think that the actual results can be interpreted in a couple of ways: (1) "C with GCD is meaningfully faster than Java for this sort of number crunching", or (2) "Java is a reasonable choice for this sort of number crunching." I lean toward the second interpretation, due to two issues. First, the Java code was, in my opinion, much cleaner, clearer and more re-usable than the C code. Second, Java is doing a lot more work for you at the same time it is crunching numbers – helpful work that can affect the reliability of your code, and/or the integrity of its results, like ensuring memory integrity and runtime type-correctness – and that's work that C mostly doesn't do at all, or does very badly, as seen in the case of running the C version of the program with GuardMalloc enabled at the cost of it taking 2.24 times longer to run than the Java program.

Those issues aside, it must be said that, for this comparatively simple program, Apple's Grand Central Dispatch and its extensions to the C language worked very well, and were easy to use. I still find the new syntax difficult, but past experience with closures, and the examples in Apple's documentation, were enough to let me produce the code I needed, in spite of that. Whether the GCD language extensions, or the GCD API, provide as complete a set of services for concurrent programming as the Java language and its "java.util.concurrent" package, I don't know. The general impression I took away from the GCD documentation was that it provides fewer features to aid the development of concurrent software than does Java, but a day's work with GCD isn't sufficient to let me judge.

To conclude: Compared to C, Java's not half bad for number crunching as I'm currently doing it (in fact, it's somewhere between merely 23% bad, and 224% good); and Apple's Grand Central Dispatch is an interesting and useful extension to the C language.

Monday, November 30, 2009

Weekend Links

From a book I happen to be reading at the moment:

Our bodies are of course what get left out of a theory that treats architecture as a language, a system of signs. Such a theory can’t explain the physical experience of two places as different as Grand Central Station and my little shack, because the quality of those experiences involves a tangle of mental and physical, cultural and biological elements that the theory can’t account for, blinded as it is by old Western habits of regarding body and mind as separate realms. Taking the side of the mind in the ancient dualism of mind and body, this theory can only explain that part of architecture that can be translated into words and pictures, published in magazines and debated at conferences. An architecture that ignores the body is certainly possible; the proof is all around us. But I doubt it will ever win our hearts.

—A Place of My Own: The Architecture of Daydreams by Michael Pollan, pg. 214.


And Now, The Links

Sunday, November 22, 2009

Weekend Links

From a book I happen to be reading at the moment:

[...T]his was real work too, something more than mere labor—time put in for pay. [....] At the end we would have something to show for it, would have added something to the stock of reality—to what Hannah Arendt once called the “huge arsenal of the given.” In The Human Condition Arendt writes of the privileged position of homo faber, man the maker of things, whom the Greeks believed stood not only above the laborer, but above even the man of action and the man of thought, or words. The laborer produces nothing lasting he can call his own, and both the man of action and the man of thought are ultimately dependent on other people, without whose regard and remembrance their deeds and creations do not matter or endure.

—A Place of My Own: The Architecture of Daydreams by Michael Pollan, pg. 165.


And Now, The Links

Monday, November 16, 2009

Weekend Links

Sunday, November 15, 2009

Bamberger Ranch Preserve Gets an Observatory

I finally get to show my photo of the XXXXXXXXXXX that David Bamberger embargoed until he could make his official announcement on-line: The Bamberger Ranch Preserve now has an observatory, as you can see below.

That was a 30-second, ISO 400 exposure shot by moonlight using a 10mm lens at f/4.5. Because the auto-focus system on my camera didn't have enough light to operate, the focus was accomplished with a bit of luck and a binary search.

The observatory will be a great addition to the Preserve's education programs, and is located within an easy walk of the the Center (see my panorama of the main room of the Center), where groups of school kids come to spend as many as three days participating in educational activities. For myself, despite the moonlit sky, I was able to see Jupiter’s clouds first-hand, for the first-time in my life.

Sunday, November 8, 2009

Weekend Links

Enchanted Rock State Natural Area, Panorama no. 8

This panorama was shot at sunset on December 29, 2008, at Texas’ justly famous Enchanted Rock, a billion-year-old granite dome exposed by eons of erosion. This was shot on one of the boulder strewn slopes, as the sun was rapidly disappearing beneath the horizon. The lighting changed so much during the shoot that I was sure this panorama was a write-off, but this weekend I decided to assemble it anyway, thinking that one or more subsections might be usable and interesting. Surprisingly (to me at least), modern panorama generating software was able to compensate for the continuously changing lighting, and produced, with a little effort, a pristine 360° image. And, because the pano was shot high dynamic range, the ever deepening shadows caused no loss of information.

This was the only panorama I managed to shoot that day. My earlier attempts to shoot a panorama whose centerpiece would have been a boulder about as big as a small house, failed when I had, with great care and not a few misgivings, setup my equipment on the absolute edge of the furthest point of the ledge on which it sat, and found that the boulder still didn’t fit entirely in frame, even with my 10mm lens (which was acting like a 16mm lens on the camera I was using).

I’d planned to spend two days shooting at Enchanted Rock, staying overnight at the Bamberger Ranch in between, but I was so disgusted at that first day’s failure (and the amount of time I wasted in getting there by following an absurd “short cut” to the Rock that Google Maps had selected for me, and the staggering number of people that were there, and the difficulty/danger of climbing to obscure boulders while carrying 40lbs of camera equipment), that after dinner with the Bambergers (and Leroy Petri) I decided to throw in the towel and just go home. At the time, Margaret said she’d like to join me at the Rock for a day of photography, and we’d planned to do that this past summer. Sadly, she died before that could happen.

What’s the line from the Death Cab for Cutie song (What Sarah Said), “every plan is a tiny prayer to Father Time”?