Writing test-code

Posted in code, physics, visualization with tags , on August 11, 2010 by maxpower3141

One of the tasks, that in my knowledge is too often neglected (well you know I do this!), is writing test-code for new functionality.

Recently in my post on an O’Caml rant, I was complaining how O’Caml made it impossible for me to do the “simplest of all computer operations” – sleep. I simply wanted to make my CPU(s) idle for a while after drawing some visualization graphics, in order to produce a fluid animation, and it seemed hopeless. I just went on and implemented (the dreaded) busy-loop. 🙂

This test-case, even though it provided visual confirmation that my dynamic friction model (well the first option anyways – I have 2 others to check still) was ok, but it was lacking something important: Error analysis – the Final Frontier for programmers, where even the uber-nerds wont go without some serious recommending.

There are a few easy things we can check out in these sort of cases:

1) Distance from constraint manifolds (for both position and velocity variables)
2) Constraint force (I actually projected the total force vector to constraint-manifold normal. subtracted gravity before projection in order to measure directly the force felt by particle due to constraint)
3) Friction force (should depend linearly on the constraint force)

Computing these values turned out to be already a task, but visualizing them over time turned out even larger task – here’s the gif-animation:

Animating particle sliding on the sine curve

Of course the classical “O’Caml list-reverse” happened, so that the plot is like a polygraph, lie-detector type of curve (For me this happens all the time with O’Caml lists – and of course any iteration through the list inverts it! :))

But furthermore the results seem good, except one tiny thing: The friction force seems to diverge when going through zero-velocity of the particle – how very odd, no? I spent already some time verifying the math – it all matches, so what’s wrong? I know my friction model in this case is not perfect, but the discrepancy should not be so noticeable.

Well, the (yet again too obvious) solution was that this model had only implemented dynamical friction!

    Rest friction not yet implemented!!

. D’Oh! 🙂 The classical – without rest friction the plot is supposed to look something like that!!!

So well, moving on to implement rest friction and then to try the two other friction models! I’ll be back with more cool things! … Like more code… And physics.. and math.. and.. Uh.. Yeah – I know – super cool!Well, I am having a glass of wine while coding now, so, I guess I’m not completely hopeless, yet? Ok? And if I play a tad on my guitar?

🙂

Update: Watchmen

Posted in cinema on August 11, 2010 by maxpower3141

Finished watching Watchmen the other day (see previous post) and I can’t explain it, but somehow I was a tad disappointed by the ending – it was clever, but not clever enough – a bit too obvious, even though somewhat controversial, yet somewhat overly optimistic, when put in perspective with the rest of the film.

Just watch the whole movie and you’ll see what I’m talking about – it’s still very much worth it.

(Disclaimer: following links contain trailers of said movies that contain graphic violence – follow the links at your own discretion)

Just that the ending, for me, is not the most beautiful part of cinematic history. These, in my heart, go to Seven, Audition and Tesis (although this is in hindsight an almost too obvious slap in the face) – Hopefully I can write something about them later on, but I find it funny, that when asked, Takashi Miike (the director of Audition), said that he made the movie to shock people – oh my, what execution. 🙂 Of course the alert reader will find a reoccurring theme in all of these… 🙂

Watchmen RULES!

Posted in cinema on August 5, 2010 by maxpower3141

Yesterday I made myself finally watch Watchmen, well the first hour of it until now, and it is quite spectacular! Here’s one of the trailers from YouTube:

I somehow resisted watching this for a long time, for some very odd reason (I think marketing people could actually discover something by studying why this movie repels), and now that I started, I have to say that I just love it!

Somehow I had the idea that this would be some kind of comedy or parody of retired super-heroes, and it didn’t seem very interesting to me, but in reality this is an extremely harsh movie, where the poor are poor and the rich are lost – kind of like, uh, mm, reality? It is just mesmerizing!

But up until now, I only managed to watch the first hour, so please, don’t spoil the ending for me! 🙂

Make your CUDA-development fly

Posted in code on August 5, 2010 by maxpower3141

The other day I came by Thrust and I immediately became a fan – without even trying it out first!

The idea of this library is to provide CUDA-developers the most important set of tools to handle those simple, but tricky parallel operations. The idea of CUDA is that the modern GPU is actually more capable in handling computations involving large data-sets than the modern CPU in practically all cases that matter and it is even quite easy to use – until you have to run some operator on the dataset that has global dependencies between the data.

