[15342] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[15347] | 24 | using System.Linq;
|
---|
[15342] | 25 | using System.Threading;
|
---|
| 26 | using HeuristicLab.Clients.Hive;
|
---|
| 27 | using HeuristicLab.Clients.Hive.Jobs;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.HiveDrain {
|
---|
| 33 | public class JobTaskOneFileDownloader {
|
---|
| 34 | public String RootLocation { get; set; }
|
---|
| 35 |
|
---|
| 36 | public Job ParentJob { get; set; }
|
---|
| 37 |
|
---|
| 38 | private ILog log;
|
---|
| 39 |
|
---|
| 40 | private RunCollection results = new RunCollection();
|
---|
| 41 |
|
---|
| 42 | private static ConcurrentTaskDownloader<ItemTask> downloader =
|
---|
| 43 | new ConcurrentTaskDownloader<ItemTask>(HeuristicLabHiveDrainApplication.MaxParallelDownloads, HeuristicLabHiveDrainApplication.MaxParallelDownloads);
|
---|
| 44 |
|
---|
| 45 | private static int jobCount = 0;
|
---|
| 46 |
|
---|
| 47 | private static bool endReached = false;
|
---|
| 48 |
|
---|
| 49 | private ManualResetEvent allJobsFinished = new ManualResetEvent(false);
|
---|
| 50 |
|
---|
| 51 | private Semaphore limitSemaphore = null;
|
---|
| 52 |
|
---|
| 53 | static JobTaskOneFileDownloader() {
|
---|
| 54 | downloader.ExceptionOccured += downloader_ExceptionOccured;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | static void downloader_ExceptionOccured(object sender, HeuristicLab.Common.EventArgs<Exception> e) {
|
---|
| 58 | HiveDrainMainWindow.Log.LogMessage(DateTime.Now.ToShortTimeString() + " ### Exception occured: " + e.Value);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public JobTaskOneFileDownloader(string path, Job parentJob, Semaphore sem, ILog log) {
|
---|
| 62 | RootLocation = path + ".hl";
|
---|
| 63 | ParentJob = parentJob;
|
---|
| 64 | limitSemaphore = sem;
|
---|
| 65 | this.log = log;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public void Start() {
|
---|
| 69 | results = new RunCollection();
|
---|
| 70 |
|
---|
[15347] | 71 | var allTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(ParentJob.Id));
|
---|
| 72 | var totalJobCount = allTasks.Count();
|
---|
| 73 | var optimizers = new List<IOptimizer>();
|
---|
| 74 | var finishedCount = -1;
|
---|
| 75 | using (var downloader = new TaskDownloader(allTasks.Select(x => x.Id))) {
|
---|
| 76 | downloader.StartAsync();
|
---|
[15342] | 77 |
|
---|
[15347] | 78 | while (!downloader.IsFinished) {
|
---|
| 79 | if (finishedCount != downloader.FinishedCount) {
|
---|
| 80 | finishedCount = downloader.FinishedCount;
|
---|
| 81 | log.LogMessage(string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", finishedCount, totalJobCount));
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[15342] | 84 |
|
---|
[15347] | 85 | IDictionary<Guid, HiveTask> allHiveTasks = downloader.Results;
|
---|
| 86 | log.LogMessage("Building hive job tree...");
|
---|
| 87 | var parentTasks = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
|
---|
[15342] | 88 |
|
---|
[15347] | 89 | foreach (var parentTask in parentTasks) {
|
---|
| 90 | BuildHiveJobTree(parentTask, allTasks, allHiveTasks);
|
---|
[15342] | 91 |
|
---|
[15347] | 92 | var optimizerTask = parentTask.ItemTask as OptimizerTask;
|
---|
[15342] | 93 |
|
---|
[15347] | 94 | if (optimizerTask != null) {
|
---|
| 95 | optimizers.Add(optimizerTask.Item);
|
---|
[15342] | 96 | }
|
---|
| 97 | }
|
---|
[15347] | 98 | }
|
---|
| 99 | if (!optimizers.Any()) return;
|
---|
| 100 | IStorableContent storable;
|
---|
| 101 | if (optimizers.Count > 1) {
|
---|
| 102 | var experiment = new Experiment();
|
---|
| 103 | experiment.Optimizers.AddRange(optimizers);
|
---|
| 104 | storable = experiment;
|
---|
| 105 | } else {
|
---|
| 106 | var optimizer = optimizers.First();
|
---|
| 107 | storable = optimizer as IStorableContent;
|
---|
| 108 | }
|
---|
| 109 | if (storable != null) {
|
---|
| 110 | log.LogMessage(string.Format("Save job as {0}", RootLocation));
|
---|
| 111 | ContentManager.Save(storable, RootLocation, true);
|
---|
| 112 | } else {
|
---|
| 113 | log.LogMessage(string.Format("Could not save job, content is not storable."));
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
[15342] | 116 |
|
---|
[15347] | 117 | private static void BuildHiveJobTree(HiveTask parentHiveTask, IEnumerable<LightweightTask> allTasks, IDictionary<Guid, HiveTask> allHiveTasks) {
|
---|
| 118 | IEnumerable<LightweightTask> childTasks = from job in allTasks
|
---|
| 119 | where job.ParentTaskId.HasValue && job.ParentTaskId.Value == parentHiveTask.Task.Id
|
---|
| 120 | orderby job.DateCreated ascending
|
---|
| 121 | select job;
|
---|
| 122 | foreach (LightweightTask task in childTasks) {
|
---|
| 123 | HiveTask childHiveTask = allHiveTasks[task.Id];
|
---|
| 124 | BuildHiveJobTree(childHiveTask, allTasks, allHiveTasks);
|
---|
| 125 | parentHiveTask.AddChildHiveTask(childHiveTask);
|
---|
| 126 | }
|
---|
[15342] | 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|