将 OpenMp Parallel for 简单转换为 c# Parallel for [英] Simple Convert OpenMp Parallel for to c# Parallel for

查看:60
本文介绍了将 OpenMp Parallel for 简单转换为 c# Parallel for的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将此 c++ ( openmp ) parallel for 转换为 c# parallel for 但它说:

hi i am converting this c++ ( openmp ) parallel for to c# parallel for but it says :

错误 1 ​​并非所有代码路径都返回类型为 lambda 表达式的值'System.Func'

Error 1 Not all code paths return a value in lambda expression of type 'System.Func<int,System.Threading.Tasks.ParallelLoopState,int,int>'

这是我的代码:

void floyd_warshall(int NumOfThreads) {
    int i, j, k;

    omp_set_num_threads(NumOfThreads);
    for (k = 0; k < n; ++k)
        #pragma omp parallel for private(i,j)
        for (i = 0; i < n; ++i)
            for (j = 0; j < n; ++j)
                /* If i and j are different nodes and if
                    the paths between i and k and between
                    k and j exist, do */
                if ((dist[i][k] * dist[k][j] != 0) && (i != j))
                    /* See if you can't get a shorter path
                        between i and j by interspacing
                        k somewhere along the current
                        path */
                    if ((dist[i][k] + dist[k][j] < dist[i][j]) || (dist[i][j] == 0))
                        dist[i][j] = dist[i][k] + dist[k][j];
}

c#

 void floyd_warshall(int NumOfThreads)
        {
            int  k;
            ParallelOptions pOp;
            pOp.MaxDegreeOfParallelism = NumOfThreads;

            for (k = 0; k < n; ++k)
             Parallel.For<int>(0, n, pOp , () => 0, (i, loop, j) =>
                 {   //  for (i = 0; i < n; ++i)
                     for (j = 0; j < n; ++j)
                         /* If i and j are different nodes and if
                             the paths between i and k and between
                             k and j exist, do */
                         if ((dist[i, k] * dist[k, j] != 0) && (i != j))
                             /* See if you can't get a shorter path
                                 between i and j by interspacing
                                 k somewhere along the current
                                 path */
                             if ((dist[i, k] + dist[k, j] < dist[i, j]) || (dist[i, j] == 0))
                                 dist[i, j] = dist[i, k] + dist[k, j];
                 }, (j) => 0);
        }

推荐答案

您可以使用 Parallel.For 方法的更简单重载,该方法不需要您的委托具有返回值.

You can use a simpler overload of the Parallel.For method that doesn't require your delegate to have a return vale.

    var pOp = new ParallelOptions { MaxDegreeOfParallelism = NumOfThreads };

    for (int k = 0; k < n; ++k)
        Parallel.For(0, n, pOp, i =>
        {   //  for (i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                /* If i and j are different nodes and if
                    the paths between i and k and between
                    k and j exist, do */
                if ((dist[i, k] * dist[k, j] != 0) && (i != j))
                    /* See if you can't get a shorter path
                        between i and j by interspacing
                        k somewhere along the current
                        path */
                    if ((dist[i, k] + dist[k, j] < dist[i, j]) || (dist[i, j] == 0))
                        dist[i, j] = dist[i, k] + dist[k, j];
        });

这篇关于将 OpenMp Parallel for 简单转换为 c# Parallel for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