Articles

Parallel Managed Code - TPL Analogue for .Net 2.0

Here is a simple, minimal, but efficient solution to supporting multi-core processors in your software with older versions of the .NET framework.

Introduction


In case you haven't noticed, computer processors aren't getting faster any more. There was a time when every few months a new range of chips would be available from Intel, or AMD, and their friends, boasting new faster processing. Nowadays, we can still get new chips but now they provide improvements other than raw speed increases.

To take advantage of the new "multi-core" chips, we need to write our software a bit differently. This is quite straightforward when using modern programming languages and frameworks, but less so for legacy systems. For example, current versions of Microsoft's .Net framework offers something known as the Parallel Task Library. It is lovely. The world is great. We are all happy. Well, that is all true until we write code with a slightly older, but still commonly used version of the .NET framework.

How can we leverage current and future CPU architectures, while still targeting the .NET 2.0 framework? We can manually manage threads in our code. Yuck! There's no way we want to do that! When recently confronted with this problem I decided to take a slightly different approach. I cloned a minimal subset of the Task Parallel Library, and I'd like to share the fruits of my labours here. With you. No ties.

An underlying requirement of the following code is to support .NET 2.0, and above, and Mono 2.2 and above (and it almost certainly works with earlier versions of Mono but that is just a pleasant coincidence).

Background


The normal key to improving software performance on modern computing architectures is to run on multiple processors, or processing cores, in parallel. Microsoft's Task Parallel Library (TPL) is designed to make it easy to develop managed code that automatically uses multiple processors. TPL does not require any particular language extensions and works with the .NET Framework version 3.5 and later.

A really good introductory article about these topics is available from Microsoft: "Optimize Managed Code For Multi-Core Machines".

Unfortunately, the TPL requires the .NET Framework version 3.5, or later.

Solution


I won't describe how the code works in any great detail, as you can read that from the source code itself. A Visual Studio solution is provided, and this should work on Visual Studio 2005 and later versions. It probably also works with Novell's MonoDevelop.

If you wish to run the included unit tests, you'll need the NUnit unit testing framework. I've tested with NUnit 2.4.5, but other version will probably be equally as good. If you don't have NUnit, just exclude the ParallelTests project from the solution in Visual Studio. Apart from that optional dependency, this code is completely standalone, just requiring a C# 2.0 compiler, whether from Microsoft, Mono, or anywhere else.

I have only implemented equivalents of the Parallel.For and Parallel.ForEach looping constructs, but these are typically sufficient for parallelising code in the most obvious way. In both cases, my versions are drop-in replacements for the delegate forms of the constructs in the TPL. Therefore, it should be trivial to "upgrade" to the official TPL at a later date.

To use this within your own code, you can simply duplicate the main Parallel.cs file in your project and then use the Parallel class directly, or you can use my Uk.Org.Adcock.Parallel assembly, as is, and reference the Parallel class from within that.

The included unit tests can act as full examples, if you need then, but it is quite simple...

Stealing an example from the MSDN article linked above, suppose you wanted to square the elements in an array, a, e.g.:

for (int i = 0; i < 100; i++)
{
  a[i] = a[i] * a[i];
}

The individual iterations are independent of each other, i.e., subsequent iterations do not depend on prior iterations, you can utilise parallelism with a call to the Parallel.For method, e.g.:

Parallel.For(0, 100, delegate(int i)
{
  a[i] = a[i] * a[i];
});

It really is that simple!

The Parallel.For method is implemented similarly, but I won't insult your intelligence by providing an example. Look at the unit tests if you really need one.

I also allow simple configuration of the maximum number of threads to utilise. By default, however, one thread per CPU core reported by the operating system will be used. Simply set the value of the Parallel.ThreadCount property, where a value of zero indicates that one thread per available CPU core should be used.

Availability and License


I am making this code available freely, under the terms of the 2-clause BSD license.

The full source code is available here: TPL Analogue for .NET 2.0 and later

Posted by Stewart on Tue, 1 Sep 2009
Tags: .Net , C# , parallel , programming
0 comments  Permalink

24 hours until kick-off

In roughly 24 hours time, the new Football League 2009/2010 season begins for Norwich City...

...and again, just like last season, and the season before, and the season before, I am quietly confident that Norwich will do well.

