Thursday, May 03, 2007

I'm sure you are often sitting at your new Vista desktop wondering "I wish I knew what the standard options and factory colors of a 2005 BMW M3 were and all I have is this VIN of my friend's M3."  Well, you may be in luck.  I have created a VINTool as a Vista desktop gadget, shown below.  Right now I'm just framing something in while a proper interface is created.  I have to make sure I'm cleared to release this, as its not 100% certain if there will be a licensing issue with some of our data.  In addition we may create some type of Vehicle notification so that you can say "I'm searching for a Triumph TR6 within 100 miles" and you'll see a message if such a thing is added to our systems.  Useful?  Useless but cool?  Neither useful or cool?  You decide.

Thursday, May 03, 2007 3:00:21 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback

Some of those thoughts I didn’t get to last night.

First of all I should say that the threading in .NET rocks in my opinion.  It’s fast, easy to program for, and very complete for a managed platform.   The inclusion of intra-process mutexen is especially nice.  So, here’s some code for utilizing some of the functionality I showed designs for yesterday.

            PrimeSieveContext ctx = new PrimeSieveContext(1000000);

 

            WorkDispatcher<long, PrimeSieveContext> facade = new WorkDispatcher<long, PrimeSieveContext>(3,ctx);

            PrimeAdderWorkUnit wu0 = new PrimeAdderWorkUnit(1, 500000);

            PrimeAdderWorkUnit wu1 = new PrimeAdderWorkUnit(500001, 750000);

            PrimeAdderWorkUnit wu2 = new PrimeAdderWorkUnit(750001, 1000000);

            facade.AddWorker(wu0);

            facade.AddWorker(wu1);

            facade.AddWorker(wu2);

<snip/>

long result = facade.Execute();

So when I said there’s a lot of room for improvement, the first item I’d like to look into is setting it up so that the WorkDispatcher decides for itself how many work units to split the problem domain into.  That is somewhat at odds with the first improvement I made.  Here’s a bit of the WorkDispatcher Execute method:

            if (_maxThreads > 1)

            {

                //Start beyond the 1st one, spawn threads, wait until they are all done

                for (int i = 1; i < _maxThreads; ++i)

                {

                    ThreadPool.QueueUserWorkItem(new WaitCallback(_workers[i].ExecuteAsync), _ctx);

                }

                TResultType rslt = _workers[0].Execute(_ctx);

 

                WorkerSharedContext<TResultType> finalCtx = _ctx as WorkerSharedContext<TResultType>;

                for (int i = 1; i < _maxThreads; ++i)

                {

                    _workers[i].CompleteHandle.WaitOne();                   

                    finalCtx.AddResultComponent(_workers[i].Result);

                }

                return finalCtx.GetContextualResult();

            }

