如何将原子指令用于OpenMP中的部分

发布于 2025-02-03 00:53:23 字数 538 浏览 3 评论 0原文

我使用OpenMP的截面指令并行化两个循环。

#pragma omp parallel sections
{
    #pragma omp section    //section 1
    {
       
        for (conditions){
            statement
    } }
    #pragma omp section    //section 2
    {
     #pragma omp for
     for (conditions){
            statement
      } }
}

原因是两个循环彼此独立。第一个循环具有依赖性语句,因此不能并行化,其执行时间很大,而第二个循环可以并行化。但是,如果我仅在第二个循环中应用并行指令,则必须等待其执行,直到第一个循环完成。

因此,我试图使用截面或任务并行执行两个循环,在第一部分中,第一个循环可以顺序执行,在第二部分中,第二个循环可以并行化。

我不知道它是否会加快程序。但是在获得任何结果之前,我被困在如何依次在节中运行第一个循环。

i am parallelizing two loops using section directive of openMP.

#pragma omp parallel sections
{
    #pragma omp section    //section 1
    {
       
        for (conditions){
            statement
    } }
    #pragma omp section    //section 2
    {
     #pragma omp for
     for (conditions){
            statement
      } }
}

The reason is both two loops are independent of each other. The first loop has dependent statements so it can not be parallelized and its execution time is large whereas the second loop can be parallelized. But if I apply parallel directives only on the second loop then it has to wait for its execution until the first one is completed.

So I am trying to execute both loops in parallel using a section or task where in the first section first loop can be executed sequentially and in the second section the second one can be parallelized.

I don't know whether it will speed up the program or not. But before getting any results, I was stuck on how to run the first loop in the section sequentially.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2025-02-10 00:53:23

您应该使用任务来实现目标(同时运行2个循环:其中一个是按顺序运行的,另一个同时运行):

#pragma omp parallel
#pragma omp single
{
    #pragma omp task    
    for (conditions){
        //serial loop
    }
    
    #pragma omp taskloop
    for (conditions){
        //parallel loop
    }        
}

请注意,您的问题与原子操作无关。

You should use tasks to achieve your goal (to run 2 loops simultaneously: one of them sequentially and the other in parallel):

#pragma omp parallel
#pragma omp single
{
    #pragma omp task    
    for (conditions){
        //serial loop
    }
    
    #pragma omp taskloop
    for (conditions){
        //parallel loop
    }        
}

Note that your question has nothing to do with atomic operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文