digitalmars.D.learn - Magic infinite loop inside foreach
- MrSmith (24/24) Jan 30 2014 I have some function which does some matrix calculations and
- MrSmith (5/29) Jan 30 2014 Somehow if i comment out
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/38) Jan 30 2014 Does that mutate temp in any way? If so, probably the change is
- Joseph Rushton Wakeling (9/12) Jan 30 2014 That does rather suggest that it's that method that is causing things to...
I have some function which does some matrix calculations and prints them. It is actually calculationg determinant of the matrix and i need to call those functions several times in the loop, until i get the final result. Here is the code. void solveAndPrint(T : CoeffMatrix!(ElementType, ElementType), ElementType)(T _matrix) { auto matrix = _matrix; TempMatrix!ElementType temp; foreach(_; 0..3) //3 is just for debug { writeln(matrix); temp = makeTemp(matrix); writeln(temp); matrix = solveTemp(temp); writeln("done1"); stdout.flush(); } writeln("done2"); stdout.flush(); } The problem is that after 'done1' is printed it is running infinitely consuming all the processor just like if has infinite loop inside. done2 is never printed.
Jan 30 2014
On Thursday, 30 January 2014 at 22:56:46 UTC, MrSmith wrote:I have some function which does some matrix calculations and prints them. It is actually calculationg determinant of the matrix and i need to call those functions several times in the loop, until i get the final result. Here is the code. void solveAndPrint(T : CoeffMatrix!(ElementType, ElementType), ElementType)(T _matrix) { auto matrix = _matrix; TempMatrix!ElementType temp; foreach(_; 0..3) //3 is just for debug { writeln(matrix); temp = makeTemp(matrix); writeln(temp); matrix = solveTemp(temp); writeln("done1"); stdout.flush(); } writeln("done2"); stdout.flush(); } The problem is that after 'done1' is printed it is running infinitely consuming all the processor just like if has infinite loop inside. done2 is never printed.Somehow if i comment out //matrix = solveTemp(temp); it works, but this method works fine, and after it done1 is printed. Strange.
Jan 30 2014
On 01/30/2014 03:08 PM, MrSmith wrote:On Thursday, 30 January 2014 at 22:56:46 UTC, MrSmith wrote:Does that mutate temp in any way? If so, probably the change is affecting how solveTemp() works the next time.I have some function which does some matrix calculations and prints them. It is actually calculationg determinant of the matrix and i need to call those functions several times in the loop, until i get the final result. Here is the code. void solveAndPrint(T : CoeffMatrix!(ElementType, ElementType), ElementType)(T _matrix) { auto matrix = _matrix; TempMatrix!ElementType temp; foreach(_; 0..3) //3 is just for debug { writeln(matrix); temp = makeTemp(matrix); writeln(temp); matrix = solveTemp(temp);Aliwriteln("done1"); stdout.flush(); } writeln("done2"); stdout.flush(); } The problem is that after 'done1' is printed it is running infinitely consuming all the processor just like if has infinite loop inside. done2 is never printed.Somehow if i comment out //matrix = solveTemp(temp); it works, but this method works fine, and after it done1 is printed. Strange.
Jan 30 2014
On 31/01/14 00:08, MrSmith wrote:Somehow if i comment out //matrix = solveTemp(temp); it works, but this method works fine, and after it done1 is printed. Strange.That does rather suggest that it's that method that is causing things to get stuck. Can you share what's inside it? And when you say "this method works fine", do you mean that you've manually tested it with the temp variable that goes in before it hangs? Could it be that whatever printout you're doing of variables like matrix is missing some tiny differences -- floating-point rounding errors? -- that are responsible for the transformations inside makeTemp or solveTemp behaving wrongly and therefore (in the latter case) getting stuck?
Jan 30 2014
On Thursday, 30 January 2014 at 23:45:14 UTC, Joseph Rushton Wakeling wrote:On 31/01/14 00:08, MrSmith wrote:Here is complete code (excluding rational.d by DSimcha) https://gist.github.com/MrSmith33/8732520 It prints: foreach start after makeTemp solveTemp start before solveTemp return foreach end and then stucks. As you can see it goes through solveTemp method and stops at the foreach end. So, i can not see any reason why it goes infinite there.Somehow if i comment out //matrix = solveTemp(temp); it works, but this method works fine, and after it done1 is printed. Strange.That does rather suggest that it's that method that is causing things to get stuck. Can you share what's inside it? And when you say "this method works fine", do you mean that you've manually tested it with the temp variable that goes in before it hangs? Could it be that whatever printout you're doing of variables like matrix is missing some tiny differences -- floating-point rounding errors? -- that are responsible for the transformations inside makeTemp or solveTemp behaving wrongly and therefore (in the latter case) getting stuck?
Jan 31 2014
On Friday, 31 January 2014 at 14:05:10 UTC, MrSmith wrote:On Thursday, 30 January 2014 at 23:45:14 UTC, Joseph Rushton Wakeling wrote:Found it! First i was thinkig that it was stuck at the end of foreach. Than i placed print at the start of the loop. Turned out that problem was in makeTemp in pow function which i poorly implemeneted and it looped infinitely. ElementType pow(ElementType, I)(ElementType number, I power) if (isIntegral!I) { ElementType result = number; writeln("pow start ", number, " ", power); stdout.flush(); foreach(_; 0..power-1) number *= number; writeln("pow end"); stdout.flush(); return number; } that printed: pow start 0/1 0 I've completely forgot about power of 0. Thanks guys!On 31/01/14 00:08, MrSmith wrote:Here is complete code (excluding rational.d by DSimcha) https://gist.github.com/MrSmith33/8732520 It prints: foreach start after makeTemp solveTemp start before solveTemp return foreach end and then stucks. As you can see it goes through solveTemp method and stops at the foreach end. So, i can not see any reason why it goes infinite there.Somehow if i comment out //matrix = solveTemp(temp); it works, but this method works fine, and after it done1 is printed. Strange.That does rather suggest that it's that method that is causing things to get stuck. Can you share what's inside it? And when you say "this method works fine", do you mean that you've manually tested it with the temp variable that goes in before it hangs? Could it be that whatever printout you're doing of variables like matrix is missing some tiny differences -- floating-point rounding errors? -- that are responsible for the transformations inside makeTemp or solveTemp behaving wrongly and therefore (in the latter case) getting stuck?
Jan 31 2014