First of all, I love the Generics in .NET 2 but several times I’ve ran across a situation where I wish one of the Template types could also be specified as implementing an interface or extending a base class.  I suppose I need to refactor the design here.   At any rate I noticed depending on which chunk of work I placed in the _workers[0] spot that I could keep both my CPUs maxed for longer and finish the work a few seconds sooner.    Ideally I should just give the WorkDispatcher hints about this rather than ordering the chunks myself, so I added the following:

        /// <summary>

        /// May cause the WorkDispatcher to re-order upon Setting this value.  The default value is FIFO

        /// </summary>

        public WorkDispatcherScheduleTypes ScheduleType

        {

            get { return _scheduleType; }

            set

            {

                _scheduleType = value;

                if (_scheduleType.Equals(WorkDispatcherScheduleTypes.OrderSensitive))

                {

                    _workers.Sort(new WorkUnitOrderComparison<TResultType, TSharedContext>());  

                }

                else if (_scheduleType.Equals(WorkDispatcherScheduleTypes.WeightSensitive))

                {

                    _workers.Sort(new WorkUnitWeightComparison<TResultType, TSharedContext>());

                    _workers.Reverse();

                }

The WorkUnits  now have assignable Weight and Order properties, no rocket science here.  Looking at the threading code in the previous figure we see where I am not yet meeting my stated goal of keeping the CPUs maxed as much as possible to complete the work using all the power available.  In this example with 3 WorkUnits and two physical processors the ideal would be to run two in parallel with the 3rd one being assigned to a physical processor as soon as one becomes available.   I read recently that this technique is being used with success in optimizing games for the PS3.  Neither ThreadPool.QueueUserWorkItem nor the framework I have here so far support any sort of event for saying “I’m done” so the WorkDispatcher can put the next WorkUnit on a physical CPU so I’ve got some more code to write.

Lastly, the PrimeAdderWorkUnit class is currently responsible for properly locking the pieces of the SharedContext it might be working with at any time.  It would be nice to create a mechanism for doing this automatically if such a means can be found in .NET.  It would be oh so nice to be able to overload the “.” or “->” operator.   I’ll revisit this when I can.

 

Thursday, May 03, 2007 9:42:39 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Wednesday, May 02, 2007

Thanks to Mr. Heidenreich I lost most of my free time this afternoon.

I had posted about the programming problems on Project Euler and Mr. Gerry Heidenreich got addicted right away and we had been talking back and forth about some of our solutions.  He pings me to tell me that he is having dreams (nightmares?) about Problem 10 in the set.  It was taking too long to run, way too long since the problems should complete in less than one minute.  We both have almost the same computer oddly enough, a 2.13ghz Core 2 Duo.  If you’re too lazy to follow the link, the problem is to add the prime numbers that are less than 1 million.

I started with his algorithm and got the run time down to about 80 seconds with some plain code optimizations and expanding upon the Sieve he was using for eliminating factors of primes.  To be fair this took a while.  To be even more fair, there are probably Math-only optimizations that would eliminate the need for what you are about to see, but there is a larger lesson here.

I’ve been saying to anyone who’ll listen that chip-makers have been letting us down for about 3 years.  We can no longer expect significantly faster and faster sequential processors to speed up the more complex apps we are using and writing.  The Consolation Prize is ubiquitous multi-core chips in laptops, desktops, and servers.   I have engaged in various debates about the magnitude of the benefit of this approach, especially for Game Programming or other tasks where the solutions (at least the currently practiced and taught ones) are fundamentally sequential in nature.  I had been meaning to look into the cost of context switching, memory copying and synchronization primitives for problems that don’t immediately appear to lend themselves to SMP.  My mediocre math skills gave me a test problem. 

My first attempt was lightning fast but gave the wrong results due to an obvious synchronization issue.  I decided to give this problem at least a fraction of the thought it deserves and came up with some initial goals.  I wanted to make a class that can encapsulate a chunk of the work.  Some sort of controller class should organize the chunks of work, and be able to handle work broken up into arbitrary #s of slices.  The controller should be able to make sure all work is done before calculating the final solution.  To solve my obviously sloppy threading issues, tasks running concurrently should be able to safely share some sort of read/write context that may be necessary to manage dependencies between chunks of work or at least the final results.

To skip to the punchline, the multithreaded method using the pattern I came up with is almost twice as fast as the single threaded method, gets the correct result, and has given me food for thought on future problems requiring significant computing.  Almost twice as fast on 2 procs as on 1 is about as good as you can get so at least I got that part right.

Now, to rewind to the solution. The first thing that jogged my memory was watching the single-threaded method run.

CPU usage would not peg the CPU even when setting the process to Highest priority and turning off everything else that was running.  I know very little about the details of the Core 2 Duo but unlike hyperthreading its two real processors, so somehow either Windows, the BIOS, or Intel are spreading the load across two processors while half of my overall horsepower goes wasted.

Here is a model of the first iteration of my work-splitting solution:

 

WorkUnit made the most sense for the Command class name, not to be confused with other Unit-Of-Work patterns.

Obviously one goal is to make something I can keep refining as I come across new problems and have a .NET pattern/framework for speeding up execution of large tasks in the future.  The problem I’m solving here is probably one of the simplest examples of concurrent tasks with data interdependencies both constantly during execution was well as result assembly when all WorkUnits are done.  In this case the shared context is that all threads of execution will be reading and writing to an array of flags.  These flags mark numbers later on in processing as “Not possibly a prime candidate” when they are factors of already processed numbers.  The process in action looks something like this:

Now I ran with two WorkUnits, each sharing part of the range of possible prime #s and calculating their own sub-sums to be contributed to the final tally when all analysis was done.  I was happy to see a different CPU load…

… as well as obtaining the correct answer at the end and in far less time than single threaded solution.  I think there is still room for improvement, quite a bit in fact.  As soon as the one work unit is done the CPU load goes back down to 50% while the WorkUnit with the larger #s completes.

I have some other initial thoughts on this framework that I’ll get to later tonight, time permitting.

Wednesday, May 02, 2007 7:16:24 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Tuesday, May 01, 2007

I've been having an issue with Folding@Home on Vista, specifically that the error "Could not connect to Work Server (results)" often, maybe always, comes up when my desktop completes a work unit.  The solution in my case was to uncheck "Use IE connection settings", apparently there are some issues with Vista+IE7+FAH 5.0.3.  I wonder how much electricity I wasted...

Tuesday, May 01, 2007 6:57:42 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback

Someone asked me the following today (paraphrased)

"If I have versions 1.0.0.0 and 2.2.2.2 of TestAsssebly both in the gac, and a program TestApp.exe that needs TestAssembly, can I instantiate classes from each assembly inside the same app domain.  Can I use both versions of the Foo class in my program?"

So I sat down to do a simple experiment.  Two versions of Test Assembly are created in the GAC and a console program is run.  Foo.ToString() returns the full name of the assembly that the Foo instance comes from.

Program:

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

using TestAssembly;

 

namespace TestApp

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                Console.WriteLine("Loading Foo...");

                Assembly foo1 =

                AppDomain.CurrentDomain.Load("TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=612615ef384d0cd0, processorArchitecture=MSIL");

 

                Foo f = new TestAssembly.Foo();

                Console.WriteLine("Typeof Foo: " + f.ToString());

 

                object f2 = foo1.CreateInstance("TestAssembly.Foo");

                Console.WriteLine("Typeof f2: " + f2.ToString());

 

                Foo f3 = (Foo)foo1.CreateInstance("TestAssembly.Foo");

                Console.WriteLine("Typeof f3: " + f3.ToString());

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex);

            }

        }

    }

}

