public SimulationThread(ProjectHLK project, List <Simulation> simulations, HowLeakyOutputType outputType = HowLeakyOutputType.DailyBin) { var remapdict = BuildOutputsRemapDict(project.OutputsCSV.Split(',').ToList()); OutputType = outputType; // SQLiteFilename=Path.Combine(project.OutputsDirectory,$"Outputs.sqlite"); HLEngine = new HowLeakyEngine(project.OutputsCSV, remapdict); Simulations = simulations; Project = project; }
// public string SQLiteFilename{get;set;} public SimulationThread(ProjectHLK project, KeyValuePair <string, List <Simulation> > pair, HowLeakyOutputType outputType = HowLeakyOutputType.DailyBin) { var remapdict = BuildOutputsRemapDict(project.OutputsCSV.Split(',').ToList()); OutputType = outputType; // SQLiteFilename=Path.Combine(project.OutputsDirectory,$"{pair.Key}.sqlite"); HLEngine = new HowLeakyEngine(project.OutputsCSV, remapdict); Simulations = pair.Value; Project = project; }
private static void SimulationEngineExecute(ProjectHLK project, string outputs, int cores, HowLeakyOutputType outputType, int?targetindex, Action onSuccess = null) { var controller = new SimulationController(); var myprogress = new GlobalProgress(project.Simulations.Count); myprogress.StartSimulations(); var tokenSource = new CancellationTokenSource(); CancellationToken ct = tokenSource.Token; project.OutputsCSV = outputs; project.AddConsoleOutput("Running simulations... please wait..."); //var runSimulationsTask = Task.Run(() => Controller.Execute(CurrentProject, MyProgress)); var taskList = new System.Collections.Generic.List <Task>(); taskList.Add(Task.Factory.StartNew(delegate { controller.Execute(project, tokenSource, cores, targetindex, outputType, myprogress); }, tokenSource.Token)); // Pass same token to StartNew. Task.WaitAll(taskList.ToArray()); }
public void Execute(ProjectHLK project, CancellationTokenSource tokenSource, int cores, int?targetindex, HowLeakyOutputType outputType, GlobalProgress progress = null) { try { if (progress == null) { Progress = new GlobalProgress(project.Simulations.Count); } else { Progress = progress; } CancellationToken ct = tokenSource.Token; var simdict = project.GroupedSimulations; ct.ThrowIfCancellationRequested(); Progress.Max = targetindex == null?project.Simulations.Count:1; project.PrepareReferenceCounts(); if (project.GroupedSimulations.Count() > 1)//cores) { Parallel.ForEach(simdict.ToList(), new ParallelOptions() { MaxDegreeOfParallelism = cores }, simkeypair => { var worker = new SimulationThread(project, simkeypair, outputType); if (ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested(); } worker.Execute(Progress, ct); Debug.WriteLine(""); }); } else { var climatedatasets = project.DataFiles.ToList(); foreach (var datafile in climatedatasets) { datafile.OpenFull(); } var simulations = targetindex == null?project.Simulations:new List <Simulation>() { project.Simulations.FirstOrDefault(x => x.Index == (int)targetindex) }; Parallel.ForEach(simulations, new ParallelOptions() { MaxDegreeOfParallelism = cores }, sim => { var worker = new SimulationThread(project, new List <Simulation>() { sim }, outputType); if (ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested(); } worker.Execute(Progress, ct); }); } } catch (Exception ex) { throw ErrorLogger.CreateException(ex); } }