public void testFIFOQueue() { FIFOQueue<String> queue = new FIFOQueue<String>(); Assert.IsTrue(queue.isEmpty()); queue.push("Hello"); Assert.AreEqual(1, queue.Count); Assert.IsFalse(queue.isEmpty()); queue.push("Hi"); Assert.AreEqual(2, queue.Count); Assert.IsFalse(queue.isEmpty()); String s = queue.pop(); Assert.AreEqual("Hello", s); Assert.AreEqual(1, queue.Count); Assert.AreEqual("Hi", queue.Peek()); queue.push("bonjour"); queue.push("salaam alaikum"); Assert.AreEqual(3, queue.Count); Assert.AreEqual("Hi", queue.pop()); Assert.AreEqual("bonjour", queue.pop()); Assert.AreEqual("salaam alaikum", queue.pop()); Assert.AreEqual(0, queue.Count); }
public void testFIFOQueue() { FIFOQueue <String> queue = new FIFOQueue <String>(); Assert.IsTrue(queue.isEmpty()); queue.push("Hello"); Assert.AreEqual(1, queue.Count); Assert.IsFalse(queue.isEmpty()); queue.push("Hi"); Assert.AreEqual(2, queue.Count); Assert.IsFalse(queue.isEmpty()); String s = queue.pop(); Assert.AreEqual("Hello", s); Assert.AreEqual(1, queue.Count); Assert.AreEqual("Hi", queue.Peek()); queue.push("bonjour"); queue.push("salaam alaikum"); Assert.AreEqual(3, queue.Count); Assert.AreEqual("Hi", queue.pop()); Assert.AreEqual("bonjour", queue.pop()); Assert.AreEqual("salaam alaikum", queue.pop()); Assert.AreEqual(0, queue.Count); }
/** * Makes a CSP consisting of binary constraints arc-consistent. * @return An object which indicates success/failure and contains * data to undo the operation. */ public DomainRestoreInfo reduceDomains(CSP csp) { DomainRestoreInfo result = new DomainRestoreInfo(); FIFOQueue<Variable> queue = new FIFOQueue<Variable>(); foreach (Variable var in csp.getVariables()) queue.Add(var); reduceDomains(queue, csp, result); return result.compactify(); }
Resource Session(Resource device, FIFOQueue <Process, IQueueable <Process> > deviceQueue) { if (deviceQueue != null && !deviceQueue.Empty()) { device.ActiveProcess = deviceQueue.Item(); } return(device); }
/** * Makes a CSP consisting of binary constraints arc-consistent. * @return An object which indicates success/failure and contains * data to undo the operation. */ public DomainRestoreInfo reduceDomains(CSP csp) { DomainRestoreInfo result = new DomainRestoreInfo(); FIFOQueue <Variable> queue = new FIFOQueue <Variable>(); foreach (Variable var in csp.getVariables()) { queue.Add(var); } reduceDomains(queue, csp, result); return(result.compactify()); }
public DomainRestoreInfo ReduceDomains(CSProblem csp) { var result = new DomainRestoreInfo(); var queue = new FIFOQueue <Variable>(); foreach (Variable var in csp.Variables) { queue.Push(var); } this.ReduceDomains(queue, csp, result); return(result.Compactify()); }
static void Main(string[] args) { var db = new SingletonDB(); var workerPool = new WorkerPool("Hen"); var queue = new FIFOQueue(workerPool, db); var scheduler = new LazyScheduler(queue); var workerFactory = new ClousotWorkerFactory(scheduler, null, db); scheduler.FeedQueue(new ISliceId[0]); // TODO: call slicer var numberOfWorkers = Environment.ProcessorCount; numberOfWorkers = 1; for (var i = 0; i < numberOfWorkers; i++) workerPool.CreateWorker(workerFactory); workerPool.WaitAll(); workerPool.StopAll(); }
/** * Reduces the domain of the specified variable to the specified * value and reestablishes arc-consistency. It is assumed that the * provided CSP is arc-consistent before the call. * @return An object which indicates success/failure and contains * data to undo the operation. */ public DomainRestoreInfo reduceDomains(Variable var, Object value, CSP csp) { DomainRestoreInfo result = new DomainRestoreInfo(); Domain domain = csp.getDomain(var); if (domain.contains(value)) { if (domain.Count > 1) { FIFOQueue<Variable> queue = new FIFOQueue<Variable>(); queue.Add(var); result.storeDomainFor(var, domain); csp.setDomain(var, new Domain(new Object[] { value })); reduceDomains(queue, csp, result); } } else { result.setEmptyDomainFound(true); } return result.compactify(); }
/** * Reduces the domain of the specified variable to the specified * value and reestablishes arc-consistency. It is assumed that the * provided CSP is arc-consistent before the call. * @return An object which indicates success/failure and contains * data to undo the operation. */ public DomainRestoreInfo reduceDomains(Variable var, Object value, CSP csp) { DomainRestoreInfo result = new DomainRestoreInfo(); Domain domain = csp.getDomain(var); if (domain.contains(value)) { if (domain.Count > 1) { FIFOQueue <Variable> queue = new FIFOQueue <Variable>(); queue.Add(var); result.storeDomainFor(var, domain); csp.setDomain(var, new Domain(new Object[] { value })); reduceDomains(queue, csp, result); } } else { result.setEmptyDomainFound(true); } return(result.compactify()); }
public DomainRestoreInfo ReduceDomains(Variable var, object value, CSProblem csp) { DomainRestoreInfo result = new DomainRestoreInfo(); Domain domain = csp.GetDomain(var); if (domain.Contains(value)) { if (domain.Count() > 1) { FIFOQueue <Variable> queue = new FIFOQueue <Variable>(); queue.Push(var); result.StoreDomainFor(var, domain); csp.SetDomain(var, new Domain(new object[] { value })); this.ReduceDomains(queue, csp, result); } } else { result.SetEmptyDomainFound(true); } return(result.Compactify()); }
protected void ReduceDomains(FIFOQueue <Variable> queue, CSProblem csp, DomainRestoreInfo info) { while (!(queue.Count == 0)) { Variable var = queue.Pop(); foreach (IConstraint constraint in csp.GetConstraints(var)) { if (constraint.GetScope().Count == 2) { Variable neighbor = csp.GetNeighbor(var, constraint); if (this.Revise(neighbor, var, constraint, csp, info)) { if (csp.GetDomain(neighbor).IsEmpty()) { info.SetEmptyDomainFound(true); return; } queue.Push(neighbor); } } } } }
private void reduceDomains(FIFOQueue <Variable> queue, CSP csp, DomainRestoreInfo info) { while (!queue.isEmpty()) { Variable var = queue.pop(); foreach (Constraint constraint in csp.getConstraints(var)) { if (constraint.getScope().Count == 2) { Variable neighbor = csp.getNeighbor(var, constraint); if (revise(neighbor, var, constraint, csp, info)) { if (csp.getDomain(neighbor).isEmpty()) { info.setEmptyDomainFound(true); return; } queue.push(neighbor); } } } } }
/// <summary> /// 构建出一个简单的连接型队列,其中前部包含元素 10,后部为空 /// </summary> /// <returns></returns> private static ConcatenatedLRUQueue<IFrame> ConstructSimpleConcatQueue() { IQueue<IFrame> front = new FIFOQueue<IFrame>(); IQueue<IFrame> back = new FIFOQueue<IFrame>(); front.Enqueue(new Frame(10)); ConcatenatedLRUQueue<IFrame> target = new ConcatenatedLRUQueue<IFrame>(front, back); return target; }
public void GetData() { bool ok = false; while (!ok) { StreamReader sr; string[] strings; ok = true; try { Console.WriteLine("Przeciagnij tu plik wejsciowy i wcisnij ENTER..."); inputDirectory = Console.ReadLine(); if (inputDirectory[0] == '\"') inputDirectory = inputDirectory.Substring(1, inputDirectory.Length - 2); Console.WriteLine(" "); sr = new StreamReader(inputDirectory); String line = ""; # region nazwa systemu while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "SYSTEM" && strings[2] != "") systemName = strings[2]; else throw (new Exception("Zla nazwa systemu")); #endregion #region kanaly line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "PRZEPLYWNOSC" && strings[2] != "") { flowability = int.Parse(strings[2]); flowability = flowability / 1000; } else throw (new Exception("Zla przeplywnosc")); #endregion #region kolejka line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "KOLEJKI" && strings[2] != "") { NumberOfQueues = int.Parse(strings[2]); } else throw (new Exception("Zla liczba pojemności kolejki")); #endregion #region readBufors queue = new FIFOQueue[NumberOfQueues]; TableOfPriorities = new string[NumberOfQueues]; for (int i = 0; i < NumberOfQueues; i++) { string name; int size; line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "NAZWA" && strings[2] != "" && strings[3] == "BUFOR" && strings[5] != "") { name = strings[2]; size = int.Parse(strings[5]); size *= 8; } else throw (new Exception("Zla nazwa rozkladu i bufora")); line = ""; TableOfPriorities[i] = name; queue[i] = new FIFOQueue(name, size); } #endregion #region liczba rozkladow line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "ROZKLADY" && strings[2] != "") NumberOfDistributions = int.Parse(strings[2]); else throw (new Exception("Zla liczba rozkładów")); #endregion #region reading distributions distribution = new ProbabilityDistributions[NumberOfDistributions]; for (int i = 0; i < NumberOfDistributions; i++) { string name; double lambda; line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "NAZWA" && strings[2] != "") { name = strings[2]; } else throw (new Exception("Zla nazwa rozkladu")); line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "LAMBDA" && strings[2] != "") { lambda = double.Parse(strings[2]); } else throw (new Exception("Zla lambda rozkladu")); distribution[i] = new ProbabilityDistributions(name, lambda); } #endregion #region liczba strumieni line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "STRUMIENIE" && strings[2] != "") NumberOfStreams = int.Parse(strings[2]); else throw (new Exception("Zla liczba strumieni")); #endregion #region wczytywanie strumieni stream = new Stream[NumberOfStreams]; for (int i = 0; i < NumberOfStreams; i++) { string name; string buforInString; int bufor = 0; string waitingTime; string size; line = ""; while (line.Length < 2 || line[0] == '#') { line = sr.ReadLine(); } strings = line.Split(' '); if (strings[0] == "NAZWA" && strings[2] != "" && strings[3] == "KOLEJKA" && strings[5] != "" && strings[6] == "CZAS" && strings[8] != "" && strings[9] == "WIELKOSC" && strings[11] != "") { name = strings[2]; buforInString = strings[5]; // bufor = ToNumberOfString(buforInString); waitingTime = strings[8]; size = strings[11]; } else throw (new Exception("Zle dane strumienia")); for (int j = 0; j < TableOfPriorities.Length;j++ ) { if (buforInString == TableOfPriorities[j]) { bufor = j; break; } } stream[i] = new Stream(name, size, waitingTime, bufor, bufor); } #endregion Console.WriteLine("Przeciagnij tu plik wyjsciowy i wcisnij ENTER..."); outputDirectory = Console.ReadLine(); Console.WriteLine(" "); if (outputDirectory[0] == '"') outputDirectory = outputDirectory.Substring(1, outputDirectory.Length - 2); } catch (Exception def) { Console.WriteLine("Zla sciezka. Sprobuj jeszcze raz."); Console.WriteLine(def.Message); ok = false; } } }
// Entry point public static int Main(string[] args, ISimpleLineWriter output) { var start = DateTime.Now; var slicingOptions = new SlicingOptions(args); // we use it to parse the clousot options, and to split the work var clousot = new NewCCI2Driver(slicingOptions.remainingArgs.ToArray(), output); if (!clousot.CheckOptions()) { return -1; } var errorCode = 0; WorkerPool workerPool = null; IQueue queue = null; IWorkerFactory localWorkerFactory = null; List<IWorkerFactory> remoteWorkerFactories = null; Func<SliceDefinition, ISliceWorkResult> workOnSlice = null; Dictionary<ISliceId, int> failedRegressions = null; var localWorkers = slicingOptions.LocalWorkers; var remoteWorkers = slicingOptions.RemoteWorkers; var workers = localWorkers + remoteWorkers; // If we have workers, we create a Worker factory if (workers > 0) { IDB db; if (remoteWorkers > 0 || clousot.options.useCache) { // Use Clousot Database db = new StdDB(clousot.options); } else { // In-memory database db = new MemorySingletonDB(); } workerPool = new WorkerPool("Worker", canCancel: false); queue = new FIFOQueue(workerPool, db); // When a job is done, it choses which jobs to put in the queue (e.g., analyze the dependencies) var scheduler = clousot.options.InferObjectInvariantsOnlyForReadonlyFields? new LazySchedulerForObjectInvariants(queue, db) : // use LazySchedulerForObjectInvariants for global fixpoint computation including object invariants new LazyScheduler(queue, db); // use LazyScheduler for the global fixpoint computation //var scheduler = new NoloopScheduler(queue); // Usual cache var clousotDB = slicingOptions.useDB ? db : null; var argsForWorker = clousot.argsForWorker.ToFList(); if (localWorkers > 0) { if (slicingOptions.cci1) localWorkerFactory = new Clousot1WorkerFactory(scheduler, argsForWorker, output, clousotDB); else localWorkerFactory = new Clousot2WorkerFactory(scheduler, argsForWorker, output, clousotDB); // TODO: use a lighter version of ClousotMain since options are already checked here } if (remoteWorkers > 0) // so far 1 factory per remote worker but we can do better { // TODO: specifiy, for each address the number of workers // We have a list, because we can have several addresses remoteWorkerFactories = slicingOptions.serviceAddress.Select(addr => new Clousot2SWorkerFactory(scheduler, argsForWorker, output, addr)).ToList<IWorkerFactory>(); } if (clousot.options.IsRegression) { failedRegressions = new Dictionary<ISliceId, int>(); } // fail if any work fails scheduler.OnWorkDone += (sliceId, returnCode) => { if (errorCode >= 0) { if (returnCode < 0) // special error code, keep only one { errorCode = returnCode; } else { int prevValue; if (clousot.options.IsRegression) { lock (failedRegressions) { Contract.Assume(failedRegressions != null); if (failedRegressions.TryGetValue(sliceId, out prevValue)) { output.WriteLine("[Regression] We already analyzed {0} with outcome {1}. Now we update the outcome to {2}", sliceId.Dll, prevValue, returnCode); } failedRegressions[sliceId] = returnCode; } } errorCode += returnCode; // regression error count, additive } } }; // What we do for each slice. Two things: // 1. Register the slice in the db // 2. Add to the queue (via the scheduler, who decides how to do it) workOnSlice = sliceDef => { var sliceId = db.RegisterSlice(sliceDef); scheduler.FeedQueue(new ISliceId[] { sliceId }); return null; }; } ISlicerResult slicerResult = null; if (slicingOptions.sliceFirst) { slicerResult = clousot.SplitWork(workOnSlice); output.WriteLine("Slicing time: {0}", DateTime.Now - start); } if (workerPool != null) { if (localWorkerFactory != null) for (var i = 0; i < localWorkers; i++) workerPool.CreateWorker(localWorkerFactory); if (remoteWorkerFactories != null) foreach (var factory in remoteWorkerFactories) workerPool.CreateWorker(factory); } if (!slicingOptions.sliceFirst) { slicerResult = clousot.SplitWork(workOnSlice); output.WriteLine("Slicing time and thread creation time : {0}", DateTime.Now - start); } if (workerPool != null) { // workerPool != null ==> queue != null Contract.Assume(queue != null); workerPool.WaitAllAnd(queue.EmptyQueueWaitHandle); // Something else can arrive at the queue, so we want to stop all of them workerPool.StopAll(); } if (slicerResult != null) { var errors = slicerResult.GetErrors(); if (errors.Any()) { foreach (var errMessage in errors) output.WriteLine(errMessage); errorCode = errors.Count(); } } output.WriteLine("Total analysis time: {0}", DateTime.Now - start); var returnValue = errorCode; if (failedRegressions != null && clousot.options.IsRegression && errorCode >= 0) { returnValue = failedRegressions.Where(pair => pair.Value != 0).Select(pair => Math.Abs(pair.Value)).Sum(); } #if DEBUG if (clousot.options.IsRegression) { Console.WriteLine("[Regression] Returned value {0}", returnValue); } #endif return returnValue; }
private void reduceDomains(FIFOQueue<Variable> queue, CSP csp, DomainRestoreInfo info) { while (!queue.isEmpty()) { Variable var = queue.pop(); foreach (Constraint constraint in csp.getConstraints(var)) { if (constraint.getScope().Count == 2) { Variable neighbor = csp.getNeighbor(var, constraint); if (revise(neighbor, var, constraint, csp, info)) { if (csp.getDomain(neighbor).isEmpty()) { info.setEmptyDomainFound(true); return; } queue.push(neighbor); } } } } }
/// <summary> /// 构建出一个嵌套的连接型队列。 /// </summary> /// <returns></returns> private static ConcatenatedLRUQueue<IFrame> ConstructNestedConcatQueue(out IQueue<IFrame>[] FIFOs) { IQueue<IFrame>[] q = new FIFOQueue<IFrame>[8]; for (int i = 0; i < q.Length; i++) { q[i] = new FIFOQueue<IFrame>(); q[i].Enqueue(new Frame((uint)(i * 10))); q[i].Enqueue(new Frame((uint)(i * 10 + 1))); } IQueue<IFrame> q01 = new ConcatenatedLRUQueue<IFrame>(q[0], q[1]), q23 = new ConcatenatedLRUQueue<IFrame>(q[2], q[3]), q45 = new ConcatenatedLRUQueue<IFrame>(q[4], q[5]), q67 = new ConcatenatedLRUQueue<IFrame>(q[6], q[7]); IQueue<IFrame> q0123 = new ConcatenatedLRUQueue<IFrame>(q01, q23), q4567 = new ConcatenatedLRUQueue<IFrame>(q45, q67); ConcatenatedLRUQueue<IFrame> target = new ConcatenatedLRUQueue<IFrame>(q0123, q4567); FIFOs = q; return target; }
public FIFONodeStore() { queue = new FIFOQueue(); }