Exemplo n.º 1
0
        internal virtual void WorkLoop(Intracommunicator comm)
        {
            startTime = DateTime.Now;
            Console.WriteLine("Starting worker {0} ", comm.Rank);
            var timer = new Stopwatch();


            int partitionHeight = info.SourceImage.Height / comm.Size;
            int partitionY      = partitionHeight * comm.Rank;

            for (int i = 0; i < info.Settings.PolygonsMax; i++)
            {
                var polygon = new DnaPolygon();
                parentDrawing.Polygons.Add(polygon);
                polygon.Init(parentDrawing, info);

                //foreach (DnaPoint p in polygon.Points)
                //{
                //    p.Y = partitionY + partitionHeight / 2;
                //}
            }

            int waitTimer = 0;

            while (true)
            {
                if (comm.Rank == 0)
                {
                    NotifyProgress(generation);
                }

                timer.Reset();
                timer.Start();

                while (timer.ElapsedMilliseconds < waitTimer)
                {
                    var currentDrawing    = parentDrawing.GetMutatedChild(info);
                    var currentErrorLevel = GetFitnessForDrawing(currentDrawing);

                    if (currentErrorLevel < parentErrorLevel)
                    {
                        parentErrorLevel = currentErrorLevel;
                        parentDrawing    = currentDrawing;
                    }
                }

                if (waitTimer < 6000)
                {
                    waitTimer += 500;
                }

                timer.Stop();

                generation++;

                var drawingInfo = new MpiWorkerDrawingInfo
                {
                    Drawing = parentDrawing
                };


                //    Stopwatch swSync = new Stopwatch();
                //    swSync.Start();
                MpiWorkerDrawingInfo[] allResults = comm.Allgather(drawingInfo);

                if (bgImage != null)
                {
                    bgImage.Dispose();
                }

                bgImage = new Bitmap(info.SourceImage.Width, info.SourceImage.Height,
                                     PixelFormat.Format32bppArgb);
                var bgGraphics = Graphics.FromImage(bgImage);

                bgGraphics.Clear(Color.Black);
                bgGraphics.SmoothingMode = SmoothingMode.HighQuality;
                for (int i = 0; i < comm.Rank; i++)
                {
                    Renderer.Render(allResults[i].Drawing, bgGraphics, 1);
                }

                if (fgImage != null)
                {
                    fgImage.Dispose();
                }

                fgImage = new Bitmap(info.SourceImage.Width, info.SourceImage.Height,
                                     PixelFormat.Format32bppArgb);
                var fgGraphics = Graphics.FromImage(fgImage);

                fgGraphics.Clear(Color.Transparent);
                fgGraphics.SmoothingMode = SmoothingMode.HighQuality;
                for (int i = comm.Rank + 1; i < comm.Size; i++)
                {
                    Renderer.Render(allResults[i].Drawing, fgGraphics, 1);
                }

                fgGraphics.Dispose();
                bgGraphics.Dispose();


                //recalc the new parent error level
                parentErrorLevel = GetFitnessForDrawing(parentDrawing);
                //    swSync.Stop();

                //    Console.WriteLine("sync {0}", swSync.Elapsed);
            }
        }