简体   繁体   English

忍不住用 OpenMP 来划分线程工作,用一个线程执行一个指令,用其余线程执行一个并行指令

[英]Can't help dividing thread work with OpenMP, doing one instruction with a single thread and a parallel for with the remaining

I post the current version of my code, which is this one我发布了我的代码的当前版本,就是这个

#   pragma omp parallel
{ 
...
...
...
#   pragma omp single nowait   
   for (int i = 0; i < M; i++) { 
       centroids[points[i].cluster].points_in_cluster++;
   }
   

   for (int i = 0; i < M; i++) { //I want thread_count - 1 to be working here
#       pragma omp for
       for (int coord = 0; coord < N; coord++){
           //int my_tid = omp_get_thread_num();
           //printf("my tid:%d my_coord: %d my i:%d\n ", my_tid, coord, i);
           centroids[points[i].cluster].accumulator.coordinates[coord] += points[i].coordinates[coord];
       }
   }
#   pragma omp barrier
...
...
...
}

and works fine already, but I want to see if times can be improved by doing the following, make one thread do what is under the omp single pragma, and the other do what is underneath, without his help.并且已经很好地工作了,但是我想看看是否可以通过执行以下操作来改善时间,让一个线程在omp single pragma 下执行操作,而另一个在没有他帮助的情况下执行下面的操作。 So if there are 8 threads, 1 will do the single section, and 7 the other part.因此,如果有 8 个线程,则 1 个将执行single部分,而 7 个将执行另一部分。

I tried with omp sections but it didn't work, because it said that work-sharing region may not be closely nested inside of work-sharing .我尝试使用omp sections但它没有用,因为它说work-sharing region may not be closely nested inside of work-sharing

You can use tasks to solve your problem.您可以使用任务来解决您的问题。 In this case one thead will run the first loop, all other threads the second loop.在这种情况下,一个 thead 将运行第一个循环,所有其他线程将运行第二个循环。

#pragma omp parallel
#pragma omp single
{
    #pragma omp task
    {
        // one task (thread) runs this part of the code
    }
    
    #pragma omp taskloop num_tasks(omp_get_num_threads()-1)
    for (....){
        // all other tasks (threads) run this loop
    }
}

By the way, I don't think this code would run faster compared to the other approach, since the overhead of using tasks is higher.顺便说一句,我不认为这段代码会比其他方法运行得更快,因为使用任务的开销更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM