Exemple #1
0
        public virtual void Shutdown(IEvolutionState state)
        {
            // prevent me from hitting this multiple times
            lock (_shutDownLock)
            {
                if (ShuttingDown)
                {
                    return;
                }

                ShuttingDown = true;
            }

            // don't want to miss any of these so we'll wrap them individually
            try
            {
                // BRS: TODO : Not sure if shutdown status was intended here?
                DataOut.Write((byte)SlaveEvaluationType.Shutdown);
            }
            catch (Exception) { } // exception, not IOException, because JZLib throws some array exceptions
            try { DataOut.Flush(); }
            catch (Exception) { }
            try { DataOut.Close(); }
            catch (Exception) { }
            try { DataIn.Close(); }
            catch (Exception) { }
            try { EvalSocket.Close(); }
            catch (IOException) { }

            State.Output.SystemMessage(ToString() + " Slave is shutting down....");
            SlaveMonitor.UnregisterSlave(this); // unregister me BEFORE I reschedule my jobs
            RescheduleJobs(State);
            lock (_jobs)
            {
                // notify my threads now that I've closed stuff in case they're still waiting
                SlaveMonitor.NotifyMonitor(_jobs);
                Reader.Interrupt(); // not important right now but...
                Writer.Interrupt(); // very important that we be INSIDE the jobs synchronization here so the writer doesn't try to wait on the monitor again.
            }
            State.Output.SystemMessage(ToString() + " Slave exits....");
        }
Exemple #2
0
        public virtual bool WriteLoop()
        {
            IJob job;

            try
            {
                lock (_jobs)
                {
                    // check for an unsent job
                    if ((job = OldestUnsentJob()) == null)
                    // automatically marks as sent
                    {
                        // failed -- wait and drop out of the loop and come in again
                        Debug("" + ThreadClass.Current().Name + "Waiting for a job to send");
                        SlaveMonitor.WaitOnMonitor(_jobs);
                    }
                }
                if (job != null)
                // we got a job inside our synchronized wait
                {
                    // send the job
                    Debug("" + ThreadClass.Current().Name + "Sending Job");
                    if (job.Type == SlaveEvaluationType.Simple)
                    {
                        // Tell the server we're evaluating a SimpleProblemForm
                        DataOut.Write((byte)SlaveEvaluationType.Simple);
                    }
                    else
                    {
                        // Tell the server we're evaluating a IGroupedProblem
                        DataOut.Write((byte)SlaveEvaluationType.Grouped);

                        // Tell the server whether to count victories only or not.
                        DataOut.Write(job.CountVictoriesOnly);
                    }

                    // transmit number of individuals
                    DataOut.Write(job.Inds.Length);

                    // Transmit the subpops to the slave
                    foreach (var t in job.Subpops)
                    {
                        DataOut.Write(t);
                    }

                    Debug("Starting to transmit individuals");

                    // Transmit the individuals to the server for evaluation...
                    for (var i = 0; i < job.Inds.Length; i++)
                    {
                        job.Inds[i].WriteIndividual(State, DataOut);
                        DataOut.Write(job.UpdateFitness[i]);
                    }
                    DataOut.Flush();
                }
            }
            catch (Exception)
            {
                Shutdown(State); return(false);
            }
            return(true);
        }