public static void Run()
        {
            var folder    = @"C:\dev\GitHub\p9-data\small\fits\simulation_point\";
            var data      = DataLoading.SimulatedPoints.Load(folder);
            var gridSizes = new int[] { 256, 512, 1024, 2048, 4096 };

            Directory.CreateDirectory("GPUSpeedup");
            var writer = new StreamWriter("GPUSpeedup/GPUSpeedup.txt", false);

            writer.WriteLine("imgSize;iterCPU;timeCPU;iterGPU;timeGPU");
            foreach (var gridSize in gridSizes)
            {
                var    visibilitiesCount = data.visibilitiesCount;
                int    subgridsize       = 8;
                int    kernelSize        = 4;
                int    max_nr_timesteps  = 1024;
                double cellSize          = (1.0 * 256 / gridSize) / 3600.0 * Math.PI / 180.0;
                var    c        = new GriddingConstants(visibilitiesCount, gridSize, subgridsize, kernelSize, max_nr_timesteps, (float)cellSize, 1, 0.0f);
                var    metadata = Partitioner.CreatePartition(c, data.uvw, data.frequencies);

                var    frequencies  = FitsIO.ReadFrequencies(Path.Combine(folder, "freq.fits"));
                var    uvw          = FitsIO.ReadUVW(Path.Combine(folder, "uvw.fits"));
                var    flags        = new bool[uvw.GetLength(0), uvw.GetLength(1), frequencies.Length];
                double norm         = 2.0;
                var    visibilities = FitsIO.ReadVisibilities(Path.Combine(folder, "vis.fits"), uvw.GetLength(0), uvw.GetLength(1), frequencies.Length, norm);

                var psfGrid = IDG.GridPSF(c, metadata, uvw, flags, frequencies);
                var psf     = FFT.BackwardFloat(psfGrid, c.VisibilitiesCount);
                FFT.Shift(psf);

                var residualVis = data.visibilities;
                var dirtyGrid   = IDG.Grid(c, metadata, residualVis, data.uvw, data.frequencies);
                var dirtyImage  = FFT.BackwardFloat(dirtyGrid, c.VisibilitiesCount);
                FFT.Shift(dirtyImage);

                var totalSize      = new Rectangle(0, 0, gridSize, gridSize);
                var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psf, totalSize), new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)));
                var bMapCPU        = bMapCalculator.Convolve(dirtyImage);
                var bMapGPU        = bMapCalculator.Convolve(dirtyImage);
                var fastCD         = new FastSerialCD(totalSize, psf);
                var gpuCD          = new GPUSerialCD(totalSize, psf, 1000);
                var lambda         = 0.5f * fastCD.MaxLipschitz;
                var alpha          = 0.5f;

                var xCPU      = new float[gridSize, gridSize];
                var cpuResult = fastCD.Deconvolve(xCPU, bMapCPU, lambda, alpha, 10000, 1e-8f);
                FitsIO.Write(xCPU, "GPUSpeedup/cpuResult" + gridSize + ".fits");

                var xGPU      = new float[gridSize, gridSize];
                var gpuResult = gpuCD.Deconvolve(xGPU, bMapGPU, lambda, alpha, 10000, 1e-8f);
                FitsIO.Write(xCPU, "GPUSpeedup/gpuResult" + gridSize + ".fits");

                writer.WriteLine(gridSize + ";" + cpuResult.IterationCount + ";" + cpuResult.ElapsedTime.TotalSeconds + ";" + gpuResult.IterationCount + ";" + gpuResult.ElapsedTime.TotalSeconds);
                writer.Flush();
            }

            writer.Close();
        }
