[英]stackoverflow in multithreaded program
I am facing a desing issue. 我正面临设计问题。 function 1 and 2 call each other few hundred of times,the recursion is not infinite.
函数1和2相互调用数百次,递归不是无限的。 while running the program,it meet a overflow condition.setting openmp stacksize is also not working.
在运行程序时,它满足溢出条件。设置openmp stacksize也不起作用。
#pragma omp parallel
{
//some variable private to each thread
//some processing
//call function1( array )
}
function1( array ){
//some variable
//some processing
//call function2( array )
//return
}
function2( array ){
//some variable
//some processing
//if condition fail return
//else call function1()
//return
} }
If calling the other function is truly the last thing in each function, you could manually inline function2 into function1 and turn the recursion into a loop: 如果确实调用另一个函数是每个函数中的最后一件事,则可以手动将function2内联到function1并将递归转换为循环:
function1(array) {
while(1) {
// some variables (from function 1)
// some processing (from function 1)
// some variables (from function 2)
// some processing (from function 2)
if condition fail break;
// loop back 'calling' function 1 again
}
}
It may be possible to achieve the same effect by moving the definition of function2
before function1
and declaring it inline -- the compiler's optimizer may then be able to inline the function and turn the tail-recursive calls into branches. 它可能会通过移动的定义,以达到同样的效果
function2
之前function1
和内联宣布它-编译器的优化器则能够内联函数,把尾递归调用到分支机构。
If your functions are more complex than you've described (and not tail recursive) then this won't work simply, though you may be able to transform things a bit to make it tail recursive. 如果您的函数比您所描述的要复杂(而不是尾部递归),那么这将无法简单地工作,尽管您可以稍微改变一下内容使其尾部递归。
Your best option is to have a single function. 最好的选择是只有一个功能。
In your comment, you say: «It's not possible because the functions call each other» . 在您的评论中,您说: «不可能,因为函数彼此调用» 。
But this is precisely why you're getting a stack overflow, so this can be considered as bad design... 但这正是为什么堆栈会溢出的原因,因此可以认为这是错误的设计...
For what I can see, you do not need separate functions. 据我所知,您不需要单独的功能。
You could easily solve the issue with, for instance: 您可以轻松解决问题,例如:
#pragma omp parallel
{
// Some variables private to each thread
// Some processing
// call function1( array )
}
function1( array )
{
start:
{
// Some variables
// Some processing
}
{
// Some variables
// Some processing
// if condition fail return
// else goto start
}
}
I know lot's of people says goto
statements are bad design choices, but in your situation I don't think it's the case... And it will just save your stack space. 我知道很多人说
goto
语句是错误的设计选择,但是在您的情况下,我认为情况并非如此……这只会节省您的堆栈空间。
Anyway, it will still be better than a non-working piece of code... 无论如何,它仍然比不起作用的代码要好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.