For example finding the maximum of a large dataset is surprisingly difficult to do efficiently in CUDA – Mark Harris of nvidia has a made a nice paper explaining how to get good performance on the same algorithm with the maximum-operator replaced by the sum operator (the algorithm itself is exactly the same, and therefore lends itself to be abstracted by the binary-operator using templates).

Even more difficult is to sort a dataset efficiently on the GPU – here is a paper about it by Nadathur Satish, Michael Garland and Mark Harris. (Btw. in this paper you can see that the GPU is not super great at this kind of tasks and here for some quite short lists (less than one million entries) the CPU can be little faster than the GPU – but keep in mind also that normally sorting is just a small piece of a bigger problem and that this benchmark was done on quite old hardware (an Intel 4-core CPU against GTX 280).)

The point I’m trying to make, is that implementing these auxiliary functions to “real work” can sometimes be notoriously difficult and doing it wrong can bottleneck your computation due to silliness – so what to do? Code them anyways always from scratch, benchmark, test and improve? This can take days, if not weeks even for good programmers!

And TADAA – in comes to rescue the Thrust library! The idea of this library is to give you versatile versions of all these algorithms in the same way that STL does with good performance. And, as far as I can tell, it just works!

Here is a blog post about a benchmark which shows thrust:sort() beating stl:sort() with ten times better perf! And this even includes copying the data from the main system memory to the GPU and back, which is just silliness: when you do GPU compute, you keep the data on the GPU!

The great thing about this thrust is that the algorithms are completely general! You can replace the data-types with any types and the operators are of course overloadable by standard template-techniques.

This example is from the thrust quickstart guide page, which computes z = ax + y for vectors, or the SAXPY-operator from BLAS:

struct saxpy_functor
{
    const float a;

    saxpy_functor(float _a) : a(_a) {}

    __host__ __device__
        float operator()(const float& x, const float& y) const { 
            return a * x + y;
        }
};

void saxpy_fast(float A, thrust::device_vector<float>& X, thrust::device_vector<float>& Y)
{
    // Y <- A * X + Y
    thrust::transform(X.begin(), X.end(), Y.begin(), Y.begin(), saxpy_functor(A));
}

All I need to do now, is to put it to good use!

You’ve got the power!

Posted in politics on August 4, 2010 by maxpower3141

So reading some news today I came across this one:

Mexico drug cartels use gory videos to spread fear

The first reaction was the common one: How awful.

Then I started thinking about the problem setting itself: How have we come here? What is the problem? What is the solution? Who should do what?

Of course voting for anything nowadays is completely broken so obviously that won’t do any good – so what can you do to make this better?

Well the next time you are about to purchase illegal narcotics think about this:

When you do drugs (I guess mostly cocaine in Mexico’s case), you are actually contributing to the killings and misery of Mexico’s already poor people! Nice, no? Because you (and admittedly many others) put money on it, these guys are getting killed! 🙂
I wonder if any of those potheads* ever think of that, no? They are, in surprisingly direct way, responsible for this human misery – lovely. Money is power, or better said, application of money is power. If you have it, please think of the consequences of it’s use, please? Pretty please?

(* ok, I don’t know if pot makes a significant contribution, but it might – just needed a buzzword there :))

Vote with your wallet! And go to the Andes and find out yourself from the natives how coca is supposed to be used!

Of course all of this is extremely simple reasoning, but perhaps not very many people think about it when making the purchase. You’ve got the power – use it wisely.

Nice particle-based physics engine

Posted in code, physics on August 4, 2010 by maxpower3141

Geeks3d has an article on Lagoa physics engine, which seems quite impressive.

Direct quote from site:

Lagoa Multiphysics is a new physics engine created by Thiago Costa. This physics engine is focused on particles.

I have been thinking about this kind of idea to approximate continuous matter as a set of constrained point-bodies (particles) and here mr. Costa brilliantly shows how impressive things you can create with this kind of system. Hopefully later on he will add more rigid bodies as well and destruction thingies.

Also this kind of solver should be just great for GPU-acceleration for physics!

Eclipse: Too many files open

Posted in code on August 4, 2010 by maxpower3141

How lovely it is when Linux (or possibly OpenSUSE) security model makes me lose changes by not letting me save a file from Eclipse. nice. Good going Linux. Not being able to save a file? How did we get here?