Esempio n. 1
0
        public static void matrixMultiply(int[,] a, int[,] b, int[,] c, Intracommunicator comm)
        {
            int i;

            int[] d = new int[16];
            for (i = 0; i < 16; i++)
            {
                d[i] = 0;
                Console.WriteLine(d[k]);
            }
            size   = n / numberOfProcesses;
            start  = processID * size;
            finish = (processID + 1) * size;


            Console.WriteLine("Proc with rank: " + processID + " is getting from: " + start + " to: " + finish);

            for (i = start; i <= finish; ++i)
            {
                for (j = 0; j < n; ++j)
                {
                    for (k = 0; k < n; ++k)
                    {
                        c[i, j] += a[i, k] * b[k, j];
                    }
                }
            }

            comm.Allgather <int>(c[i, j], ref d);
            for (k = 0; k < n; k++)
            {
                Console.WriteLine("d[ " + k + "] = " + d[k]);
            }
        }
Esempio n. 2
0
        public static void matrixM(int[,] a, int[,] b, int[,] c, Intracommunicator comm)
        {
            int[] d = new int[16];
            for (i = 0; i < 16; i++)
            {
                d[i] = 0;
                Console.WriteLine(d[k]);
            }

            /*size = n / numproc;
             * start = procid * size;
             * finish = (procid + 1) * size;*/
            int root = 0;
            int count = n / numproc;
            int remainder = n % numproc;
            int start, stop;

            if (procid < remainder)
            {
                start = procid * (count + 1);
                stop  = start + count;
            }
            else
            {
                start = procid * count + remainder;
                stop  = start + (count - 1);
            }


            Console.WriteLine("proc with rank " + procid + " is getting from " + start + " to " + stop);

            /*Console.WriteLine("start = "+ start);
            *  Console.WriteLine("start = " + stop);*/
            for (i = start; i <= stop; ++i)
            {
                //Console.WriteLine("here");
                for (j = 0; j < n; ++j)
                {
                    for (k = 0; k < n; ++k)
                    {
                        c[i, j] += a[i, k] * b[k, j];
                        //Console.WriteLine("rack "+procid+" c[" + i + "][" + j + "] = " + c[i, j]);
                        comm.Allgather <int>(c[i, j], ref d);
                    }
                }
            }
            for (k = 0; k < n; k++)
            {
                Console.WriteLine("d[ " + k + "] = " + d[k]);
            }
            // comm.Gather<int[,]>(c,root);
            comm.Barrier();
            // PrintMatrix(c);
        }
Esempio n. 3
0
        public static R DoMPIRun <RI, R>(Parameters parameters, int runs, Func <ParameterValues, RI> main, Func <IEnumerable <RI>, R> agg)
        {
            Intracommunicator comm = Communicator.world;

            int runsPerProc = runs / comm.Size;

            int startRun = comm.Rank * runsPerProc;

            var rand = new jp.takel.PseudoRandom.MersenneTwister();

            parameters.SkipNRandomRuns(rand, startRun);

            var results = new List <RI>();

            //Console.WriteLine("{0}: Doing {1} runs starting at {2}", comm.Rank, runsPerProc, startRun);

            foreach (var p in parameters.GetRandom(rand, runsPerProc, startRun))
            {
                var r = main(p);
                results.Add(r);
                //Console.WriteLine("{0}: Run {1} done", comm.Rank, p.RunId);
            }

            var allResults = comm.Allgather(results);

            //Console.WriteLine("{0}: Allgather", comm.Rank);

            var allResultsFlat = new List <RI>();

            foreach (var r in allResults)
            {
                foreach (var r2 in r)
                {
                    allResultsFlat.Add(r2);
                }
            }

            var aggResult = agg(allResultsFlat);


            return(aggResult);
        }
Esempio n. 4
0
        public static void FloydWarshall(int[,] graph, int verticesCount, int processID, int numberOfProcesses, Intracommunicator comm)
        {
            int[,] distance = new int[verticesCount, verticesCount];


            int size   = constant / numberOfProcesses;
            int start  = processID * size;
            int finish = (processID + 1) * size;

            for (k = 0; k < verticesCount; k++)
            {
                for (int i = start; i < finish; ++i)
                {
                    for (int j = 0; j < verticesCount; ++j)
                    {
                        distance[i, j] = graph[i, j];
                    }
                }
            }

            for (k = 0; k < verticesCount; k++)
            {
                for (int i = start; i < finish; ++i)
                {
                    for (int j = 0; j < verticesCount; ++j)
                    {
                        if (distance[i, k] + distance[k, j] < distance[i, j])
                        {
                            distance[i, j] = distance[i, k] + distance[k, j];
                        }
                    }
                }
            }

            comm.Allgather <int[, ]>(distance, 0);
            Print(distance, verticesCount, processID);
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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);
            }
        }