Wednesday, 8 September 2010

Some good films

I've seen some rather good films lately, but I don't feel like writing a full entry for each of them (as I've said before my knowledge about cinema is rather limited, I rarely watch old films, I'm much more into stories than into interpretations...).
So here's a listing of a few good films to spend your time with:

  • WAZ. Great horror/thriller film. It's been compared to Saw and Seven in most reviews, and I agree with it, but would like to point that it's not that bloody as the last Saw installments, there's blood and "cruelty", but these are absolutely necessary. A well thought story about catharsis through revenge, about destroying those who destroyed you, using the same weapons. Same as with Amanda in Saw, I seem to love this kind of vindictive women.

  • Before the Devil knows you're dead. Very good story about 2 brothers trying to save their spoiled lives by robing their parent's jeweler's. The great plan ends up being a total disaster, and the consequences are even worst. I like how it depicts the decadent lives of 2 men and how they sink more and more.

  • In Dreams, pretty good and not much known terror film. Revolves around the pain of a woman whose only child has been murdered by a serial killer. The woman has terrible nightmares about the new crimes to come and is about to get nuts. Intense film where you almost taste the suffering of the main character.

  • Desert Flower. Based on a true story about a Somali woman that has suffered female genital cutting, I was expecting a harsher experience, but anyway it's an entertaining film and a good story. It's strange to see how something I reject so much as it's the fashion industry (I see it as a clear example of how banal and lacking in interests our Western Society has turned) in this case manages to fix the life of someone (in a sense a victim of that same social decay).

  • Man on Fire. Another good story about broken lives, revenge and redemption. A war mercenary turned into a body guard unexpectedly finds a reason to live and to die for. The setting for the story (modern day crime ravaged Mexico) fits pretty well.

  • The burning plain. Great film writtend and directed by the screenwriter of Amores perros, 21 grams... and starring Charlize Theron. So, the credentials are great, and the film really lives up to them. An interesting story stretching over more than a decade and told in a non lineal way. Sons that repeat their parent's "sins" and painfully drown in fear, guilt and remorse end up finding redemption in love. As a plus, I can't help saying that Chalize Theron along with a great interpretation gives us great views of her body ;-)

Friday, 3 September 2010

Chernobyl wild life sanctuary

This entry is related to the one I posted last week regarding how happy the planet would feel if he managed to get ridden of that plague that we are...

Reading an article in Muy Interesante (a Spanish popular science magazine) about Chernobyl 24 years after the accident, I've found that wildlife has flourished in the place, and rare species have returned, leading the Ukrainian government to designate the area a wildlife sanctuary.
There's some controversy around this, with claims against this positive effect, but anyway:

  • on one side it remains as a really interesting research case, from which we can learn about how organisms are able to adapt to cope with high radiation levels, and how really damaging different radiation levels are to life

  • on other side, We, Humans, seem rather more damaging for other species than a Nuclear explosion and its associated radiation...


