Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/20/13 16:51:58 (12 years ago)
Author:
mkommend
Message:

#2024: Changed parallel engine to use parallel.foreach instead of spawning task manually to enable more options for parallelization.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ParallelEngine/3.3/ParallelEngine.cs

    r7259 r9323  
    3737  public class ParallelEngine : Engine {
    3838    private CancellationToken cancellationToken;
     39    private ParallelOptions parallelOptions;
     40
     41    [Storable(DefaultValue = -1)]
     42    private int degreeOfParallelism;
     43    public int DegreeOfParallelism {
     44      get { return degreeOfParallelism; }
     45      set {
     46        if (degreeOfParallelism != value) {
     47          degreeOfParallelism = value;
     48          OnDegreeOfParallelismChanged();
     49        }
     50      }
     51    }
    3952
    4053    [StorableConstructor]
    4154    protected ParallelEngine(bool deserializing) : base(deserializing) { }
    42     protected ParallelEngine(ParallelEngine original, Cloner cloner) : base(original, cloner) { }
     55    protected ParallelEngine(ParallelEngine original, Cloner cloner)
     56      : base(original, cloner) {
     57      this.DegreeOfParallelism = original.DegreeOfParallelism;
     58    }
    4359    public ParallelEngine() : base() { }
    4460
     
    4763    }
    4864
     65    public event EventHandler DegreeOfParallelismChanged;
     66    protected void OnDegreeOfParallelismChanged() {
     67      var handler = DegreeOfParallelismChanged;
     68      if (handler != null) handler(this, EventArgs.Empty);
     69    }
     70
     71
    4972    protected override void Run(CancellationToken cancellationToken) {
    5073      this.cancellationToken = cancellationToken;
     74      parallelOptions = new ParallelOptions();
     75      parallelOptions.MaxDegreeOfParallelism = DegreeOfParallelism;
    5176      Run(ExecutionStack);
    5277    }
     
    6590          coll = (OperationCollection)next;
    6691          if (coll.Parallel) {
    67             Task[] tasks = new Task[coll.Count];
    6892            Stack<IOperation>[] stacks = new Stack<IOperation>[coll.Count];
    6993            for (int i = 0; i < coll.Count; i++) {
    7094              stacks[i] = new Stack<IOperation>();
    7195              stacks[i].Push(coll[i]);
    72               tasks[i] = Task.Factory.StartNew(Run, stacks[i], cancellationToken);
    7396            }
    7497            try {
    75               Task.WaitAll(tasks);
     98              Parallel.ForEach(stacks, parallelOptions, Run);
    7699            }
    77100            catch (AggregateException ex) {
Note: See TracChangeset for help on using the changeset viewer.