Output:

C:\Projects\StrongNameTest\TestApp\bin\Debug>TestApp.exe
Loading Foo...
Typeof Foo: TestAssembly.Foo TestAssembly, Version=2.2.2.2, Culture=neutral, Pub
licKeyToken=612615ef384d0cd0
Typeof f2: TestAssembly.Foo TestAssembly, Version=1.0.0.0, Culture=neutral, Publ
icKeyToken=612615ef384d0cd0
System.InvalidCastException: Unable to cast object of type 'TestAssembly.Foo' to
 type 'TestAssembly.Foo'.
   at TestApp.Program.Main(String[] args) in c:\Projects\StrongNameTest\TestApp\
Program.cs:line 25

The part in bold is entertaining, at least.  I have a strong suspician that if I interact with f3 as an Object only, using Reflection to Invoke properties and methods then I could in fact use all the version 1.0.0.0 Foo functionality (which is vast and important) within the same app domain.

I also hit a bit of a snag when testing this application.  I was previously unaware that Visual Studio does not let you add references to assemblies added to the GAC via gacutil on the local machine.  See: http://msdn2.microsoft.com/en-us/library/wkze6zky(VS.80).aspx

 

Tuesday, May 01, 2007 1:29:02 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [1]  |  Trackback
 Thursday, April 26, 2007

Someone turned me on to Project Euler last night and I have to admit its quite a gem.  From the site:

"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems."

Each problem should take less than a minute to solve once the solution is implemented; my newer computer gives me a much needed handicap.  This is excellent for me since Math is an area where I've not been very successful at self study.  I didn't register yet but I solved #1 and #2 as fast as I could type and then began over-engineering my solution to #3 by attempting to implement a Sieve of Atkin in C#.

Thursday, April 26, 2007 9:26:02 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback

With consoles getting better at becoming livingroom media hubs and all-around entertainment devices I think remote desktop functionality should be considered in the future.  As I sit in my home office today working, it would be nice if I could force my console to wake on LAN and check for firmware updates or download content from my laptop, or see what other users are online for a quick game of Motorstorm over lunch.  What do you think?

Thursday, April 26, 2007 9:13:54 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Tuesday, April 24, 2007

Hosting a Designer Surface in Windows Forms Part Zero

Note that my 1st article in this series begins with Zero because I am partial to c-family languages.  Yes, I'm hilarious.  My apologies to those with smaller screen sizes, but the screenshots need to be readable.

The first step we take is to create  UserControl that will contain the entire UI representation of the designer surface.  You could put all of this on a Form but in my case It'd be handy to have several of these open at once using a tab page interface.  On the UserControl I place a Panel with AutoScroll=true and a PropertyGrid.  The PropertyGrid should look familiar as its essentially what you've come to know and love from VS2005.  I also add another UserControl I’ve created called DefaultToolboxService.  This control has a Checkbox and a ListBox control on it.  So far I’ve got something like this:

 

