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(); }
/// <summary> /// Major cycle implementation for the Serial CD /// </summary> /// <param name="obsName"></param> /// <param name="data"></param> /// <param name="c"></param> /// <param name="useGPU"></param> /// <param name="psfCutFactor"></param> /// <param name="maxMajorCycle"></param> /// <param name="lambda"></param> /// <param name="alpha"></param> /// <param name="deconvIterations"></param> /// <param name="deconvEpsilon"></param> public static void ReconstructSerialCD(string obsName, MeasurementData data, GriddingConstants c, bool useGPU, int psfCutFactor, int maxMajorCycle, float lambda, float alpha, int deconvIterations, float deconvEpsilon) { var metadata = Partitioner.CreatePartition(c, data.UVW, data.Frequencies); var psfVis = new Complex[data.UVW.GetLength(0), data.UVW.GetLength(1), data.Frequencies.Length]; for (int i = 0; i < data.Visibilities.GetLength(0); i++) { for (int j = 0; j < data.Visibilities.GetLength(1); j++) { for (int k = 0; k < data.Visibilities.GetLength(2); k++) { if (!data.Flags[i, j, k]) { psfVis[i, j, k] = new Complex(1.0, 0); } else { psfVis[i, j, k] = new Complex(0, 0); } } } } Console.WriteLine("gridding psf"); var psfGrid = IDG.GridW(c, metadata, psfVis, data.UVW, data.Frequencies); var psf = FFT.WStackIFFTFloat(psfGrid, c.VisibilitiesCount); FFT.Shift(psf); var totalWatch = new Stopwatch(); var currentWatch = new Stopwatch(); var totalSize = new Rectangle(0, 0, c.GridSize, c.GridSize); var psfCut = PSF.Cut(psf, psfCutFactor); var maxSidelobe = PSF.CalcMaxSidelobe(psf, psfCutFactor); IDeconvolver deconvolver = null; if (useGPU & GPUSerialCD.IsGPUSupported()) { deconvolver = new GPUSerialCD(totalSize, psfCut, 1000); } else if (useGPU & !GPUSerialCD.IsGPUSupported()) { Console.WriteLine("GPU not supported by library. Switching to CPU implementation"); deconvolver = new FastSerialCD(totalSize, psfCut); } else { deconvolver = new FastSerialCD(totalSize, psfCut); } var psfBMap = psfCut; using (var gCalculator = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psfBMap, totalSize), new Rectangle(0, 0, psfBMap.GetLength(0), psfBMap.GetLength(1)))) using (var gCalculator2 = new PaddedConvolver(PSF.CalcPaddedFourierCorrelation(psf, totalSize), new Rectangle(0, 0, psf.GetLength(0), psf.GetLength(1)))) { var currentGCalculator = gCalculator; var maxLipschitz = PSF.CalcMaxLipschitz(psfCut); var lambdaLipschitz = (float)(lambda * maxLipschitz); var lambdaTrue = (float)(lambda * PSF.CalcMaxLipschitz(psf)); var switchedToOtherPsf = false; var xImage = new float[c.GridSize, c.GridSize]; var residualVis = data.Visibilities; DeconvolutionResult lastResult = null; for (int cycle = 0; cycle < maxMajorCycle; cycle++) { Console.WriteLine("Beginning Major cycle " + cycle); var dirtyGrid = IDG.GridW(c, metadata, residualVis, data.UVW, data.Frequencies); var dirtyImage = FFT.WStackIFFTFloat(dirtyGrid, c.VisibilitiesCount); FFT.Shift(dirtyImage); FitsIO.Write(dirtyImage, obsName + "_dirty_serial_majorCycle" + cycle + ".fits"); currentWatch.Restart(); totalWatch.Start(); var maxDirty = Residuals.GetMax(dirtyImage); var gradients = gCalculator.Convolve(dirtyImage); var maxB = Residuals.GetMax(gradients); var correctionFactor = Math.Max(maxB / (maxDirty * maxLipschitz), 1.0f); var currentSideLobe = maxB * maxSidelobe * correctionFactor; var currentLambda = (float)Math.Max(currentSideLobe / alpha, lambdaLipschitz); var objective = Residuals.CalcPenalty(dirtyImage) + ElasticNet.CalcPenalty(xImage, lambdaTrue, alpha); var absMax = deconvolver.GetAbsMaxDiff(xImage, gradients, lambdaTrue, alpha); if (absMax >= MAJOR_EPSILON) { lastResult = deconvolver.Deconvolve(xImage, gradients, currentLambda, alpha, deconvIterations, deconvEpsilon); } if (lambda == currentLambda & !switchedToOtherPsf) { currentGCalculator = gCalculator2; lambda = lambdaTrue; maxLipschitz = PSF.CalcMaxLipschitz(psf); switchedToOtherPsf = true; } FitsIO.Write(xImage, obsName + "_model_serial_majorCycle" + cycle + ".fits"); currentWatch.Stop(); totalWatch.Stop(); if (absMax < MAJOR_EPSILON) { break; } FFT.Shift(xImage); var xGrid = FFT.Forward(xImage); FFT.Shift(xImage); var modelVis = IDG.DeGridW(c, metadata, xGrid, data.UVW, data.Frequencies); residualVis = Visibilities.Substract(data.Visibilities, modelVis, data.Flags); } Console.WriteLine("Reconstruction finished in (seconds): " + totalWatch.Elapsed.TotalSeconds); } }
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"); } }