Thursday, November 11, 2010

No threads attached?!? #*%&@

I found this cartoon (if I can call it one) on my desktop. I sketched it ( more like GIMPed it ) when I attended the Java threads session once upon a time, long time ago ( :D I love this line )

Back then, I had something in my mind I wanted to rant about. But then.... laziness won over me.

Unfortunately, today i don't remember what I wanted to write. So here is the cartoon I GIMPed.



Let me see, if I can write about threads when I start using them :D

Signing off now. Gotta write a test :(

Wednesday, November 10, 2010

Sort out all your problems!

"Hi!! meet Akshay"
"Hey!!!!!! I 'm Akshay too!" :) "what do you do?"
"U moron! I am you"
A dazed me, "guess I need to sleep now! good night" and turns to walk away.
"You have problems to sort out"
A half zombie-ish me, "Sort!!?!! Did you say sort??!!"
An angry Akshay, "You will have problems to sort all your life"
"Well... problems can take care of themselves, I m out now! " walking straight to bed next moment ... ... ... snore... ... ... snore ... ... ... snore ... ... ...


Me: What kind of losers try to sort problems all their lives?
Akshay : Loser! here are the kinds!! SortBenchMarkers

Me: Woah! they have no better life :O they sort problems all life??? I am sure they are not married! :P
Akshay: Sort problems is all their life... :-" (go type this on yahoo IM! thats my reaction)

Me: hmmmm....
Akshay: Why don't u try to figure out how they sort out problems.

Me: Well.. I have my own problems to sort!
Akshay: Yes! you do. Do the problems go a 108 or a 109

Me: Ouch! Hey that's a number system I have heard of!!
Akshay: <Completely devilish smile> He he!! let me remind you...

Me: Noooooo... I do remember. I do remember.
Akshay: So what are you going to do about it?

Me: You seem to have learnt everything, why don't you rattle off?
Akshay: Naaah! Not all. Everything is not interesting. I was looking at how much energy is spent is sorting some records.

Me: Dude! you really need to get a life. Spend energy on better things. Get a life :-|
Akshay: You can get a life for both of us. Now listen!

Me: Well... do I have a choice??
Akshay: Since I don't give you one, you don't have one :P

Akshay: The whole intent of this competition is to find out some awesome brains working on even better sorting algorithms.
Me: Yaaawn...

Akshay: Do you know how much energy you spent on yawning?
Me: Dude... leave me alone. Let me yawn in peace. I 'll keep quiet. You carry on :)

Akshay: Loser! with that energy of "keeping quiet" for few hours, I could sort a 108. Check it out!
Me: Whatever!

<Small pause>

Me: Hey, you got me curious. How do they calculate the energy spent on doing  some records manipulation? My lappy heats up like a toaster when i m on Photoshop.
Akshay: They use some specific circuitry and software to do it. What else? You think they sit with thermometers on the processor? <Sorry couldn't help! I had to crack it up>

Me: Hmmm.. Cool stuff!
Akshay: Actually hot! They measure how hot it gets. Elementary dude! The best part about the sorting, they get results in close to 20 seconds.


Me: Wow... wow.. wow... woah! Dude! Losers. I got a sort working in under that time for an assignment??! <triumphant smile> :)

Akshay: Loser! I know all about it, you were sorting some 16 byte strings. These guys work on 100 bytes record and a 109 records. Get the scale?

Me: <Whimper>

Me: Well go on then :(


Akshay: You know, most of time taken is for disk access. Even though they use solid state devices, disks kill the sort. They spent a whooping $6500 on the hardware. To get flash based disks.

Me: A 100% losers! $6500 on computer to do just a SORT? <dreamy eyes> I would have bought a 1DmIV and probably a big lens! :)


Akshay: Stop dreaming you Idiot! Listen to the config, they work with an intel xeon quad core processor machine, fitted with a 12 Gig RAM. <a small pause> Stop drooling. You ain't getting to install Photoshop on that!
Me: <gingerly> I was not thinking of that!
 

Akshay: With all this, they get to sort a 10Gig dataset in about 25kJoules.

