static int[] CalculationInTask(object p) { var p1 = p as Tuple <int, int, System.Threading.Barrier, List <string> >; System.Threading.Barrier barrier = p1.Item3; List <string> data = p1.Item4; int start = p1.Item1 * p1.Item2; int end = start + p1.Item2; Console.WriteLine("Task {0}: range from {1} to {2}", Task.CurrentId, start, end); int[] charCount = new int[26]; for (int j = start; j < end; j++) { char c = data[j][0]; charCount[c - 97]++; } Console.WriteLine("Task {0} is done. {1} - a, {2} - z", Task.CurrentId, charCount[0], charCount[25]); barrier.RemoveParticipant(); Console.WriteLine("Task {0} was removed; Remaining tasks: {1}", Task.CurrentId, barrier.ParticipantsRemaining); return(charCount); }
/// <summary>Computes a parallel inclusive prefix scan over the array using the specified function.</summary> public static void InclusiveScanInPlaceParallel <T>(T[] arr, Func <T, T, T> function) { int procCount = Environment.ProcessorCount; T[] intermediatePartials = new T[procCount]; using (var phaseBarrier = new System.Threading.Barrier(procCount, _ => ExclusiveScanInPlaceSerial(intermediatePartials, function, 0, intermediatePartials.Length))) { // Compute the size of each range int rangeSize = arr.Length / procCount; int nextRangeStart = 0; // Create, store, and wait on all of the tasks var tasks = new Task[procCount]; for (int i = 0; i < procCount; i++, nextRangeStart += rangeSize) { // Get the range for each task, then start it int rangeNum = i; int lowerRangeInclusive = nextRangeStart; int upperRangeExclusive = i < procCount - 1 ? nextRangeStart + rangeSize : arr.Length; tasks[rangeNum] = Task.Factory.StartNew(() => { // Phase 1: Prefix scan assigned range, and copy upper bound to intermediate partials InclusiveScanInPlaceSerial(arr, function, lowerRangeInclusive, upperRangeExclusive, 1); intermediatePartials[rangeNum] = arr[upperRangeExclusive - 1]; // Phase 2: One thread only should prefix scan the intermediaries... done implicitly by the barrier phaseBarrier.SignalAndWait(); // Phase 3: Incorporate partials if (rangeNum != 0) { for (int j = lowerRangeInclusive; j < upperRangeExclusive; j++) { arr[j] = function(intermediatePartials[rangeNum], arr[j]); } } }); } // Wait for all of the tasks to complete Task.WaitAll(tasks); } }
static void Main(string[] args) { const int numberTasks = 2; const int partitionSize = 1000000; var data = new List <string>(FillData(partitionSize * numberTasks)); var barrier = new System.Threading.Barrier(numberTasks); var taskFactory = new TaskFactory(); var tasks = new Task <int[]> [numberTasks]; for (int i = 0; i < numberTasks; i++) { tasks[i] = taskFactory.StartNew <int[]>(CalculationInTask, Tuple.Create(i, partitionSize, barrier, data)); } barrier.SignalAndWait(); var resultCollection = tasks[0].Result.Zip(tasks[1].Result, (c1, c2) => c1 + c2); }
static void Main(string[] args) { var options = new SwitchOptions(); if (!CommandLine.ParseArguments(args, options)) return; try { var source = System.IO.File.Open(options.inFile, System.IO.FileMode.Open); var emulator = new IN8(); source.Read(emulator.MEM, 0, 0xFFFF); // Attach devices var teletypeTerminal = new TT3(); teletypeTerminal.Connect(emulator, 0x04); System.Threading.Barrier barrier = new System.Threading.Barrier(2); var screenThread = new System.Threading.Thread(() => { var screen = new ICM_CD2.VisualHardwareGrid(emulator); screen.AddHardware(typeof(ICM_CD2.ICM), new Microsoft.Xna.Framework.Point(16, 16), 0x02, 0x03); screen.AddHardware(typeof(ICM_CD2.SevenSegment), new Microsoft.Xna.Framework.Point(528 + 32, 16), 0x05); barrier.SignalAndWait(); screen.Run(); }); screenThread.SetApartmentState(System.Threading.ApartmentState.STA); screenThread.Start(); barrier.SignalAndWait(); var cycleTime = DateTime.Now; uint cycles = 0; while ((emulator.STATE_FLAGS & 0x80) != 0x00) { cycles += emulator.Step(); if (cycles > 10) { Console.SetCursorPosition(0, 0); //Limit to 1 mhz var endCycleTime = DateTime.Now; Console.WriteLine( String.Format("Running at {0} per {1}", cycles, (endCycleTime - cycleTime).TotalMilliseconds)); cycleTime = endCycleTime; cycles -= 10; System.Threading.Thread.Sleep(1); } } Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(String.Format("Finished in state {0:X2}", emulator.STATE_FLAGS)); Console.WriteLine(String.Format("A:{0:X2} B:{1:X2} C:{2:X2} D:{3:X2} E:{4:X2} H:{5:X2} L:{6:X2} O:{7:X2}", emulator.A, emulator.B, emulator.C, emulator.D, emulator.E, emulator.H, emulator.L, emulator.O)); Console.WriteLine(String.Format("IP:{0:X4} SP:{1:X4} CLK:{2:X8}", emulator.IP, emulator.SP, emulator.CLOCK)); } catch (Exception e) { Console.WriteLine("An error occured."); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }
static void Main(string[] args) { DiscoverHardware(System.Reflection.Assembly.GetExecutingAssembly()); /* * Input to support * [*] Run hex coded ML * [*] Load binary file to memory and run * [*] Set memory size * [ ] Binding hardware * [*] Single step debugging mode * [*] Break points */ Console.SetWindowSize(120, 40); try { Emulator = new Emulator(); Emulator.MEM[0] = 0xCA; var breakpoints = new List<ushort>(); bool stepping = false; var hardware = new List<String>(); foreach (var arg in args) { if (arg[0] == 'X') arg.Split(' ').Skip(1).Select(s => Convert.ToByte(s, 16)).ToArray().CopyTo(Emulator.MEM, 0); else if (arg[0] == 'B') breakpoints.Add(Convert.ToUInt16(arg.Substring(1), 16)); else if (arg[0] == 'S') stepping = true; else if (arg[0] == 'F') System.IO.File.ReadAllBytes(arg.Substring(1)).CopyTo(Emulator.MEM, 0); else if (arg[0] == 'M') Emulator.MEMORY_SIZE = Convert.ToUInt16(arg.Substring(1), 16); else if (arg[0] == 'A') { var assembly = System.Reflection.Assembly.LoadFile(arg.Substring(1)); DiscoverHardware(assembly); } else if (arg[0] == 'H') hardware.Add(arg.Substring(1)); } System.Threading.Barrier barrier = new System.Threading.Barrier(2); var screenThread = new System.Threading.Thread(() => { var screen = new IN8.VisualHardwareGrid(Emulator); foreach (var item in hardware) { var hargs = item.Split(' '); var x = Convert.ToInt32(hargs[1]); var y = Convert.ToInt32(hargs[2]); screen.AddHardware(HardwareTypes[hargs[0]], new Microsoft.Xna.Framework.Point(Convert.ToInt32(hargs[1]), Convert.ToInt32(hargs[2])), hargs.Skip(3).Select(s => Convert.ToByte(s, 16)).ToArray()); } barrier.SignalAndWait(); screen.Run(); }); screenThread.SetApartmentState(System.Threading.ApartmentState.STA); screenThread.Start(); barrier.SignalAndWait(); while (Emulator.STATE_FLAGS == 0) { Emulator.Step(); if (stepping || breakpoints.Contains(Emulator.IP)) { stepping = true; Dump("BREAK"); Console.Write("WAITING FOR COMMAND"); TOP: var key = Console.ReadKey(true); if (key.Key == ConsoleKey.S) continue; else if (key.Key == ConsoleKey.C) { stepping = false; continue; } else { Console.Beep(); goto TOP; } } } Dump("STOP"); Console.Write("PRESS ANY KEY TO EXIT"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine("Exception. " + e.Message); Console.ReadKey(); } }
public Barrier(int n_threads) { b = new System.Threading.Barrier(n_threads); }