Sunday, September 2, 2007

Portable 'supercomputer'

The story of the Calvin College portable supercomputer really irks me. It is getting a lot of attention for doing nothing. You can see it on engadget, Slashdot, and others. Anyone can build a cluster like what they have. The only credit I will give them is that not everyone would go through the benchmark and analysis as they did. On the other hand, these people are still on the same old trail of HPC. They ignore the entire issue of programming -- they used HPL code. Ask if they can write their own code for the microwolf. What kind of real application performance can they achieve? The engadget article says "Talk about a solid price-to-performance ratio." - what load of crap. How about asking real questions and reporting real HPC news?

Ninject Dependency Injection Framework

I have been playing with Ninject for a couple days now and I am having a lot of fun. One feature I have been really digging is Contextual Binding. The kernel allows multiple modules to be loaded that define the bindings of your types. So depending on which modules and bindings are set, I can change the way other binding resolve. Here is an example:

Say I want to resolve a different type of car dependent on a selected engine. I have an interface IEngine and two classes V4, V6 that inherit from IEngine. If V4 is resolved by the kernel then the car returned would be a Civic; an Accord if a V6 is returned. Here is the code in custom modules:

    internal class EngineModule : StandardModule
{
public override void Load()
{
Bind<IEngine>().To( typeof ( V4 ) );
}
}


    internal class CarModule : StandardModule
{
public override void Load()
{
IContext context = new StandardContext( Kernel, typeof ( IEngine ) );
IBinding binding = Kernel.GetBinding<IEngine>( context );
Type implementationType = binding.Provider.GetImplementationType( context, false );

Bind<ICar>().To( typeof ( Accord ) ).OnlyIf(
delegate
{
if ( implementationType == typeof ( V6 ) )
{
return true;
}

return false;
} );

Bind<ICar>().To( typeof ( Civic ) ).OnlyIf(
delegate
{
if ( implementationType == typeof ( V4 ) )
{
return true;
}

return false;
} );
}
}



I can then load the modules into my kernel and use it to resolve cars:

    internal class Program
{
private static void Main( string[] args )
{
IModule[] modules = new IModule[] { new SettingsModule(), new EngineModule(), new CarModule() };
IKernel kernel = new StandardKernel( modules );

ICar car = kernel.Get<ICar>();
Console.WriteLine( "I just got a brand new {0} {1}.", car.Manufacturer, car.Model );
Console.WriteLine( "It has a {0} cylinder engine with {1} horsepower and {2} ft-lb torqe.", car.Engine.Cylinders, car.Engine.Horsepower, car.Engine.Torque );
Console.WriteLine( "Time to start the Engine!" );
car.Engine.Start();
Console.WriteLine( "Sound good, but I am going to turn it off for now." );
car.Engine.Stop();
Console.ReadLine();
}
}



Nate Kohari just wrote a first cut user guide that is very helpful when getting started.