0

The job is to convert the values from the src array and write the new values to the dst array.

When I create ForkJoinTasks in a while loop

val height: Int = 4;
val numTasks: Int = 2;

var tasks = scala.collection.mutable.ListBuffer.empty[ForkJoinTask[Unit]]

val jump: Int = src.height / numTasks
var from: Int = 0;

while (from < height) {
  val end: Int = height.min(from + jump);
  val t: ForkJoinTask[Unit] = task {
    run(src, dst, from, end) // (2,2), (2,1), what is happening?
  }
  from = end
}

for (t <- tasks.toList) {
  t.join()
}

Then strangely, the run function takes the (from, end) arguments value as (2, 2). But If I split it manually into two tasks, then it works normally, as (0,1) and (1,2).

val t1 = task {
  run(src, dst, 0, height / 2); // (0, 1)
}
val t2 = task {
  run(src, dst, height / 2, height); // (1, 2)
}

t1.join()
t2.join()

I am having hard time to figure out what is going on. This is my very first Scala program, so I might be missing something very trivial. Any comment would be appreciated.

Thank you.

user557583
  • 33
  • 5

1 Answers1

0

Removing references to src or dst your code seems ok as returns ranges

0 2
2 4

import shapeless.PolyDefns.~>
import shapeless.{ HList, HNil }
import io.netty.util.internal.chmv8.ForkJoinTask

object main extends App {

  val height: Int = 4;
  val numTasks: Int = 2;

  var tasks = scala.collection.mutable.ListBuffer.empty[ForkJoinTask[Unit]]

  val jump: Int = height / numTasks
  var from: Int = 0;

  while (from < height) {
    val end: Int = height.min(from + jump);
    val t: ForkJoinTask[Unit] =  {
      run( from, end) // (2,2), (2,1), what is happening?
    }
    from = end
  }

  for (t <- tasks.toList) {
    t.join()
  }


  def run (from: Int, end:Int) ={
    println(s"$from $end")
    null
  }

}
Krlos
  • 140
  • 8
  • I realized why. Having from as var made it referenced so when its value changes in the loop, the value passed to the function run also changes. In your example such will not be caught, but if you do heavier calculation in run function, you will notice it. – user557583 Sep 23 '16 at 07:49