Realistically, Norwich really do have a better chance of producing less embarrassing results, because this season they play in the lower trenches of the football league.

Some people seem to be quite delirious at the prospect of supporting a "big club" at this level, like whoever it was that created this:

Well, I guess that only time will tell...

Posted by Stewart on Fri, 7 Aug 2009
Tags: norwich , football , canaries
0 comments  Permalink

Do not sit on the fence

This is always good advice:

Do not sit on the fence

Posted by Stewart on Fri, 10 Jul 2009
Tags: humour
0 comments  Permalink

"Lens Error, please restart camera", Or, No More Canon

Buyer beware for Canon Ultra-Compact digital cameras...

My wife and I, plus a good friend had three Canon IXUS 70 cameras between us. In America, these are known as the "Canon PowerShot SD1000 7.1MP Digital Elph Camera with 3x Optical Zoom"

They were fantastic "point-and-shoot" ultra compact digital cameras. They produced really good photographs, had plenty of settings to play with, and were really simple to use. The battery life was amazing (I had more than 350 photos on one charge) and, I am told, they looked good. Now, all three have died with a fatal "Lens Error, please restart camera" error!

Canon IXUS-70 - stuck lens

Canon IXUS-70 - stuck lens error message

This is clearly a common fault, as proven by a simple web search. Canon deny a faulty design and always seem to avoid free repairs under their warranty, at least in the UK, by claiming that the camera contains dust or sand particles. In the UK, repairs cost £109+VAT+postage. There is no certainty that the problem won't occur again within a few months, so that price is too high to justify the repair over a new replacement. Effectively, a Canon warranty is a worthless warranty.

Complex electronics like this will often have small problems, but Canon's attitude on this matter is a disgrace. So, after buying only ever Canon digital cameras, I can't recommend that anyone buys one now despite good usability and brilliant picture quality.

My wife and I also have, between us, two Canon photo printers. These work well, but are now too expensive to run. It is cheaper to get the photos professionally printed (for 5"x8" or similar sizes), or to use my dead-cheap but solid Lexmark printer instead (for A4 or similar sizes).

I guess Canon's customer care have just lost them a formerly loyal customer.

Now I have to decide on my next digital camera...

Posted by Stewart on Fri, 10 Jul 2009
Tags: Worthless warranty , cameras , Canon , rant
0 comments  Permalink

Technorati - A Blog Index

Technorati is one of the more established blog indexing sites. These are my continuing adventures in the blogosphere...

Technorati

According to themselves:

Technorati was founded to help bloggers succeed by collecting, highlighting, and distributing the global online conversation. Founded as the first blog search engine, Technorati has expanded to a full service media company providing services to the blogs and social media sites and connecting them with advertisers who want to join the conversation, and whose online properties introduce blog content to millions of consumers.

Technorati.com

The leading blog search engine, Technorati.com indexes millions of blog posts in real time and surfaces them in seconds. The site has become the definitive source for the top stories, opinions, photos and videos emerging across news, entertainment, technology, lifestyle, sports, politics and business. Technorati.com tracks not only the authority and influence of blogs, but also the most comprehensive and current index of who and what is most popular in the Blogosphere.

-- http://technoratimedia.com/about/

Wikipedia offers a very brief history of Technorati.

About four-years behind the curve, I just signed up for an account. This is mainly just to register this blog on there.

To "claim" a blog on Technorati, a code needs to be inserted in a blog-post. Well, here it is: ksey6gw9u7.

As of now, find me on Technorati!

Add to Technorati Favorites

Posted by Stewart on Thu, 9 Jul 2009
Tags: internet , blogging , website
0 comments  Permalink

Computational fragment-based drug design on a mitotic kinesin

Inhibition of the mitotic spindle formation could be an interesting target for cancer therapeutics. Scientists at MEDIT recently published results of a exploratory computational fragment-based approach on an allosteric binding pocket of the Eg5 mitotic kinesin...

During cellular metaphase, a stage in eukaryotic cell mitosis, the mitotic spindle maintains a constant shape and size as their principal constituent, microtubules, are continuously polymerized, depolymerized and transported towards the two spindle poles. Kinesin motor proteins are involved in the mitotic spindle assembly, in addition to other cellular processes such as intracellular vesicle transport, chromosome segregation, cell division and motility. The role of Kinesin is discussed in this wikipedia article.