As you can tell my initial goal is simple: I want to select an item in my ListBox and have it be automagically created when I click on my designer area. The next step will be to start tying in some of the plumbing to allow this to happen.  This is a good place to introduce the notion of Designer Services.

Designer Services

From the class library documentation:

Services are a foundation of the .NET Framework design-time architecture. Services provide design-time objects access to specific features and methods implemented by a service object that provides a service or services.  Let’s look at a few lines of code from the constructor of the TemplateDesignerControl:

        public TemplateDesignerControl()

        {

            InitializeComponent();

            _serviceContainer = new ServiceContainer();

            _serviceContainer.AddService(typeof(INameCreationService), new DefaultNameCreationService());

            _serviceContainer.AddService(typeof(IUIService), new DefaultUIService());

            _designerHost = new DefaultDesignerHost(_serviceContainer);

 

            _serviceContainer.AddService(typeof(IToolboxService), _toolboxSvc);

            _toolboxSvc.DesignPanel = _viewHostPnl;

            LoadToolboxItems();

ServiceContainer is a framework class that’s already implemented for me.  Obviously this instance is going to hold all the Services I need for my designer.  INameCreationService and IUIService we’ll come back to in the next article so for now we’ll look at DefaultToolBoxService.  DefaultToolBoxService was instantiated in InitializeComponent() because it is also a UserControl as mentioned above.  Now might be a good time to start modeling these relationships:

We’ll continue to flesh this diagram out as we move along.

Obviously the declaration for DefaultToolboxService, then, is...

public class DefaultToolBoxService : UserControl, IToolboxService

...because it is both a visual Control and an implementation of an important Service class.  In my case, these do not need to vary independently but the implementations could be separate.  If you wish, you can review (http://msdn2.microsoft.com/en-us/library/system.drawing.design.itoolboxservice(vs.80).aspx) the IToolBoxService interface before moving on.  The implementation is straightforward: Add the types of items you’d like to create to the ListBox and implement the IToolboxService accordingly.  The design-time environment, which in our case is also the run-time environment, can query what item is selected and attempt to create an instance of the appropriate class.  Adding items is accomplished like so:

        protected void LoadToolboxItems()

        {

            ToolboxItem labelItem = new ToolboxItem(typeof(CarSpot.TemplateDesigner.Controls.CustomLabel));

            labelItem.DisplayName = "Text";

            _toolboxSvc.AddToolboxItem(labelItem);

Running the code so far you would see a Toolbox, a Panel with nothing in it, and a PropertyGrid with no selected object.  In the next article we’ll start to look at our IDesignerHost implementation and the sequence of events that occur when I want to see a designer view of my “designed document” with a CarSpot.TemplateDesigner.Controls.CustomLabel on it.

Tuesday, April 24, 2007 11:28:37 AM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Friday, April 20, 2007

I just installed my favorite free UML tool, StarUML, on my new laptop, and started working on some UML models for the designer surface.  This weekend you can expect some more posts on this effort.

Friday, April 20, 2007 2:12:09 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback

http://news.yahoo.com/s/ap/20070420/ap_on_go_co/congress_executive_pay

From the article:

 "The House voted Friday to give shareholders at public corporations a voice in executive pay packages that typically equal 500 times the salaries of workers at those companies. "

Last I heard, shareholders already have a Voice in what goes on in public corporations in the form of the various voting for corporate officials that is mandated by the SEC.  You elect board members and such that you feel represent your interests.  Oddly enough this is also our system of goverment.  I do think both executive pay and our public servants' performance and subsequent patterns of perpetual  re-election are disconnected from reality.  We keep putting people in public office who sell us down the river and we manage to forget this and put the same Senator/Governor/House Rep back in office the next time we have a chance.  Many executives pay are out of touch with their company performance: when an airline is going under and people's pensions are tossed overboard I don't see any reason to authorize "emergency bonuses" for key executives.  I understand the notion that you don't want these high level powerplayers who drove the company into the ground to suddenly quit but something about this doesn't sit right.  Many executives absolutely deserve what they have and I often point out Bill Gates or Warren Buffet as examples of leaders who are, well, leading.

The specifics of the bill are not in the AP article, but I wonder what this is actually going to accomplish?  Will I be forced to stop signing Proxy documents and go to shareholder meetings or cast remote ballots?  Direct Democracy, anyone?  The government is already placing a number of insane and costly requirements on public companies, we don't need any more nonsense.

We have all the tools we need to improve our nation: freely available information, access to our own bank accounts, and the right to vote are some of the more important ones.  We just repeatedly choose not to use these tools.

Friday, April 20, 2007 12:16:20 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, April 19, 2007

I took my sample and did it with a much cooler source image, namely the Tesla Roadster, which is absolutely the coolest car I can think of.

 

Thursday, April 19, 2007 12:11:50 PM (Central Standard Time, UTC-06:00)  #    Disclaimer  |  Comments [0]  |  Trackback

We have a cool feature in our backend data hub we call Image Overlay.  Basically given images of a vehicle we can do all sorts of transformations including overlaying text ("Buy this car!"), overlaying images ("GM Certified logo") scaling and matte-ing (shrinking the input image down to have a single color margin to draw text in) and support for various geometric primitives.  Our implementation is one of the few in our industry and is probably far more complete, flexible, and better-er than the few competing implementations.  Our backend uses some PERL stuff for this and I wondered if a .NET engine would be as fast or faster and how it would scale, given that the claim has been made that C# is 1:1 performance with C++ and despite my love of C# I found this claim questionable.  So, supposing I have an input image which is assuredly not a car:

And a tiny screenshot of my most anticipated upcoming PS3 game, LAIR:

And I want to do a few operations in realtime in .NET:

  • create a Matte
  • scale the source image
  • Add some text
  • import the Dragon screenshot from LAIR
  • Time the operation

Here is the source code for entering some parameters, which could obviously come from some other source:

<div>

<h3>Source Image:</h3>

<asp:HyperLink ID="_exampleLnk" runat="server" Target="_blank" NavigateUrl="~/sample.jpg">

View source image

</asp:HyperLink>

<br />

Matte and overlay some text:

<asp:TextBox ID="_overlayTxt" runat="server" Text="go MSFT!"></asp:TextBox>

<br />

Import Img X <asp:TextBox runat="server" ID="_xTxt" Width="2em" Text="100"></asp:TextBox>

Import Img Y <asp:TextBox runat="server" ID="_yTxt" Width="2em" Text="100"></asp:TextBox>

<asp:Button ID="_submitBtn" runat="server" Text="Lay it over" OnClick="_submitBtn_Click" />

<br />

<asp:Label ID="_resultTimeLbl" runat="server"></asp:Label>

<br />

<asp:Image ID="_resultImg" runat="server" />

</div>

... and the code behind....

protected void _submitBtn_Click(object sender, EventArgs e)
{
string getQuery = "Image.aspx?overlay={0}&x={1}&y={2}";
int x;
int y;
int.TryParse(_xTxt.Text, out x);
int.TryParse(_yTxt.Text, out y);
_resultImg.ImageUrl = string.Format(getQuery, _overlayTxt.Text, x, y);
}

... and the result...

So, on a Windows Server 2003 running ASP.Net 2.0, how much code does it take to generate this?  Surprisingly little:

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

bool high = true;

HighResTimer hpt = new HighResTimer();
hpt.Start();
System.Drawing.Imaging.ImageCodecInfo codecInfo;
System.Drawing.Imaging.EncoderParameters parms;

System.Drawing.Imaging.ImageCodecInfo[] infoz = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
codecInfo = infoz[1];
parms = new System.Drawing.Imaging.EncoderParameters(1);
parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

string txt = Request["overlay"];
int x = int.Parse(Request["x"]);
int y = int.Parse(Request["y"]);
Response.ContentType = codecInfo.MimeType;
System.Drawing.Image newImage = new Bitmap(640, 480);
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(Server.MapPath("~/sample.jpg"));
System.Drawing.Image lairImage = System.Drawing.Image.FromFile(Server.MapPath("~/lair.jpg"));
Graphics g = Graphics.FromImage(newImage);

//Interpolation mode, smoothing mode, CompositingQuality,PixellOffsetMode,etc.
if (high)
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
}

try
{
//Matte color
g.Clear(Color.LightGray);

//Overlay font
Font textFont = new Font("Verdana", 9.0f, FontStyle.Bold | FontStyle.Underline);
Brush fontBrush = Brushes.Black;
//Scales the image down to matte it
g.DrawImage(srcImage, new RectangleF(0, 0, 600, 440));
g.DrawImage(lairImage, new RectangleF(x, y, 150, 84));
g.DrawString(txt, textFont, fontBrush, new PointF(10, 441));
g.DrawString("Damon Certified used o-scope", textFont, Brushes.Red, new PointF(430, 370));
hpt.Stop();
g.DrawString(