Ejemplo n.º 2
0
        public static void DebugILGPU()
        {
            var    frequencies  = FitsIO.ReadFrequencies(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\freq.fits");
            var    uvw          = FitsIO.ReadUVW(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\uvw.fits");
            var    flags        = new bool[uvw.GetLength(0), uvw.GetLength(1), frequencies.Length]; //completely unflagged dataset
            double norm         = 2.0;
            var    visibilities = FitsIO.ReadVisibilities(@"C:\dev\GitHub\p9-data\small\fits\simulation_point\vis.fits", uvw.GetLength(0), uvw.GetLength(1), frequencies.Length, norm);

            var    visibilitiesCount = visibilities.Length;
            int    gridSize          = 256;
            int    subgridsize       = 8;
            int    kernelSize        = 4;
            int    max_nr_timesteps  = 1024;
            double cellSize          = 1.0 / 3600.0 * PI / 180.0;
            var    c = new GriddingConstants(visibilitiesCount, gridSize, subgridsize, kernelSize, max_nr_timesteps, (float)cellSize, 1, 0.0f);

            var watchTotal     = new Stopwatch();
            var watchForward   = new Stopwatch();
            var watchBackwards = new Stopwatch();
            var watchDeconv    = new Stopwatch();

            watchTotal.Start();
            var metadata = Partitioner.CreatePartition(c, uvw, frequencies);

            var psfGrid = IDG.GridPSF(c, metadata, uvw, flags, frequencies);
            var psf     = FFT.Backward(psfGrid, c.VisibilitiesCount);

            FFT.Shift(psf);

            var psfCutDouble = CutImg(psf);
            var psfCut       = ToFloatImage(psfCutDouble);

            FitsIO.Write(psfCut, "psfCut.fits");


            var totalSize      = new Rectangle(0, 0, gridSize, gridSize);
            var imageSection   = new Rectangle(0, 128, gridSize, gridSize);
            var bMapCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfCut, totalSize), new Rectangle(0, 0, psfCut.GetLength(0), psfCut.GetLength(1)));
            var fastCD         = new FastSerialCD(totalSize, psfCut);

            fastCD.ResetLipschitzMap(ToFloatImage(psf));
            var gpuCD  = new GPUSerialCD(totalSize, psfCut, 100);
            var lambda = 0.5f * fastCD.MaxLipschitz;
            var alpha  = 0.8f;

            var xImage      = new float[gridSize, gridSize];
            var residualVis = visibilities;

            /*var truth = new double[gridSize, gridSize];
             * truth[30, 30] = 1.0;
             * truth[35, 36] = 1.5;
             * var truthVis = IDG.ToVisibilities(c, metadata, truth, uvw, frequencies);
             * visibilities = truthVis;
             * var residualVis = truthVis;*/
            for (int cycle = 0; cycle < 4; cycle++)
            {
                //FORWARD
                watchForward.Start();
                var dirtyGrid  = IDG.Grid(c, metadata, residualVis, uvw, frequencies);
                var dirtyImage = FFT.BackwardFloat(dirtyGrid, c.VisibilitiesCount);
                FFT.Shift(dirtyImage);
                FitsIO.Write(dirtyImage, "dirty_" + cycle + ".fits");
                watchForward.Stop();

                //DECONVOLVE
                watchDeconv.Start();
                bMapCalculator.ConvolveInPlace(dirtyImage);
                FitsIO.Write(dirtyImage, "bMap_" + cycle + ".fits");
                //var result = fastCD.Deconvolve(xImage, dirtyImage, lambda, alpha, 1000, 1e-4f);
                var result = gpuCD.Deconvolve(xImage, dirtyImage, lambda, alpha, 1000, 1e-4f);

                if (result.Converged)
                {
                    Console.WriteLine("-----------------------------CONVERGED!!!!------------------------");
                }
                else
                {
                    Console.WriteLine("-------------------------------not converged----------------------");
                }
                FitsIO.Write(xImage, "xImageGreedy" + cycle + ".fits");
                FitsIO.Write(dirtyImage, "residualDebug_" + cycle + ".fits");
                watchDeconv.Stop();

                //BACKWARDS
                watchBackwards.Start();
                FFT.Shift(xImage);
                var xGrid = FFT.Forward(xImage);
                FFT.Shift(xImage);
                var modelVis = IDG.DeGrid(c, metadata, xGrid, uvw, frequencies);
                residualVis = Visibilities.Substract(visibilities, modelVis, flags);
                watchBackwards.Stop();

                var hello = FFT.Forward(xImage, 1.0);
                hello = Common.Fourier2D.Multiply(hello, psfGrid);
                var hImg = FFT.Backward(hello, (double)(128 * 128));
                //FFT.Shift(hImg);
                FitsIO.Write(hImg, "modelDirty_FFT.fits");

                var imgRec = IDG.ToImage(c, metadata, modelVis, uvw, frequencies);
                FitsIO.Write(imgRec, "modelDirty" + cycle + ".fits");
            }
        }