Matrix data structure optimization: Nearly 50% perf gain

Some time ago I did some very basic performance analysis of solving linear equation systems with Iridium. Since then we decided to rewrite the Matrix class to use jagged arrays instead of rectangular ones, see this forum discussion. There already was a discussion about that issue some long time ago, but at that time we decided to go for the more clean and safe way of rectangular arrays. Unfortunately the C# compiler today still can't optimize loops on rectangular arrays as good as loops on jagged arrays. So we finally moved forward to jagged arrays, and indeed, we got a performance improvement (solving a linear equation system) by nearly 50%.

Unfortunately, the change of the data structure comes at a cost: The semantics of the two following members changes, as they now do deep-copies instead of using the data structure directly as internal data structure. Have a look at the mentioned forum discussion on why this might be an issue.

public Matrix(double[,] A)
public static implicit operator double[,] (Matrix m)

If you want to avoid deep-copying, e.g. for performance reasons, then use double[][] instead of double[,] to fill the matrix.

The changes are already submitted to the repository, and will be included in the next iridium release.

(Migrated Comments)

Joannes Vermorel, October 15, 2007

This is an excellent news. Thanks for upgrading the matrix to jagged array. The use of rectangular arrays was a (poor) choice of mine when I did initially port the JAMA package to Math.NET.

Among the other benefits that you gain with jagged arrays is the possibility to perform multi-thread operations on matrices (using BLAS).

Best regards, Joannes