Angel “Java” Lopez on Blog

June 3, 2009

First NHibernate 2.x example

Filed under: .NET, C Sharp, NHibernate — ajlopez @ 9:23 am

I was working creating a .NET solution, containing a minimal NHibernate 2.x example. I’m using NHibernate GA 2.0.1 binary distribution, following the reference manual instruction.

You can download the source code from my Skydrive folder:

NHibernateExample1.zip 

I was using the online documentation from

http://nhforge.org/doc/nh/en/

and I built the same manual in .pdf format, from the SVN trunk:

https://nhibernate.svn.sourceforge.net/svnroot/nhibernate

You can download the manual in .pdf from

nhibernate-reference.pdf

The solution contains two projects: a class library, and a console application:

The domain contains only one class, Cat (following the first example included in the NHibernate doc):

namespace NHibernateExample1.Domain
{
    public class Cat
    {
        private string id;
        private string name;
        private char sex;
        private float weight;

        public Cat()
        {
        }
        public virtual string Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }

        public virtual char Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        public virtual float Weight
        {
            get { return weight; }
            set { weight = value; }
        }
    }
}

There is an embedded resource describing the mapping: Cat.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernateExample1.Domain" assembly="NHibernateExample1.Domain">
  <class name="Cat" table="Cats">
    <!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
    <id name="Id">
      <column name="CatId" sql-type="char(32)" not-null="true"/>
      <generator class="uuid.hex" />
    </id>
    <!-- A cat has to have a name, but it shouldn' be too long. -->
    <property name="Name">
      <column name="Name" length="16" not-null="true" />
    </property>
    <property name="Sex" />
    <property name="Weight" />
  </class>
</hibernate-mapping>

There is an ExecuteAll.cmd in Sql folder, that creates the database (you can pass the MS SQL Server as parameter, .\SQLEXPRESS is the default value):

The console application is simple: configures NHibernate, gets a session, inserts new objects inside a transaction, executes a query, and list the result:

        static void Main(string[] args)
        {
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

            ISession session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();

            Cat cat;

            cat = new Cat() { Name = "Moe", Sex = 'M', Weight = 9.0f };

            session.Save(cat);

            cat = new Cat() { Name = "Larry", Sex = 'M', Weight = 8.5f };

            session.Save(cat);

            cat = new Cat() { Name = "Sue", Sex = 'F', Weight = 7.5f };

            session.Save(cat);

            tx.Commit();

            IQuery query = session.CreateQuery("select c from Cat as c where c.Sex = :sex");

            query.SetCharacter("sex", 'M');

            foreach (Cat kitty in query.Enumerable())
                System.Console.Out.WriteLine("Male Cat: " + kitty.Name);

            session.Close();

            System.Console.ReadLine();
        }

Running the console application inserts this data in the database:

The example is simple. Now I have an updated running example using NHibernate 2.0, I’m planning to update my AjGenesis templates for NHibernate to use that version.

Angel “Java” Lopez

http://www.ajlopez.com/en

http://twitter.com/ajlopez

April 27, 2009

Presenting AjCat

Filed under: .NET, C Sharp, Programming Languages — ajlopez @ 9:16 am

Some weeks ago, I was working on an interpreter of the Cat language:

The Cat Programming Language

Cat is a functional stack-based programming language inspired by the Joy programming language. The primary differences is that Cat provides a static type system with type inferencing (like ML or Haskell), and a term rewriting macro language extension language called MetaCat.

Cat is a high-level intermediate language translation that can also be used as a stand alone language for simple application development. In this way it occupies a similar niche to PostScript. Cat is also an appropriate language for teaching of basic programming concepts.

If you are not familiar with Cat language, you can learn more at:

Cat Tutorial
Cat Specification
Cat Primitives

I published the code as part of my Code Katas Google code project. It’s named AjCat:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjCat

The solution contains three projects:

The implementation is not complete. It’s only support integers, no .NET object support yet, and no graphics primitives.

Running the console program, you can enter and evaluate expressions:

