Sunday, April 20, 2008

Moments...

Have you ever felt, for a moment, that time would stop, and this moment would stop forever? Know it sounds awefully cliched, almost movie worthy cliched, but it's interesting. Better minds than me, have said “Life is not measured by the number of breaths we take, but by the moments that take our breath away.” So it might be worth paying moments more than a cursory glance. I truly do believe in the statement, so if have never have had any such moment, look for one, mebbe you had one but never noticed.

In your free time, try reminiscing for once. What you'll notice, that with each memory, you also tend to recall some bit of the emotions that you felt at the time. In fact, the moments clearest in our minds tend to coincide with our emotional moments (ya that includes being angry as well). Depending on the kind of person you are, you'll probably pay more attention to certain emotions than others. If you think I'm picking up from some theory, nope, in fact I'll be highly surprised if there's a theory that says so.

Why is it important? if what I'm saying is right, then maybe at the fag end of your life, with nothing much to do, you will reminesce. And it's these moments that'll come up in your mind, so it pays to pay some attention, as you might just have some choice in what your life's movie reel look's like. This is mirrored in a lot of literature you'll find, and I'll mention one line from one very nice poem I read once, "Don't dance so fast, time is short, the music won't last"

So try and live life in the moment, mebbe you'll pick a memory that you'll cherish for all life. But don't live in that memory, it's a memory, and is best left in the mind, not to be wondered about. It can never come back, never be changed, only remembered.

Thursday, April 17, 2008

Exhausting efforts of optimization!

Warning: Any non-geeks, avoid this article, u're unlikely to appreciate, let alone understand the article(no offense, but it is the case here).

I write this post after having spent the past 8 hours on trying to optimize my program. My code was originally written in Visual C++ (i.e. Windows) and on finding no tools to let me profile my code, i shifted to gcc (i.e. linux). Why a commercial programming suite lacks a profiling tool is baffling. It would only let me do a profile guided optimization. In my experience, a good programmer with a profiler can do a hell lot better job at optimization than any compiler. In fact, after my efforts, adding any other optimization flag other than the obv -O3 actually slowed down the code!. That is one big reason i like linux and gcc. It's a developer's os, meant expressly for development, which is why its adoption by other people baffles me a bit, they're not using the most powerful features of the os, and giving up quite a bit of user friendliness in the process.

One of my professors once said, that some code written by a novice programmer can be speeded up about 10-20 times by an highly experienced programmer who knows his hardware, even without knowing what the program does. Though this might be a bit of an insult to some, it's true to an extent. I've actually hit the 10 times mark on some of my own code :D , and the code wasn't badly written to begin with. But, something the confuses me is that there is no comprehensive guide to writing fast code. There are some basic tenets which if followed, can give very good performance on any machine. In my opinion, hardware specific optimizations are best left to compilers, unless u're really gud at it. you'll always find the basic stuff like locality of reference, pass by reference etc. Yet no one usually talks about the stuff you can do if you spend some time with the program. Statistical analysis is an amazing tool. The biggest thing that tend to slow down code is branching. Only very experienced people can truly minimize the branch penalty. But you can do some things on your own. See which options are more likely to occur, and try to keep these options higher up in the 'if' condition checks. Simple as this may sound, it's often ignored. Plus, removing unnecessary conditional checks. See if some condition checks can be reduced, e.g. if u're using nested if's see if conditions on the outer loop variable can be confined to the outer loop, it can speed things up by quite a bit.

Adding threads is also a good idea, especially for scientific codes. OpenMP is an amazing tool that you can use if you don't want to get your hands dirty with thread management. Most modern compilers support it, and using it, you can usually speed up your code by about twice (in most cases which can be parallelized) with about 5 minutes of effort. But not always, it actually slowed down my code by twice, even though it's eminently parallelizable. The fact that it was GCC may have something to do with it, their OpenMP implementation is still new. Open source guys, don't scream that i don't know what i'm doing, that's the reason and it's not gcc's fault, I'm decently experienced at code optimization, and i quite put the blame squarely on gcc, cause the same code was speeded up by vc++ when openmp was switched on.

You'll be amazed how some basic thinking can help you write code that can make those crappy looking algos run better than more sophisticated ones. To those who only believe in big-Oh's , try meeting a systems guy with programming experience, you'll be amazed at how O(n2) may be less than O(nlogn) :D

Sunday, April 6, 2008

Wonderful childhood

As kids, we're always in a hurry to grow up. We want to outgrow the dictatorial regime that is our parents and get the freedom to do whatever we want, do whatever we want (and probably eat whatever we want :D). We're carefree, have nothing to think about, and tend to generally have fun. Ok, this is getting a little movie-like perfect, but that's an opinion of a grown up. One of the most, if not the most wonderful parts of childhood, in hindsight, is the freedom from making decisions. Everything is decided for us, be it which school, what to wear (to a certain extent) etc. etc. At that time, we little realise what a perk this is. Resolving a quarrel is so easy! a parent just has to intervene, and we're done, their word is the law.

These last 2 lines are probably the most important. As you come to the nervous teens and adolescence, you realise how much decisions can hurt, and haunt. Your decisions can be small and insignificant, or ones that shape your life. Don't know about you, but I'm cursed to wonder about what ifs, about each an every decision I make. I'm probably built that way, and my upbringing and having to sit cooped up indoors for long times (I was sick a lot as a kid, asthma), makes me wonder about each and every decision I make, and how that affects me, and more so, others. I always remember my carefree childhood, when the most difficult decision to make was probably which ice-cream flavour to take. Talking about the best time, the worst time is probably adolescence. I have had the opportunity of going to one of the best universities in India, but as I near the end of the degree, I still wonder whether how I spent my 5 years here was right. Whether doing something differently would have made things different, whether the path I've picked is the right one. I believe, decisions taken during this period will stay with one for all life, be it as cherished memories, or nightmares that haunt. This is what makes one realise the true value of the innocence and carefree times that were our childhood. Which is why I hate reports of child prodigies, a child should live their childhood with as little burden as possible. Let them go outside and experience the world, don't push them to work 3 hours a day to master some instrument or chess. What you do as a child makes or breaks you as a person. Most of us focus on working harder and harder, making our minds toil to the ends of their limits, trying to push a little further. Don't trust me? try asking some undergrad from a top institute to sit down and do nothing, just relax and take in the atmosphere, they won't be able to. Why? because your mind aggressively trims powers in the early years which it considers irrelevant, to make space for what, we and it, consider important. And running in the rat race makes you exactly that, a rat, running mindlessly in its wheel, unware that it can stop. Still wondering why many brilliant minds burn out? or sportstars retire in their prime because nothing drives them? See if Phelps or Hingis ring bells. When you work hard, so hard, something inside you dies, that child. That child inside you lets you laugh, to enjoy, to love, and look wonderously at every new day.

Remember my next point? that wonderous resolution of quarrels? well, as you grow up, you become more and more sure of yourself. Gaining knowledge breeds confidence, and whether we like it or not, confidence breeds arrogence. Just as courage is not lack of fear but the ability to overcome it, humility is not the lack of arrogance, but the ability to overcome it. As we become more and more sure of ourselves, we disregard authority, of everyone around us. As we become powerful, even more so. Why do you think that global problems are so hard to resolve? there no one to scold the 2 idiots and make them say sorry. Plus, the slight problem that things that people do as adults tend to become hard to tide over by a simple sorry. Maybe they are, but we attach more to them than as kids. Maybe resolving things this way would be easier, but we'll probably never find out.

So, the next time you try to look at that little kid and think you're better, think again. They have a lot more things you can never have, and can be someone you never will.