Inhibition of mitotic spindle formation is an interesting target in cancer therapeutics. The anti-mitotic agents to date cause serious side-effects, such as neurotoxicity, and the development of drug resistance restricts their application.

A different approach to inhibit the mitotic spindle formation is to inhibit the mitotic motors that interact with microtubules. Eg5 is exclusively involved in the formation and function of the mitotic spindle, driving a relative sliding of microtubules in the mitotic spindle. Inhibition of this results in cell cycle arrest and apoptosis, without interfering with other microtubule-dependent processes. Several small molecules are known to inhibit human Eg5 by binding to the catalytic motor domain. These observations imply the utility of Eg5 as an anticancer drug target.

Eg5 allosteric binding site

The Eg5 allosteric binding site surface (Grey: hydrophobic, Blue: hydrophilic) showing co-crystallized monastrol (green) and mon-97 (blue) ligands, as superimposed with MED-SuMo.

JCAMD cover

My colleagues and I at MEDIT, recently developed a fragment-based drug design protocol. Based on that protocol, the aim of the work presented here was to populate the hydrophobic pocket of the Eg5 allosteric binding site, hopefully to design potential inhibitor molecules.

In the earlier work, we defined a new structural entity known as the MED-Portion. This represents ligand molecules, or substituent fragments, annotated with fragmentation anchors and interaction sites from the protein environment.

MED-Portions retrieved from known protein structures with MED-SuMo were hybridised using the MED-Hybridise and 3D substructure filtering rules to discover novel molecules having similar binding mode to the known inhibitors. Of these potential ligands, we found those that are present in publicly available chemical libraries, such as the PubChem database. We also validated the protocols through a process of recreating the structures of known inhibitors.

