Locklin on science

Yet another stab at explaining the debt crisis

Posted in finance journalism by Scott Locklin on December 7, 2011

Only fast languages are interesting

Posted in Clojure, Lush, tools by Scott Locklin on November 30, 2011

If this isn’t a Zawinski quote, it should be.

I have avoided the JVM my entire life. I am presently confronted with problems which fit in the JVM; JVM libraries, concurrency, giant data: all that good stuff. Rather than doing something insane like learning Java, I figured I’d learn me some Clojure. Why not? It’s got everything I need: JVM guts, lispy goodness; what is not to love?

Well, as it turns out, one enormous, gaping lacuna is Clojure’s numerics performance. Let’s say you want to do something simple, like sum up 3 million numbers in a vector. I do shit like this all the time. My entire life is summing up a million numbers in a vector. Usually, my life is like this:

 (let* ((tmp (rand (idx-ones 3000000))))
    (cputime (idx-sum tmp)))

0.02

20 milliseconds to sum 3 million random numbers enclosed in a nice tight vector datatype I can’t get into too much trouble with. This is how life should be. Hell, let me show off a little:

(let* ((tmp (rand (idx-ones 30000000))))
    (cputime (idx-sum tmp)))

0.18

180 milliseconds to sum up 30 million numbers. Not bad. 60 times worse than I’d like it to be (my computer runs at 2Ghz), but I can live with something like that.

Now, let’s try it in Clojure:

(def rands (repeatedly rand))
(def tmp (take 3000000 rand))
(time (reduce + tmp))

Java heap space
[Thrown class java.lang.OutOfMemoryError]

Restarts:
0: [QUIT] Quit to the SLIME top level

Backtrace:
0: clojure.lang.RT.cons(RT.java:552)
(blah blah blah java saying fuck you java blah)

Oh. Shit. Adding 3 million numbers makes Clojure puke. OK. How well does it do at adding, erm, 1/10 of that using my piddley little default JVM with apparently not enough heap space (@130mb).

(time (reduce + tmp)) "Elapsed time: 861.283 msecs"

Um, holy shit. Well, there is this hotspot thing I keep hearing about…

 

user> (def ^doubles tmp (take 300000 rands))
user> (time (reduce + tmp))
  "Elapsed time: 371.451 msecs" 149958.38785575028 

user> (time (reduce + tmp))
  "Elapsed time: 107.619 msecs" 149958.38785575028 

user> (time (reduce + tmp))
  "Elapsed time: 46.096 msecs" 149958.38785575028 

user> (time (reduce + tmp))
  "Elapsed time: 43.776 msecs"

Great; now I’m only a factor of 20 away from Lush speed … assuming I run the same code multiple times, which has a probability close to zero. Otherwise, with a typedef, I’m a factor of 200 away.

Maybe I should try using Incanter? I mean, they’re using parallel Colt guts in that. Maybe it’s better? Them particle physicists at CERN are pretty smart, right?

user> (def tmp (sample-uniform 300000 :mean 0))
#'user/tmp
user> (time (sum tmp))
"Elapsed time: 97.398 msecs" 150158.83021894982
user> (def tmp (sample-uniform 3000000 :mean 0))
#'user/tmp
user> (time (sum tmp))
java.lang.OutOfMemoryError: Java heap space (NO_SOURCE_FILE:0)
user>

A bit of hope, then …. Yaaargh!

Let’s look into that heap issue: firing up jconsole and jacking into fresh swank and clojure repl processes, I see … this:

I can’t really tell what’s going on here. I don’t really want to know. But it seems pretty weird to me than an idle Clojure process is sitting around filling up the heap, then garbage collecting. Presumably this has something to do with lein swank (it doesn’t do it so much with lein repl). Either way, this isn’t the kind of thing I like seeing.

Now, I’m not being real fair to Clojure here. If I define my random vector as a list in Lush (which isn’t really fair to Lush), and do an apply + on it, the stack will blow up also. The point is, Lush has datatypes for fast numerics: it’s designed to do fast numerics. Clojure doesn’t have such datatypes, and as a result, its numeric abilities are limited.

Clojure is neat, lein is very neat, and I’ve learned a lot about Java guts from playing with these tools. Maybe I can use it for glue code somewhere. I’m not going to be using it for numerics. Yeah, I probably should have listened to Mischa, but then if I had, I’d be writing things in numeric Perl.

 

Edit Add:

Thanks to Rob and Mike for showing me the way, and thanks everyone else for demonstrating my n00bness and 4am retardation

