我想使用英特尔 MKL(2019 更新 2)的LAPACKE_dsyevd计算实对称矩阵的所有特征值和所有特征向量。
我在 C# 中有以下方法:
public static class MKL
{
public static double[,] SymmetricEig(double[,] a, out double[] w)
{
int n1 = a.GetLength(0);
int n2 = a.GetLength(1);
if (n1 != n2) throw new System.Exception("Matrix must be square");
double[,] b = Copy(a);
int matrix_layout = 101; // row-major arrays
char jobz = 'V';
char uplo = 'U';
int n = n2;
int lda = n;
w = new double[n];
_mkl.LAPACKE_dsyevd(matrix_layout, jobz, uplo, n, b, lda, w);
return b;
}
}
和
class _mkl
{
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
internal static extern lapack_int LAPACKE_dsyevd(
int matrix_layout, char jobz, char uplo, lapack_int n, [In, Out] double[,] a, lapack_int lda, [In, Out] double[] w);
}
和以下测试代码:
int n = 32766; // 32767 or greater --> Not enough memory to allocate work array in LAPACKE_dsyevd
double[,] A = CreateRandomSymmetricMatrix(n);
double[] w = new double[n];
double[,] B = MKL.SymmetricEig(A, out w);
和
static double[,] CreateRandomSymmetricMatrix(int n1)
{
double[,] m = new double[n1, n1];
for (int i1 = 0; i1 < n1; i1++)
{
for (int i2 = 0; i2 <= i1; i2++)
{
m[i1, i2] = r.NextDouble();
m[i2, i1] = m[i1, i2];
}
}
return m;
}
如果n
大于 32766,则会失败并显示以下错误消息:
没有足够的内存在 LAPACKE_dsyevd 中分配工作数组
我的电脑有 128 GB 的 RAM,应该足够了。我知道<gcAllowVeryLargeObjects enabled="true" />
并已将其设置为 true。我也很清楚 C# 中多维数组的 65535^2 限制,请参阅2d-Array with more than 65535^2 elements --> Array dimensions exceeded supported range。
顺便说一句,我可以使用 MATLAB 计算 n = 40000 或更大的矩阵的特征值分解。而且我认为 MATLAB 也在后台使用英特尔 MKLin 来计算它。
那么如何使用英特尔 MKL 在 C# 中计算非常大的矩阵(n > 40000)的特征值分解?