Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/26/12 11:12:57 (10 years ago)
Author:
ascheibe
Message:

#1950

  • added more aggressive locking so that the views don't read run collections that get modified in the meantime
  • start downloading of tasks after the job has been uploaded completely
  • fixed exceptions that got thrown when waiting for the threads that upload the tasks
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Clients.Hive/3.3/HiveTasks/OptimizerHiveTask.cs

    r8884 r8939  
    3131namespace HeuristicLab.Clients.Hive {
    3232  public class OptimizerHiveTask : HiveTask<OptimizerTask> {
    33 
    34     Object batchRunLocker = new Object();
    35 
    3633    #region Constructors and Cloning
    3734    public OptimizerHiveTask() { }
     
    6057    protected override void UpdateChildHiveTasks() {
    6158      base.UpdateChildHiveTasks();
    62       if (Task != null && syncTasksWithOptimizers) {
    63         if (!ItemTask.ComputeInParallel) {
    64           this.childHiveTasks.Clear();
    65         } else {
    66           if (ItemTask.Item is Optimization.Experiment) {
    67             Optimization.Experiment experiment = (Optimization.Experiment)ItemTask.Item;
    68             foreach (IOptimizer childOpt in experiment.Optimizers) {
    69               var optimizerHiveTask = new OptimizerHiveTask(childOpt);
    70               optimizerHiveTask.Task.Priority = Task.Priority; //inherit priority from parent
    71               this.childHiveTasks.Add(optimizerHiveTask);
    72             }
    73           } else if (ItemTask.Item is Optimization.BatchRun) {
    74             Optimization.BatchRun batchRun = ItemTask.OptimizerAsBatchRun;
    75             if (batchRun.Optimizer != null) {
    76               while (this.childHiveTasks.Count < batchRun.Repetitions) {
    77                 var optimizerHiveTask = new OptimizerHiveTask(batchRun.Optimizer);
    78                 optimizerHiveTask.Task.Priority = Task.Priority;
     59      childHiveTasksLock.EnterWriteLock();
     60      try {
     61        if (Task != null && syncTasksWithOptimizers) {
     62          if (!ItemTask.ComputeInParallel) {
     63            this.childHiveTasks.Clear();
     64          } else {
     65            if (ItemTask.Item is Optimization.Experiment) {
     66              Optimization.Experiment experiment = (Optimization.Experiment)ItemTask.Item;
     67              foreach (IOptimizer childOpt in experiment.Optimizers) {
     68                var optimizerHiveTask = new OptimizerHiveTask(childOpt);
     69                optimizerHiveTask.Task.Priority = Task.Priority; //inherit priority from parent
    7970                this.childHiveTasks.Add(optimizerHiveTask);
    8071              }
    81               while (this.childHiveTasks.Count > batchRun.Repetitions) {
    82                 this.childHiveTasks.Remove(this.childHiveTasks.Last());
     72            } else if (ItemTask.Item is Optimization.BatchRun) {
     73              Optimization.BatchRun batchRun = ItemTask.OptimizerAsBatchRun;
     74              if (batchRun.Optimizer != null) {
     75                while (this.childHiveTasks.Count < batchRun.Repetitions) {
     76                  var optimizerHiveTask = new OptimizerHiveTask(batchRun.Optimizer);
     77                  optimizerHiveTask.Task.Priority = Task.Priority;
     78                  this.childHiveTasks.Add(optimizerHiveTask);
     79                }
     80                while (this.childHiveTasks.Count > batchRun.Repetitions) {
     81                  this.childHiveTasks.Remove(this.childHiveTasks.Last());
     82                }
    8383              }
    8484            }
    8585          }
    8686        }
     87      }
     88      finally {
     89        childHiveTasksLock.ExitWriteLock();
    8790      }
    8891    }
     
    223226    /// </summary>
    224227    private void UpdateOptimizerInBatchRun(BatchRun batchRun, OptimizerTask optimizerTask) {
    225       if (batchRun.Optimizer == null) {
    226         batchRun.Optimizer = (IOptimizer)optimizerTask.Item; // only set the first optimizer as Optimizer. if every time the Optimizer would be set, the runs would be cleared each time
    227       }
    228       lock (batchRunLocker) {
     228      itemTaskLock.EnterWriteLock();
     229      try {
     230        if (batchRun.Optimizer == null) {
     231          batchRun.Optimizer = (IOptimizer)optimizerTask.Item; // only set the first optimizer as Optimizer. if every time the Optimizer would be set, the runs would be cleared each time
     232        }
    229233        foreach (IRun run in optimizerTask.Item.Runs) {
    230234          if (!batchRun.Runs.Contains(run)) {
     
    234238        }
    235239      }
     240      finally {
     241        itemTaskLock.ExitWriteLock();
     242      }
    236243    }
    237244
     
    241248    /// </summary>
    242249    private void UpdateOptimizerInExperiment(Optimization.Experiment experiment, OptimizerTask optimizerTask) {
    243       if (optimizerTask.IndexInParentOptimizerList < 0)
    244         throw new IndexOutOfRangeException("IndexInParentOptimizerList must be equal or greater than zero! The Task is invalid and the optimizer-tree cannot be reassembled.");
    245 
    246       while (experiment.Optimizers.Count < optimizerTask.IndexInParentOptimizerList) {
    247         experiment.Optimizers.Add(new UserDefinedAlgorithm("Placeholder")); // add dummy-entries to Optimizers so that its possible to insert the optimizerTask at the correct position
    248       }
    249       if (experiment.Optimizers.Count < optimizerTask.IndexInParentOptimizerList + 1) {
    250         experiment.Optimizers.Add(optimizerTask.Item);
    251       } else {
    252         // if ComputeInParallel==true, don't replace the optimizer (except it is still a Placeholder)
    253         // this is because Jobs with ComputeInParallel get submitted to hive with their child-optimizers deleted
    254         if (!optimizerTask.ComputeInParallel || experiment.Optimizers[optimizerTask.IndexInParentOptimizerList].Name == "Placeholder") {
    255           experiment.Optimizers[optimizerTask.IndexInParentOptimizerList] = optimizerTask.Item;
    256         }
     250      itemTaskLock.EnterWriteLock();
     251      try {
     252        if (optimizerTask.IndexInParentOptimizerList < 0)
     253          throw new IndexOutOfRangeException("IndexInParentOptimizerList must be equal or greater than zero! The Task is invalid and the optimizer-tree cannot be reassembled.");
     254
     255        while (experiment.Optimizers.Count < optimizerTask.IndexInParentOptimizerList) {
     256          experiment.Optimizers.Add(new UserDefinedAlgorithm("Placeholder")); // add dummy-entries to Optimizers so that its possible to insert the optimizerTask at the correct position
     257        }
     258        if (experiment.Optimizers.Count < optimizerTask.IndexInParentOptimizerList + 1) {
     259          experiment.Optimizers.Add(optimizerTask.Item);
     260        } else {
     261          // if ComputeInParallel==true, don't replace the optimizer (except it is still a Placeholder)
     262          // this is because Jobs with ComputeInParallel get submitted to hive with their child-optimizers deleted
     263          if (!optimizerTask.ComputeInParallel || experiment.Optimizers[optimizerTask.IndexInParentOptimizerList].Name == "Placeholder") {
     264            experiment.Optimizers[optimizerTask.IndexInParentOptimizerList] = optimizerTask.Item;
     265          }
     266        }
     267      }
     268      finally {
     269        itemTaskLock.ExitWriteLock();
    257270      }
    258271    }
     
    365378    }
    366379
     380    public void ExecuteReadActionOnItemTask(Action action) {
     381      itemTaskLock.EnterReadLock();
     382      try {
     383        action();
     384      }
     385      finally {
     386        itemTaskLock.ExitReadLock();
     387      }
     388    }
     389
    367390    #region Helpers
    368391    /// <summary>
Note: See TracChangeset for help on using the changeset viewer.