Search code examples
javaalgorithmjob-schedulingsimgrid

SimGrid. Infinite loop when no free host can be found


There are hosts for executing tasks in locations.get(i). For dispatching tasks between them, firstly, I look for free hosts, then if there are free hosts, I send task to fastest host. But this algorithm starts to hang if there is no free host. How can I fix it?

for (int  i = 0; i < taskCount; i++){
  while (true) {
     ArrayList<Host> hostList = getFreeHost(locations.get(i));
        if (!hostList.isEmpty()){
           Host destination = getFastestHost(hostList);
           InstructionTask instructionTask = new InstructionTask(taskName.get(i), compSize.get(i), commSize.get(i), destination.getName());
           instructionTask.send(destination.getName());
           break;
        }
   }
}

Solution

  • If there is no free host, you are looping infinitely because no host will get freed at this precise time. If the list is empty, you want to sleep for a little time, like 0.1 second. That way, the work dispatching process will somehow wait until a machine gets freed.

    A better solution would be to not rely on active sleep as I propose, but to use a synchronization with a mutex or something. Do so if you cannot stand the very little unused time between the instant where the host gets free and the instant where the dispatching host notices after sleeping for a while.