<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Marcus Cuda</title>
	
	<link>http://cuda.net</link>
	<description>(make it work, make it pretty, make it fast) -&gt; (red to green, refactor, profile and tune where neeed)</description>
	<lastBuildDate>Tue, 30 Jun 2009 14:03:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/cuda" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>SAS Tranport File Parser</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/gpr3IIXQ80g/</link>
		<comments>http://cuda.net/blog/2009/06/30/sas-tranport-file-parser/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 14:03:13 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[QuantPro]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=332</guid>
		<description><![CDATA[In my current consulting project, I&#8217;m working with a lot of SAS transports files (about 120 for a total of 30 GB).  I need to get the data into a format that is usable by dnAnalytics and QuantPro.  I usually use DBMS/Copy or StatTransfer to convert SAS file into CSV and then go [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In my current consulting project, I&#8217;m working with a lot of SAS transports files (about 120 for a total of 30 GB).  I need to get the data into a format that is usable by dnAnalytics and QuantPro.  I usually use DBMS/Copy or StatTransfer to convert SAS file into CSV and then go from there.   In this case, I thought I would just write a SAS transport file parser and add it to QuantPro.  This would allow me to import the SAS data directly to QuantPro.</p>
<p>The XPORT file format is a really simple, but with some minor gotchas.  First, the data is stored in a big-endian byte order.  Second, all floating point numbers are stored in an old IBM mainframe format (not IEEE 754).  This format is not supported by .NET but it was simple enough to create a converter (but I probably didn’t do it in most efficient manner).  </p>
<p>The parser has been added into QuantPro.IO.Sas namespace.  It can be used as followed to copy variables from a SAS XPORT file into a QuantPro data store:</p>
<pre class="brush: csharp;">
var reader = new TransportFileReader(&quot;data.xpt&quot;);
foreach( var member in reader.Members ){
   using( var qpFile = new ZipFileStore(member.DatasetName) ){
     var variables = reader.GetVariables(member.DatasetName);
     foreach( var variable in variables ){
       qp.Add(variable);
    }
  }
}
</pre>
<p>The parser supports multiple data members but ignores format labels.  Data in SAS transport files is stored as either a string or a floating point number.  Format labels can then be used to convert the floating point number into integers or dates.  The parser only returns a string or double variable.  It is up to the user to do any conversion.  I’ll be adding support for format labels sometime in the future.</p>
<p>A little aside about DBMS/Copy.  It looks like the utility is no longer available.  SAS bought out Conceptual (the company the produced DBMS/Copy), but I cannot find it on the SAS site.  That is a shame since it was probably the best tool for converting data from one format to another.   Maybe its time to create QuantPro Transfer, a free tool with similar functionality as DBMS/Copy and StatTransfer.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=SAS+Tranport+File+Parser+http://cuda.net/?p=332" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=SAS+Tranport+File+Parser+http://cuda.net/?p=332" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=SAS+Tranport+File+Parser+http://cuda.net/?p=332" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=SAS+Tranport+File+Parser+http://cuda.net/?p=332" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/06/30/sas-tranport-file-parser/&amp;title=SAS+Tranport+File+Parser" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/06/30/sas-tranport-file-parser/&amp;title=SAS+Tranport+File+Parser" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=gpr3IIXQ80g:XID74f4mTzM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=gpr3IIXQ80g:XID74f4mTzM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=gpr3IIXQ80g:XID74f4mTzM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=gpr3IIXQ80g:XID74f4mTzM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=gpr3IIXQ80g:XID74f4mTzM:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/gpr3IIXQ80g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/06/30/sas-tranport-file-parser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/06/30/sas-tranport-file-parser/</feedburner:origLink></item>
		<item>
		<title>Tinkering with NDepend</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/awHolOnR3u4/</link>
		<comments>http://cuda.net/blog/2009/06/12/tinkering-with-ndepend/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 12:38:36 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=316</guid>
		<description><![CDATA[I was curious about NDepend so I thought I would give it a spin.  Wow – it could do much more than I thought.  Three features I found very useful are project dependencies, build comparisons, and static analysis (NDepend has many more features).  Project dependencies let you see how every detail of [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I was curious about <a href="http://www.ndepend.com">NDepend</a> so I thought I would give it a spin.  Wow – it could do much more than I thought.  Three features I found very useful are project dependencies, build comparisons, and static analysis (NDepend has many more features).  Project dependencies let you see how every detail of your project depends on every other detail – from assemblies to namespaces to types to methods to fields.  Want to know which types use a particular a method, no problem.  NDepend makes it easy to find where your code is tightly coupled.</p>
<p>Comparing builds allows you to see what was added, removed, or changed (code, names, comments, or items marked as obsolete).  I compared the last two dnAnalytics releases and I see that we removed three classes and added six new namespaces, 92 classes, and 960 methods.  We also modified 74 methods in 24 classes. This beats having to go through incomplete source control logs and support tickets to generate a change log.  There is one glitch.  It includes all compiler generated types and methods.  I don’t care about them and it would be great if NDepend could filter them out.</p>
<p>My favorite feature is static analysis.  NDepend includes a Code Query Language (CQL).  CQL lets you query any detail about your code. NDepend has about 150 queries built it – all of which can be customized – and users can write their own.  It includes such task as “Show me the methods with more the X lines” or “Fields that can be marked as readonly”.  It even has tasks to find breaking changes between builds and detecting naming convention violations.  You can integrate NDepend into your build process to flag problems.  Quickly going through some of the built-in queries I see that dnAnalytics is using non-generic collections in a couple places and even several instances of throwing Exception (how did that get past a code review?).  Queries also include compiler generated classes and there should be an option not to.  </p>
<p>I had some minor issues with NDepend.  The GUI is a little sluggish and sometime difficult to navigate.  But they have predefined views and you can create your own.   NDepend generates some default project metrics – two of which are Efferent coupling [1] and Instability [2].  A problem with these metrics is that they incorporate dependencies on the .NET framework base class libraries (BCL).  For a single DLL project like dnAnalytics you get odd results.  dnAnalytics instability metric is 1 – completely unstable.  NDepend should have an option to not include the BCL when computing metrics.</p>
<p>Overall, I’m very impressed.  It’s a complex piece of software and there is a bit of learning curve. I recommend that new users watch the short tutorials.  </p>
<p>From  <a href="http://www.ndepend.com/Metrics.aspx">NDepend’s website</a>:<br />
[1] Efferent coupling (Ce): The number of types inside this assembly that depends on types outside this assembly. High efferent coupling indicates that the concerned assembly is dependant. Notice that types declared in framework assemblies are taken into account.<br />
[2] Instability (I): The ratio of efferent coupling (Ce) to total coupling. I = Ce / (Ce + Ca). This metric is an indicator of the package&#8217;s resilience to change. The range for this metric is 0 to 1, with I=0 indicating a completely stable package and I=1 indicating a completely instable package.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Tinkering+with+NDepend+http://cuda.net/?p=316" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=Tinkering+with+NDepend+http://cuda.net/?p=316" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=Tinkering+with+NDepend+http://cuda.net/?p=316" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=Tinkering+with+NDepend+http://cuda.net/?p=316" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/06/12/tinkering-with-ndepend/&amp;title=Tinkering+with+NDepend" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/06/12/tinkering-with-ndepend/&amp;title=Tinkering+with+NDepend" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=awHolOnR3u4:efecmOpYjLk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=awHolOnR3u4:efecmOpYjLk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=awHolOnR3u4:efecmOpYjLk:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=awHolOnR3u4:efecmOpYjLk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=awHolOnR3u4:efecmOpYjLk:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/awHolOnR3u4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/06/12/tinkering-with-ndepend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/06/12/tinkering-with-ndepend/</feedburner:origLink></item>
		<item>
		<title>Generic Matrices with Pluggable Storage</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/iFjQWeIQFE0/</link>
		<comments>http://cuda.net/blog/2009/06/03/generic-matrices-with-pluggable-storage/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 12:07:26 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[numerics]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=305</guid>
		<description><![CDATA[[Update: The code below will perform roughly the same as a non-generic version on the x86 CLR, but is about 30% slower on the x64 CLR.  This due to the fact that the x64 CLR doesn't inline methods with struct parameters]
I’ve spent last several days trying to create a generic matrix class, Matrix&#60;T&#62;.  [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>[<strong>Update:</strong> The code below will perform roughly the same as a non-generic version on the x86 CLR, but is about 30% slower on the x64 CLR.  This due to the fact that the x64 CLR <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=93858">doesn't inline methods with struct parameters</a>]</p>
<p>I’ve spent last several days trying to create a generic matrix class, Matrix&lt;T&gt;.  Since .NET doesn’t support generic arithmetic [1], it’s not an easy thing to do.  My goal was to create a fast, clean implementation – but it seems that fast and clean are mutually exclusive in this case.  All the clean designs I came up with were at least twice as slow as the non-generic case.  So could I come up with a fast design wasn’t too repulsive?  I think I might have.</p>
<p>We are not only going to make the matrix class generic, but also support multiple storage schemes.  For this discussion, we are only going to have two – jagged array storage and 1-D array storage.  To handle the generic arithmetic problem, we’ll use  generic calculators similar to the ones discussed in this <a href="http://www.codeproject.com/KB/cs/genericnumerics.aspx">CodeProject article</a> – but we are going to hide them from the end user.  We are also going to restrict the possible numeric types to double, float, complex, and complex32.  </p>
<p>Lets start with a simple matrix class that only supports addition (error checking as been removed to condense the code).</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">public class Matrix&lt;T&gt; where T : struct {
   private static readonly MatrixCalculator&lt;T&gt; Calculator = new MatrixCalculator&lt;T&gt;();
   private readonly MatrixStorage&lt;T&gt; _storage;

   public Matrix(int rows, int columns){
       _storage = new Dense&lt;T&gt;(rows, columns);
       StorageType = _storage.GetType();
   }

   public Matrix(MatrixStorage&lt;T&gt; storage) {
       _storage = storage;
       StorageType = _storage.GetType();
   }

   public int Rows { get { return _storage.Rows; } }

   public int Columns { get { return _storage.Columns; } }

   public T this[int row, int column] {
       get { return _storage[row, column]; }
       set { _storage[row, column] = value; }
   }

   internal MatrixStorage&lt;T&gt; Storage { get { return _storage; } }

   internal Type StorageType { get; private set; }

   public Matrix&lt;T&gt; Add(Matrix&lt;T&gt; other){
       return Calculator.Add(this, other);
   }

   public static Matrix&lt;T&gt; operator +(Matrix&lt;T&gt; left, Matrix&lt;T&gt; right) {
       return left.Add(right);
   }
}</pre>
<p>The matrix class has two dependencies, MatrixStorage and MatrixCalculator.<br />
MatrixStorage:</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">public abstract class MatrixStorage&lt;T&gt; where T : struct{
   static MatrixStorage(){
       var type = typeof (T);
       if( type != typeof(double) &amp;&amp; type != typeof(float) &amp;&amp; type != typeof(Complex) &amp;&amp; type != typeof(Complex32)) {
           throw new NotSupportedException();
       }
   }

   protected MatrixStorage(int rows, int columns) {
       Rows = rows;
       Columns = columns;
   }

    public int Rows { get; private set; }
    public int Columns { get; private set; }

    public abstract T this[int row, int column] { get; set; }
}

public class Dense&lt;T&gt; : MatrixStorage&lt;T&gt; where T : struct {
   internal T[] _data;

   public Dense(int rows, int columns) : base(rows, columns) {
       _data = new T[rows*columns];
   }

   public override T this[int row, int column] {
       get { return _data[column*Rows + row]; }
       set { _data[column*Rows + row] = value; }
   }
}

public class JaggedDense&lt;T&gt; : MatrixStorage&lt;T&gt; where T : struct {
   internal T[][] _data;

   public JaggedDense(int rows, int columns) : base(rows, columns) {
       _data = new T[rows][];
       for (var row = 0; row &lt; rows; row++) {
           _data[row] = new T[columns];
       }
   }
   public override T this[int row, int column] {
       get { return _data[row][column]; }
       set { _data[row][column] = value; }
   }
}
</pre>
<p>The MatrixStorage static constructor is where we reject unsupported types.  This just shows 1-D and jagged arrays for dense storage, but we can easily add sparse, diagonal, band, identity, and triangular storage schemes.</p>
<p>MatrixCalculator:</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">internal class MatrixCalculator&lt;T&gt; where T : struct {
   public Matrix&lt;T&gt; Add(Matrix&lt;T&gt; left, Matrix&lt;T&gt; right) {
       return CalculationRunner&lt;T, Matrix&lt;T&gt;&gt;.Execute(typeof (MatrixCalculator&lt;,&gt;), &quot;Add&quot;, left, right);
   }
}
internal partial class MatrixCalculator&lt;T, K&gt; where T : struct where K : ICalculator&lt;T&gt;, new() {
   private static readonly K Calculator = new K();
   public Matrix&lt;T&gt; Add(Matrix&lt;T&gt; left, Matrix&lt;T&gt; right) {
       if (left.StorageType == typeof (Dense&lt;T&gt;) &amp;&amp; right.StorageType == typeof (Dense&lt;T&gt;)){
           return Add((Dense&lt;T&gt;) left.Storage, (Dense&lt;T&gt;) right.Storage);
       }
       if (left.StorageType == typeof (JaggedDense&lt;T&gt;) &amp;&amp; right.StorageType == typeof (JaggedDense&lt;T&gt;)){
           return Add((JaggedDense&lt;T&gt;) left.Storage, (JaggedDense&lt;T&gt;) right.Storage);
       }
       return Add(left.Storage, right.Storage);
   }

    private static Matrix&lt;T&gt; Add(MatrixStorage&lt;T&gt; left, MatrixStorage&lt;T&gt; right){
       var matrix = new Matrix&lt;T&gt;(left.Rows, left.Columns);
       for (var row = 0; row &lt; left.Rows; row++) {
          for (var column = 0; column &lt; left.Columns; column++) {
               matrix[row, column] = Calculator.Add(left[row, column], right[row, column]);
           }
       }
       return matrix;
   }
}
</pre>
<p>MatrixCalculator&lt;T, K&gt; is a partial class and we&#8217;ll add optimized versions of the Add method for 1-D and jagged array storage in separate files &#8211; but this will give you the gist.</p>
<p>The MatrixCalculator is dependent on an ICalculator and CalculationRunner.  The ICalculator solves the generic arithmetic problem with a negligible impact on performance. </p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">internal interface ICalculator&lt;T&gt; where T : struct {
   T Zero { get; }
   T One { get; }
   T Add(T left, T right);
   T Subtract(T left, T right);
   T Divide(T left, T right);
   T Multiply(T left, T right);
   T Sqrt(T value);
   bool LessThan(T left, T right);
   bool GreaterThan(T left, T right);
   bool LessThanEqualTo(T left, T right);
   bool GreaterThanEqualTo(T left, T right);
   T Conjugate(T value);
}

internal struct DoubleCalculator : ICalculator&lt;double&gt; {
    public double Zero{ get { return 0.0; }  }
    public double One{get { return 1.0; } }

    public double Add(double left, double right)  { return left + right; }
    public double Subtract(double left, double right) { return left - right; }
    public double Divide(double left, double right){ return left/right; }
    public double Multiply(double left, double right){ return left*right; }
    public double Sqrt(double value) { return Math.Sqrt(value); }
    public bool LessThan(double left, double right) { return left &lt; right; }
    public bool GreaterThan(double left, double right) { return left &gt; right; }
    public bool LessThanEqualTo(double left, double right) { return left &lt;= right; }
    public bool GreaterThanEqualTo(double left, double right) { return left &gt;= right; }
    public double Conjugate(double value) { return value; }
 }
internal struct FloatCalculator : ICalculator&lt;float&gt; {...}
internal struct ComplexCalculator : ICalculator&lt;Complex&gt; {...}
internal struct Complex32Calculator : ICalculator&lt;Complex32&gt; {...}
</pre>
<p>The CalculationRunner is used to instantiate a MatrixCalculator based on the type of T and invoke the requested method.  It is also used to create other types of calculation classes such as the matrix decomposition classes.  The runner uses reflection to create the classes and caches them &#8211; there isn&#8217;t too much of a performance hit.  The current caching code is a little sloppy, so I&#8217;ll just show an outline of the class.</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">internal static class CalculationRunner&lt;T, K&gt;  {
...
  public static K Execute(Type type, string methodName, params object[] parmeters) {
       MethodInfo method;
       object obj;

       var dataType = typeof (T);

       if (Cache.ContainsKey(type))  {
           var methodCache = Cache[type];
           if (methodCache.ContainsKey(dataType))   {
               var execInfo = methodCache[dataType];
               method = execInfo.Method;
               obj = execInfo.Object;
            }  else  {
                var calcType = GetCalcType(dataType);
                var fullType = type.MakeGenericType(new[] {dataType, calcType});
                method = fullType.GetMethod(methodName);
                obj = Activator.CreateInstance(fullType);
                methodCache.Add(dataType, new ExecutableInfo {Method = method, Object = obj});
           }
       } else {
            var calcType = GetCalcType(dataType);
            var fullType = type.MakeGenericType(new[] {dataType, calcType});
            method = fullType.GetMethod(methodName);
            obj = Activator.CreateInstance(fullType);
            var methodCache = new Dictionary&lt;Type, ExecutableInfo&gt; {{dataType, new ExecutableInfo {Method = method, Object = obj}}};
            Cache.Add(type, methodCache);
       }
       return (K) method.Invoke(obj, parmeters);
   }
....
}
</pre>
<p>That&#8217;s basically it.  We can now add double/float/complex/complex32 matrices using either 1-D or jagged arrays for storage. Example:</p>
<pre class="brush: csharp;">
var defaultMatrix = new Matrix&lt;double&gt;(10,10);
var oneDMatrix = new Matrix&lt;double&gt;(new Dense&lt;double&gt;(10,10));
var jaggedMatrix = new Matrix&lt;double&gt;(new JaggedDense&lt;double&gt;(10,10));
var result = denseMatrix + jaggedMatrix;
</pre>
<p>Doing some quick benchmarking, there is almost no performance difference between this  approach and the non-generic approach. We now need add a similar vector class and all the BLAS and LAPACK algorithm classes &#8211; each of which will require a class similar to MatrixCalculator.  </p>
<p>Is it worth it? I don&#8217;t know.  There is a lot of plumbing code &#8211; but it does seem to be less code than if we created a whole set of non-generic classes: DenseMatrix, SparseMatrix, FloatDenseMatrix, FloatSparseMatrix, ComplexDenseMatrix, etc.</p>
<p>[1] See <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94264">https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=94264</a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Generic+Matrices+with+Pluggable+Storage+http://cuda.net/?p=305" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=Generic+Matrices+with+Pluggable+Storage+http://cuda.net/?p=305" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=Generic+Matrices+with+Pluggable+Storage+http://cuda.net/?p=305" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=Generic+Matrices+with+Pluggable+Storage+http://cuda.net/?p=305" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/06/03/generic-matrices-with-pluggable-storage/&amp;title=Generic+Matrices+with+Pluggable+Storage" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/06/03/generic-matrices-with-pluggable-storage/&amp;title=Generic+Matrices+with+Pluggable+Storage" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=iFjQWeIQFE0:oMZsQ_5Bwzs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=iFjQWeIQFE0:oMZsQ_5Bwzs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=iFjQWeIQFE0:oMZsQ_5Bwzs:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=iFjQWeIQFE0:oMZsQ_5Bwzs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=iFjQWeIQFE0:oMZsQ_5Bwzs:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/iFjQWeIQFE0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/06/03/generic-matrices-with-pluggable-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/06/03/generic-matrices-with-pluggable-storage/</feedburner:origLink></item>
		<item>
		<title>ERF Time Series Page Requirements</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/c792NvPI9R8/</link>
		<comments>http://cuda.net/blog/2009/05/14/erf-timeseries-requirments/#comments</comments>
		<pubDate>Thu, 14 May 2009 12:54:50 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ERF]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=297</guid>
		<description><![CDATA[This project has been pushed back a bit.  I&#8217;ve been busy working on the dnAnalytics&#8217; parallel Map code and the QuantPro data storage.  I&#8217;ll get back to the ERF rewrite the first week of June.  I wanted to start hashing out the sites requirements.  User are most interested in viewing, charting, [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>This project has been pushed back a bit.  I&#8217;ve been busy working on the dnAnalytics&#8217; parallel Map code and the QuantPro data storage.  I&#8217;ll get back to the ERF rewrite the first week of June.  I wanted to start hashing out the sites requirements.  User are most interested in viewing, charting, and downloading a particular time series, so I&#8217;m going to focus on the time series page requirements.</p>
<p>For a given time series, the series page:</p>
<ol>
<li>should display information about the series &#8211; such as its date range, frequency, and unit of measure.</li>
<li>should display a chart of the series, where
<ul>
<li>users can set the time range.</li>
<li>users can change the title.</li>
<li>users can add a second series of the same frequency to the chart.</li>
<li>[Optional]users can select the chart format &#8211; PNG, GIF, or JPG.</li>
</ul>
</li>
<li>should display a summary of the data, that:
<ul>
<li>displays the series&#8217; last 10 values.</li>
<li>displays the series&#8217; last 10 period over period values.</li>
<li>displays the series&#8217; last 10 year over year values.</li>
<li>if the series is not an annual series, convert to an annual series and display<br />
the last ten values.</li>
<li>if the series is not an annual series, convert to an annual series and display<br />
the last ten values as an annual rate.</li>
</ul>
</li>
<li>should allow users to download the series, where:
<ul>
<li>users can select Excel or CSV as the download format.</li>
<li>users can include the period over period and year over year values in the download.</li>
<li>users can select other series in the same group to be included in the download.</li>
<li>users can include the series descriptions in the download.</li>
</ul>
</li>
</ol>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=ERF+Time+Series+Page+Requirements+http://cuda.net/?p=297" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=ERF+Time+Series+Page+Requirements+http://cuda.net/?p=297" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=ERF+Time+Series+Page+Requirements+http://cuda.net/?p=297" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=ERF+Time+Series+Page+Requirements+http://cuda.net/?p=297" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/05/14/erf-timeseries-requirments/&amp;title=ERF+Time+Series+Page+Requirements" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/05/14/erf-timeseries-requirments/&amp;title=ERF+Time+Series+Page+Requirements" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=c792NvPI9R8:9-vBmO0IZuU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=c792NvPI9R8:9-vBmO0IZuU:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=c792NvPI9R8:9-vBmO0IZuU:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=c792NvPI9R8:9-vBmO0IZuU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=c792NvPI9R8:9-vBmO0IZuU:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/c792NvPI9R8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/05/14/erf-timeseries-requirments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/05/14/erf-timeseries-requirments/</feedburner:origLink></item>
		<item>
		<title>ERF Data Model Update</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/_Loev6kCuYg/</link>
		<comments>http://cuda.net/blog/2009/05/05/erf-data-model-update/#comments</comments>
		<pubDate>Tue, 05 May 2009 13:21:14 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ERF]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=291</guid>
		<description><![CDATA[While writing the data acquisition programs, I ran into a couple of issues with the current model.  Most notably, NHibernate will not persist null  values for a list.  This looks like a NHibernate optimization, since it keeps the values ordered with an index column. So when recreating the list, it knows where to put [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>While writing the data acquisition programs, I ran into a couple of issues with the current model.  Most notably, NHibernate will not persist null  values for a list.  This looks like a NHibernate optimization, since it keeps the values ordered with an index column. So when recreating the list, it knows where to put the null values.  The problem is that if the last element in a list is null, that information is lost.  That doesn&#8217;t work for us, so we&#8217;ll use a magic value (double.MinValue) to represent missing values.  This isn&#8217;t as bad as it sounds since we had to use Nullable<T>.HasValue calls anyway.  Now we just do a value != double.MinValue (we&#8217;ll add a double extension method HasValue that wraps that).</p>
<p>Another change is that we are now storing the date for each observation.  Previously, we were only storing the first observation and then using offsets to determine each elements date.  This was to save storage space.  But I forgot that NHibernate stores a position index for each observation anyway.  We might as well store the date and that will remove the need for all the offset calculations.</p>
<p>The new TimeSeries class:</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">public class TimeSeries : DataItem, IComparable&lt;TimeSeries&gt;, IEquatable&lt;TimeSeries&gt; {
        public TimeSeries()
        {
            Data = new SortedList&lt;DateTime, double&gt;();
        }

        public virtual IndexType IndexType { get; set; }

        public virtual Group Owner { get; set; }

        public virtual bool Reload { get; set; }

        public virtual IDictionary&lt;DateTime, double&gt; Data { get; private set; }
}</pre>
<p>The new Mapping:
<pre class="brush: xml; collapse: true; light: false; toolbar: true;">&lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot; assembly=&quot;Erf.Data&quot; namespace=&quot;Erf.Data.Model&quot;&gt;
  &lt;class name=&quot;TimeSeries&quot;&gt;
    &lt;id name=&quot;Id&quot;&gt;
      &lt;generator class=&quot;native&quot; /&gt;
    &lt;/id&gt;
    &lt;property name=&quot;Enabled&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;Reload&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;DisplayOrder&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;IndexType&quot; not-null=&quot;true&quot;/&gt;

    &lt;many-to-one name=&quot;Owner&quot; not-null=&quot;true&quot; lazy=&quot;false&quot;/&gt;

    &lt;map name=&quot;Titles&quot; table=&quot;TimeSeriesTitles&quot; cascade=&quot;all&quot; lazy=&quot;false&quot; outer-join=&quot;true&quot;&gt;
      &lt;key column=&quot;ID&quot;/&gt;
      &lt;index column=&quot;Language&quot; type=&quot;int&quot;&gt;&lt;/index&gt;
      &lt;element type=&quot;string&quot; column=&quot;Title&quot; not-null=&quot;true&quot;/&gt;
    &lt;/map&gt;

    &lt;map name=&quot;Data&quot; lazy=&quot;true&quot; sort=&quot;natural&quot; batch-size=&quot;1000&quot; collection-type=&quot;sorted-list&quot; generic=&quot;true&quot;&gt;
      &lt;key column=&quot;ItemId&quot;/&gt;
      &lt;index type=&quot;DateTime&quot; column=&quot;Period&quot;/&gt;
      &lt;element type=&quot;double&quot; column=&quot;Value&quot; not-null=&quot;true&quot;/&gt;
    &lt;/map&gt;

  &lt;/class&gt;
&lt;/hibernate-mapping&gt;</pre>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=ERF+Data+Model+Update+http://cuda.net/?p=291" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=ERF+Data+Model+Update+http://cuda.net/?p=291" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=ERF+Data+Model+Update+http://cuda.net/?p=291" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=ERF+Data+Model+Update+http://cuda.net/?p=291" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/05/05/erf-data-model-update/&amp;title=ERF+Data+Model+Update" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/05/05/erf-data-model-update/&amp;title=ERF+Data+Model+Update" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=_Loev6kCuYg:Of83EUOGiBY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=_Loev6kCuYg:Of83EUOGiBY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=_Loev6kCuYg:Of83EUOGiBY:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=_Loev6kCuYg:Of83EUOGiBY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=_Loev6kCuYg:Of83EUOGiBY:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/_Loev6kCuYg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/05/05/erf-data-model-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/05/05/erf-data-model-update/</feedburner:origLink></item>
		<item>
		<title>ERF Rewrite – Data Model</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/P41csCOG3Lg/</link>
		<comments>http://cuda.net/blog/2009/04/29/erf-rewrite-data-model/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 14:30:26 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ERF]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=240</guid>
		<description><![CDATA[Our data model is pretty simple.   We have Agencies that provide TimeSeries (the actual data) that are categorized in high level Categories and lower level Groups.  We break our model into four enity types: Agency, Category, Group, and TimeSeries.  An Agency class represents an entity that produces data (such as the Central Bank of Turkey or [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Our data model is pretty simple.   We have Agencies that provide TimeSeries (the actual data) that are categorized in high level Categories and lower level Groups.  We break our model into four enity types: Agency, Category, Group, and TimeSeries.  An Agency class represents an entity that produces data (such as the Central Bank of Turkey or the Turkish Treasury Department).  Each agency will have a collection of Category objects.  A Category is a high level grouping of data (such Public Finance, Price Index, or Production index).  Categories will have a collection for Group objects.  Groups are a lower level grouping of closely related time series (such as GDP, Consumer Price Index, etc.).  A group also maintains properties of the time series they contain, such the data&#8217;s frequency and unit of measure.  A TimeSeries object represents a single series of time ordered data.  So we basically have a tree going from Agencies to Categories to Groups to Time Series.</p>
<p>Each of this classes expose a common set of functionality &#8211; the item&#8217;s Id, its display title in multiple languages, its display order, and if the item is enabled or not.  We&#8217;ll place this functionality in a base class called DataItem.</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">public abstract class DataItem  {
  protected DataItem() {
    Titles = new Dictionary&lt;Language, string&gt;();
  }

  public virtual int Id { get; protected set; }

  public virtual IDictionary&lt;Language, string&gt; Titles { get; private set; }

  public virtual bool Enabled { get; set; }

  public virtual int DisplayOrder { get; set; }

  public override int GetHashCode()  { return Id.GetHashCode(); }
}</pre>
<p>Language is an enumeration of supported languages &#8211; currently only English and Turkish.</p>
<p>The Agency class (comments and some code removed for brevity):</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">public class Agency : DataItem, IEnumerable&lt;Category&gt;, IEquatable&lt;Agency&gt; {
  public Agency() { Categories = new SortedSet&lt;Category&gt;(); }

  protected internal virtual ISet&lt;Category&gt; Categories { get; private set; }

  public virtual IEnumerator&lt;Category&gt; GetEnumerator(){...}

  public virtual void AddCategory(Category category) {
    category.Owner = this;
    Categories.Add(category);
  }

  public virtual void RemoveCategory(Category category)  {
   Categories.Remove(category);
  }

  public virtual void RemoveCategory(int id) {...}
</pre>
<p>Agency NHibernate mapping file:</p>
<pre class="brush: xml; collapse: true; light: false; toolbar: true;">&lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot; assembly=&quot;Erf.Data&quot; namespace=&quot;Erf.Data.Model&quot;&gt;
  &lt;class name=&quot;Agency&quot;&gt;
    &lt;id name=&quot;Id&quot;&gt; &lt;generator class=&quot;native&quot; /&gt;&lt;/id&gt;
    &lt;property name=&quot;Enabled&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;DisplayOrder&quot; not-null=&quot;true&quot;/&gt;

    &lt;map name=&quot;Titles&quot; table=&quot;AgencyTitles&quot;  cascade=&quot;all&quot; lazy=&quot;false&quot;&gt;
      &lt;key column=&quot;ID&quot;/&gt;
      &lt;index column=&quot;Language&quot; type=&quot;int&quot;&gt;&lt;/index&gt;
      &lt;element type=&quot;string&quot; column=&quot;Title&quot; not-null=&quot;true&quot;/&gt;
    &lt;/map&gt;

    &lt;set name=&quot;Categories&quot; inverse=&quot;true&quot; cascade=&quot;all&quot; lazy=&quot;true&quot; sort=&quot;natural&quot;&gt;
      &lt;key column=&quot;Owner&quot;/&gt;
      &lt;one-to-many class=&quot;Category&quot;/&gt;
    &lt;/set&gt;

  &lt;/class&gt;
&lt;/hibernate-mapping&gt;</pre>
<p>We don&#8217;t lazy load the titles since the amount of data is small and they&#8217;ll be used often.  The Category class is almost identical in structure, so we&#8217;ll skip it.</p>
<p>Group class:</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;"> public class Group : DataItem, IComparable&lt;Group&gt;, IEnumerable&lt;TimeSeries&gt;, IEquatable&lt;Group&gt; {
  public Group() {
    Series = new SortedSet&lt;TimeSeries&gt;();
    FrequencyLabels = new Dictionary&lt;Language, string&gt;();
    UnitLabels = new Dictionary&lt;Language, string&gt;();
  }

  public virtual Frequency Frequency { get; set; }

  public virtual Category Owner { get; set; }

  public virtual IDictionary&lt;Language, string&gt; FrequencyLabels { get; private set; }

  public virtual IDictionary&lt;Language, string&gt; UnitLabels { get; private set; }

  protected internal virtual ISet&lt;TimeSeries&gt; Series { get; private set; }

  public virtual void AddSeries(TimeSeries series) {
    series.Owner = this;
    Series.Add(series);
  }

  public virtual void RemoveSeries(TimeSeries series){
    Series.Remove(series);
  }

  public virtual void RemoveSeries(int id){...}
}</pre>
<p>Similar structure as the Agency class, but now we add information about the time series the group holds &#8211; frequency and unit of measure. </p>
<pre class="brush: xml; collapse: true; light: false; toolbar: true;">&lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot; assembly=&quot;Erf.Data&quot; namespace=&quot;Erf.Data.Model&quot;&gt;
  &lt;class name=&quot;Group&quot; table=&quot;GroupTable&quot;&gt;
    &lt;id name=&quot;Id&quot;&gt;
      &lt;generator class=&quot;native&quot; /&gt;
    &lt;/id&gt;
    &lt;property name=&quot;Enabled&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;DisplayOrder&quot; not-null=&quot;true&quot;/&gt;

    &lt;many-to-one name=&quot;Owner&quot; not-null=&quot;true&quot;/&gt;

    &lt;map name=&quot;Titles&quot; table=&quot;GroupTitles&quot; cascade=&quot;all&quot; lazy=&quot;false&quot;&gt;
      &lt;key column=&quot;ID&quot;/&gt;
      &lt;index column=&quot;Language&quot; type=&quot;int&quot;&gt;&lt;/index&gt;
      &lt;element type=&quot;string&quot; column=&quot;Title&quot; not-null=&quot;true&quot;/&gt;
    &lt;/map&gt;

    &lt;map name=&quot;FrequencyLabels&quot; cascade=&quot;all&quot; lazy=&quot;false&quot;&gt;
      &lt;key column=&quot;ID&quot;/&gt;
      &lt;index column=&quot;Language&quot; type=&quot;int&quot;&gt;&lt;/index&gt;
      &lt;element type=&quot;string&quot; column=&quot;Title&quot; not-null=&quot;true&quot;/&gt;
    &lt;/map&gt;

    &lt;map name=&quot;UnitLabels&quot; cascade=&quot;all&quot; lazy=&quot;false&quot;&gt;
      &lt;key column=&quot;ID&quot;/&gt;
      &lt;index column=&quot;Language&quot; type=&quot;int&quot;&gt;&lt;/index&gt;
      &lt;element type=&quot;string&quot; column=&quot;Title&quot; not-null=&quot;true&quot;/&gt;
    &lt;/map&gt;

    &lt;set name=&quot;Series&quot; inverse=&quot;true&quot; cascade=&quot;all&quot; lazy=&quot;true&quot; sort=&quot;natural&quot;&gt;
      &lt;key column=&quot;Owner&quot;/&gt;
      &lt;one-to-many class=&quot;TimeSeries&quot;/&gt;
    &lt;/set&gt;

    &lt;joined-subclass name=&quot;TcbGroup&quot;&gt;
      &lt;key column=&quot;TcbGroupId&quot;/&gt;
      &lt;property name=&quot;Database&quot; column=&quot;DB&quot; not-null=&quot;true&quot; unique=&quot;true&quot;/&gt;
    &lt;/joined-subclass&gt;

    &lt;joined-subclass name=&quot;TreasuryGroup&quot;&gt;
      &lt;key column=&quot;TreasuryGroupId&quot;/&gt;
      &lt;property name=&quot;Url&quot; column=&quot;URL&quot; not-null=&quot;true&quot; unique=&quot;true&quot;/&gt;
      &lt;property name=&quot;LastUpdated&quot; column=&quot;UPDATED&quot; not-null=&quot;true&quot; /&gt;
    &lt;/joined-subclass&gt;

  &lt;/class&gt;
&lt;/hibernate-mapping&gt;</pre>
<p>The mapping file differs from the Agency mapping file in that we now have subclasses of Group.  These subclasses (TcbGroup and TreasuryGroup) store information we use to retrieve the group&#8217;s series.  If you are building a system off of this, you would modify the mapping to include your own subclasses.  Here using <a href="http://fluentnhibernate.org/">Fluent NHibernate</a> would be better than mapping files, since users extending the system wouldn&#8217;t have to muck around with these mapping files (replacing my subclasses with theirs).  We would put the subclass mapping into another DLL and users wouldn&#8217;t reference it.  I just couldn&#8217;t get Fluent NHibernate to work with joined subclasses.  I&#8217;ll probably remove the mapping files once Fluent NHibernate can support them.</p>
<p>Finally, we have our TimeSeries class:</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;"> public class TimeSeries : DataItem, IComparable&lt;TimeSeries&gt;, IEquatable&lt;TimeSeries&gt; {
  private DateTime _firstPeriod;

  protected TimeSeries() { Data = new List&lt;double?&gt;(); }

  public TimeSeries(DateTime firstPeriod) : this()  { _firstPeriod = firstPeriod; }

  public virtual DateTime FirstPeriod  {
            get { return _firstPeriod; }
            set { _firstPeriod = value; }
  }

   public virtual double? this[DateTime date] { get{...} }

   public virtual IndexType IndexType { get; set; }

   public virtual Group Owner { get; set; }

   public virtual bool Reload { get; set; }

   public virtual IList&lt;double?&gt; Data { get; private set; }
}</pre>
<p>Again similar structure.  The IndexType enumeration gives us information about the series.  Are the series values at an annual rate, are the series an annual over annual series, both, or just a plain series?  The Reload properties tell us that we should reload all the series data at the next update.  Generally, I only update the last year of a series&#8217; data when adding new data.  Sometimes there is a major revision and we want to update it all.</p>
<p>For storing the actual time series data, we are not using a map (DateTime, double?).  The reason is to save database space.  Our database is currently about 400MB and adding a date for each observation, would increase the storage significantly.  The only issue with this approach is that you have to make sure you have a data point (a null for missing dates) for each date.  We&#8217;ll use the Erf.Data.Extensions.DateExtensions DateTime extension methods to help us here.</p>
<p>TimeSeries mapping file:</p>
<pre class="brush: xml; collapse: true; light: false; toolbar: true;">&lt;hibernate-mapping xmlns=&quot;urn:nhibernate-mapping-2.2&quot; assembly=&quot;Erf.Data&quot; namespace=&quot;Erf.Data.Model&quot;&gt;
  &lt;class name=&quot;TimeSeries&quot;&gt;
    &lt;id name=&quot;Id&quot;&gt;
      &lt;generator class=&quot;native&quot; /&gt;
    &lt;/id&gt;
    &lt;property name=&quot;Enabled&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;Reload&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;DisplayOrder&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;IndexType&quot; not-null=&quot;true&quot;/&gt;
    &lt;property name=&quot;FirstPeriod&quot; not-null=&quot;true&quot;/&gt;

    &lt;many-to-one name=&quot;Owner&quot; not-null=&quot;true&quot; lazy=&quot;false&quot;/&gt;

    &lt;map name=&quot;Titles&quot; table=&quot;TimeSeriesTitles&quot; cascade=&quot;all&quot; lazy=&quot;false&quot; outer-join=&quot;true&quot;&gt;
      &lt;key column=&quot;ID&quot;/&gt;
      &lt;index column=&quot;Language&quot; type=&quot;int&quot;&gt;&lt;/index&gt;
      &lt;element type=&quot;string&quot; column=&quot;Title&quot; not-null=&quot;true&quot;/&gt;
    &lt;/map&gt;

    &lt;list name=&quot;Data&quot; cascade=&quot;all&quot; lazy=&quot;true&quot; batch-size=&quot;1000&quot; generic=&quot;true&quot; &gt;
      &lt;key column=&quot;ItemId&quot;/&gt;
      &lt;index column=&quot;Position&quot;/&gt;
      &lt;element type=&quot;double&quot; column=&quot;Value&quot; not-null=&quot;false&quot;/&gt;
    &lt;/list&gt;

    &lt;joined-subclass name=&quot;TcbTimeSeries&quot;&gt;
      &lt;key column=&quot;TcbTimeSeriesId&quot;/&gt;
      &lt;property name=&quot;Mnemonic&quot; not-null=&quot;true&quot; unique=&quot;true&quot;/&gt;
    &lt;/joined-subclass&gt;
  &lt;/class&gt;
&lt;/hibernate-mapping&gt;</pre>
<p>The only difference here is that we don&#8217;t lazy load the series owner/group.  The reason is that we need to the series frequency and unit when displaying the series data.  We also have a subclass, TcbTimeSeries, that exposes the FAME mnemonic for the series (used to retrieve the data).  Again, you&#8217;ll have to create subclasses of TimeSeries if you need to store any additional information and create your own joined subclass mapping.</p>
<p>Our data repository classes are really simple: Add, Update, Remove, and GetById.  </p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">public interface IRepository&lt;T&gt; where T : DataItem {
  void Add(T item);
  void Update(T item);
  void Remove(T item);
  T GetById(int id);
}</pre>
<p>The also have a class specific method to load a detached object&#8217;s collection. For example the AgencyRepository:</p>
<pre class="brush: csharp; collapse: true; light: false; toolbar: true;">public class AgencyRespository : Repository&lt;Agency&gt;{
  public void LoadCategories(Agency agency) {
    using (var session = SessionFactory.OpenSession()) {
      session.Lock(agency, LockMode.None);
        NHibernateUtil.Initialize(agency.Categories);
    }
  }
}</pre>
<p>The data model appears pretty anemic, but it is sufficient for what we need &#8211; retrieve time series data for displaying, charting, placing into data files (Excel, delimited, etc), and passing the data off to analysis programs.</p>
<p>I&#8217;ll be spending next couple days rewriting our data retrieval and update programs to work with the new model.  That code is commissioned by the ERF and I&#8217;m not allowed (yet) to release it.  Once that&#8217;s done, we&#8217;ll work on porting the web site the new model and ASP.NET MVC.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=ERF+Rewrite+--+Data+Model+http://cuda.net/?p=240" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=ERF+Rewrite+--+Data+Model+http://cuda.net/?p=240" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=ERF+Rewrite+--+Data+Model+http://cuda.net/?p=240" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=ERF+Rewrite+--+Data+Model+http://cuda.net/?p=240" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/29/erf-rewrite-data-model/&amp;title=ERF+Rewrite+--+Data+Model" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/29/erf-rewrite-data-model/&amp;title=ERF+Rewrite+--+Data+Model" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=P41csCOG3Lg:VYkKqov7imo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=P41csCOG3Lg:VYkKqov7imo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=P41csCOG3Lg:VYkKqov7imo:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=P41csCOG3Lg:VYkKqov7imo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=P41csCOG3Lg:VYkKqov7imo:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/P41csCOG3Lg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/04/29/erf-rewrite-data-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/04/29/erf-rewrite-data-model/</feedburner:origLink></item>
		<item>
		<title>Rewriting the ERF Data Site Series</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/R1qVc0hFOQw/</link>
		<comments>http://cuda.net/blog/2009/04/24/re-writing-the-erf-data-site-series/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 18:36:00 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ERF]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=249</guid>
		<description><![CDATA[I&#8217;m starting a short series of posts on rewriting the Economic Research Forum (ERF) data site.  This a brief introduction to the project.  I&#8217;ll write about the data model in the next day or so.
What is the Economic Research Forum Data Site
The purpose of the Economic Research Forum&#8217;s data web site is to serve as [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I&#8217;m starting a short series of posts on rewriting the <a href="http://data.economicresearchforum.org/">Economic Research Forum</a> (ERF) data site.  This a brief introduction to the project.  I&#8217;ll write about the data model in the next day or so.</p>
<p><strong>What is the Economic Research Forum Data Site</strong><br />
The purpose of the Economic Research Forum&#8217;s data web site is to serve as a central repository for Turkish economic data.  The site currently hosts data from the Central Bank of Turkey and Koc University&#8217;s Economics Department.  The site will be adding more data sources over time with the Turkish Treasury Department next on the list.</p>
<p><strong>Background</strong><br />
The site was originally developed using DevExpress&#8217; ASP.NET controls and eXpress Persistent Objects (XPO) hosted on Windows Server 2003 with SQL Server 2005.  Our goal is to convert the site to 100% free [1] and/or open source software running under Linux.  We&#8217;ll be migrating from DevExpress&#8217;s ASP.NET controls to ExtJS, XPO to NHibnernate, and SQL Server to MySQL.  We&#8217;ll be running the site on Debian 5 using Mono 2.4/2.6.  The move is not about open source idealism, but dollars and cents.</p>
<p><strong>Source Code</strong><br />
The code for site is released under the GPL v3 with an open source exception.  The exception allows any OSI approved open source project to use the code without having to GPL their own code.  The code is up on GitHub &#8211; <a title="GitHub Repository" href="http://github.com/cuda/erf/tree">http://github.com/cuda/erf/tree</a> .  So far only the data model and related tests have been checked in.  We&#8217;ll start adding the web site code next week.</p>
<p>[1] free as in beer, not the Free Software Foundation&#8217;s definition of free (as in speech).</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Rewriting+the+ERF+Data+Site+Series+http://cuda.net/?p=249" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=Rewriting+the+ERF+Data+Site+Series+http://cuda.net/?p=249" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=Rewriting+the+ERF+Data+Site+Series+http://cuda.net/?p=249" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=Rewriting+the+ERF+Data+Site+Series+http://cuda.net/?p=249" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/24/re-writing-the-erf-data-site-series/&amp;title=Rewriting+the+ERF+Data+Site+Series" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/24/re-writing-the-erf-data-site-series/&amp;title=Rewriting+the+ERF+Data+Site+Series" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=R1qVc0hFOQw:5Rir8GN_X54:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=R1qVc0hFOQw:5Rir8GN_X54:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=R1qVc0hFOQw:5Rir8GN_X54:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=R1qVc0hFOQw:5Rir8GN_X54:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=R1qVc0hFOQw:5Rir8GN_X54:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/R1qVc0hFOQw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/04/24/re-writing-the-erf-data-site-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/04/24/re-writing-the-erf-data-site-series/</feedburner:origLink></item>
		<item>
		<title>Planning Contributions for dnAnalytics 2009.8</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/dzkyKgbbIDk/</link>
		<comments>http://cuda.net/blog/2009/04/08/planning-contributions-for-dnanalytics-20098/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 09:59:50 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[dnAnalytics]]></category>
		<category><![CDATA[numerics]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=232</guid>
		<description><![CDATA[dnAnalytics 2009.4 will be released at the end of the month.  So its time to start planning on what I can add to the next release.
One tedious task is going to be updating the unit tests.  The unit tests were started in 2003 and are a mess.  They are mixed in with integration [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>dnAnalytics 2009.4 will be released at the end of the month.  So its time to start planning on what I can add to the next release.</p>
<p>One tedious task is going to be updating the unit tests.  The unit tests were started in 2003 and are a mess.  They are mixed in with integration tests and numerical accuracy tests, and have inconsistent naming and Assert patterns.  I’d like to break the tests into three separate projects and standardize the unit test naming.  Also, I&#8217;d like to add some type of random testing to help catch corner case problems.  I’ve been planning on using <a href="http://www.codeplex.com/fscheck">FsCheck</a> to handle this, but recently learned about the Theory tests in NUnit 2.5.   I need take a deeper look at them and see if they can accomplish the same thing.  It would be nice to use only one tool.</p>
<p>As for new code, I’m looking to add parallel map extension methods and genetic optimization.  The map methods will allow users to something like the following on vectors, matrices, and IList&lt;double&gt; types:<br />
<code><br />
obj.Map(x=&gt;1/x};<br />
obj.Map(Math.Sqrt);<br />
</code><br />
The first inverts the each element of the that object and the second would replace their values with their square root.  We’ll parallelize this so it uses all available cores.</p>
<p>A project I completed last month made heavy use of genetic optimization.  It was amazing how simple it was to solve complex problems.  I think this will a very helpful addition to the library.  I’ll start a blog series on creating the optimization framework when I start working on the code.</p>
<p>If I can, I’d like to squeeze in matrix and vector views.   Views will allow users to treat sub-matrices/vector and arrays as actual matrices and vectors.  This has two advantages – less copying overhead and reduced memory usage.  Also, if I can pick up a relatively cheap NIVIDA GTX 200 series video card, I can start adding NVIDIA CUDA support to the native version.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Planning+Contributions+for+dnAnalytics+2009.8+http://cuda.net/?p=232" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=Planning+Contributions+for+dnAnalytics+2009.8+http://cuda.net/?p=232" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=Planning+Contributions+for+dnAnalytics+2009.8+http://cuda.net/?p=232" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=Planning+Contributions+for+dnAnalytics+2009.8+http://cuda.net/?p=232" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/08/planning-contributions-for-dnanalytics-20098/&amp;title=Planning+Contributions+for+dnAnalytics+2009.8" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/08/planning-contributions-for-dnanalytics-20098/&amp;title=Planning+Contributions+for+dnAnalytics+2009.8" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=dzkyKgbbIDk:ZZoADZVf4to:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=dzkyKgbbIDk:ZZoADZVf4to:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=dzkyKgbbIDk:ZZoADZVf4to:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=dzkyKgbbIDk:ZZoADZVf4to:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=dzkyKgbbIDk:ZZoADZVf4to:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/dzkyKgbbIDk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/04/08/planning-contributions-for-dnanalytics-20098/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/04/08/planning-contributions-for-dnanalytics-20098/</feedburner:origLink></item>
		<item>
		<title>Creating a Parallel.For Clone</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/IRhIgvfosf0/</link>
		<comments>http://cuda.net/blog/2009/04/06/creating-a-parallelfor-clone/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 10:45:49 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[dnAnalytics]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[parallel]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=200</guid>
		<description><![CDATA[The next release of dnAnalytics is going to provide a Map class that applies a &#8220;function&#8221; to each element of a vector or matrix.   Since each mapping of the function is independent of the others, it&#8217;s a great candidate to parallelize.  .NET 4.0 provides a parallel for loop (Parallel.For) that is perfect for this.  However, [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The next release of dnAnalytics is going to provide a Map class that applies a &#8220;function&#8221; to each element of a vector or matrix.   Since each mapping of the function is independent of the others, it&#8217;s a great candidate to parallelize.  .NET 4.0 provides a parallel for loop (Parallel.For) that is perfect for this.  However, we need to support .NET 2.0.  The plan is to create a drop in replacement for Parallel.For that can be used under .NET 2.0.  Then depending on the end user&#8217;s environment, dnAnalytics will use either the .NET 4.0 Parallel.For or our clone.  We should be clear that this is <strong>NOT</strong> a general replacement for the .NET 4.0 parallel for loop.  The clone will be optimized for dnAnalytics, not for general applications.</p>
<p>Below is my thought process (or thinking out loud) of creating the Parallel.For clone.  The code is a work in progress and has not been optimized.</p>
<p>For our Parrallel.For loop, we have three options:</p>
<ol>
<li>Create threads on the fly.</li>
<li>Use the .NET ThreadPool.</li>
<li>Create our own thread pool.</li>
</ol>
<p>We&#8217;re going with option 3, since it gives us control over the number of running threads and minimizes thread creation.</p>
<pre class="brush: csharp;">internal static class ThreadQueue{
   private static readonly Queue _queue = new Queue();
   private static int _count;
   private static bool _running = true;
   private static Thread[] _threads = SetThreads(2);

   public static int ThreadCount{
      get { return _count; }
   }

   private static Thread[] SetThreads(int count){
      _count = count;
      var threads = new Thread[_count];

      for (var i = 0; i &lt; _count; i++) {
         threads[i] = new Thread(Start);
         threads[i].IsBackground = true;
         threads[i].Start();
      }
      return threads;
   }

   public static void Enqueue(Task task){
      lock (_queue){
         _queue.Enqueue(task);
         Monitor.Pulse(_queue);
      }
   }

   private static void Start() {
      while (true){
         Task task = null;
         lock (_queue){
            if (_queue.Count &gt; 0){
               task = _queue.Dequeue();
            } else{
               Monitor.Wait(_queue);
            }
         }
         if (task != null){
            task.Compute();
            task.Set();
         }
         if (!_running){
            break;
         }
      }
   }
}</pre>
<p>The thread queue starts up N threads and the threads wait for a Task to be queued.  In the sample code the number of threads is set to 2, but we&#8217;ll make that configurable and default to the number of CPU cores.  Next, we need to create the Task class.  Here we are going to deviate from the System.Threading.Task design by having the Task class extend EventWaitHandle.  This will simplify thread notification.  Our Task implementation is going to be for internal use only, so deviating is not a problem.  </p>
<pre class="brush: csharp;">internal class Task : EventWaitHandle{
   private readonly Action _body;

   public Task(Action body) : base(false, EventResetMode.ManualReset){
      if (body == null){
         throw new ArgumentNullException(&quot;body&quot;);
      }
      _body = body;
   }

   public Exception Exception { get; private set; }

   public bool ThrowException { get { return Exception != null; }  }

   public void Compute()  {
      try {
         _body();
      } catch (Exception e){
         Exception = e;
      }
   }
}</pre>
<p>The Task class will swallow any exceptions thrown by the mapped function (Compute method), but it will be aggregated with any other exceptions in the actually Parallel.For loop and re-thrown as an AggregateException.</p>
<p>Now we can create the actual For loop.</p>
<pre class="brush: csharp;">public static class Parallel{
   public static void For(int fromInclusive, int toExclusive, Action body){
      if (body == null){
         throw new ArgumentNullException(&quot;body&quot;);
      }
      var count = toExclusive - fromInclusive;
      var size = count/ThreadQueue.ThreadCount;
      if (count &lt; 1) {
         return;
      }

      var actions = new List();
      for (var i = 0; i &lt; ThreadQueue.ThreadCount - 1; i++){
         var start = fromInclusive + (i*size);
          var stop = fromInclusive + ((i + 1)*size);
          actions.Add( () =&gt;; {
             for (var j = start; j &lt; stop; j++) {
                body(j);
             }
          } ) ;
       }
       actions.Add( () =&gt;  {
          for (var i = fromInclusive + ((ThreadQueue.ThreadCount - 1)*size); i &lt; toExclusive; i++){
             body(i);
          }
       } );
       Invoke(actions.ToArray());
   }

   public static void Invoke(params Action[] actions) {
      var tasks = new List();
      foreach (var action in actions){
         tasks.Add(new Task(action));
      }
      foreach (var task in tasks){
         ThreadQueue.Enqueue(task);
      }
      WaitHandle.WaitAll(tasks.ToArray());

      var exceptions = new List();
      foreach (var task in tasks) {
         if(task.ThrowException) {
            exceptions.Add(task.Exception);
         }
      }
      if( exceptions.Count &gt; 0) {
         throw new AggregateException(exceptions);
      }
   }
}</pre>
<p>Real simple.  Break the loop into X number of tasks, queue up the tasks, wait for them to finish, and then check for any exceptions.  Using only X number of thread tasks works well for us, since these are CPU bound loops.  If that wasn&#8217;t the case, there would be idle threads.  You could then increase performance by breaking up the loop into smaller tasks (feeding the idle threads).</p>
<p>So how does this perform?  Lets compare it to serial code and against the Parallel Extension Library (PEL) June CTP (I don&#8217;t have access to the VS 2010 CTP).  We&#8217;ll look at two cases, (A) a computational light operation (1/i) and then (B) a slightly heavier operation (Sinh(90) + Cosh(90)).  We&#8217;ll also use two array sizes, 1000 and 1000000.</p>
<p><strong>Case A</strong><br />
N=1000, our Code is 3.2x slower than the serial code and the PEL code is 7.7x slower.<br />
N=1000000, our Code is 37% faster than the serial code PEL code is 18% faster.</p>
<p><strong>Case B</strong><br />
N=1000, our Code is 1.1x slower than the serial code and the PEL code is 1.6x slower.<br />
N=1000000, our Code is 43% faster than the serial code PEL code is 33% faster.</p>
<p>These results make sense.  When the size of the array is relatively small, the threading overhead outweighs the parallel gain.   And as the the operation performed gets more computationally intense, the threading overhead becomes smaller relative to the computing time.</p>
<p>Folks might be wondering why our code seems to out perform the Parallel Extension Library.  This is probably due to two reasons. First, PEL is a CTP release and isn&#8217;t optimized for speed.  Second, we are focusing only one type of usage &#8211; tight numerical loops.  PEL has to be optimized to handle any type of loop. </p>
<p>The code stills needs some work before it is integrated into the dnAnalytics code base.</p>
<p><strong>Update:</strong> A modified version of the code above has been added to dnAnalytics.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Creating+a+Parallel.For+Clone+http://cuda.net/?p=200" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=Creating+a+Parallel.For+Clone+http://cuda.net/?p=200" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=Creating+a+Parallel.For+Clone+http://cuda.net/?p=200" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=Creating+a+Parallel.For+Clone+http://cuda.net/?p=200" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/06/creating-a-parallelfor-clone/&amp;title=Creating+a+Parallel.For+Clone" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/04/06/creating-a-parallelfor-clone/&amp;title=Creating+a+Parallel.For+Clone" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=IRhIgvfosf0:HeA403GPOek:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=IRhIgvfosf0:HeA403GPOek:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=IRhIgvfosf0:HeA403GPOek:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=IRhIgvfosf0:HeA403GPOek:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=IRhIgvfosf0:HeA403GPOek:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/IRhIgvfosf0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/04/06/creating-a-parallelfor-clone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/04/06/creating-a-parallelfor-clone/</feedburner:origLink></item>
		<item>
		<title>LaTeX for Sandcastle Works with SHFB 1.8</title>
		<link>http://feedproxy.google.com/~r/cuda/~3/DEZY_cp9u6U/</link>
		<comments>http://cuda.net/blog/2009/03/24/latex-for-sandcastle-works-with-shfb-18/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 08:22:24 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[sandcastle]]></category>

		<guid isPermaLink="false">http://cuda.net/?p=151</guid>
		<description><![CDATA[I upgraded to Sandcastle Help File Builder (SHFB) 1.8 and it wasn&#8217;t finding our LaTeX build component.   It looks like SHFB now longer looks for the &#8220;BuildComponents&#8221; directory in the program directory but in %ProgramData%.  After moving the component there, it works fine.  I&#8217;ve updated the documentation to reflect this change.
I started compiling mimeTeX with [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I upgraded to Sandcastle Help File Builder (SHFB) 1.8 and it wasn&#8217;t finding our LaTeX build component.   It looks like SHFB now longer looks for the &#8220;BuildComponents&#8221; directory in the program directory but in %ProgramData%.  After moving the component there, it works fine.  I&#8217;ve updated the documentation to reflect this change.</p>
<p>I started compiling mimeTeX with Intel&#8217;s C++ compiler and having it auto-vectorize (using SSE2) its loops where possible.  This should speed it up a bit.  The new build can be downloaded from <a title="LaTeX Sandcastle download" href="http://cuda.net/downloads/latex_sandcastle.zip">http://cuda.net/downloads/latex_sandcastle.zip</a>.</p>
<p>The source has also been moved over to <a title="LaTeX Sandcastle on GitHub" href="http://github.com/cuda/latex-sandcastle/tree/master">GitHub</a> and the I setup a <a title="LaTeX Sandcastle on Lighthouse" href="http://cuda.lighthouseapp.com/projects/26824-latex-sandcastle">Lighthouse issue tracker</a> for the project.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=LaTeX+for+Sandcastle+Works+with+SHFB+1.8+http://cuda.net/?p=151" title="Post to Twitter"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-twitter3.png" alt="[Post to Twitter]" border="0" /></a> <a class="tt" href="http://twitter.com/home/?status=LaTeX+for+Sandcastle+Works+with+SHFB+1.8+http://cuda.net/?p=151" title="Post to Twitter">Tweet This Post</a>&nbsp; <a class="tt" href="http://plurk.com/?status=LaTeX+for+Sandcastle+Works+with+SHFB+1.8+http://cuda.net/?p=151" title="Post to Plurk"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-plurk.png" alt="[Post to Plurk]" border="0" /></a> <a class="tt" href="http://plurk.com/?status=LaTeX+for+Sandcastle+Works+with+SHFB+1.8+http://cuda.net/?p=151" title="Post to Plurk">Plurk This Post</a>&nbsp; <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/03/24/latex-for-sandcastle-works-with-shfb-18/&amp;title=LaTeX+for+Sandcastle+Works+with+SHFB+1.8" title="Post to Delicious"><img class="nothumb" src="http://cuda.net/wp-content/plugins/tweet-this/icons/tt-delicious.png" alt="[Post to Delicious]" border="0" /></a> <a class="tt" href="http://delicious.com/post?url=http://cuda.net/blog/2009/03/24/latex-for-sandcastle-works-with-shfb-18/&amp;title=LaTeX+for+Sandcastle+Works+with+SHFB+1.8" title="Post to Delicious">Delicious This Post</a>&nbsp; </p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/cuda?a=DEZY_cp9u6U:SmckuZ2pkmE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/cuda?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=DEZY_cp9u6U:SmckuZ2pkmE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/cuda?i=DEZY_cp9u6U:SmckuZ2pkmE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/cuda?a=DEZY_cp9u6U:SmckuZ2pkmE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/cuda?i=DEZY_cp9u6U:SmckuZ2pkmE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/cuda/~4/DEZY_cp9u6U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://cuda.net/blog/2009/03/24/latex-for-sandcastle-works-with-shfb-18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://cuda.net/blog/2009/03/24/latex-for-sandcastle-works-with-shfb-18/</feedburner:origLink></item>
	</channel>
</rss>
