1

我是使用 Parallel.For 的新手,并且坚持以下内容。

在我的 For 循环中,我使用了一个指针偏移量(iPathMatrixOffset),每次迭代我都需要将它增加一个固定的数字(nStages)。基本上在 r 次迭代之后,偏移量的值将是 r * nStages。

我目前在循环内声明这个变量并将其设置为 r*nStages。

我认为这不是最有效的方法 - 这是性能关键代码(这就是我在这里使用基于指针的代码的原因) - 我想尝试看看在每次迭代结束时使用 nStages 递增它是否更快。

但我不知道如何声明一个仅对线程本地的变量,我无法从 MSDN 中弄清楚。

这一定是一个常见的问题......我当然忽略了一些明显的事情。

帮助表示赞赏。

当前代码:

Parallel.For(0, nStates, r =>
{
    int iPathMatrixOffset;
    double local_maxV;

    iPathMatrixOffset = r * nStages;

    // some code, calculates local_maxV

    // store result in path matrix
    fixed (double* pPathMatrix = &PathMatrix[0, t])
    {
        *(pPathMatrix + iPathMatrixOffset) = local_maxV;
    }
});

想做类似的事情:

Parallel.For(0, nStates, r =>
{
    double local_maxV;
    // some code

    // store result in path matrix
    fixed (double* pPathMatrix = &PahMatrix[0, t])
    {
        *(pPathMatrix + iPathMatrixOffset) = local_maxV;
    }
    iPathMatrixOffset += nStages;

});
4

0 回答 0