(let [ds (double-array 30000000)]
(dotimes [i 30000000] (aset ds i (Math/random)))
(time (areduce ds i res 0.0 (+ res (aget ds i)))))

"Elapsed time: 65.018392 msecs"

 

I daresay, this makes clojure “interesting” -or at least more interesting than it was a few hours ago. It would be nice if someone had already written some package which makes taking the sum of 3 million numbers a bit less of a chore (a la idx-sum). I mean, what’s going to happen when I have to multiply two matrices together?

On genocidal environmentalist space aliens

Posted in fun by Scott Locklin on November 28, 2011

http://takimag.com/article/gaia_unfriendly/print#axzz1ehr50sFj

It’s true, there are probably thousands of papers this bad, but this one made the papers, so someone had to make fun of them.

Great thinkers: Eudoxus of Cnidos

Posted in history by Scott Locklin on November 13, 2011

Εύδοξος ο Κνίδιος

It’s a modern conceit that we are the most sophisticated people who ever lived.

Much of what we know comes from a bunch of pederasts in ancient Magna Graecia, and most of that has been forgotten. The agglomeration of knowledge over billions of people and thousands of years has given us greater ease and power over nature than the Greeks had. But the Greeks in many ways remain our superiors as people, and they were in every measurable way our intellectual superiors. There was no other civilization we know of who remotely compared to what the Greeks accomplished. Not the Persians, the Chinese or the Japanese or even the Edwardian English (who probably came closest to the influence of the Greeks, if not their genius). I don’t know why Von Dainken talked about Aliens influencing the ancient Egyptians in his silly Chariots of the gods books. No pile of rocks can compare to even a minor work of Phidias.

A Greek whose ideas haunt me: Eudoxus of Cnidos. He was born around 408 BCE, son of Aischimes. Eudoxus was born poor. He walked miles every day to listen to Plato’s lectures. Eventually that cheesed him off (actually, I believe it was Plato’s lack of mathematical sophistication), and Eudoxus went his own way; ending up in Heliopolis where he learned Astronomy. Later in life he returned to Athens to rival Plato, and eventually he retired to Cnidos and was considered the greatest mathematician of his time.

Eudoxus is best known for his contribution to the theory of ratios to Book V of Euclid’s Elements in which he developed the idea of real numbers, which underlies much of mathematics today; in particular that which requires the floating point part of your computer. He also invented an early form of calculus, called the “method of exhaustion” -really, Leibniz and Newton didn’t do much beyond this which wasn’t notation. In fact, much of Euclid’s elements is just an exposition of Eudoxus work. Other achievements: he discovered the leap-year, invented a sundial and a lot of what we know about him was due to an epic poem. When was the last time any scientist or mathematician rated an epic poem? That’s how badass Eudoxus was.

Eudoxus was the first astrophysicist; his contributions in this regard are more or less how I first ran into him, via an idea called the Gutzwiller trace formula, which is an interesting mathematical object worth a blog entry or two on its own. Eudoxus developed a theory of orbital mechanics which is essentially how we do it today; it is perturbative in nature. In other words, his theory takes a rough answer, then adds terms to it to make it better. His theory is numerically very good. Variations on it were happily used for thousands of years; up to the time of Kepler. Eudoxus’ idea was really the first modern physical theory.

Eudoxus invented real numbers, orbital mechanics and a form of calculus, 2000 years before Newton. He also discovered the leap year, invented a sundial, described constellations, wrote epicurean philosophy, discovered many theorems in solid geometry, he is alleged to have been a pretty good interior designer; he wrote books of geography and sociology, meteorology, Egyptian theology and was revered as a wise lawgiver in his home town. His productive years were between 26 and 53, when he died. Compare him to the imagined smartest man who ever lived, Einstein, who started research when he was 21, and died at 76. Eudoxus accomplished much more in a shorter period of time. He did it before Alexander the Great; before the invention of the steam engine; before Rome became an empire; before the Great Wall of China was built. Eudoxus lived so long ago, he is at the bare edge of recorded history. He lived in a time which had more in common with myth and legend than with modern history, but his discoveries invented what we are.

“Willingly would I burn to death like Phaeton, were this the price for reaching the sun and learning its shape, its size and its substance.” -Eudoxus

Ancient Greek civilization produced at least many dozens and perhaps hundreds of men like this. How can we moderns hope to compare to the Greeks?

Follow

Get every new post delivered to your Inbox.

Join 85 other followers