question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't compile internal While loop or For loop with break

See original GitHub issue

Hi,

I used Aparapi before for GPU calculations Java on GPU: Pricing options with Monte Carlo simulation. I was excited to hear about Torando VM and wanted to repeat my calculations on FPGA.

I noticed an issue when moving from Aparapi to Tornado VM. I have a Monte Carlo simulation (n elements with m calculations/simulations per each). Seems that Tornado can’t compile simulations with breaks (while loop or for loop with break).

Maybe Tornado thinks that I modify incoming arrays, but this isn’t the case.

Do you have any suggestion about how to fix or debug this issue, please?

public static void mapCalcMc(float[] arrS, float[] arrB, int iterPerElement, int logicIterNum, float[] results) {
        int arrLength = arrS.length;
        int currentIter = 0;

        for (@Parallel int i = 0; i < arrLength; i++) {
            for (@Parallel int j = 0; j < iterPerElement; j++) {
                int k = 0;
                float curA = arrS[i];
                float b = arrB[i];
                while (k < logicIterNum
                        && curA > b//Condition that can't be compiled
                ) {
                    k++;
                }

                results[currentIter] = 0.0f;
                currentIter++;
            }
        }
    }

UPD:

  1. I run code on AMD 4Gb GPU locally. Tornado works for me both on CPU and GPU with other tasks.
  2. Code above is “Minimum Breaking Example”. I tried to remove all non-essential logic.
  3. Here is the code to trigger task:
TaskSchedule task = new TaskSchedule("s0")
    .task("map", MonteCarloTornadoGPU::mapCalcMc, new float[10], new float[10],
      10,
      100, // It looks crazy but if I replace 100 wit 10 code works. 
           // This param is used for number of iterations in no-op loop 
       new float[100])
     .streamOut(gpuInterimMapResults);
task.execute();

I found a strange behavior, when I increase number of iterations in no-op while loop, app crashes (see comment to task code).

Attaching kernel out. kernel.txt

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AlexeyPirogovcommented, Apr 1, 2021

I’m sorry, seems I didn’t provide enough information in my original post. I extended post with extra details.

0reactions
gigiblendercommented, Dec 20, 2021

@AlexeyPirogov

Thank you for reporting this. It looks that this issue is now solved.

The kernel Tornado generates is printed below:

#pragma OPENCL EXTENSION cl_khr_fp64 : enable  
#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable  
__kernel void mapCalcMc(__global uchar *_heap_base, ulong _frame_base, __constant uchar *_constant_region, __local uchar *_local_region, __global int *_atomics)
{
  long l_7, l_8, l_21, l_6, l_19, l_20; 
  ulong ul_11, ul_2, ul_1, ul_0, ul_22, ul_9; 
  int i_24, i_25, i_26, i_27, i_16, i_17, i_18, i_23, i_13, i_14, i_15, i_3, i_4, i_5; 
  float f_12, f_10; 

  __global ulong *_frame = (__global ulong *) &_heap_base[_frame_base];


  // BLOCK 0
  ul_0  =  (ulong) _frame[3];
  ul_1  =  (ulong) _frame[4];
  ul_2  =  (ulong) _frame[7];
  i_3  =  get_global_id(1);
  // BLOCK 1 MERGES [0 11 ]
  i_4  =  0;
  i_5  =  i_3;
  for(;i_5 < 10;)
  {
    // BLOCK 2
    l_6  =  (long) i_5;
    l_7  =  l_6 << 2;
    l_8  =  l_7 + 24L;
    ul_9  =  ul_1 + l_8;
    f_10  =  *((__global float *) ul_9);
    ul_11  =  ul_0 + l_8;
    f_12  =  *((__global float *) ul_11);
    i_13  =  get_global_id(0);
    // BLOCK 3 MERGES [2 10 ]
    i_14  =  i_4;
    i_15  =  i_13;
    for(;i_15 < 10;)
    {
      // BLOCK 4
      // BLOCK 5 MERGES [4 7 ]
      i_16  =  0;
      for(;i_16 < 100;)
      {
        // BLOCK 6
        i_17  =  isless(f_10, f_12);
        if(i_17 == 1)
        {
          // BLOCK 7
          i_18  =  i_16 + 1;
          i_16  =  i_18;
        }  // B7
        else
        {
          // BLOCK 8
          break;
        }  // B8
        // BLOCK 9
      }  // B9
      // BLOCK 10 MERGES [9 8 ]
      l_19  =  (long) i_14;
      l_20  =  l_19 << 2;
      l_21  =  l_20 + 24L;
      ul_22  =  ul_2 + l_21;
      *((__global float *) ul_22)  =  0.0F;
      i_23  =  get_global_size(0);
      i_24  =  i_23 + i_15;
      i_25  =  i_14 + 1;
      i_14  =  i_25;
      i_15  =  i_24;
    }  // B10
    // BLOCK 11
    i_26  =  get_global_size(1);
    i_27  =  i_26 + i_5;
    i_4  =  i_14;
    i_5  =  i_27;
  }  // B11
  // BLOCK 12
  return;
}  //  kernel

I will close this issue for now. If anything else shows up, please let us know with a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

If inside a while loop, can't break out of the while loop
"break" can only get you out of 1 loop. Your most fundamental problem is the infinite loops. To have one infinite loop is...
Read more >
Can't break the while loop bash - Unix & Linux Stack Exchange
1 Answer. Save this answer. Show activity on this post. Since the value of var is never modified over the course of the...
Read more >
Using Break and Continue Statements When Working with ...
While both loops iterate 5 times, each has a conditional if statement with a break statement. The outer loop will break if the...
Read more >
JavaScript break Statement - W3Schools
In in a loop, it breaks out of the loop and continues executing the code after the loop (if any). Using Lables. The...
Read more >
Break statement in Java - GeeksforGeeks
Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found