Monday, October 31, 2011

Multithread Barrier (yes, it's a join!)

This post doesn't want to clearify all aspects of parallel programming. It's just an example to show how you have to thinkg when writing parallel code!

When you deal with concurrent programming, everything is going to be screwed up very easily. Debugging is a pain in the ass, and every one who has ever written a multithreaded software, although very simple as a concurrent server, has experimented with such a pain.

One fundamental operation in multithreaded programs is synchronization. In particular, one operation which I consider very important is a thread barrier, that is a line of code in the program which guarantees that if a thread is executing before it, no one else is executing after it, and if there is some thread after it, then every other thread is, at least, executing that line, but not anything before...

Friday, October 21, 2011

Super Mario Flash

I want to use the space on my blog to spread the word about this Super Mario Flash game, developed by Pouetpu Games.

I find this to be an excellent game, in terms of playability, sound quality, game design, robustness of the code, and furthermore... c'mon guys it's Super Mario! :)

The feature i love the most is the level builder, which reminds me of the old days when I was still a beginner programmer, and I passed from developing my platform games with QBasic to doing the same using The Games Factory by ClickTeam (yes, it was ages ago, when I didn't have the geometry background needed to program with openGL/DirectX).

The only caveat is: try not to end like this:

Sunday, October 16, 2011

The web in 60 seconds (and its storage requirements)

Have you ever wondered how is the web evolving?

A recent research has shown some facts which have been summarized in this picture:

Let's now concentrate on some of these facts. By this research we see that every 60 seconds:

  • 6600 pictures are uploaded on Flickr
  • 25+ hours of videos are uploaded on YouTube
  • 98000 twits are published
  • 21500+ blog posts are sent (summing up the ones on tumblr and the others)
  • 695000 status updates on Facebook
Fire up your calculator now and check these facts:

  • Consider, for the sake of simplicity, that one picture is 2 Mb on average. Every 60 seconds we use 13,2 Gb of storage for new pictures on Flickr.
  • By the fact that the picture shows that 600+ videos are uploaded every 60 seconds, this means that one video is 2,5 minutes long. Assuming (as a worst case) that every video is uploaded in HD, by simplicistic calculations we can say that one video uses 500 Mb of storage. Therefore, we burn 300 Gb on YouTube's hard drives every minute.
  • One tweet is 140 characters long. This means that (having no meta-data per tweet, which is of course impossible) we have 140 bytes per tweet, which leads to 13,4 Mb of tweets per minute. Of course, I'm not taking into account the possibility of attaching pictures to them.
  • A good post's length should be (by some computations on this) 2500 characters in length. Assume this is the mean value, and that no one is attaching pictures and other content in blogs. Assume blog posts are just plain text, so no formatting, which means no more characters for css/html style. Again, no meta-data is stored along with any blog post. This leads to 2,5 Kb per post and 53,8 Mb per minute.
  • A Facebook status update is bounded by 420 keystrokes. Taking this as an upper bound, under the same assumptions as before, we have 420 bytes per update and 278,4 Mb per minute.
This means that considering only these social activities, every minute we use 313545,6 Mb of storage around the world.

Nowadays we have 1 TB hard drives widely available. If social sites were only using this kind of storage, in 3,34 minutes one disk would be filled up by our social bullshit. (And yes, with this post I'm contributing!)

This means that as we have 1440 minutes in one day, we trash 431,14 1TB hard drives per day. Which is 157365,27 per year.

My computations are very simplistic. But maybe this is the sole reason why Cloud Computing is taking its step. And we all should give up at trying to develop the new best algorithm for optimizing computation. As we maybe only need storage...

Friday, October 7, 2011

LaTeX in HTML with Javascript and Google Charts

Using LaTeX in HTML documents is really useful. I'm myself using it in this blog.

Of course, creating images for each formula and manually embedding it would be a pain. So, why don't we automatize it? It would be really nice to use them as in LaTeX documents, so simply writing them in between the dollar sign.

Well, several services exists on the Internet. I've been using the one offered by WatchMath for a while painlessly, until this summer when what everyone can expect when relying on an external service happened. I found that the formulas where not rendering anymore. I visited their page and found out that they changed the location of their script.

Too bad, fixing it was just as easy as pointing to the new location.

Nevertheless, today I came into the Mathematical Formulas page on Google Charts. This is a set of APIs Google has developed to support several things such as statistics and charts on Blogger, or equations in Google Documents. Luckily, they are exposing all of them to the public!

So the idea came to me immediately: why cannot I code a script to exploit this service and replace the formulas in my blog?! Google is not going to redirect its engine, likely! So this approach should not produce any service interruption!

I felt lazy today, so I decided to go for JQuery, instead of coding plain Javascript. As a first step, we have to "include" the JQuery library in our page. Since I am going to implement this into my blog, I'm not likely given any space to store the library downloaded from the official site. So, I went to Google to look for an online version to link to, and (guess what?!) I found it on Google APIs:

<script src="">

Now, let's look at how to convert formulas enclosed in dollar signs all within an HTML page:

(Oct 20th 2011 update: as a great dumbass, I used code which contains dollar signs... therefore the script was trying to texify portions of this code. I only noticed this few minutes ago and fixed it replacing the dollar signs with a dollar-sign image... Then, be careful if you copy&paste this code, as you will have to re-insert the dollars, or JQuery won't understand a bit!)

(Dec 3rd 2011 update: I never noticed how much I used the dollar sign until I developed this script!! It has texified so much text which wasn't expected to be, that I decided to change the approach a bit. Now, Instead of parsing the whole document, I'm just parsing the divs the class of which is "latex". The code in this post has been updated accordingly.)


The first thing I do is to specify some constants so that if I want to change the look&feel I can leave the code untouched. These are:

  • height (unspecified, which means: "Google, give me your default height", which is good for my blog);
  • background color (which is set as black, but completely transparent, this is what the extra 00's are there for);
  • text color (again, black).

Then I declare the convert_latex() function, which takes the <body>'s content and scans it using a regular expression, looking for all the occurrences of text enclosed in dollar signs. Every occurrence is replaced with the value returned by an ad-hoc function which takes the found text (not including the dollars), formats it accordingly to the Google Mathematical Formulas syntax and inserting our formatting, and inserts it in an <img> tag.

The last touch is to insert the following line:

$(document).ready(function() {convert_latex();});
which calls our function when the document is completely loaded. Of course, the last two snippets must be enclosed within <script> tags.

The final result is that now I can write $\LaTeX$ using Google!

Monday, October 3, 2011

Fortunato di essere un programmatore

Questo scritto è datato "21 ottobre 2008, ore 1.38" ed era apparso sul mio primo blog (oramai distrutto da qualche system restyle). Gli impegni di questi giorni ed una lunga telefonata di qualche giorno fa me l'hanno fatto tornare in mente. Sono andato in giro a ripescarlo ed eccolo qui. Ho solo riadattato qua e là lo stile da quello che usavo 3 anni fa a quello di oggi. Ma il senso decisamente non cambia...

Fortunato di essere un programmatore
pubblicato da Alessandro Pellegrini il giorno martedì 21 ottobre 2008 alle ore 1.38

Nelle scorse settimane, ho lavorato ad un progetto che richiedeva ogni tipo di sforzo di programmazione. Ora è a buon punto, quindi posso tornare (quasi!) agli impegni normali. Tuttavia, quando la gente mi sente parlare delle "ore di pazzia" che ho trascorso, spesso mi dice che le dispiace. Non dovrebbe!
Non è un tipo di vita che farei spesso, né per lunghi periodi, o senza un compenso adatto, ma la verità è che questi concentrati di programmazione sono alcuni tra i periodi che preferisco nella mia vita. Nelle giuste condizioni, scrivere software provoca un piacere talmente intenso che dovrebbe essere reso illegale.