This code is dedicated to Rodolfo Finocchieti (@rodolfof in Twitter) who pointed me to Cat language, a fascinanting idea.

Tests are green:

And good code coverage

Angel “Java” Lopez
http://www.ajlopez.com/
http://twitter.com/ajlopez

March 11, 2009

TDD and Code Kata: Writing a Lexer for AjRuby

Filed under: C Sharp, Programming Languages, Software Development — ajlopez @ 7:20 am

Usually, I write programs as code katas, to train me in some software development topics. During last months, I wrote many mini interpreters, using TDD-like approach, code coverage, and C#. Past week, I was working in the kickoff of yet another one, AjRuby. The starting point: the lexer.

A lexer is a program that analizes a source code text, and returns the tokens, the "words" that compose the program. In the case of AjRuby, I wrote a lexer that follows the Ruby language grammar. It’s not complete yet, but it’s working.

You can see my progress at my code katas Google code project:

http://code.google.com/p/ajcodekatas/source/browse#svn/trunk/AjRuby

The solution has a library project, and a test project:

It has a Token class, that represents each token:

public class Token { public string Value { get; set; } public TokenType TokenType { get; set; } }

The TokenType is an enum:

public enum TokenType { Name, Integer, Real, Boolean, String, Operator, Separator }

There is a LexerTest.cs class, containing tests like:

[TestMethod] public void ShouldProcessName() { Lexer lexer = new Lexer("name"); Token token; token = lexer.NextToken(); Assert.IsNotNull(token); Assert.AreEqual(TokenType.Name, token.TokenType); Assert.AreEqual("name", token.Value); token = lexer.NextToken(); Assert.IsNull(token); }

There is a test for every kind of token to be processed. Other example:

[TestMethod] public void ShouldProcessNameWithInitialUnderscore() { Lexer lexer = new Lexer("_name"); Token token; token = lexer.NextToken(); Assert.IsNotNull(token); Assert.AreEqual(TokenType.Name, token.TokenType); Assert.AreEqual("_name", token.Value); token = lexer.NextToken(); Assert.IsNull(token); }

I wrote the tests using TDD ideas: write the test, compile with error, fix the compiler issues, run the tests in red, fix them, get green status. I skipped most of the refactor steps. I guess the tested code could be improved, but by now, I’m trying to get a working lexer. The tests are in green state, good!

My goal is to have > 80% of code coverage, at any time during the project development:

It’s good to write something only for fun and learning. For me, it’s part of the continuous improvement every software developer should pursuit.

Angel "Java" Lopez

http://www.ajlopez.com/en

http://twitter.com/ajlopez

March 9, 2009

Distributed Web Crawler using AjMessages

Filed under: .NET, AjMessages, C Sharp — ajlopez @ 8:09 am

Last January, I update my AjMessages project to support communication using DSS/CCR and Windows Communication Foundation. AjMessages is a program that can run in a distributed way, sending asynchronous messages from one logical node to another. The logical nodes can be hosted in one or more physical machines. More info about AjMessages at:

AjMessages- a message processor
Distributed Applications with AjMessages using DSS/CCR

You can download the source code from

http://www.codeplex.com/ajmessages

I had the idea of write a distributed message processor. Years ago, I wrote my first attempt, named AjServer

Hacia el AjServer (Spanish Post)

After that project, I met Fabriq ideas and implementation, where there are logical message handler, that can be distributed in a transparent way, via configuration:

In 2007, I wrote the first version of AjMessages, as a proof of concept: I could reproduce most of the Fabriq ideas, trying to raise some abstraction, in order to have a little more flexibility. Fabriq was more SOA-oriented. AjMessages is oriented to arbitrary message processing, to have a grid-like application.

This is the current AjMessages solution:

