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 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 #3
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 #4
0
        public void WorkerQueueIsConnectedEvent()
        {
            WithSync(() => {
                var wq = new WorkQueueInfo(new RabbitHostInfo("localhost"), "fooQueue", false);
                using (var c = new AsyncWorkQueueConsumer(wq))
                    using (var p = new WorkQueueProducer(wq))
                    {
                        bool cIsConnected     = false;
                        bool pIsConnected     = false;
                        RabbitMessage msg     = null;
                        c.Received           += x => msg = x;
                        c.IsConnectedChanged += x => cIsConnected = x;
                        p.IsConnectedChanged += x => pIsConnected = x;

                        Waiter.Wait(() => cIsConnected && pIsConnected);
                        RabbitTestHelper.StopRabbitService();
                        Waiter.Wait(() => !cIsConnected && !pIsConnected);

                        msg.ShouldBeNull();
                        p.Send(new TestMessage());

                        RabbitTestHelper.StartRabbitService();
                        Waiter.Wait(() => cIsConnected && pIsConnected);

                        Waiter.Wait(() => msg != null);
                        msg.ShouldBeOfType <TestMessage>();
                    }
            });
        }
Example #5
0
        public void SyncWorkQueue()
        {
            WithSync(() => {
                var wq = new WorkQueueInfo(new RabbitHostInfo("localhost"), "fooQueue", false);
                using (var p = new WorkQueueProducer(wq))
                {
                    var task = Task.Factory.StartNew(() => {
                        WithSync(() => {
                            using (var c = new SyncWorkQueueConsumer(wq))
                            {
                                Lists.Repeat(1000, _ => {
                                    var msg = c.Receive();
                                    c.Ack(msg);
                                });
                            }
                        });
                    });

                    Lists.Repeat(1000, _ => p.Send(new TestMessage()));

                    task.Wait(2000).ShouldBeTrue();
                }
            });
        }