Beispiel #1
0
        /*public static void Read_matrix(int[] local_mat, int n, int procid, int numproc, Intracommunicator comm)
         * {
         *  int i, j;
         *  int[] temp_mat = new int[40];
         *  int num;
         *  if (procid == 0)
         *  {
         *
         *      for (i = 0; i < n; i++)
         *          for (j = 0; j < n; j++)
         *          {
         *              num = i * n + j;
         *              temp_mat[num] = Convert.ToInt32(Console.ReadLine());
         *          }
         *      comm.Scatter<int>(temp_mat);
         *      //free(temp_mat);
         *  }
         *  else
         *  {
         *      comm.Scatter<int>(temp_mat);
         *  }
         *
         * }*/
        public static void Print_matrix(int[] local_mat, int n, int procid, int numproc, Intracommunicator comm)
        {
            int i, j;

            int[] temp_mat = new int[40];

            if (procid == 0)
            {
                temp_mat = comm.Gather <int>(n, 0);
                for (i = 0; i < n; i++)
                {
                    for (j = 0; j < n; j++)
                    {
                        if (temp_mat[i * n + j] == INF)
                        {
                            Console.WriteLine("inf ");
                        }
                        else
                        {
                            Console.WriteLine(temp_mat[i * n + j]);
                        }
                    }
                    Console.WriteLine("\n");
                }
                temp_mat = null;
            }
            else
            {
                temp_mat = comm.Gather <int>(numproc, 0);
            }
        }