There is core project, and two transport projects: one for WCF support, and another to DSS/CCR (Microsoft Robotics technology). If you don’t have DSS/CCR, you can remove the related projects. The core AjMessages is free of dependencies on such technologies. AjMessages.SampleApp contains simple handler: a decrement handler, that takes a message with an integer, and produces a new message with a new decremented integer. AjMessages.WebCrawler implements the message and handlers that make a distributed web crawler application. You can run it in only one machine or in many hosts.

Each AjMessage message has:

Body: an arbitrary payload
Headers: additional information, key-value.
Action: describing the target of the message, using pattern Application/Node/Action

(more detail in the mentioned post AjMessages- a message processor)

Let’s explore the distributed web crawler example. This application is composed by nodes, that can be orchestrated to visit a link, and gets from that initial page all the links and related pages, up to a level. Logically, it can be described as:

The first messages is send to the node Controller, action Dispatch. It contains the initial page to visit. The message is enriched, and passed to node Controller, action Resolve. It’s in charge to keep a list of visited page, and to control the deep of processing. If the link is aproved, then a message is send to node Downloader, action Download. The content of the page is downloaded, added to the message, and forwarded to node Harvester, action Harvest. This action analyze the content of the page, and emits many message, one for each link found in  it. The recipient of the new links is the node Resolver, action Resolve. So, the process continues up to visit all pages (with a limit on the deep of processing).

This application can be described using an XML configuration file, example:

<?xml version="1.0" encoding="utf-8" ?> <AjMessages> <Application Name="WebCrawler"> <Node Name="Dispatcher"> <Handler Name="DispatcherHandler" Type="AjMessages.WebCrawler.Handlers.Dispatcher, AjMessages.WebCrawler"/> <Action Name="Dispatch" Handler="DispatcherHandler"/> </Node> <Node Name="Harvester"> <Handler Name="HarvesterHandler" Type="AjMessages.WebCrawler.Handlers.Harvester, AjMessages.WebCrawler"/> <Action Name="Harvest" Handler="HarvesterHandler"/> </Node> <Node Name="Downloader"> <Handler Name="DownloaderHandler" Type="AjMessages.WebCrawler.Handlers.Downloader, AjMessages.WebCrawler"/> <Action Name="Download" Handler="DownloaderHandler"/> </Node> <Node Name="Controller"> <Handler Name="ResolverHandler" Type="AjMessages.WebCrawler.Handlers.Resolver, AjMessages.WebCrawler"/> <Action Name="Resolve" Handler="ResolverHandler"/> </Node> </Application> </AjMessages>

An application is composed by nodes. Each node can be viewed as a “logical class”. Each node can handle Actions, that are the targets of messages. An action can be composed by one or more steps, of message handler. This is the extensibility point of the application: you must provided the steps, the message handler, and write a configuration file to orchestrate the message processing.

But one thing is the application, and another its distribution over physical machines. You can have two hosts, and install different logical nodes in each one:

In this diagram, the Dispatcher and Resolver are in one host, and the Downloader and Harvester in the other one. But you can put harversters in each host, or in twenty machines. It’s up to you how to distribute the load of the work. When a message is sent to a target, AjMessage forwards it to an appropiate host, that have a node capable of attending the message action.

The distribution of logical nodes into physical host is defined via configuration, an example:

<?xml version="1.0" encoding="utf-8" ?> <AjMessages> <Host Name="Server1" Address="http://localhost:50002/AjMessages" Activate="true"> <Application Name="AjMessages"> <Node Name="Administration"/> </Application> <Application Name="WebCrawler"> <Node Name="Controller"/> <Node Name="Dispatcher"/> <Node Name="Harvester"/> <Node Name="Downloader"/> </Application> </Host> <Host Name="Server2" Address="http://localhost:50003/AjMessages"> <Application Name="AjMessages"> <Node Name="Administration"/> </Application> <Application Name="WebCrawler"> <Node Name="Dispatcher"/> <Node Name="Harvester"/> <Node Name="Downloader"/> </Application> </Host> </AjMessages>

Running the Web Crawler example

You can test the program, launching two host, in the same machine. Run the console program AjMessage.Console. Then enter the command

fork

