在C中将OMP转换为Pthread

时间:2018-10-05 04:01:28

标签: c pthreads openmp

我在OMP中运行以下代码来计算Dijkstra算法,我想将其转换为Pthreads。我知道对于pthread,我必须将工作与输入线程的数量平均分配,然后将其重新加入。有人可以告诉我如何创建它吗?

//Set max nThreads
    omp_set_num_threads(nThreads);
    omp_set_schedule(omp_sched_auto, G->N/100);
        dijkstra(G, 0, 0);

void dijkstra(graph* G, long initial_node, char debug)
{
    long i,j,k;
    long aN; //actualNode
    G->D[initial_node] = 0;
    aN = initial_node;

    if(debug){
        printf("Running dijkstra on graph\n");
        printGraph(G);
    }

    for(i = 0; i < G->N; i++)
    {
        G->visited[aN] = VISITED;

        if(debug){
            printf("It[%d] aN [%d]",i, aN); printStatus(G); printf("\n");
        }

        //Find all nodes connected to aN
        #pragma omp parallel for schedule(runtime)
        for(j=0;j<G->N;j++){
            if( (G->node[aN][j] != NO_CONN) ){
                if( (G->D[aN] + G->node[aN][j]) < G->D[j] ){
                    G->D[j] = (G->D[aN] + G->node[aN][j]);
                }
            }
        }

        aN = getNextNode(G);
    }
}

0 个答案:

没有答案