For more information see:

  • Ksenia Oguievetskaia, Laetitia Martin-Chanas, Artem Vorotyntsev, Olivia Doppelt-Azeroual, Xavier Brotel, Stewart A. Adcock, Alexandre G. de Brevern, Francois Delfaud and Fabrice Moriaud, "Computational fragment-based drug design to explore the hydrophobic sub-pocket of the mitotic kinesin Eg5 allosteric binding site", Journal of Computer-Aided Molecular Design, 2009 June 17. Epub ahead of print (I'll update this as soon as the paper is in print)

Entrez PubMed SpringerLink

Posted by Stewart on Mon, 22 Jun 2009
Tags: science , publications , MEDIT , FBLD , drug discovery
0 comments  Permalink

Page Rank and Me

Page Rank is a critical part of the glue that holds the world wide web together.

google

Part of the algorithm that google apply for ranking sites in search results relies on a value known as PageRank, supposedly named after Larry Page, one of google's founders. Wikipedia has a good article about PageRank.

According to google:

PageRank relies on the uniquely democratic nature of the web by using its vast link structure as an indicator of an individual page's value. In essence, Google interprets a link from page A to page B as a vote, by page A, for page B. But, Google looks at more than the sheer volume of votes, or links a page receives; it also analyzes the page that casts the vote. Votes cast by pages that are themselves "important" weigh more heavily and help to make other pages "important".

Basically, webpages are assigned some value that empirically represents the quantity of links pointing to that page, weighted by the authority of those links. It can be considered, in some way, a measure of a webpages' popularity and authority.

There are numerous websites that can tell you the pank rank of a specific page, including www.mygooglepagerank.com. That particular site can give you a small icon to include on you page so visitors can see the page rank. Why you would want to do that, I am not sure. However, it gave me an idea for an interesting experiment to answer a simple question: What is the page rank of each page in this website?

To answer my question, I constructed a page that in addition to being a site map displays the page rank of each of the pages. You are welcome to visit my Page Rank Map.

As of today, the result of my experiment is that my entire site has a page rank of zero! A google query of "site:saa.dyndns.org" confirms that the site is fully indexed. Therefore the reason for the low page ranks are that the site is unpopular!

I'll be interested to see how this progresses over time. I'll probably be more interested than anyone else. Other sites that I manage, that I was at one time responsible for, or over which I have some degree of control, typically have a Page Rank of between 4 and 6 provided that they've been on the web for at least a year or two. Based on this, my hypothesis is that the page rank will develop as soon as I actually write an article that someone might be interested in. I expect that will be sometime late 2010.

Posted by Stewart on Tue, 16 Jun 2009
Tags: internet , me , website
0 comments  Permalink

A Social Bookmarking Site For Us Code-Monkeys

I've recently come across yet another social bookmarking site, DZone. This site bills itself as "Fresh links for developers", and unusually for such a site, the links are actually interesting...

Of course, your mileage might vary - this site targets developer-geeks and that is obviously why it particularly appeals to me!

DZone logo

When I look at the most recent links, or even the most popular links, somewhere like digg, then there might be one or two I find interesting. On the other hand, there might be nothing there worth the bits. The first time I looked at the popular links on DZone about half of the titles piqued my interest. I actually clicked-through to probably ten of the linked sites. They nearly all turned out to be blog posts, of varying quality, and two of these posts were even directly applicable to my current projects at work. Half of an hour well spent, I think.

Based on this new discovery, I've just added DZone to my social bookmark links on every page. That'll be the topic for another post.

DZone is a much more focused and specialised site than Digg or Reddit. The audience will be much smaller but conversely there must be many more people interested in the specific topics that I'm likely to babble-on about.

And okay, I'll freely admit it: The purpose of this post is just to submit to DZone to see what happens!

Posted by Stewart on Tue, 16 Jun 2009
Tags: internet , website , social bookmarks , programming
2 comments  Permalink

Replacement for Ancient UNIX Crypt

A legacy UNIX crypt (1) utility replacement...

In the dark ages of computing, UNIX had a really dodgy and cryptographically unsound encryption/decryption tool named crypt. Nowadays, most UNIX variants do not include this tool because there are far better alternatives available. That is probably a good thing, and we should be happy. Unfortunately, many files were previously encrypted using this tool, and even some strange people still use it (crypt was still bundled with the last version of Silicon Graphics' IRIX operating system, for example). That causes a problem, especially since I couldn't find a free version of crypt that worked under Linux when I needed it.

That is no big problem for me because, back in 2001, I wrote my own version. Yippee! And, you can download it here. For free, even! The source-code is available under the terms of a permissive open-source license, so you can pretty much do whatever you want with it.

It works exactly as the original UNIX version did:

crypt [-s] passkey < file > encrypted_file
crypt [-s] passkey < encrypted_file > file

Full compilation instructions for the source code are included within the source code, for your convenience.

Warning: This is NOT designed to be cryptographically secure. It is solely intended as a means to decrypt files which were encrypted using the legacy UNIX crypt utility, although it probably has some pedagogical value.

Posted by Stewart on Tue, 2 Jun 2009
Tags: open source , programming , UNIX , crypt , C
0 comments  Permalink

Temporary Snippets in Visual Studio

Despite being a long-term user of Visual Studio, various versions and with various languages, I've just found a feature that I could have been using for years...

Often, as a programmer, as a presenter, as a trainer, I need small, throw-away, code snippets. Let me be clear that I don't mean permanently useful pieces of code as in Visual Studio snippets. I mean a temporary collection of small fragments of code that I will use a few times, and can then be discarded.

For throw-away snippets, my tool of choice has until now been Notepad. Copy and paste then works a treat. It is not a very high-tech solution, and there are obvious inconveniences. But now, my feeble brain now holds this technique:

Drag code to the Visual Studio Toolbox...

Drag...

...now that can be used as a snippet by dragging it back onto the source pane!

...and Drop

Since late 2005, I've used Visual Studio for more than 5 days out of every seven, and I never knew this was possible! Some other useful features took a long time to discover, like Ctrl+K-X for auto-formatting. For at least 2 years I (and many of my colleagues - as this was a common gripe) reformatted code by retyping the final "}" in the block. But, nothing like this toolbox trick.

I don't think I'm the only person who has missed this feature, as demonstrated by this question on stackoverflow where I discovered it.

Sara Ford's weblog used to offer a "Visual Studio tip of the day", and I now notice that she described this tip, in a round-about-way back on June 6th, 2005. I obviously missed that one! Sadly, Sara Ford now gives tips about the nearly-wholly uninteresting CodePlex site.

What other hugely useful features of Visual Studio am I blissfully unaware of?

Posted by Stewart on Mon, 11 May 2009
Tags: Visual Studio , programming
2 comments  Permalink