This command launches a second host. Now, you are ready to configure the two hosts. In the first one, enter:

load ConfigurationServer1.xml

This file loads the initial AjMessage application, and define a WCF endpoint to listen messages. Then, enter:

load ConfigurationWebCrawler.xml

This command loads the Web Crawler node definitions (no deploy info yet). The third command define the distribution in hosts:

load ConfigurationWebCrawlerNode1.xml

Now, back to second host console. Enter the corresponding commands:

load ConfigurationServer2.xml
load ConfigurationWebCrawler.xml
load ConfigurationWebCrawlerNode2.xml

You are ready to launch the first web crawling. Go to first console, and enter:

send WebCrawler/Dispatcher/Dispatch http://ajlopez.zoomblog.com

(that is my Spanish non technical blog… ;-)

The web crawling begins to work, in both consoles. A typical view on first console:

Next steps

I should fix some problems in DSS/CCR tranport (I could run a simpler app, but not the web crawler). Two points to implements:

- Remote configuration of nodes
- Distribution of message handlers bits to remote nodes
- One configuration for all the nodes (now, each node is configured separately)

I began to experiment with a more abstract way of processing message, that should implement uses cases like:

- A simple hello world
- Decrement example, as in AjMessages sample app (see AjMessage.SampleApp)
- Distributed web crawler
- Enterprise Service Bus-like app

My first steps at:

http://code.google.com/p/ajcodekatas/source/browse#svn/trunk/AjProcessor

But the project is still in its infancy. Keep tuned!

Angel “Java” Lopez
http://www.ajlopez.com/
http://twitter.com/ajlopez

February 25, 2009

Presenting AjLambda: Lambda calculus implementation in C#

Filed under: .NET, C Sharp, Functional Programming, Programming Languages — ajlopez @ 8:21 am

I was working on my “code kate” AjLambda, a Lambda Calculus implementation, written using C#:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLambda

(If you are new to Lambda Calculus notation, there are tutorial links at the end of this post)

Yes! It’s the geeky project I wrote this year…;-).. The solution is simple, it contains three projects:

It has a core library, AjLambda, a test project AjLambda.Tests and an AjLambda.Console program to run. The core library has few main classes:

Expression is the base abstract class. .Reduce is the method that implements the reduction of an expression. .Replaces is used to change a variable by other. Sometimes, it’s needed to choose a new name for a variable, that it doesn’t collides with another free variable name. .FreeVariables is the method to obtain the list of free variables in an expression. For example, in the lambda expression:

\x.xy

x and y are variables, but y is a free variable, and x is a bound parameter.

Variable express the variable (a lower-case letter in this implementation). Lambda is the lamba (parameter plus body). The lambda is entered using:

\x.x

The short version with multiple parameters is supported:

\xy.yx

This is the .Reduce implementation for Pair (a pair of expression, left and right): 

public override Expression Reduce() { if (this.left is Lambda) return ((Lambda)this.left).Apply(this.right); Expression newLeft = this.left.Reduce(); if (newLeft != this.left) return new Pair(newLeft, this.right); Expression newRight = this.right.Reduce(); if (newRight == this.right) return this; return new Pair(newLeft, newRight); }

Running AjLamnda.Console with a parameter:

AjLambda.Console Boot.l

loads the auxiliary file Boot.l, where I defined some predefined functions:

; True
T = \xy.x
; False
F = \xy.y
; And
AND = \ab.abF
; If Then Else
IFTHENELSE = \x.x
; Natural numbers
0 = \fx.x
1 = \fx.fx
2 = \fx.f(fx)
3 = \fx.f(f(fx))
; Succesor
SUCC = \nfx.f(nfx)
; Add
ADD = \nmfx.nf(mfx)
; Multiply
MULT = \nmf.n(mf)
; Is Zero
ISZERO = \nxy.n(\z.y)x
; Predecessor
PRED = \nfx.n(\gh.h(gf))(\u.x)(\u.u)
; Pair Functions
PAIR = \xyf.fxy
FIRST = \p.pT
SECOND = \p.pF
NIL = \x.T
NULL = \p.p(\xy.F)
; Fixed Point
A = \xy.y(xxy)
Y = A A
; Factorial
FACT = Y (\f.\n.IFTHENELSE (ISZERO n) (1) (MULT n (f (PRED n)))))

