delite: "while" loop incorrect behavior

27 views
Skip to first unread message

Alexander Filippov

unread,
Mar 6, 2015, 10:06:58 AM3/6/15
to opt...@googlegroups.com
Hello everyone,

I have discovered some unexplainable behavior of "while" loop construction in delite. 
Let us consider this simple piece of code

  val length = 3
  var state: Rep[DeliteArray[Int]] = darray_fromfunction(length, {x => 0})   //create array of given length filled with zeros

  while ( darray_reduce[Int]( state, {(a,b) => a+b}, 0) < 30) {              // check if sum of array elements is less then 30
    state = darray_fromfunction(state.length, { i => state(i) + 1} )         // add 1 to each element
  }

  state                                                                      //return state


I expect the loop iterate 9 times and get array with all elements equal to 9. But what I see in fact is (1,1,1), which means that the loop iterated only once.

If I modify the code in the following way:
    val length = 3
  var state: Rep[DeliteArray[Int]] = darray_fromfunction(length, {x => 0})   //create array of given length filled with zeros

  while ( darray_reduce[Int]( state, {(a,b) => a+b}, 0) < 30) {              // check if sum of array elements is less then 30
    val p: Rep[Unit] = println("New iteration")
    state = darray_fromfunction(state.length, { i => state(i) + 1} )         // add 1 to each element
  }

  state  

then the programm goes into infinite loop and print "New iteration" infinitely.

Could you please point me on what I am doing wrong? Or, in case of I happened to hit unsupported scenario, provide me with some workaround in order I can use "while" constructions?

Thank you in advance,
Alexander Filippov




Kevin Brown

unread,
Mar 6, 2015, 6:50:58 PM3/6/15
to opt...@googlegroups.com
Hi,

The problem is the explicit type of 'state' as a Rep.  Variables in lms have a special type Var[T] and an implicit function readVar: Var[T] => Rep[T] that we use to capture every read of the variable. 

By explicitly typing 'var state' as a Rep you're actually creating a stage-time variable (all writes to the variable occur during staging rather than at runtime), hence the very weird behavior.  If you leave off the type, scala should infer the type as a Var and the program should work correctly.

If you get a compile error of the form "foo is not defined on Var[T]" then the problem is likely that the implicit conversion is not being applied, which unfortunately is known to happen occasionally.
You can circumvent this problem by calling the implicit explicitly though, e.g.

val currentState = readVar(state)

Hope that helps.

--Kevin


--
You received this message because you are subscribed to the Google Groups "OptiML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to optiml+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Samoilov

unread,
Mar 9, 2015, 9:48:48 AM3/9/15
to opt...@googlegroups.com
Hi Kevin,

Thanks for your help!
Just figured out how to circumvent the problem with the weird 'while' behavior and your hint with readVar really helps.

Alexander
Reply all
Reply to author
Forward
0 new messages