Repository: beehive-lab/tornadovm
Issue: TestArrays#testInitParallel on AMD - failed with '[REASON] [ERROR] reset() was called after warmup()'

When running `TestArrays#testInitParallel`  I get '[REASON] [ERROR] reset() was called after warmup()' exception thrown from `TornadoVM.compileTaskFromBytecodeToBinary`.

In fact, `TornadoTestBase.before` indeed reset ALL devices, but `TornadoVM.warmup` validates only one (VM-default ???) device. And if this device is different from SCHEDULE.TASK-device than it leads to an error (task device is changed via `s0.t0.device=0:1` command arg).


Changed files:
examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication1D.java
runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/TornadoTaskSchedule.java
runtime/src/main/java/uk/ac/manchester/tornado/runtime/graph/TornadoExecutionContext.java
runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/TaskMetaData.java
examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication2D.java
runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/AbstractMetaData.java

diff --git a/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication1D.java b/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication1D.java
index 403293a56..f36a5bf4c 100644
--- a/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication1D.java
+++ b/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication1D.java
@@ -20,11 +20,12 @@ package uk.ac.manchester.tornado.examples.compute;
 import java.util.Random;
 import java.util.stream.IntStream;
 
+import uk.ac.manchester.tornado.api.GridTask;
 import uk.ac.manchester.tornado.api.TaskSchedule;
 import uk.ac.manchester.tornado.api.WorkerGrid;
 import uk.ac.manchester.tornado.api.WorkerGrid1D;
-import uk.ac.manchester.tornado.api.GridTask;
 import uk.ac.manchester.tornado.api.annotations.Parallel;
