避免线程创建openMP中的开销 [英] Avoiding overhead in thread creation openMP

查看:237
本文介绍了避免线程创建openMP中的开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我的代码中,有各种函数可以更改各种数组,并且调用这些函数的顺序很重要.由于多次调用所有函数,因此创建和销毁线程已成为很大的开销. 编辑我的问题,因为我可能已经简化了我当前的问题. 一个例子

So in my code there are various function that alter various arrays and the order in which the functions are called is important. As all functions are called a big number of times creating and destroying the threads has become a big overhead. EDIT on my question as I may have oversimplified my current problem. An example

double ans = 0;
for (int i = 0; i < 4000; i++){
    funcA(a,b,c);
    funcB(a,b,c);
    ans = funcC(a,b,c):
}
prinft(ans);

其中的funcA,funcB和func C

where funcA, funcB and func C are

void funcA (int* a, point b, int* c){
#pragma omp parallel for shared(a,b,c)
    for (int ii = 0; ii < b.y; ii++){
        for (int jj = 0; jj < b.x; jj++){
          \\ alter values of a and c
        }
    }
}

void funcB (int* a, point b, int* c){
#pragma omp parallel for shared(a,b,c)
    for (int ii = 0; ii < b.y; ii++){
        for (int jj = 0; jj < b.x; jj++){
          \\ alter values of a and c
        }
    }
}

double funcC (int* a, pointb, int* c){
    double k = 0;
#pragma omp parallel for shared(a,b,c) reduction(+:k)
    for (int ii = 0; ii < b.y; ii++){
        for (int jj = 0; jj < b.x; jj++){
          \\ alter values of a and c
            k += sqrt(a[ii*jj] + c[ii**jj]);
        }
    }
    return k;
}

有没有办法在所有功能使用的初始for循环之前创建一组线程,并且这些线程不会不断被破坏并再次创建,并且仍然在函数调用中保持正确的顺序?

Is there any way to create a team of threads before the initial for loop that are used by all functions and are not constantly being destroyed and created again, and still keep the correct order in the function calls?

我正在寻找的是一种按顺序运行funcA funB和funcC的方法.但是函数内部有一些代码,这些代码将使用多个线程.我想要一种在开始时创建线程的方法,然后将它们仅用于那些并行部分,因此最后的答案是正确的.有没有办法避免分叉和加入40000次?

What I am looking for is a way to run funcA funB, funcC in that order. But the functions have some code inside them that will use multiple threads. I want a way to create the threads at the start and then they will only be used on those parallel sections so the answer at the end is correct. Is there a way to avoid the forking and joining 40000 times?

推荐答案

假设其余代码正确,则下面的代码应该可以按照您希望的方式工作:

Assuming the rest of your code is correct, the following should just work the way you want it to:

#pragma omp parallel shared( a, b, c )
for (int i = 0; i < 4000; i++){
    funcA(a,b,c);
    funcB(a,b,c);
    funcC(a,b,c):
}

现在,不同的功能定义如下:

With the different functions now defined as follows:

void funcA( int* a, point b, int* c ) {
    #pragma omp for schedule( static )
    for (int ii = 0; ii < b.y; ii++) {
        for (int jj = 0; jj < b.x; jj++) {
          \\ alter values of a and c
        }
    }
}

void funcB( int* a, point b, int* c ) {
    #pragma omp for schedule( static )
    for (int ii = 0; ii < b.y; ii++) {
        for (int jj = 0; jj < b.x; jj++) {
          \\ alter values of a and c
        }
    }
}

void funcC( int* a, point b, int* c ) {
    #pragma omp for schedule( static )
    for (int ii = 0; ii < b.y; ii++) {
        for (int jj = 0; jj < b.x; jj++) {
          \\ alter values of a and c
        }
    }
}

函数中的这些OpenMP伪指令被称为孤立伪指令,因为它们出现在任何OpenMP并行区域之外的代码中.但是,在运行时,任何现有的OpenMP线程团队都会以您希望的方式使用它们.

These OpenMP directives inside the functions are called orphaned directives as they appear in the code outside of any OpenMP parallel region. However, at run time, they will be used by any pre-existing OpenMP thread team the way you want them to.

此外,我在每个循环中都放置了schedule( static )子句.这对于代码正确性而言不是必需的,但这可以通过确保每个线程始终在函数和调用之间处理相同的索引来提高性能.

In addition, I've put a schedule( static ) clause to each of the loops. This isn't necessary for code correctness, but this can improve performance by ensuring that each thread always deals with the same indexes across functions and calls...

这篇关于避免线程创建openMP中的开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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