Me: Well what if they used some low power machines? Like the NetBooks?


Akshay: Are you nuts? They would need a huge memory to store as much data as possible in memory. Atoms support a max of 4GB ( I need to verify ). Also lot of other constraints, the bus speeds, L2 Caches? Moreover, they would need quite a few SATA Ports for the solid state disks. Atom boards have just about 2.
There is another factor to consider, the power ratio between fixed power cost (@idling) to peak utilisation, is higher for Atoms. So the over all power performance takes a beating.

Akshay: Oh! well that said, they have also used an atom processor based machine to do the sorting! Its interesting to see the results


Me: So, couldn't they do any better on the timing? I still feel my assignment sort needs some credit! :D
Akshay: There are some problems there too. The OS used was Ubuntu 9.04. Linux has some problems related to the block I/O on solid state devices. This hits the performance again.

Me: You managed to get me interested in this stuff!! Let's do some Math and see how it performed?
Akshay: Dude, not now. I 'm sleepy!! You can check out this link, for details if you are interested.

Me: Well, OK! Unfair thought! You didn't allow me to sleep!

<Mumble> <Mumble>

Sudhix: DUDE!! Wake up! what are you talking to yourself? It's high time you go sleep!

Friday, October 8, 2010

CODE is not meant to be read -- 2

Aloha! ( I just finished mid-sem exams :D ) Back to coding now. WHEW!! such a relief to code than to sit n mug the literature in texts.

I made a statement sometime ago, "Segmentation faults are better than infinite loops", which makes me think, how in the first place, the code went into an infinite loop?
Possible reasons :
  • messed the loop variant
  • missed an assignment
  • don't know where the loop should end in the code!
Possible resolutions, pretty obvious;
  • based on the looping type, increment or decrement the variant appropriately,
  • if its pointers that one is messing around, the most typical assignment node = node->next; is messed, un-mess it.
Ouch! the last one hurts! How to figure out where the loop should end in the code??

That's the motivation for this rant (a continuation of my previous rant). Write readable code!!

Its not a small task to try and understand code written "once upon a time, long time ago". We need comments accompanying the code to understand what the code does and why? I have written about commenting code in my previous post.

Matters are worsened when the code itself is not readable.
Try to figure out what the code below does?

void maxh (char uidArray[][17],long int n) {
int i,j,k;
char item[17];
for(k=1;k<n;k++) {
strcpy(item,uidArray[k]);
i=k; j=(i-1)/2;
while(i>0 && strcmp(item,uidArray[j])>0) {
strcpy(uidArray[i],uidArray[j]);
i=j; j=(i-1)/2; }
strcpy(uidArray[i],item); } }

For now, lets not bother what the code does ( that's a part of  my Algos assignment code btw :D )

Any normal human would find the code above un-readable.

Reasons are simple, I have not explained what the code does, neither have i written the code in some readable manner.

If the same code above was written little better**, at least we can try to figure out what the code does.
void maxHeapify(char uidArray[][17],long int n) {
    int i,j,k;
    char item[17];
    for(k=1;k0 && strcmp(item,uidArray[j])>0) {
            strcpy(uidArray[i],uidArray[j]);
            i=j;
            j=(i-1)/2;
        }
        strcpy(uidArray[i],item);
    }
} 
 
Indentation and spacing play an important role while writing good code. Coding conventions are something that every one of us programmers have to follow. Primary intent of having coding conventions is to ensure uniformity in the code.

Consider a project on which five programmers are working. If each of them follow their own ( mostly no-standard ) convention to write code, the day when bugs become hard to detect and fix is not very far! This is one of the prime reason why a standard is enforced while coding.

In fact many of the programming languages have their own  coding conventions.

As always, I am not enlisting any of those here. I am just ranting about non indented code :-D

However, if I may suggest, I am listing here some pointers to the coding conventions and rules of various languages.

C is a common language most of "programmers" get introduced to initially, it pays to know how code is written in C. There is a three part literature on the same: Naming, Style and formatting and some best practices in www.cprogramming.com

We can find conventions for Java here.

Perl is my favorite language. Here is how we write Perl code.

** I have removed the comments here to keep the post length small :)

PS:
Links provided are copyrights of the respective authors.

Sunday, September 26, 2010

CODE is not meant to be read!!

"Why call it code if it has to be read??!!??"
Someone jovially asked me this when I was lecturing about commenting the code he had written.

Oh!! I don't have an answer to this question! Honestly, that beat me TOTALLY! Having put forth the disclaimer, I am ranting why we should comment the code we write.

Many a times work of art is not understood by the common man unless explained. Take examples of the Oscars, how many times we complain, the movie that won the Oscars was totally unworthy of it! Let's step aside and take a moment, digest the truth; the reality is that more often than not WE did not understand the worth of the movie. ( Oh wait!!!! don't get back to me for that, may be that was a bad example. The idea was anyway not to rant about Oscar winning movies ;-) Read on!

Coding is an art, no doubt about it. The art of writing in a Language unknown to man, know to machines, forging lines of the story with the best possible approach to reach the result ( it is misnomered as algorithm and logic ). Thus, solving a problem -- only the machine way.

As goes with many forms of art, the code is not well understood by mere mortals. The artist forging code must enable mortals understand his code. How do we do this??

Simple, tag your code with comments!!

Its a given fact that, ones own code is not understood over a period of time. Remember, Thou art the lord, until thou finishes the line.Once you hit the carriage return, the burden of maintaining code is added on to the carriage hitched to your back.

When we get back to the code written "once upon a time" its inhuman on the self to expect to know what the code did in the first place. Worst still, if the realization hits upon just then, and we start writing comments to explain the code, I wouldn't be surprised to see a comment like this at that point of time :

/* When I wrote this code, God and I knew what it did, now only God knows what it does */
(found the above comment here)

I have heard of programmers' statements, "if some one doesn't understand what code is written here, he is not worth re-using/copying it". I bow to thee O programmer, but, what if its you who doesn't understand the code after a month?

Its a simple fact to realize, we are not here to prove our prowess as to how sharp a coder we are, we are here to solve a problem, often collectively. This is something we as programmers need to digest at the earliest.


For one single moment, we need to step into the shoes of the guy who sits in the same chair as ourselves and read the code we wrote. He would feel so miserable about the code, and again, I wouldn't be surprised to see a comment like this written by him :

/* Get me the guy who wrote this, it is punishment enough to make him read it. */

I guess I have put my point forth on why we should tag our code with comments. There are numerous formal articles on the aspect of commenting, if my ranting has inspired you to go look up for some such articles, i would suggest literate programming. If the name Don Knuth scares you ( like it scares me ;-) ) I would suggest something more simple to assimilate ( at www.cprogramming.com ). 
I also came across a nice article on tips to comment your code.

If at the end of all this, you feel like commenting out this blog post from your memory, feel free to do so.

I would leave you to enjoy some funny comments that I stumbled upon.

PS:
Links provided are copyrights of the respective authors.

Friday, September 24, 2010

jAvA JaVa

Learning a programming language in class really s*c*s.
I have always considered a programming language a tool to implement the logic and get the results out. Each of the languages have their own use and abuse ( well what ever it meant! ), choose the most optimal as per the need.

Oh!! this rant btw, refers to learning Java in the classroom.

I would any day prefer to learn the constructs of object oriented design and programming, implement the same using Java in a mini project.

Instead it feels so lifeless to be learning a language sitting in the class.

Yesterday's class covered the concept of interfaces in Java. The concept of interfaces is quite interesting to add abstraction to the code. Make code extensible etc., One can read about interfaces here.

Let me apply some new learning to express what i think :D
Code follows

/**
* Life is an abstract class;
* - will be explained in due course of time.
* The class Me defines what i am.
* Currently i implement Zombie!
* So i am a Zombie and lifeless :P
* Yes you got it. Sitting in a programming
* language class is being lifeless.
*/

public interface Zombie {
public Life lifeStatus();
}

public class Me implements Zombie{

public Life lifeStatus() {
return NoLife;
}
}