private static DirtyImage ForwardCalculateB(Intracommunicator comm, GriddingConstants c, List <List <Subgrid> > metadata, Complex[,,] visibilities, double[,,] uvw, double[] frequencies, Complex[,] PsfCorrelation, float[,] psfCut, float maxSidelobe, Stopwatch watchIdg) { Stopwatch another = new Stopwatch(); comm.Barrier(); if (comm.Rank == 0) { watchIdg.Start(); } var localGrid = IDG.Grid(c, metadata, visibilities, uvw, frequencies); float[,] image = null; float maxSideLobeLevel = 0.0f; var grid_total = comm.Reduce <Complex[, ]>(localGrid, SequentialSum, 0); if (comm.Rank == 0) { var dirtyImage = FFT.BackwardFloat(grid_total, c.VisibilitiesCount); FFT.Shift(dirtyImage); if (comm.Rank == 0) { FitsIO.Write(dirtyImage, "dirtyImage.fits"); } maxSideLobeLevel = maxSidelobe * Residuals.GetMax(dirtyImage); //remove spheroidal image = Residuals.CalcGradientMap(dirtyImage, PsfCorrelation, new Rectangle(0, 0, psfCut.GetLength(0), psfCut.GetLength(1))); watchIdg.Stop(); } comm.Broadcast(ref maxSideLobeLevel, 0); comm.Broadcast(ref image, 0); return(new DirtyImage(image, maxSideLobeLevel)); }
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); }
private static void ProducerConsumer(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; ProdConsSyncBuffer <int> monitor = new ProdConsSyncBuffer <int>(new MpiHandler(comm), 100); var prod = new IntegersProducer(comm.Rank, monitor); comm.Barrier(); if (comm.Rank % 2 == 0) { prod.Produce(10); } else { var cons = new IntegersConsumer(comm.Rank, monitor); cons.Consume(10); } comm.Barrier(); } }
static void Main(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; procid = comm.Rank; numproc = comm.Size; comm.Barrier(); matrixM(a, b, c, comm); // PrintMatrix(c); } }
static void Main(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; processID = Communicator.world.Rank; numberOfProcesses = Communicator.world.Size; comm.Barrier(); matrixMultiply(a, b, c, comm); } }
static void main(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; for (int i = 1; i <= 5; ++i) { comm.Barrier(); if (comm.Rank == 0) { Console.WriteLine("Everyone is on step " + i + "."); } } } }
static void Main(string[] args) { string str = Console.ReadLine(); int n = int.Parse(str); using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; comm.Barrier(); for (int j = 1; j <= n; j++) { if (Check_Prime(j)) { Console.WriteLine(j + "is a prime number"); } } } }
static void Main(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; if (comm.Rank == 0) { Console.WriteLine("Everyone get to the CHOPPA!"); } else { Random rand = new Random(); System.Threading.Thread.Sleep(rand.Next(1000, 7000)); } comm.Barrier(); if (comm.Rank == 0) { Console.WriteLine("Everyone is on the CHOPPA!"); } } }
private const int ROOT_PROCESS_ID = 0; // Define Root Process ID public static int Main(string[] args) { using (new MPI.Environment(ref args)) // Initialize MPI Environment { Intracommunicator comm = Communicator.world; // Define a shortcut to world communicator string[] contents = null; // Right here, we will store the lines of the text file char what = '\0'; // The character which the user enters int totalCharacters = 0; // Total characters per process int totalOccurrences = 0; // Total occurrences per process double totalTime = 0; // Total time per process bool verbose = false; // Verbose output on command line if (args.Length > 0 && args[0].ToLower() == "--verbose") // If the flag "--verbose" exists, set verbose = true { verbose = true; } int allCharacters; // All characters by all processes int allOccurrences; // All occurrencess by all processes // If we're in the root process if (comm.Rank == ROOT_PROCESS_ID) { // Ask the user to enter the character he want to search for Console.Write("Enter the character you want to search for: "); what = (char)Console.Read(); contents = File.ReadAllLines(PATH); // Load contents of file inside the array } comm.Broadcast(ref contents, ROOT_PROCESS_ID); // Broadcast contents from root comm.Broadcast(ref what, ROOT_PROCESS_ID); // Broadcast character from root // All processes must reach this point before going on // This is important for time measurement comm.Barrier(); double start = MPI.Environment.Time; // The start of time measuring // Let's divide the load for processes, each process is responsible for a specific number of lines for (int i = comm.Rank; i < contents.Length; i += comm.Size) { if (verbose) { Console.WriteLine("Process ID = {0}, Line number = {1}", comm.Rank, i); } for (int j = 0; j < contents[i].Length; j++) { if (contents[i][j] == what) // An occurrence has been found { if (verbose) { Console.WriteLine("Process {0} has found the character at {1}:{2}", comm.Rank, i + 1, j + 1); } totalOccurrences += 1; } totalCharacters += 1; } } // All processes must reach here before we stop the timer comm.Barrier(); double end = MPI.Environment.Time; // The end of time measurement allOccurrences = comm.Reduce(totalOccurrences, Operation <int> .Add, ROOT_PROCESS_ID); // Accumulate all occurrencess allCharacters = comm.Reduce(totalCharacters, Operation <int> .Add, ROOT_PROCESS_ID); // Accumulate all characters totalTime = comm.Reduce(end - start, Operation <double> .Max, ROOT_PROCESS_ID); // The overall time is the longest time of all processes Console.WriteLine("Total occurrences found by process {0} is {1}", comm.Rank, totalOccurrences); Console.WriteLine("Total characters processed by {0} is {1}", comm.Rank, totalCharacters); // Finally, if we're here, print all information if (comm.Rank == ROOT_PROCESS_ID) { Console.WriteLine("Overall occurrences = {0}", allOccurrences); Console.WriteLine("Overall characters = {0}", allCharacters); Console.WriteLine("Overall time = {0}s", totalTime); } } return(0); }
static void RunTests(int root) { Intracommunicator world = Communicator.world; world.Barrier(); if (world.Rank == root) { System.Console.WriteLine("Testing from root " + root); } // Test addition of integers int sum = world.Reduce(world.Rank, addInts, root); int expected = world.Size * (world.Size - 1) / 2; if (world.Rank == root) { MPIDebug.Assert(sum == expected); } else { MPIDebug.Assert(sum == default(int)); } if (world.Rank == root) { System.Console.WriteLine("Sum of ranks = " + sum); } // Test addition of integer points if (world.Rank == root) { Point point_sum = world.Reduce(new Point(world.Rank, world.Size - world.Rank), Point.Plus, root); MPIDebug.Assert(point_sum.x == sum && point_sum.y == (world.Size + 1) * world.Size / 2); System.Console.WriteLine("Sum of points = (" + point_sum.x + ", " + point_sum.y + ")"); } else { world.Reduce(new Point(world.Rank, world.Size - world.Rank), Point.Plus, root); } // Test addition of integer arrays if (world.Rank == root) { System.Console.Write("Testing reduction of integer arrays..."); int[] arraySum = null; world.Reduce(new int[] { world.Rank, world.Size - world.Rank }, Operation <int> .Add, root, ref arraySum); MPIDebug.Assert(arraySum[0] == sum && arraySum[1] == (world.Size + 1) * world.Size / 2); System.Console.WriteLine(" done."); } else { world.Reduce(new int[] { world.Rank, world.Size - world.Rank }, Operation <int> .Add, root); } // Test concatenation of string arrays if (world.Rank == root) { System.Console.Write("Testing reduction of string arrays..."); string[] strArray = null; world.Reduce(new string[] { world.Rank.ToString(), "World" }, Operation <string> .Add, root, ref strArray); string[] expectedStrs = new string[2] { "", "" }; for (int p = 0; p < world.Size; ++p) { expectedStrs[0] += p.ToString(); expectedStrs[1] += "World"; } MPIDebug.Assert(expectedStrs[0] == strArray[0]); MPIDebug.Assert(expectedStrs[1] == strArray[1]); System.Console.WriteLine(" done."); } else { world.Reduce(new string[] { world.Rank.ToString(), "World" }, Operation <string> .Add, root); } // Test reduction on boolean values if (world.Rank == root) { System.Console.Write("Testing reduction of bools..."); bool result = world.Reduce(true, Operation <bool> .LogicalAnd, root); MPIDebug.Assert(result == true); System.Console.WriteLine(" done."); } else { world.Reduce(true, Operation <bool> .LogicalAnd, root); } // Test reduction on boolean arrays if (world.Rank == root) { System.Console.Write("Testing reduction of bool arrays..."); bool[] boolArray = null; world.Reduce(new bool[] { false, world.Rank % 2 != 0, true }, Operation <bool> .LogicalOr, root, ref boolArray); MPIDebug.Assert(boolArray[0] == false); MPIDebug.Assert(boolArray[1] == (world.Size > 1)); MPIDebug.Assert(boolArray[2] == true); System.Console.WriteLine(" done."); } else { world.Reduce(new bool[] { false, world.Rank % 2 != 0, false }, Operation <bool> .LogicalOr, root); } }
public void runBenchMark() { worldcomm = this.WorldComm; np = worldcomm.Size; node = worldcomm.Rank; for (int i = 1; i <= T_max; i++) { Timer.resetTimer(i); } int niter = initialConfig(); Problem.problemConfig(np1, np2, layout_type); Blocks.blocksConfig(nx, ny, nz, np1, np2, layout_type); dims = Problem.dims; twiddle = Problem.twiddle; u0 = Problem.Field_u0; u1 = Problem.Field_u1; u2 = Problem.Field_u2; Compute_index_map.setParameters(twiddle, dims[0, 2], dims[1, 2], dims[2, 2]); Compute_index_map.go(); Compute_initial_conditions.setParameters(u1); Compute_initial_conditions.go(); Fftinit.setParameters(dims[0, 0]); Fftinit.go(); Fft.setParameters(1, u1, u0); Fft.go(); for (int i = 1; i <= T_max; i++) { Timer.resetTimer(i); } worldcomm.Barrier(); Timer.start(T_total); Compute_index_map.setParameters(twiddle, dims[0, 2], dims[1, 2], dims[2, 2]); Compute_index_map.go(); Compute_initial_conditions.setParameters(u1); Compute_initial_conditions.go(); Fftinit.setParameters(dims[0, 0]); Fftinit.go(); Fft.setParameters(1, u1, u0); Fft.go(); double[] sums = new double[niter_default * 2]; for (int iter = 0; iter < niter; iter++) { Evolve.setParameters(u0, u1, twiddle, dims[0, 0], dims[1, 0], dims[2, 0]); Evolve.go(); Fft.setParameters(-1, u1, u2); Fft.go(); Checksum.setParameters(iter, sums, u2, dims[0, 0], dims[1, 0], dims[2, 0]); Checksum.go(); } Verify.setParameters(niter, sums); int verified = Verify.go(); Timer.stop(T_total); double total_time = Timer.readTimer(T_total); double ntotal_f = (double)(nx * ny * nz); double mflops = 0.0; if (total_time != 0) { mflops = 0.000001 * ntotal_f * (14.8157 + 7.19641 * Math.Log(ntotal_f) + (5.23518 + 7.21113 * Math.Log(ntotal_f)) * niter) / total_time; } else { mflops = 0.0; } if (node == 0) { BMResults results = new BMResults(BMName, problem_class.ToString()[0], nx, ny, nz, niter, total_time, mflops, "floating point", verified, true, np, -1); results.print(); } }
public static void DoTest(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator world = Communicator.world; world.Barrier(); // Test addition of integers if (world.Rank == 0) { System.Console.Write("Testing exclusive scan of strings..."); } int partial_sum = world.ExclusiveScan(world.Rank, addInts); int expected = world.Rank * (world.Rank - 1) / 2; MPIDebug.Assert(partial_sum == expected); if (world.Rank == 0) { System.Console.WriteLine(" done."); } // Test addition of integer points if (world.Rank == 0) { System.Console.Write("Testing exclusive scan of strings..."); } Point point_sum = world.ExclusiveScan(new Point(world.Rank, 1), Point.Plus); MPIDebug.Assert(point_sum.x == partial_sum && point_sum.y == world.Rank); if (world.Rank == 0) { System.Console.WriteLine(" done."); } // Test addition of integer arrays if (world.Rank == 0) { System.Console.Write("Testing exclusive scan of integer arrays..."); } int[] arraySum = world.ExclusiveScan(new int[] { world.Rank, 1 }, Operation <int> .Add); MPIDebug.Assert((world.Rank == 0 && arraySum == null) || (world.Rank != 0 && arraySum[0] == partial_sum && arraySum[1] == world.Rank)); if (world.Rank == 0) { System.Console.WriteLine(" done."); } // Test concatenation of strings if (world.Rank == 0) { System.Console.Write("Testing exclusive scan of strings..."); } string str = world.ExclusiveScan(world.Rank.ToString(), Operation <string> .Add); string expectedStr = null; if (world.Rank != 0) { expectedStr = ""; for (int p = 0; p < world.Rank; ++p) { expectedStr += p.ToString(); } } MPIDebug.Assert(expectedStr == str); if (world.Rank == 0) { System.Console.WriteLine(" done."); } // Test concatenation of string arrays if (world.Rank == 0) { System.Console.Write("Testing exclusive scan of string arrays..."); } string[] strArray = world.ExclusiveScan(new string[] { world.Rank.ToString(), "World" }, Operation <string> .Add); string[] expectedStrs = null; if (world.Rank != 0) { expectedStrs = new string[2] { "", "" }; for (int p = 0; p < world.Rank; ++p) { expectedStrs[0] += p.ToString(); expectedStrs[1] += "World"; } MPIDebug.Assert(expectedStrs[0] == strArray[0]); MPIDebug.Assert(expectedStrs[1] == strArray[1]); } else { MPIDebug.Assert(expectedStrs == null); } if (world.Rank == 0) { System.Console.WriteLine(" done."); } } }
private void runBenchmark() { setup_mpi(); if (!active) { Console.WriteLine("not active !"); System.Environment.Exit(0); } //int niter = -1; if (node == root) { BMArgs.Banner(BMName, problem_class.ToString()[0], false, total_nodes); } // Process.make_set(); for (int c = 0; c < ncells; c++) { if ((cell_size[c, 0] > Problem.IMAX) || (cell_size[c, 1] > Problem.JMAX) || (cell_size[c, 2] > Problem.KMAX)) { Console.WriteLine("Problem size too big for compiled array sizes"); System.Environment.Exit(0); } } //Problem.initialize_problem_data(); //Problem.set_constants(0); Initialize.go(); Lhsinit.go(); Exact_rhs.go(); //compute_buffer_size(5); //--------------------------------------------------------------------- // do one time step to touch all code, and reinitialize //--------------------------------------------------------------------- Adi.go(); Initialize.go(); //--------------------------------------------------------------------- // Synchronize before placing time stamp //--------------------------------------------------------------------- comm_setup.Barrier(); Timer.resetAllTimers(); Timer.start(t_total); Console.WriteLine("STARTING"); Console.Out.Flush(); for (int step = 1; step <= niter; step++) { if (node == 0 && (step % 20 == 0 || step == 1 || step == niter)) { Console.WriteLine("Time step " + step); } Adi.go(); } Timer.stop(1); Verify.go(); int verified = Verify.Verified; double tmax = Timer.readTimerGlobal(t_total); if (node == root) { double time = Timer.readTimer(t_total); results = new BMResults(BMName, problem_class.ToString()[0], grid_points[0], grid_points[1], grid_points[2], niter, time, getMFLOPS(time, niter), "floating point", verified, true, total_nodes, bid); results.print(); } worldcomm.Barrier(); }
static void Main(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator world = Communicator.world; if (world.Rank == 0) { System.Console.Write("Testing scatter of integers..."); int[] ranks = new int[world.Size]; for (int i = 0; i < world.Size; ++i) { ranks[i] = i; } int myRank = world.Scatter(ranks, 0); Debug.Assert(myRank == 0); System.Console.WriteLine(" done."); System.Console.Write("Testing scatter of strings..."); string[] rankStrings = new string[world.Size]; for (int i = 0; i < world.Size; ++i) { rankStrings[i] = i.ToString(); } string myRankString = world.Scatter(rankStrings, 0); Debug.Assert(myRankString == world.Rank.ToString()); System.Console.WriteLine(" done."); } else { int myRank = world.Scatter <int>(null, 0); Debug.Assert(myRank == world.Rank); string myRankString = world.Scatter <string>(null, 0); Debug.Assert(myRankString == world.Rank.ToString()); } if (world.Rank == 0) { System.Console.Write("Testing Scatter of bools..."); bool[] odds = new bool[world.Size]; for (int i = 0; i < world.Size; ++i) { odds[i] = i % 2 == 1; } bool amIOdd = world.Scatter(odds, 0); Debug.Assert(!amIOdd); System.Console.WriteLine(" done."); } else { bool amIOdd = world.Scatter <bool>(null, 0); Debug.Assert(amIOdd == (world.Rank % 2 == 1)); } world.Barrier(); if (world.Rank == 0) { int size = world.Size; System.Console.Write("Testing ScatterFromFlattened of integers..."); int[] inRanks = new int[(size * size - size) / 2]; int[] outRanks = null; int[] counts = new int[size]; int p = 0; for (int i = 0; i < world.Size; ++i) { counts[i] = i; for (int j = 0; j < i; j++) { inRanks[p + j] = i; } p += i; } world.ScatterFromFlattened(inRanks, counts, 0, ref outRanks); Debug.Assert(outRanks.Length == 0); System.Console.WriteLine(" done."); } else { int[] outRanks = null; int[] counts = new int[world.Size]; for (int i = 0; i < world.Size; ++i) { counts[i] = i; } world.ScatterFromFlattened(null, counts, 0, ref outRanks); for (int i = 0; i < world.Rank; i++) { Debug.Assert(outRanks[i] == world.Rank); } } if (world.Rank == 0) { int size = world.Size; System.Console.Write("Testing ScatterFromFlattened of strings..."); string[] inRanks = new string[(size * size - size) / 2]; string[] outRanks = null; int[] counts = new int[size]; int p = 0; for (int i = 0; i < world.Size; ++i) { counts[i] = i; for (int j = 0; j < i; j++) { inRanks[p + j] = i.ToString(); } p += i; } world.ScatterFromFlattened(inRanks, counts, 0, ref outRanks); Debug.Assert(outRanks.Length == 0); System.Console.WriteLine(" done."); } else { string[] outRanks = null; int[] counts = new int[world.Size]; for (int i = 0; i < world.Size; ++i) { counts[i] = i; } world.ScatterFromFlattened(null, counts, 0, ref outRanks); for (int i = 0; i < world.Rank; i++) { Debug.Assert(outRanks[i] == world.Rank.ToString()); } } } }
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 void close_enviroment() { worldcomm.Barrier(); mpi.Dispose(); }
// return true if it converges. Output: solution matrix, errors, loops it took public static Boolean solve(Matrix A, Matrix b, out Matrix x, out Matrix err, out int loops, Intracommunicator comm) { // check sanity. rank 0 only if (comm.Rank == 0 && (!A.isSquare || !b.isColumn || (A.Height != b.Height))) { Exception e = new Exception("Matrix A must be square! Matrix b must be a column matrix with the same height as matrix A!"); throw e; } // follow samples in Wikipedia step by step https://en.wikipedia.org/wiki/Gauss%E2%80%93Seidel_method benchmark bm = new benchmark(), bm2 = new benchmark(), bm3 = new benchmark(); double sequential = 0, parallel = 0, communication = 0; bm.start(); bm2.start(); // decompose A into the sum of a lower triangular component L* and a strict upper triangular component U int size = 0; Matrix L = null, U = null, L_1; if (comm.Rank == 0) { size = A.Height; Matrix.Decompose(A, out L, out U); } bm2.pause(); sequential += bm2.getElapsedSeconds(); bm2.start(); comm.Broadcast(ref size, 0); comm.Broadcast(ref U, 0); comm.Broadcast(ref b, 0); bm2.pause(); communication += bm2.getElapsedSeconds(); // Inverse matrix L* comm.Barrier(); L_1 = MatrixParallel.Inverse(L, comm, ref sequential, ref parallel, ref communication); // Main iteration: x (at step k+1) = T * x (at step k) + C // where T = - (inverse of L*) * U, and C = (inverse of L*) * b // split T & C into groups of rows, each for one slave, according to the nature of this algorithm // each slave will have one piece of T & one piece of C stored locally. the rest of T & C is not needed // there might be cases where jobs > slaves, so some might get no job at all // Changes: only split L_1. Slaves will calculate T & C (pieces) themselves bm2.start(); Matrix jobDistro = Utils.splitJob(size, comm.Size); int startRow = 0, endRow = 0, myJobSize = (int)jobDistro[0, comm.Rank]; for (int p = 0; p < comm.Size; p++) { if (p != comm.Rank) { startRow += (int)jobDistro[0, p]; } else { endRow = startRow + (int)jobDistro[0, p] - 1; break; } } Matrix[] L_1Ps = new Matrix[comm.Size]; if (comm.Rank == 0) { int slaveStart = 0; for (int p = 0; p < comm.Size; p++) { L_1Ps[p] = Matrix.extractRows(L_1, slaveStart, slaveStart + (int)jobDistro[0, p] - 1); slaveStart += (int)jobDistro[0, p]; } } bm2.pause(); sequential += bm2.getElapsedSeconds(); bm2.start(); Matrix L_1P = comm.Scatter(L_1Ps, 0); bm2.pause(); communication += bm2.getElapsedSeconds(); bm2.start(); Matrix T = -L_1P * U; Matrix C = L_1P * b; bm2.pause(); parallel += bm2.getElapsedSeconds(); // the actual iteration // if it still doesn't converge after this many loops, assume it won't converge and give up Boolean converge = false; int loopLimit = 100; x = Matrix.zeroLike(b); // at step k for (loops = 0; loops < loopLimit; loops++) { bm3.start(); // (re-)distributing x vector. Must be done every single loop // this loop needs x from the previous loop comm.Broadcast(ref x, 0); bm3.pause(); communication += bm3.getElapsedSeconds(); // calculation step bm3.start(); comm.Barrier(); Matrix new_x = T * x + C; // check convergence converge = Matrix.SomeClose(new_x, x, 1e-15, startRow); // collect result x comm.Barrier(); x = comm.Reduce(new_x, Matrix.Concatenate, 0); // collect convergence. consider converged if ALL slaves claim so converge = comm.Reduce(converge, bothTrue, 0); comm.Broadcast(ref converge, 0); // make sure EVERYONE breaks/coninues bm3.pause(); parallel += bm3.getElapsedSeconds(); if (converge) { loops++; break; } } bm2.start(); // round the result slightly err = null; if (comm.Rank == 0) { x.Round(1e-14); err = A * x - b; err.Round(1e-14); } bm2.pause(); sequential += bm2.getElapsedSeconds(); bm.pause(); if (showBenchmark) { Console.WriteLine("Sequential part took " + sequential + " secs."); Console.WriteLine("Parallel part took " + parallel + " secs."); Console.WriteLine("Communication took " + communication + " secs."); Console.WriteLine("Total: " + bm.getResult() + " (" + bm.getElapsedSeconds() + " secs). Seq + Parallel: " + (sequential + parallel)); } return(converge); }
public static void DoTest(string[] args) { using (new MPI.Environment(ref args)) { Intracommunicator world = Communicator.world; world.Barrier(); // Test addition of integers int partial_sum = world.Scan(world.Rank, addInts); int expected = (world.Rank + 1) * world.Rank / 2; MPIDebug.Assert(partial_sum == expected); if (world.Rank == world.Size - 1) { System.Console.WriteLine("Sum of ranks = " + partial_sum); } // Test addition of integer points Point point_sum = world.Scan(new Point(world.Rank, 1), Point.Plus); MPIDebug.Assert(point_sum.x == partial_sum && point_sum.y == world.Rank + 1); if (world.Rank == world.Size - 1) { System.Console.WriteLine("Sum of points = (" + point_sum.x + ", " + point_sum.y + ")"); } // Test addition of integer arrays if (world.Rank == world.Size - 1) { System.Console.Write("Testing scan of integer arrays..."); } int[] arraySum = world.Scan(new int[] { world.Rank, 1 }, Operation <int> .Add); MPIDebug.Assert(arraySum[0] == partial_sum && arraySum[1] == world.Rank + 1); if (world.Rank == world.Size - 1) { System.Console.WriteLine(" done."); } // Test concatenation of strings if (world.Rank == world.Size - 1) { System.Console.Write("Testing scan of strings..."); } string str = world.Scan(world.Rank.ToString(), Operation <string> .Add); string expectedStr = ""; for (int p = 0; p <= world.Rank; ++p) { expectedStr += p.ToString(); } MPIDebug.Assert(expectedStr == str); if (world.Rank == world.Size - 1) { System.Console.WriteLine(" done."); } // Test concatenation of string arrays if (world.Rank == world.Size - 1) { System.Console.Write("Testing scan of string arrays..."); } string[] strArray = world.Scan(new string[] { world.Rank.ToString(), "World" }, Operation <string> .Add); string[] expectedStrs = new string[2] { "", "" }; for (int p = 0; p <= world.Rank; ++p) { expectedStrs[0] += p.ToString(); expectedStrs[1] += "World"; } MPIDebug.Assert(expectedStrs[0] == strArray[0]); MPIDebug.Assert(expectedStrs[1] == strArray[1]); if (world.Rank == world.Size - 1) { System.Console.WriteLine(" done."); } } }
static void Main(string[] args) { var LogFile = "log-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm") + ".txt"; var Size = 1; using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; switch (comm.Rank) { ///Перший процесс case 0: Console.WriteLine($"Start: - {comm.Rank}"); ///Ініціалізація початкових даних Vector b = new Vector(Size); Matrix A = new Matrix(Size, 2, 0); ///Обрахування вектора y1 var y1 = A * b; // y1.WriteToFile(LogFile, "Vector y1"); Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); int x = 0x5a; x = 0b1010101010101; Console.WriteLine($"Barrier Rank{comm.Rank}/- {DateTime.Now}"); comm.Barrier(); var Y3to1 = comm.Receive <Matrix>(1, 0); Console.WriteLine($"Receive from 1 to 0"); Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Send <Vector>(y1, 3, 0); comm.Barrier(); ///Другий доданок var Y3y1 = Y3to1 * y1; Console.WriteLine($"Y3y1 well - {Y3y1.vector[0]}"); comm.Send <Vector>(Y3y1, 3, 0); break; ////////////////////////////////////////////////////////// ///Другий процесс /////////////////////////////////////////////////////////// case 1: Console.WriteLine($"Start: - {comm.Rank}"); ///Ініціалізаці початкових даних Matrix B2 = new Matrix(Size, 2, 0); Matrix A2 = new Matrix(Size, 2, 0); //Початок обрахунку Y3 var Y3 = A2 * B2; Console.WriteLine($"BarrierRank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); //Отримання пересланих даних від 3 процесса var C2r = comm.Receive <Matrix>(2, 0); Console.WriteLine($"Receive from 2 to 1"); ///Закінчення обрахунку Y3 Y3 = Y3 - C2r; var Y3qr = Y3 * Y3; Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); comm.Send <Matrix>(Y3, 0, 0); comm.Send <Matrix>(Y3, 3, 0); var y2to2 = comm.Receive <Vector>(3, 0); Console.WriteLine($"Receive from 3 to 1"); Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); ///перший доданок var Y3y2 = Y3qr * y2to2; comm.Send <Vector>(Y3y2, 3, 0); comm.Send <Matrix>(Y3qr, 3, 0); Console.WriteLine($"Sendet fron 1 to 3"); break; ///////////////////////////////////////////////////////////////////////// ///Третій процесс/// ///////////////////////////////////////////////////////////////////////// case 2: Console.WriteLine($"Start: - {comm.Rank}"); //Ініціалізаці Почаккових матриць Matrix C2 = new Matrix(Size); Matrix A1 = new Matrix(Size, 2, 0); Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); //Пересилання comm.Send <Matrix>(C2, 1, 0); comm.Send <Matrix>(A1, 3, 0); Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); x = 0x7b; Console.WriteLine($"Monitor is working"); Console.WriteLine($"Monitor is working"); Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); Console.WriteLine($"Barier 3 in 2 process ended and look "); break; ///////////////////////////////////////////////////////////// ///Четвертий процесс //////////////////////////////////////////////////////////// case 3: Console.WriteLine($"Start: - {comm.Rank}"); Vector c1 = new Vector(Size, 2, 0); Vector b1 = new Vector(Size, 2, 0); var b1c1 = b1 + (20 * c1); Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); Matrix A1r = comm.Receive <Matrix>(2, 0); var y2 = A1r * b1c1; Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); var y1to4 = comm.Receive <Vector>(0, 0); var Y3to4 = comm.Receive <Matrix>(1, 0); comm.Send <Vector>(y2, 1, 0); var y2y2 = y2 * y2; var Y3New = y2y2 * Y3to4; Console.WriteLine($"Barrier Rank{comm.Rank}/ - {DateTime.Now}"); comm.Barrier(); ///3 доданок var Y3Vector = Y3New * y1to4; Console.WriteLine($"Vector Y3v calculaiter "); var Y3y2r = comm.Receive <Vector>(1, 0); var Y3qur = comm.Receive <Matrix>(1, 0); var Y3y1r = comm.Receive <Vector>(0, 0); Console.WriteLine("All receiver in 3 process"); var firsObj = Y3y2r + Y3y1r + Y3Vector; var X = Y3qur * firsObj; Console.WriteLine("Finish"); X.WriteToFile(LogFile, "Finish Result "); break; } } Console.ReadLine(); }
static void Main(string[] args) { Bitmap originalImage, firstQuarter, secondQuarter, thirdQuarter, forthQuarter; using (new MPI.Environment(ref args)) { Intracommunicator comm = Communicator.world; if (comm.Rank == 0) { //SPLIT-RED originalImage = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2.jpg")); Rectangle rect = new Rectangle(0, 0, originalImage.Width / 2, originalImage.Height / 2); firstQuarter = originalImage.Clone(rect, originalImage.PixelFormat); firstQuarter.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); rect = new Rectangle(originalImage.Width / 2, 0, originalImage.Width / 2, originalImage.Height / 2); secondQuarter = originalImage.Clone(rect, originalImage.PixelFormat); secondQuarter.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); rect = new Rectangle(0, originalImage.Height / 2, originalImage.Width / 2, originalImage.Height / 2); thirdQuarter = originalImage.Clone(rect, originalImage.PixelFormat); thirdQuarter.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part3.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); rect = new Rectangle(originalImage.Width / 2, originalImage.Height / 2, originalImage.Width / 2, originalImage.Height / 2); forthQuarter = originalImage.Clone(rect, originalImage.PixelFormat); forthQuarter.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part4.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } comm.Barrier(); //4PARTSREDTOBLACK Bitmap spiderman1 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part1.jpg")); Bitmap spiderman2 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part2.jpg")); Bitmap spiderman3 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part3.jpg")); Bitmap spiderman4 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part4.jpg")); if (comm.Rank == 0) { for (int i = 0; i < spiderman1.Width; i++) { for (int j = 0; j < spiderman1.Height; j++) { if ((spiderman1.GetPixel(i, j).R - spiderman1.GetPixel(i, j).G) > 40) { spiderman1.SetPixel(i, j, Color.Black); } } } spiderman1.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part1B.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } if (comm.Rank == 1) { for (int i = 0; i < spiderman2.Width; i++) { for (int j = 0; j < spiderman2.Height; j++) { if ((spiderman2.GetPixel(i, j).R - spiderman2.GetPixel(i, j).G) > 40) { spiderman2.SetPixel(i, j, Color.Black); } } } spiderman2.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part2B.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } if (comm.Rank == 2) { for (int i = 0; i < spiderman3.Width; i++) { for (int j = 0; j < spiderman3.Height; j++) { if ((spiderman3.GetPixel(i, j).R - spiderman3.GetPixel(i, j).G) > 40) { spiderman3.SetPixel(i, j, Color.Black); } } } spiderman3.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part3B.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } if (comm.Rank == 3) { for (int i = 0; i < spiderman4.Width; i++) { for (int j = 0; j < spiderman4.Height; j++) { if ((spiderman4.GetPixel(i, j).R - spiderman4.GetPixel(i, j).G) > 40) { spiderman4.SetPixel(i, j, Color.Black); } } } spiderman4.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part4B.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } comm.Barrier(); if (comm.Rank == 0) { //4PARTSBLACK-MERGED Image img1 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part1B.jpg")); Image img2 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part2B.jpg")); Image img3 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part3B.jpg")); Image img4 = new Bitmap(Image.FromFile(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\Spiderman2_part4B.jpg")); int width = img3.Width + img4.Width; int height = img1.Height + img3.Height; Bitmap imgFinal = new Bitmap(width, height); Graphics g = Graphics.FromImage(imgFinal); g.Clear(Color.Black); g.DrawImage(img1, new Point(0, 0)); g.DrawImage(img2, new Point(img1.Width, 0)); g.DrawImage(img3, new Point(0, img1.Height)); g.DrawImage(img4, new Point(img1.Width, img1.Height)); g.Dispose(); img1.Dispose(); img2.Dispose(); img3.Dispose(); img4.Dispose(); imgFinal.Save(@"C:\Users\user pc\Desktop\UiTMKJM\PART 7 SEPT2017_JAN2018\CSC580\RedSplitBlackMerge\SpidermanColorChangedMerged.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); imgFinal.Dispose(); } } }
public List <int> GetNum(int num) { Console.WriteLine("ввели num " + num); List <int> end = new List <int>(); Intracommunicator cls = Communicator.world; Console.WriteLine("ввели cls.Size " + cls.Size); Console.WriteLine("cls.Rank " + cls.Rank); if (cls.Rank == 0) { //Console.WriteLine("cls.Rank **********************************************************************" + cls.Rank); int part = num / cls.Size; int a = part; for (int i = 1; i <= cls.Size - 1; i++) { if (i == cls.Size - 1) { List <int> list = new List <int>(); list.Add(a + 1); list.Add(num); cls.Send <List <int> >(list, cls.Size - 1, 0); } else { List <int> list = new List <int>(); list.Add(a + 1); list.Add(a + part); cls.Send <List <int> >(list, i, 0); a += part; } } for (int i = 2; i <= part; i++) { if (ChekSimple(i)) { end.Add(i); } } List <int> general_list; for (int i = 1; i < cls.Size; i++) { Console.WriteLine("Rank !!!!!!!!!!!!!!!!!"); general_list = cls.Receive <List <int> >(i, 0); for (int j = 0; j < general_list.Count; j++) { end.Add(general_list[j]); } } cls.Barrier(); } else { List <int> list = new List <int>(); List <int> list1 = cls.Receive <List <int> >(0, 0); //Console.WriteLine("dsfdsgsdgdsgsdgsdg_____________dsfgsdgsdggdsgsdg"); Console.WriteLine("1){0} 2){1} ", list1[0], list1[1]); if ((list1[0] == -1) || (list1[1] == -1)) { list.Add(-1); //cls.Send<List<int>>(list, 0, 0); return(list); } else { for (int i = list1[0]; i <= list1[1]; i++) { if (ChekSimple(i)) { list.Add(i); } } cls.Send <List <int> >(list, 0, 0); } cls.Barrier(); } return(end); }
public static Matrix Inverse(Matrix matrix, Intracommunicator comm, ref double timeS, ref double timeP, ref double timeC) { if (comm.Rank == 0 && !matrix.isSquare) { Exception e = new Exception("Matrix must be square!"); throw e; } benchmark bm = new benchmark(), bm2 = new benchmark(); bm.start(); int n = 0; int[] perm = new int[10]; int toggle = 0; Matrix lum = null; if (comm.Rank == 0) { n = matrix.dim1; lum = LUPDecompose(matrix, out perm, out toggle); } bm.pause(); timeS += bm.getElapsedSeconds(); bm.start(); comm.Broadcast(ref n, 0); comm.Broadcast(ref lum, 0); if (comm.Rank != 0) { perm = new int[n]; } comm.Broadcast(ref perm, 0); comm.Broadcast(ref toggle, 0); comm.Barrier(); bm.pause(); timeC += bm.getElapsedSeconds(); if (lum == null) { return(zeroLike(matrix)); } bm.start(); Double det = 0; if (comm.Rank == 0) { det = Determinant(lum, perm, toggle); } bm.pause(); timeS += bm.getElapsedSeconds(); bm.start(); comm.Broadcast(ref det, 0); comm.Barrier(); bm.pause(); timeC += bm.getElapsedSeconds(); if (det == 0) // not invertible { // still return for the sake of simplicity // Zero matrix * any matrix = zero matrix // so it's never a valid answer return(zeroLike(matrix)); } bm.pause(); int slaves = comm.Size; Matrix jobDistro = Utils.splitJob(n, slaves); int startCol = 0, endCol = 0, size = (int)jobDistro[0, comm.Rank]; for (int p = 0; p < slaves; p++) { if (p != comm.Rank) { startCol += (int)jobDistro[0, p]; } else { endCol = startCol + (int)jobDistro[0, p] - 1; break; } } bm.pause(); timeP += bm.getElapsedSeconds(); bm.start(); Matrix result = new Matrix(n, size); for (int i = startCol; i < startCol + size; ++i) { double[] b = new double[n]; for (int j = 0; j < n; ++j) { if (i == perm[j]) { b[j] = 1.0; } else { b[j] = 0.0; } } double[] x = HelperSolve(lum, b); for (int j = 0; j < n; ++j) { result[j, i - startCol] = x[j]; } } bm.pause(); timeP += bm.getElapsedSeconds(); bm.start(); // collect result result = comm.Reduce(result, ConcatenateColumn, 0); bm.pause(); timeP += bm.getElapsedSeconds(); return(result); }