Beispiel #2
0
        public static void FloydWarshall(int[,] graph, int verticesCount, int rank, int ntasks, Intracommunicator comm)
        {
            int[,] distance = new int[verticesCount, verticesCount];


            for (int i = rank; i < verticesCount; i += ntasks)
            {
                for (int j = 0; j < verticesCount; ++j)
                {
                    distance[i, j] = graph[i, j];
                }
            }

            for (int k = 0; k < verticesCount; ++k)
            {
                for (int i = rank; i < verticesCount; i += ntasks)
                {
                    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.Gather <int[, ]>(distance, 0);
            Print(distance, verticesCount, rank);
        }
Beispiel #3
0
        public static int[] SimpleParallel(Intracommunicator communicator, int[] p1, int[] p2)
        {
            var perProcess = p1.Length / communicator.Size;
            var start      = communicator.Rank * perProcess;

            if (communicator.Rank == communicator.Size - 1)
            {
                perProcess += p1.Length % communicator.Size;
            }

            var result = new int[p1.Length + p2.Length - 1];


            for (var offset = 0; offset < perProcess; offset++)
            {
                var i = start + offset;

                for (var j = 0; j < p2.Length; j++)
                {
                    result[i + j] += p1[i] * p2[j];
                }
            }

            var results = communicator.Gather(result, 0);

            if (communicator.Rank == 0)
            {
                return(results.Aggregate(new int[p1.Length + p2.Length - 1], Add).ToArray());
            }

            return(null);
        }
Beispiel #4
0
        public static void FloydWarshall(int[,] graph, int verticesCount, int procid, int numproc, Intracommunicator comm)
        {
            int[,] distance = new int[verticesCount, verticesCount];

            int size   = 4 / numproc;
            int start  = procid * size;
            int finish = (procid + 1) * size;

            //Console.WriteLine("here");
            // Console.WriteLine("procid = "+procid);
            for (k = 0; k < verticesCount; k++)
            {
                Communicator.world.Barrier();

                for (int i = start; i < finish; ++i)
                {
                    for (int j = 0; j < verticesCount; ++j)
                    {
                        //Console.WriteLine("line "+i+" for procid "+procid);
                        //Console.WriteLine("procid = " + procid);
                        distance[i, j] = graph[i, j];
                        //Console.WriteLine("graph[" + 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];
                        }
                        //Console.WriteLine("min distance["+i+", "+j+"] = "+ distance[i, j]);
                    }
                }
            }
            comm.Gather <int[, ]>(distance, 0);
            Print(distance, verticesCount, procid);
        }
        /// <summary>
        /// Calculates the catchment result counts for each catchment coordinator.
        /// This is required so that the coordinator knows how many results to expect from
        /// each process in the GatherFlattened call that accumulates the gridded results.
        /// </summary>
        private void CalculateCatchmentResultCounts()
        {
            if (IsSlave)
            {
                Log.DebugFormat("Rank {0}: Calculating catchment result counts", WorldRank);

                // Count how many cells I have for each catchment
                Dictionary <string, int> myResultsPerCatchment = new Dictionary <string, int>(MyWork.Catchments.Count);
                foreach (CellDefinition cell in MyWork.Cells)
                {
                    if (myResultsPerCatchment.ContainsKey(cell.CatchmentId))
                    {
                        myResultsPerCatchment[cell.CatchmentId]++;
                    }
                    else
                    {
                        myResultsPerCatchment.Add(cell.CatchmentId, 1);
                    }
                }

                // Gather all the individual result counts into the coordinators
                foreach (CatchmentDefinition catchment in MyWork.Catchments)
                {
                    Intracommunicator comm           = communicatorsByCatchmentId[catchment.Id];
                    int[]             resultsPerRank = comm.Gather(myResultsPerCatchment[catchment.Id], 0);
                    if (comm.Rank == 0)
                    {
                        // Count how many catchments this process is coordinating
                        CatchmentCoordinatorCount++;

                        // Lazy creation of the dictionary. We don't know if the current
                        // process needs one of these or not until this point
                        if (CatchmentResultCountPerCatchmentCommunicator == null)
                        {
                            CatchmentResultCountPerCatchmentCommunicator = new Dictionary <string, int[]>(comm.Size);
                        }

                        // Store the result counts
                        CatchmentResultCountPerCatchmentCommunicator.Add(catchment.Id, resultsPerRank);
                    }
                }
            }
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            using (new MPI.Environment(ref args))
            {
                Intracommunicator comm = Communicator.world;
                Random            rand = new Random();

                int[] randomNums = comm.Gather(rand.Next(1, 1001), 0);

                if (comm.Rank == 0)
                {
                    Array.Sort(randomNums);

                    foreach (int num in randomNums)
                    {
                        Console.WriteLine(num);
                    }
                }
            }
        }
        public static float[,] Reconstruct(Intracommunicator comm, DistributedData.LocalDataset local, GriddingConstants c, int maxCycle, float lambda, float alpha, int iterPerCycle = 1000, bool usePathDeconvolution = false)
        {
            var watchTotal    = new Stopwatch();
            var watchForward  = new Stopwatch();
            var watchBackward = new Stopwatch();
            var watchDeconv   = new Stopwatch();

            watchTotal.Start();

            var metadata = Partitioner.CreatePartition(c, local.UVW, local.Frequencies);

            var patchSize = CalculateLocalImageSection(comm.Rank, comm.Size, c.GridSize, c.GridSize);
            var totalSize = new Rectangle(0, 0, c.GridSize, c.GridSize);

            //calculate psf and prepare for correlation in the Fourier space
            var psf = CalculatePSF(comm, c, metadata, local.UVW, local.Flags, local.Frequencies);

            Complex[,] PsfCorrelation = null;
            var maxSidelobe = PSF.CalcMaxSidelobe(psf);

            lambda = (float)(lambda * PSF.CalcMaxLipschitz(psf));

            StreamWriter writer = null;

            if (comm.Rank == 0)
            {
                FitsIO.Write(psf, "psf.fits");
                Console.WriteLine("done PSF gridding ");
                PsfCorrelation = PSF.CalcPaddedFourierCorrelation(psf, totalSize);
                writer         = new StreamWriter(comm.Size + "runtimestats.txt");
            }

            var deconvovler = new MPIGreedyCD(comm, totalSize, patchSize, psf);

            var residualVis = local.Visibilities;
            var xLocal      = new float[patchSize.YEnd - patchSize.Y, patchSize.XEnd - patchSize.X];


            for (int cycle = 0; cycle < maxCycle; cycle++)
            {
                if (comm.Rank == 0)
                {
                    Console.WriteLine("cycle " + cycle);
                }
                var dirtyImage = ForwardCalculateB(comm, c, metadata, residualVis, local.UVW, local.Frequencies, PsfCorrelation, psf, maxSidelobe, watchForward);

                var bLocal = GetImgSection(dirtyImage.Image, patchSize);

                MPIGreedyCD.Statistics lastRun;
                if (usePathDeconvolution)
                {
                    var currentLambda = Math.Max(1.0f / alpha * dirtyImage.MaxSidelobeLevel, lambda);
                    lastRun = deconvovler.DeconvolvePath(xLocal, bLocal, currentLambda, 4.0f, alpha, 5, iterPerCycle, 2e-5f);
                }
                else
                {
                    lastRun = deconvovler.Deconvolve(xLocal, bLocal, lambda, alpha, iterPerCycle, 1e-5f);
                }

                if (comm.Rank == 0)
                {
                    WriteToFile(cycle, lastRun, writer);
                    if (lastRun.Converged)
                    {
                        Console.WriteLine("-----------------------------CONVERGED!!!!------------------------");
                    }
                    else
                    {
                        Console.WriteLine("-------------------------------not converged----------------------");
                    }
                }
                comm.Barrier();
                if (comm.Rank == 0)
                {
                    watchDeconv.Stop();
                }

                float[][,] totalX = null;
                comm.Gather(xLocal, 0, ref totalX);
                Complex[,] modelGrid = null;
                if (comm.Rank == 0)
                {
                    watchBackward.Start();
                    var x = new float[c.GridSize, c.GridSize];
                    StitchImage(totalX, x, comm.Size);
                    FitsIO.Write(x, "xImage_" + cycle + ".fits");
                    FFT.Shift(x);
                    modelGrid = FFT.Forward(x);
                }
                comm.Broadcast(ref modelGrid, 0);

                var modelVis = IDG.DeGrid(c, metadata, modelGrid, local.UVW, local.Frequencies);
                residualVis = Visibilities.Substract(local.Visibilities, modelVis, local.Flags);
            }
            writer.Close();

            float[][,] gatherX = null;
            comm.Gather(xLocal, 0, ref gatherX);
            float[,] reconstructed = null;
            if (comm.Rank == 0)
            {
                reconstructed = new float[c.GridSize, c.GridSize];;
                StitchImage(gatherX, reconstructed, comm.Size);
            }

            return(reconstructed);
        }
 public Tools.Collections.SerializableDictionary <string, MpiTimeSeries>[] Gather(Tools.Collections.SerializableDictionary <string, MpiTimeSeries> serializableDictionary, int root, int sender)
 {
     return(communicator.Gather(serializableDictionary, root));
 }