我知道对于C#主程序,堆栈大小为1MB(32位和任何位)或4MB(64位),请参见为什么C#的堆栈大小恰好为1MB?
除了创建另一个线程,像以下示例那样,是否有一种方法可以更改
我正在使用
我遇到了堆栈溢出错误,因为我调用了Intel MKL的LAPACKE_dtrtri,它非常递归,参见http://www.netlib.org/lapack/explore-html/df/d5c/lapacke__dtrtri_8c_source.html。
以下代码显示了我如何调用Intel MKL:
BackgroundWorker
DoWork
线程的默认堆栈大小是多少?除了创建另一个线程,像以下示例那样,是否有一种方法可以更改
BackgroundWorker
DoWork
线程的堆栈大小?private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Thread thread = new Thread(delegate()
{
// do work with larger stack size
}, 8192 * 1024);
thread.Start();
thread.Join();
}
我正在使用
BackgroundWorker
,因为我有一个Windows Forms
应用程序,在其中我在DoWork
事件中进行一些计算。我这样做是因为我想向GUI的状态行报告返回值,并且我希望用户可以取消这些计算。我遇到了堆栈溢出错误,因为我调用了Intel MKL的LAPACKE_dtrtri,它非常递归,参见http://www.netlib.org/lapack/explore-html/df/d5c/lapacke__dtrtri_8c_source.html。
以下代码显示了我如何调用Intel MKL:
public static double[,] InvTriangularMatrix(double[,] a, bool isupper)
{
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 uplo = isupper ? 'U' : 'L';
char diag = 'N';
int lda = Math.Max(1, n1);
int info = _mkl.LAPACKE_dtrtri(matrix_layout, uplo, diag, n1, b, lda);
if (info > 0) throw new System.Exception("The " + info + "-th diagonal element of A is zero, A is singular, and the inversion could not be completed");
if (info < 0) throw new System.Exception("Parameter " + (-info) + " had an illegal value");
return b;
}
并且
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]
internal static extern int LAPACKE_dtrtri(
int matrix_layout, char uplo, char diag, lapack_int n, [In, Out] double[,] a, int lda);
InvTriangularMatrix
在我的 DoWork
事件中被调用。当我没有设置堆栈大小时,LAPACKE_dtrtri
函数内会出现堆栈溢出错误。
矩阵的大小可以达到 1000 x 1000 到 100000 x 100000 的数量级。如果矩阵大于 65535 x 65535,则参见 2d-Array with more than 65535^2 elements --> Array dimensions exceeded supported range。
Stack<T>
实现的迭代方法。 - Matthew WatsonBackgroundWorker
,因为它具有ReportProgress
和Cancel
功能。我启动另一个线程是因为我需要增加堆栈大小。 - Wollmich