Over the years I've watched several documentaries about the Chernobyl accident and its effects, but no doubt that the best one I've watched is this, an excellent research that reveals what governments kept hidden for many years, that after the first explosion there were chances for a much worse second one to happen, one that could have wiped out from the map a good part of Europe.
I remember from another documentary (that I can't find now) how lucky we were in Asturies regarding the radiation, winds kept blowing in such a way that the main radioactive cloud did not reach us. You can see the cloud behavior on this video.

Monday, 30 August 2010

Duck Typing and C#

The dynamic keyword in C# 4 (and the underlying DLR machinery making it possible) is amazing and powerful. Some people tend to get confused with Object, var and dynamic... but differences should be rather obvious:

  • var has nothing to do with dynamism, it's just compile time type inference (aka wrist friendliness).

  • Object is just that, everyone's ancestor that can be downcasted (with all its risks and inconveniences) to whatever we need

  • dynamic is pure dynamism, compiler assumes you know what you're doing when you declare something as dynamic and does not interfere. At runtime it will try to find the Method or Property that you're invoking-accessing, either using that object's implementation of IDynamicMetaObjectProvider, or resorting to Reflection. This is what we're used in languages like JavaScript, Python and so on... and is called Duck Typing



So dynamic and DLR are incredibly powerful, allowing us to mix the dynamic and static paradigms in the same language, but anyway, there are some limitations for which we'll need some extra help.

Let's say we have two compatible types (same public methods-properties) but while one of them implements an interface defining those semantics, the other one, for whatever reason (comes from a different code base for example) doesn't. Say we would like to use instances of both types in a piece of code. That's fine while that piece of code is dynamic, I mean, we're passing the object of one of the compatible types to a method expecting a dynamic parameter, but what happens if that method was implemented in a static fashion, expecting typed parameters (an interface)?
We would need to create a new class that implements that interface and redirects calls to the methods with the corresponding name in the initial class. Well, that's what dynamic proxies are for, intercepting and redirecting calls!.

For some odd reason, even in .Net 4, dynamic proxies are not part of the Framework (while Java has had them since the 1.3 times), so we would have to resort to one of the third party implementations out there (Castle, LinFu).
Castle DynamicProxy seems to be the most widely used, but I was already aware (I had toyed with it a bit some years ago) of the problem with its lack of documentation. Hopefully, this excellent tutorial encouraged me to jump onto it again.
Castle DP is arguably a powerful beast, but at first sight it didn't seem to provide out of the box support for what I needed (in principle you normally use it to intercept classes that already implement the common interface).
Googling for "C# and Duck Typing" I found a good article that explains how to accomplish what we want and shows me that I had read too fast the Castle samples.
He's using the CreateInterfaceProxyWithoutTarget. This is an interesting Proxy Generation method, as it does not need a class that already implements the interface, all the magic is in the interceptor. For this case Mauricio creates an interceptor that holds a reference to the proxied object, so we can invoke the corresponding method.

Proxy without target. This one is tricky. You don’t supply target implementation for the interface. Dynamic Proxy generates it for you at runtime, using interceptors to provide behavior for its methods.

The usage:

duck.As<IQuack>().Quack();

is very similar to the usage of this nice library that I had found some time ago (by the way, it also provides other useful functionalities like conversion between compatible delegates)
Note that this library has its roots in the pre-Extension Methods times, so that's why the syntax is uglier:

ICanAdd adder = DuckTyping.Cast<ICanAdd>(myAdder);

Back in 2008 the author (David Meyer) wondered if the advent of C# 4 would turn the library obsolete, as we've seen above, some parts of it are still useful, so I'll keep it in my programmer's toolkit.

You can see a usage sample here.
In order to compile it you'll need the DeftTech.DuckTyping.dll Assembly. You can get it all in this zip.

Saturday, 28 August 2010

There will come soft rains

I've just come across by chance this beatiful poem expressing how much Nature would enjoy a world without humans.


There will come soft rains and the smell of the ground,
And swallows circling with their shimmering sound;

And frogs in the pool singing at night,
And wild plum trees in tremulous white;

Robins will wear their feathery fire,
Whistling their whims on a low fence-wire;

And not one will know of the war, not one
Will care at last when it is done.

Not one would mind, neither bird nor tree,
If mankind perished utterly;

And Spring herself when she woke at dawn
Would scarcely know that we were gone.


The most astonishing thing is that this poem dates back to 1920!
At first it surprised me a bit finding someone that almost 100 years ago had the idea of the extinction of mankind revolving around his head. I think that nowadays, due to the many issues that we're facing now (climate change, globalization of the economy, oil depletion, fish depletion, religious fanatism, moral decline of Western society and above all, overpopulation) this idea of humanity being wiped out is not so strange for a certain percentage of the population, but wouldn't have expected such thoughts in that time.
Well, thinking it twice, I guess that after having witnessed in WWI how much the destructive power of humans had increased, thinking of the "total annihilation" was a pretty natural thought.

This idea of how great it would be for the planet to get rid of us (the architects of the Sixth Extinction) is rather clear to me, and is pretty well set out by Michael Boulter in that great documentary "Dodo's Guide to surviving extinction", but it's something that I don't think is understood by most of the population (when thinking about the many challenges that humanity will confront in the near future people tend to think about the end of the world, but no guys, it's not the end of "the world", it's just the end of humanity). This is normal due to our terribly anthropocentric view of everything, an anthropocentrism that will be one of the main causes of our self destruction...

I watched a documentary some years ago where Sir Martin Rees stated that the probability of extinction of Human race by the end of this century was around 50%, well, if we think in terms of a partial destruction (getting to an apocalyptical world like the ones depicted in The Book of Elli or The Road) I bet those chances are much higher...

By the way, "there will come soft rains" was the title of the E150's last track in their split 7" with Ivich (buff... that was 1997...). It was a beautiful melodic electronic song composed by their drum player (Carlos) that had little to do with the fast and furious music usually distilled by these BCN Punks.

Friday, 20 August 2010

Floods, Shock and Profit

At this point in time I guess everyone is aware to a greater or lesser extent of the terrible floods that devastated Pakistan last month.

In the last days media have warned of the attempts of Fundamentalist Islam to proselytize in the refugee camps, trying to gain supporters among these people who have lost it all...
What these bastards are trying is what Naomi Klein describes so brilliantly in her splendid book The Shock Doctrine, exploiting a chaos situation where people are weak, confused and terryfied, to make them accept a wicked and oppressive ideology...

I wonder if these repulsive fanatics will be trying to blame us (Westerns, Kafirs, infidels...) of the floods... of course they could resort to some Conspiracy Theory and say that we're using weather as a weapon through HAARP or something similar... or maybe, they'll be telling the victims that Allah has sent the rains to punish them for not being devote enough muslims...

During the floods part of the rescue services already took care of discriminating those who were not muslims

...were not rescued from their homes because rescuers felt that Muslims must be given priority...

Parallel Programming

An fast write up mainly to share a couple of links (and a reminder of things I'd like to go more in depth in the near future)

In these times when multi-core processors are ubiquitous (even for smartphones in the near future) it's clear that parallel programming is more and more important. .Net framework has tried to address this doing Parallel Extensions mainstream by including them in .Net 4 framework.

Parallel Extensions look exciting and promising to me, so finding this free book (Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4) has been really good news. I've only gone through the first pages and I've already learnt some interesting things about dynamic partitioning, oversubscription...

This reminded me about another excellent free book (Threading in C# by Joseph Albahari) that I had been reading some months ago. It's great news to see that it's been updated for .Net 4, including a full new chapter on Parallel Extensions (and many updates to the previous chapters to reflect other MultiThreading improvements to the Framework).

We tend to think of Parallelism just in terms of Threads or Processes, but there's more to it, for example:

  • SIMD instructions (that's SSE for x86 architecture). These instructions leverage Data Level Parallelism, performing the same instruction on multiple data, for example if we want to add different pairs of points, an only Add instruction could run in parallel over the different pairs. There's a good explanation here
    Mono has added support for SIMD instructions, which can be really useful for Multimedia applications doing heavy use of matrices and vectors. It's contained in an independent assembly that can be used both from Mono and from Microsoft's implementation.


  • GPGPUs. The programmability of GPUs has gone a long way since the initial vertex, pixel shaders. These shaders are used for graphic tasks, so while advanced and interesting, do not seem so revolutionary as being able to run "normal calculations" on the GPU and use the results from the code running on the CPU.
    So far, there are main technologies in this field:

    • CUDA. This is the first one I was aware of. I remember how surprised I was when I saw some code a couple of years ago showing how dynamically compile and load into the GPU some code embedded in a Python script

    • OpenCL (Update, 2010/09/27): I've just read a very interesting entry on InfoQ about using OpenCL via pure Java (code written in a Java subset gets translated to OpenCL code at runtime). It's pretty interesting what the author says on current usages of GPUs in common programming:

      Looking at the OpenCL side, I have seen everything from financial services to energy exploration (I used to work in that field). I also know folk that are looking at GPUs for seismic data analysis. Applications that have the potential to see major benefits of using the GPU for computation include the big-data analytics domain.

    • Direct Compute. Thought it's Microsoft Centric there don't seem to be direct bindings from .Net, but there are some articles out there on how to make them work together.



  • By the way, back to the shaders, it's really amazing being able to run Shaders in your browser via Silverlight!!!

Saturday, 14 August 2010

Windows Uptime

It's a bit odd that Windows does not provide an uptime command whose sole purpose were that, telling us the Uptime of our machine, but well, we have different options to get it:

  1. net statistics server
    as this command will display a bit more of information than just the uptime, we can trim it to just our desired line by using the grep equivalent provided by the Windows Resource Kit, qgrep:
    net statistics server | qgrpe -e "Statistics since"
    As you can test, it returns when the the computer was last booted.

  2. SystemInfo
    this command provides a lot of information about our system, but because of that it can take some seconds to run, so it's less useful for this task. Again, you can refine the results with qgrep:
    SystemInfo | qgrep -e "System Up Time"
    Again, it returns when the computer was last booted.

  3. WMI and WSH
    WMI is one of those wonderful Windows technologies of which potential not everyone is fully aware. I'm used to work with it from WSH (another underrated Windows technology), so I've prepared a script that uses WMI for either getting when the computer was booted:



    var wmiDate = WScript.CreateObject("WbemScripting.SWbemDateTime");
    var wmiObj = GetObject("winmgmts:\\\\" + strComputer + "\\root\\cimv2");
    var aux = wmiObj.ExecQuery("
    Select * from Win32_OperatingSystem");
    //WMI queries always return a collection, so get the first element (the only element in this case)
    var osInfo = new Enumerator(aux).item();
    wmiDate.Value = osInfo.LastBootUpTime;
    WScript.Echo(wmiDate.GetVarDate());


    or obtaining how many seconds have elapsed since then:



    var wmiObj = GetObject("winmgmts:\\\\" + strComputer + "\\root\\cimv2");
    //returns a collection with an only item
    var aux = wmiObj.ExecQuery("
    Select * From Win32_PerfFormattedData_PerfOS_System");
    var osPerfInfo = new Enumerator(aux).item();
    WScript.Echo("
    UpTime: " + osPerfInfo.SystemUpTime + " seconds");


    As you can see in the full source for this second case I had to add some extra code to turn the seconds into weeks, days, hours... this made me think that with .Net I just would need to create a TimeSpan object from those seconds, and it would already provide me the Days, Hours, minutes... so, why not to use the successor technology to WSH, one with full access to the .Net power? this takes us to:

  4. WMI and Windows PowerShell
    After playing for a while with the PowerShell console (If you have PowerShell installed, it should be here: C:\WINDOWS\system32\windowspowershell\v1.0)
    I realized this would involve several lines and I would better write a PowerShell script, so I came up with this uptime.ps1 file (it's my first PowerShell script, so please, be understanding):



    $perfObject = gwmi Win32_PerfFormattedData_PerfOS_System
    $seconds = $perfObject.SystemUpTime
    write-output ($seconds.ToString() + " seconds uptime")

    $timeSpan = new-object System.TimeSpan -argumentlist ($seconds * 1000 * 1000 * 10)
    write-output ($timeSpan.Days.ToString() + " days, " + $timeSpan.Hours.ToString() + " hours, " + $timeSpan.Minutes.ToString() + " minutes, " + $timeSpan.Seconds.ToString() + " seconds")


    in order to run it from the console you'll need to set permissions for running .ps1 scripts (yes, I agree it's weird that it's not enabled by default, but MS is taking security really serious), so type:

    Set-ExecutionPolicy RemoteSigned

    then, if you cd to the folder containing the script, just type:

    .\uptime.ps1

    and that's all.