Example #1
0
        public void Train(DataSet data, Generation gen, IEnumerable <MixtureInfo> population, Action <TrainProgress> progress)
        {
            var hostInfo = RabbitHostInfo.FromAppSettings();

            using (var trainRequests = new WorkQueueProducer(new WorkQueueInfo(hostInfo, "TrainRequests", false)))
                using (var trainNotifications = new AsyncWorkQueueConsumer(new WorkQueueInfo(hostInfo, "TrainNotifications", false)))
                {
                    List <TrainRequest> outstanding = (from mixture in population
                                                       from chrom in mixture.Chromosomes
                                                       select new TrainRequest(mixture.MixtureId.ToString(), chrom)).ToList();

                    int total = outstanding.Count;

                    var orderedRequests = outstanding.OrderByDescending(x => {
                        return(Math.Pow(x.Chromosome.TrainingSizePct, 2)
                               * (x.Chromosome.NetworkType == NetworkType.Rnn ? x.Chromosome.RnnTrainingEpochs : 100));
                    }).ToList();

                    trainNotifications.Received += msg => {
                        var notification = (TrainNotification)msg;
                        outstanding.RemoveAll(x => x.MixtureId == notification.OriginalRequest.MixtureId &&
                                              x.Chromosome.OrderInMixture == notification.OriginalRequest.Chromosome.OrderInMixture);
                        progress(new TrainProgress(total - outstanding.Count, total));
                        trainNotifications.Ack(msg);
                    };

                    foreach (var req in orderedRequests)
                    {
                        trainRequests.Send(req);
                    }

                    Waiter.Wait(() => !outstanding.Any());
                }
        }
Example #2
0
        public void AsyncWorkQueue()
        {
            WithSync(() => {
                var wq = new WorkQueueInfo(new RabbitHostInfo("localhost"), "fooQueue", false);
                using (var c1 = new AsyncWorkQueueConsumer(wq))
                    using (var c2 = new AsyncWorkQueueConsumer(wq))
                        using (var p = new WorkQueueProducer(wq))
                        {
                            Lists.Repeat(1000, _ => p.Send(new TestMessage()));

                            var c1Count = 0;
                            var c2Count = 0;

                            c1.Received += msg => {
                                c1Count++;
                                c1.Ack(msg);
                            };

                            c2.Received += msg => {
                                c2Count++;
                                c2.Ack(msg);
                            };

                            Waiter.WaitOrDie(1000, () => c1Count + c2Count == 1000);
                            ((double)c1Count / c2Count).ShouldBeCloseTo(1.0, 0.05);
                        }
            });
        }
Example #3
0
        public void Run()
        {
            if (Cancelled)
            {
                return;
            }

            TaskSync = SyncContext.Current;
            Thread.CurrentThread.Name = "Slave Thread";

            var hostInfo = RabbitHostInfo.FromAppSettings();

            using (var notifications = new WorkQueueProducer(new WorkQueueInfo(hostInfo, "TrainNotifications", false)))
                using (var requests = new AsyncWorkQueueConsumer(new WorkQueueInfo(hostInfo, "TrainRequests", false)))
                {
                    requests.Received += msg => {
                        var req = (TrainRequest)msg;
                        Train(req, () => Cancelled);
                        requests.Ack(msg);
                        Console.Write(".");
                        notifications.Send(new TrainNotification(req));
                    };

                    Waiter.Wait(() => IsStopped);
                }
        }
Example #4
0
        public void Run()
        {
            TaskSync = SyncContext.Current;

            Requests = new AsyncWorkQueueConsumer(new WorkQueueInfo(ConfigurationManager.AppSettings["RabbitHost"], "TrainRequests", false));

            Requests.Received += msg =>
            {
                Handle((TrainRequest)msg);
                Requests.Ack(msg);
            };

            Waiter.Wait(() => Requests == null);
        }