You can ask about the predefined names. I defined the first natural numbers, including zero:

Names are words that begin with upper case letter. Variable names are lower letter (from ‘a’ thru ‘z’, only one letter per variable).

As usual, I defined SUCC and PRED (succesor and predecessor functions):

Each reduction step is printed, until no more reduce is possible.

You can define new functions using Name = <expression>:

More information about Lambda Calculus at:

Lecture Notes on the Lambda Calculus (pdf) (excellent paper to learn Lambda Calculus)

Peter Selinger- Papers

Lambda calculus – Wikipedia, the free encyclopedia

A Tutorial Introduction to the Lambda Calculus

http://delicious.com/ajlopez/lambda

Lambda Calculus implementation from Fred’s Page

Angel “Java” Lopez
http://www.ajlopez.com/
http://twitter.com/ajlopez

January 30, 2009

Dynamic Expressions Example

Filed under: .NET, C Sharp, Programming Languages — ajlopez @ 6:26 am

Yesterday, I was preparing a presentation for today, explaining lambda calculus, its history, and lambda application in languages. At the end of presentation, I’ll discuss lambda ideas in .NET: delegates, lambda notation, and lambda expression.

During a quick research, I found a really cool implementation of dynamic expression parsing, in the example:

DynamicLinq.cs

That code points as source the “the Gu” blog post:

Dynamic LINQ (Part 1- Using the LINQ Dynamic Query Library)

The code is included in the DynamicQuery directory in:

C# Dynamic Query Library (included in the -LinqSamples-DynamicQuery directory)

My initial idea was to write a formula graph winform app, using System.Linq.Expressions. But the above implementation has a notable feature: instead of buildin an expression tree composing a expressions, it takes an string (!!!) and parse on the fly, converting to a compiled version.

OK, this trick is used by many Linq query implementations, but I didn’t know that the code samples from Microsoft include this code.

Using the DynamicLinq.cs, I was able to create a Win Form app:

Note that you can use classes from .NET. “x” is the parameter I use to calculate the formula.

This is a key code in the Calculator.cs file:

 

public Delegate CompileFormula(string formula) { ParameterExpression x = Expression.Parameter(typeof(double), "x"); LambdaExpression e = DynamicExpression.ParseLambda(new ParameterExpression[] { x }, null, formula); return e.Compile(); }

The compiled Delegate, can be invoked, provided the “x” parameter value:

 

public double[] Calculate(Delegate formula, double from, double to, double step) { List<double> values = new List<double>(); for (double x = from; x <= to; x += step) values.Add((double) formula.DynamicInvoke(x)); return values.ToArray(); }

You can download this sample code from:

http://code.google.com/p/ajcodekatas/source/browse#svn/trunk/DynamicExpressionsExample

Another dynamic implementation:

To Bind or Not To Bind – Dynamic Expression Trees – Part 3 – B# .NET Blog
To Bind or Not To Bind – Dynamic Expression Trees – Part 2 – B# .NET Blog
To Bind or Not To Bind – Dynamic Expression Trees – Part 1 – B# .NET Blog

Some ideas:

- Extend the example to 3D (I could use the F#/DirectX visualization demo as a base code)

- Send a formula as text, to be processed in a distributed application, grid, or HPC cluster.

- Code kata writing a .Parse and .Compile to any of my scripting languages

If you need more power, the next level of dynamic languages and their compilation, using .NET, is the Microsoft.Scripting namespace in IronPython source code:

http://www.codeplex.com/IronPython/SourceControl

Introductions to IronPython, DLR initiative:

CLR Inside Out- IronPython and the Dynamic Language Runtime
CLR Inside Out- Dynamic Languages and Silverlight
InfoQ- Microsoft Surpasses Java’s Dynamic Language Support-
CLR Inside Out – IronPython

Related links about lambda, Dynamic Language Runtime, and dynamic languages in general:

http://delicious.com/ajlopez/lambda
http://delicious.com/ajlopez/dlr
http://delicious.com/ajlopez/dynamiclanguages

Angel “Java” Lopez
http://www.ajlopez.com/en
http://twitter.com/ajlopez

January 27, 2009

AjGa: a genetic algorithm library

Filed under: Artificial Intelligence, C Sharp, Software Development — ajlopez @ 7:15 am

I was coding a genetic algorithm library, using C#. The code is in my AjCodeKatas Google Code project, at:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjGa

The main project is AjGa (with AjGa.Tests for testing):

The main interfaces are:

I use generics, with two generic types: G, as the gene implementation type, and V, as the value type (e.g. int, double), that it will be used to catalog the genomes.

IPopulation is a colletion of genomes.

IGenome  is a collection of genes, that is evaluated to value.

IEvaluator is in charge of evaluate a genome.

The implementation of IEvolution runs generations over a population, employing mutator, crossover operators, to change the population composition after each run.

There are operator interfaces, to generate one genome, mutate an existing one, or apply a crossover over two genomes.

To test the ideas, I implemented a project with gene, genome, and operators, to attack the classical Travelling Salesman Problem:

In this example, an evaluator has a list of positions of cities to visit:

 

public class Evaluator : IEvaluator<int, int> { private List<Position> positions; public Evaluator(List<Position> positions) { this.positions = positions; }

The gene type is int, and tha value of each genome is expressed as a int. The genome keeps a list of integers, representing the ordinal number of the cities to visit:

 

using AjGa; public class Genome : BaseGenome<int, int> { public Genome(int size) { for (int k = 0; k < size; k++) { AddGene(k); } }

That is, each gene is an int, and a genome with 2, 0, 1 represents: visit the third city, then the first one, and finish the trip in the second city.

You can run the WinForm project, to test the library:

The above run has a random distribution of cities. The cities can be distributed in a grid:

This way, you can test the efficient of the algorithm to reach the optimum solution.

If you want to implement a new GA project, you should:

- Define the gene type

- Define the value type

- Some operators (creators, mutators, crossovers)

Tests are green:

Code coverage in fine:

I spent 4 hours in the first version, 3 hours exploring TSP, and around 8 hours preparing and tuning the TSP WinForm application.

As usual, I enjoyed write this software!

Angel “Java” Lopez
http://www.ajlopez.com/en
http://twitter.com/ajlopez

December 26, 2008

Fractals using MPI.NET and HPC

Filed under: .NET, C Sharp, High Performance Computing — ajlopez @ 5:50 am

I updated my fractal example to support MPI.NET (Message Passing Interface with .NET) and parametric tasks in Windows HPC Server 2008. The example can be download from my ajcodekatas Google code:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/FractalExample

There are two solutions. Fractal.sln contains:

The Fractal.Console project is a console application that takes parameters from the command line. It uses that parameters to generate a serialized sector of the fractal, writing a file:

 

static void Main(string[] args) { if (args[4].Equals("*")) args[4] = "0"; SectorInfo sectorinfo = new SectorInfo() { RealMinimum = Convert.ToDouble(args[0]), ImgMinimum = Convert.ToDouble(args[1]), Delta = Convert.ToDouble(args[2]), FromX = Convert.ToInt32(args[3]), FromY = Convert.ToInt32(args[4]), Width = Convert.ToInt32(args[5]), Height = Convert.ToInt32(args[6]), MaxIterations = Convert.ToInt32(args[7]), MaxValue = Convert.ToInt32(args[8]) }; Calculator calculator = new Calculator(); Sector sector = calculator.CalculateSector(sectorinfo); SectorSerializer serializer = new SectorSerializer(); string filename = string.Format("{0}-{1}-{2}-{3}-{4}.bin", args[9], sectorinfo.FromX, sectorinfo.FromY, sectorinfo.Width, sectorinfo.Height); serializer.Serialize(sector, filename); }

 

You can run the project in Debug mode, with parameters:

Launch the new Fractal.GUIFiles project. It’s a winform application, with a new button Read:

 

Click Read button, and load the generated sector file located in the Fractal.Console bin\debug directory:

This is the result:

Creating the sectors with HPC

The console application can be used in a cluster. Suppose the application is installed in c:\FractalConsole in each node of a cluster. Suppose the name of the head node is HEAD-NODE, and that it has a shared directory named \shared. Then, we can submit a parametric job:

job submit /parametric:0-500:100 c:\FractalConsole\Fractal.Console.exe 0.3 0.3 0.01 0 * 1000 100 2000 4 \\HEAD-NODE\shared\sector

This command submit a parametric job to the cluster. The asterisk in the parameter lists will be replaced by the values 0,100,200,300,400 and 500 (this is the Y coordinate of the top left point of sector). Each execution will produce a file with a serialized sector in the shared directory, that you can read and show using the Fractal.GUIFiles app.

Using MPI.NET

There is a second solution Fractal.MPI:

This code uses MPI (Message Passing Interface). The rank 0 receives a sector, and then, the sector is partitioned between all ranks in execution. Each instance writes a file, representing a subsector of the original sector. 

To compile and run this example I installed the HPC Pack I downloaded from:

HPC Pack 2008 SDK download

and then, I installed MPI.NET Software

(I installed MPI.NET SDK.msi but I expanded MPI.NET-1.0.0.zip too: it has better examples, with VS solutions)

(note: if you want to run under XP Pro, you must download the previous version of the HPC SDK:
Microsoft Compute Cluster Pack SDK

The new SDK has an issue with XP. More info at:

http://social.microsoft.com/Forums/en-US/windowshpcdevs/thread/19deb181-15c2-40be-bb5e-2d4604b984a4
http://www.pluralsight.com/community/blogs/drjoe/archive/2008/10/10/32-bit-sdk-for-hpc-server-2008-fails-quot-the-procedure-entry-point-getprocessidofthread-could-not-be-located-quot.aspx
)

You can run the program using mpiexec utility, that launch many instances of the same program:

mpiexec -n 10 Fractal.Mpi.Exe 0 0 0.01 0 0 500 1000 2000 4 sector

The sectors will be produced by ten instances:

that you can read and show using the Fractal.GUIFiles.

You can run the above command in an HPC cluster, using:

job submit /numnodes=10 mpiexec c:\FractalMpi\Fractal.Mpi.Exe 0 0 0.01 0 0 500 1000 2000 4 \\HEAD-NODE\shared\sector

(assuming you had deployed the application in each node, inside c:\FractalMpi folder)

You have a more complete example at:

Learning Parallel Programming — from shared-memory multi-threading to distributed-memory multi-processing

Angel “Java” Lopez
http://www.ajlopez.com/en
http://twitter/ajlopez

December 22, 2008

AjTalk: my Smalltalk-like interpreter project updated

Filed under: .NET, AjTalk, C Sharp, Smalltalk — ajlopez @ 7:30 am

I committed new code to my Google code project AjTalk:

http://code.google.com/p/ajtalk/

The previous version was described in my post

AjTalk- a Smalltalk-like interpreter

It’s an interpreter written using C#. This post describes the work in progress in this solution:

There are interfaces and base classes to support objects and classes:

Every method is compiled to bytecodes. Block and Method are the classes that contains bytecodes programs:

Blocks are anonymous code block. A Method has name and belongs to a class.

The bytecodes are described in an enumeration:

using System; namespace AjTalk { public enum ByteCode : byte { Nop = 0, GetVariable = 1, SetVariable = 2, GetArgument = 3, SetArgument = 4, GetConstant = 5, GetLocal = 6, SetLocal = 7, GetClassVariable = 8, SetClassVariable = 9, GetGlobalVariable = 10, SetGlobalVariable = 11, GetBlock = 12, GetSelf = 20, GetClass = 21, GetSuperClass = 22, NewObject = 23, Pop = 24, ReturnSub = 25, ReturnPop = 26, Add = 40, Substract = 41, Multiply = 42, Divide = 43, Send = 50 } }

In this committed version, there is support to create and access .NET objects. You can write

@System.IO.FileInfo new: ‘aFile.txt’

to create a new .NET object (@ is used to mark a name with special chars like ‘.’).

In this version, I added support to load .st files. It’s not an standard .st file: I will use this files to define the initial class library. Now, there are used only in tests. An example:

nil subclass: #Figure instanceVariables: 'x y' !Figure methods! x ^x ! y ^y ! x: newX x := newX ! y: newY y := newY ! ! Figure subclass: #Rectangle instanceVariables: 'width height' !Rectangle methods! width ^width ! height ^height !

 I bootstrapped the system with a global nil variable. In the loader tests, the nil variable is bound to understand messages like subclass: and ifFalse:.

I changed the unit test to use VS test support. All in green:

The code coverage is good, 84.29%:

I added support to blocks in method, but there is no parameter support, yet:

nil ifFalse: [GlobalName := 'foo']

Next steps

The proposed roadmap is:

- Add more .NET object support

- Write the initial class in base library

- Implements Class, Metaclass, Behaviour “a la” Smalltalk-80

- Load initial library in console application

- Invoke AjTalk from .NET application

- Implements indexed variables

- Extends the bytecode set

As usual, I had fun written this code!

Angel “Java” Lopez
http://www.ajlopez.com/
http://twitter.com/ajlopez

December 17, 2008

Fractal application revisited

Filed under: .NET, C Sharp — ajlopez @ 9:46 am

This year, I wrote a fractal demo application using DSS/CCR

Distributed Agents and Fractals using DSS/VPL

Now, I’m exploring parallel execution using Task Parallel Library, threads, or MPI (Message Passing Interface). But before writing new versions of fractal app using parallel programming, I added to my ajcodekatas Google code, a base application, WinForm Fractal, that generates fractal images using the GUI thread:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/FractalExample

 

There is a class library, with core classes, and a WinForm project, that uses a Calculator object to calculate a Sector of fractal. An excerpt of that invocation:

 

private void Calculate() { Bitmap bitmap = new Bitmap(pcbFractal.Width, pcbFractal.Height); pcbFractal.Image = bitmap; pcbFractal.Refresh(); realWidth = realDelta * pcbFractal.Width; imgHeight = imgDelta * pcbFractal.Height; realMin = realCenter - realWidth / 2; imgMin = imgCenter - imgHeight / 2; int width = pcbFractal.Width; int height = pcbFractal.Height; SectorInfo sectorinfo = new SectorInfo() { FromX = 0, FromY = 0, Width = width, Height = height, RealMinimum = realMin, ImgMinimum = imgMin, Delta = realDelta, MaxIterations = colors.Length, MaxValue = 4 }; Calculator calculator = new Calculator(); Sector sector = calculator.CalculateSector(sectorinfo); this.DrawValues(sector.FromX, sector.FromY, sector.Width, sector.Height, sector.Values); }

Run Fractal.GUI project, and click on Calculate:

You can drag the mouse to select a zone. Releasing the mouse, a new image is generated. You can change the colors, that are randomly selected:

Next Steps

I want to add new versions:

- Multithread version

- Parallel version using Task Parallel Library

- Distributed version (MPI.NET? AjMessages?)

If you are impatient, there is an excellent tutorial, implementing a fractal application using HPC 2008 and the above technologies, ready to download from:

Learning Parallel Programming — from shared-memory multi-threading to distributed-memory multi-processing

Suggestions, comments, welcome!

Angel “Java” Lopez
http://www.ajlopez.com/en
http://twitter.com/ajlopez

Older Posts »

Blog at WordPress.com.