+import uk.ac.manchester.tornado.api.enums.TornadoDeviceType;
 
 public class MatrixMultiplication1D {
 
@@ -110,8 +111,10 @@ public class MatrixMultiplication1D {
         String formatGPUFGlops = String.format("%.2f", gpuGigaFlops);
         String formatCPUFGlops = String.format("%.2f", cpuGigaFlops);
 
-        System.out.println("\\tCPU Execution: " + formatCPUFGlops + " GFlops, Total time = " + (endSequential - startSequential) + " ms");
-        System.out.println("\\tGPU Execution: " + formatGPUFGlops + " GFlops, Total Time = " + (end - start) + " ms");
+        TornadoDeviceType deviceType = t.getDevice().getDeviceType();
+
+        System.out.println("\\tSingle Threaded CPU Execution: " + formatCPUFGlops + " GFlops, Total time = " + (endSequential - startSequential) + " ms");
+        System.out.println("\\tTornadoVM Execution on " + deviceType + " (Accelerated): " + formatGPUFGlops + " GFlops, Total Time = " + (end - start) + " ms");
         System.out.println("\\tSpeedup: " + speedup + "x");
         System.out.println("\\tVerification " + verify(matrixC, resultSeq, size));
     }
diff --git a/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication2D.java b/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication2D.java
index f9b3877f1..452712e7a 100644
--- a/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication2D.java
+++ b/examples/src/main/java/uk/ac/manchester/tornado/examples/compute/MatrixMultiplication2D.java
@@ -17,14 +17,15 @@
  */
 package uk.ac.manchester.tornado.examples.compute;
 
-import java.util.Random;
-
 import uk.ac.manchester.tornado.api.GridTask;
 import uk.ac.manchester.tornado.api.TaskSchedule;
 import uk.ac.manchester.tornado.api.WorkerGrid;
 import uk.ac.manchester.tornado.api.WorkerGrid2D;
 import uk.ac.manchester.tornado.api.annotations.Parallel;
 import uk.ac.manchester.tornado.api.collections.types.Matrix2DFloat;
+import uk.ac.manchester.tornado.api.enums.TornadoDeviceType;
+
+import java.util.Random;
 
 public class MatrixMultiplication2D {
 
@@ -113,8 +114,9 @@ public class MatrixMultiplication2D {
         String formatGPUFGlops = String.format("%.2f", gpuGigaFlops);
         String formatCPUFGlops = String.format("%.2f", cpuGigaFlops);
 
-        System.out.println("\\tCPU Execution: " + formatCPUFGlops + " GFlops, Total time = " + (endSequential - startSequential) + " ms");
-        System.out.println("\\tGPU Execution: " + formatGPUFGlops + " GFlops, Total Time = " + (end - start) + " ms");
+        TornadoDeviceType deviceType = t.getDevice().getDeviceType();
+        System.out.println("\\tSingle Threaded CPU Execution: " + formatCPUFGlops + " GFlops, Total time = " + (endSequential - startSequential) + " ms");
+        System.out.println("\\tTornadoVM Execution on " + deviceType + " (Accelerated): " + formatGPUFGlops + " GFlops, Total Time = " + (end - start) + " ms");
         System.out.println("\\tSpeedup: " + speedup + "x");
         System.out.println("\\tVerification " + verify(matrixC, resultSeq, size));
     }
diff --git a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/graph/TornadoExecutionContext.java b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/graph/TornadoExecutionContext.java
index e0a00adea..4075442f4 100644
--- a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/graph/TornadoExecutionContext.java
+++ b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/graph/TornadoExecutionContext.java
@@ -257,6 +257,7 @@ public class TornadoExecutionContext {
      *
      * @return {@link TornadoAcceleratorDevice}
      */
+    @Deprecated
     public TornadoAcceleratorDevice getDefaultDevice() {
         return meta.getLogicDevice();
     }
diff --git a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/TornadoTaskSchedule.java b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/TornadoTaskSchedule.java
index 74c96a677..136dd0c6a 100644
--- a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/TornadoTaskSchedule.java
+++ b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/TornadoTaskSchedule.java
@@ -252,9 +252,17 @@ public class TornadoTaskSchedule implements AbstractTaskGraph {
         return executionContext.getTask(id);
     }
 
+    /**
+     * Returns the device attached to any of the tasks. Currently, TornadoVM
+     * executes all tasks that belong to the same task-schedule on the same device.
+     * Therefore, this call returns the device attached to the first task or the
+     * first task is planning to be executed.
+     * 
+     * @return {@link TornadoDevice}
+     */
     @Override
     public TornadoDevice getDevice() {
-        return meta().getLogicDevice();
+        return executionContext.getDeviceFirstTask();
     }
 
     private void triggerRecompile() {
diff --git a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/AbstractMetaData.java b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/AbstractMetaData.java
index a9a090942..90441abdd 100644
--- a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/AbstractMetaData.java
+++ b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/AbstractMetaData.java
@@ -99,6 +99,7 @@ public abstract class AbstractMetaData implements TaskMetaDataInterface {
      * Set a device in the default driver in Tornado.
      *
      * @param device
+     *            {@link TornadoDevice}
      */
     public void setDevice(TornadoDevice device) {
         this.driverIndex = device.getDriverIndex();
@@ -113,7 +114,9 @@ public abstract class AbstractMetaData implements TaskMetaDataInterface {
      * Set a device from a specific Tornado driver.
      *
      * @param driverIndex
+     *            Driver Index
      * @param device
+     *            {@link TornadoAcceleratorDevice}
      */
     public void setDriverDevice(int driverIndex, TornadoAcceleratorDevice device) {
         this.driverIndex = driverIndex;
diff --git a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/TaskMetaData.java b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/TaskMetaData.java
index 2f538d394..b4dae8bdf 100644
--- a/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/TaskMetaData.java
+++ b/runtime/src/main/java/uk/ac/manchester/tornado/runtime/tasks/meta/TaskMetaData.java
@@ -234,9 +234,7 @@ public class TaskMetaData extends AbstractMetaData {
 
     @Override
     public TornadoAcceleratorDevice getLogicDevice() {
-        if (scheduleMetaData.isDeviceManuallySet()) {
-            return scheduleMetaData.getLogicDevice();
-        } else if (scheduleMetaData.isDeviceDefined() && !isDeviceDefined()) {
+        if (scheduleMetaData.isDeviceManuallySet() || (scheduleMetaData.isDeviceDefined() && !isDeviceDefined())) {
             return scheduleMetaData.getLogicDevice();
         }
         return super.getLogicDevice();