/// <summary> /// Cancels any current <see cref="Wait" /> calls /// </summary> public void Cancel() { ThrowIfDisposed(); _waitForEvent.Value.Cancel(); _waitForEventForGroups.AsParallel().ForAll(w => w.Value.Cancel()); }
/// <summary> /// Get the the <c>id</c> of the face matching <paramref name="template"/> the closest and the <c>confidence</c> of the match /// </summary> /// <param name="template"></param> /// <returns><c>id</c> of the best matching face and <c>confidence</c> value [0, 1]</returns> public Match <TTemplate> GetBestMatch(TTemplate template) { var matches = from f in _storedFaces.AsParallel() let faceInfo = f.Value let match = faceInfo.GetSimilarity(template) select new Match <TTemplate>(f.Key, match.similarity, match.snapshot, faceInfo); return(matches.Max()); }
public List <FileReference> GetFileReferences(string programName) { if (string.IsNullOrWhiteSpace(programName)) { return(null); } Console.Write(@"Searching for Program " + programName); var candidates = _files.AsParallel().Where(file => file.Key.Contains(programName)).Select(file => file.Value).ToList(); Console.WriteLine(candidates.Any() ? " => succeeded" : " => failed"); return(candidates); }
private void OnUpdate() { var results = _requests .AsParallel() .Select(r => { var context = _battleContextsByRoomId[r.Key]; var pushModels = _onUpdate(context, r.Value.ToArray()); _responses.AddOrUpdate( r.Key, key => pushModels, (key, models) => pushModels); return(0); }) .ToArray(); foreach (var responses in _responses) { var data = _serializer.Serialize(responses.Value); _disquuun.AddJob(responses.Key, data); } _requests.Clear(); _responses.Clear(); }
private static decimal queryTotals(string storeId, int year, int week, string suplierName, string suplierType) { var xxx = orders55.AsParallel().AsEnumerable() .Where(x => x.Key.Id == (FirstValueOrDefault(storeId, x.Key.Id))) .Where(x => x.Key.DateF.Year == (FirstValueOrDefault(year, x.Key.DateF.Year))) .Where(x => x.Key.DateF.Week == (FirstValueOrDefault(week, x.Key.DateF.Week))) ; ConcurrentBag <decimal> sumTotal = new ConcurrentBag <decimal>(); ////Parallel.ForEach(xxx, entry => // //foreach (KeyValuePair<Index, IEnumerable<OrderDetail>> entry in xxx) xxx.AsParallel().ForAll(entry => { sumTotal.Add( entry.Value/*.IeList*/ /*.AsParallel()*/ .Where(z => z.SuplierName == (FirstValueOrDefault(suplierName, z.SuplierName))) .Where(z => z.SuplierType == (FirstValueOrDefault(suplierType, z.SuplierType))) .Select(x => x.cost) .Sum() ) ; }); return(sumTotal.AsParallel().Sum()); }
public static Tuple <string, int>[] compareMatch(string toCmp, ConcurrentDictionary <string, IBnBObject> all) { return((from kvp in all.AsParallel().WithMergeOptions(ParallelMergeOptions.FullyBuffered) select Tuple.Create(kvp.Value.Name, GetDamerauLevenshteinDistance(toCmp, kvp.Value.Name))) .OrderBy(selector => selector.Item2).ToArray()); }
static void RemoveLowestFee(ConcurrentDictionary <UInt256, PoolItem> pool, int count) { if (count <= 0) { return; } if (count >= pool.Count) { pool.Clear(); } else { UInt256[] delete = pool.AsParallel() .OrderBy(p => p.Value.Transaction.NetworkFee / p.Value.Transaction.Size) .ThenBy(p => p.Value.Transaction.NetworkFee) .ThenBy(p => new BigInteger(p.Key.ToArray())) .Take(count) .Select(p => p.Key) .ToArray(); foreach (UInt256 hash in delete) { pool.TryRemove(hash, out _); } } }
static HashSet <Date> GetDateHashSet(ConcurrentDictionary <Index, IEnumerable <OrderDetail> > dic) { List <Date> l = dic.AsParallel().Select(x => x.Key.DateF).ToList(); HashSet <Date> result = new HashSet <Date>(l); return(result); }
public ItemPurchaseTracker Clone() { ItemPurchaseTracker tracker = new ItemPurchaseTracker(ItemId); PerMatchCounts.AsParallel().ForAll(kvp => tracker.PerMatchCounts.TryAdd(kvp.Key, kvp.Value.Clone())); return(tracker); }
public static void Run(params string[] args) { int peopleCount = 1000; if (args.Length > 0) { Int32.TryParse(args[0], out peopleCount); } string[] maleNames = null, femaleNames = null, surnames = null; var y = InstrumentedOperation.Test(() => LoadAllData(out maleNames, out femaleNames, out surnames), "LoadAllData"); var allNames = (maleNames.Shuffle().Union(femaleNames.Shuffle())) .ToArray() .Shuffle() .SelectMany(firstname => surnames.Select(lastname => Tuple.Create(firstname, lastname))) .Select(name => new Person(name.Item1, name.Item2)); peopleCount = 1000000; ConcurrentDictionary <string, Person> xs = new ConcurrentDictionary <string, Person>(); InstrumentedOperation.Test(() => { allNames.ToArray() .Shuffle() .Take(peopleCount) .AsParallel() .ForAll(p => xs.TryAdd(p.Key, p)); }, "Adding people to the dictionary (data parallelism)"); ConcurrentBag <Person> xs1 = new ConcurrentBag <Person>(), xs2 = new ConcurrentBag <Person>(); InstrumentedOperation.Test(() => { Parallel.Invoke ( () => Parallel.ForEach(xs, x => { var p = x.Value; if (p.FullName.Length > 12) { xs1.Add(p); } }), () => { xs.AsParallel() .Select(x => x.Value) .Where(p => p.FullName.StartsWith("A") || p.FullName.StartsWith("B")) .ForAll(xs2.Add); } ); }, "Filtering people from the dictionary (task parallelism)"); ConsoleEx.WriteLn(xs1.Select(p => p.FullName).AsEnumerable(), 5, "fullname length greater than 12: "); ConsoleEx.WriteLn(xs2.Select(p => p.FullName).AsEnumerable(), 5, "fullname starts with A or B: "); }
private List <IMessageModel> GetlistOfUnconfirmedMessagesParallel() { return(_workingSet .AsParallel() .Select(kvp => kvp.Value) .Where(m => _completitionTimeoutChecker.IsExceeded(m)) .ToList()); }
/// <summary> /// tcp广播 /// </summary> /// <param name="tag">套接字标记</param> /// <param name="buffer">字节流</param> /// <returns>发送结果</returns> public void Broadcast(string tag, List <byte> buffer) { foreach (var socket in _sockets.AsParallel() .Where(s => s.Value.Tag == tag)) { socket.Value.Send(null, buffer); } }
public async Task SendChip(SocketMessage message, string[] args) { string name = (args.Length < 2) ? args[0] : args[1]; bool exists = this.chipLibrary.TryGetValue(name.ToLower(), out Chip Value); if (exists) { await message.Channel.SendMessageAsync("```" + Value.All + "```"); //await sendChipAsEmbed(message, Value); return; } //else doesn't exist var chipList = (from kvp in chipLibrary.AsParallel(). WithMergeOptions(ParallelMergeOptions.FullyBuffered) where kvp.Key.Contains(name.ToLower()) select kvp.Value.Name).OrderBy(chip => chip).ToArray(); switch (chipList.Length) { case 0: { //no chips found //await message.Channel.SendMessageAsync("That doesn't exist"); //return; //StringSearch.compareMatch(name, chipLibrary); var res = (from kvp in chipLibrary.AsParallel().WithMergeOptions(ParallelMergeOptions.FullyBuffered) select Tuple.Create(kvp.Value.Name, StringSearch.GetDamerauLevenshteinDistance(name.ToLower(), kvp.Value.Name.ToLower()))) .OrderBy(selector => selector.Item2).Take(5); var toret = (from tup in res select tup.Item1); StringBuilder builder = new StringBuilder(); builder.Append("Did you mean: "); builder.Append(string.Join(", ", toret)); await message.Channel.SendMessageAsync(builder.ToString()); return; } case 1: { //one chip has a name that contains it this.chipLibrary.TryGetValue(chipList[0].ToLower(), out Chip foundVal); await message.Channel.SendMessageAsync("```" + foundVal.All + "```"); //await sendChipAsEmbed(message, foundVal); return; } default: { await SendStringArrayAsMessage(message, chipList); return; } } }
public Variant Set(string key, Variant value, BaseProtocol pFrom = null) { if (value == null || value == VariantType.Null) { UnSet(key); return(value); } if (!_versionIncremented) { Version++; _versionIncremented = true; } Payload[key] = value; var updateDirtyInfo = new DirtyInfo { PropertyName = key, Type = Defines.SOT_SC_UPDATE_DATA }; Synchronization?.Invoke(updateDirtyInfo); #if PARALLEL _dirtyPropsByProtocol.AsParallel().ForAll(x => x.Value.Add(new DirtyInfo { PropertyName = key, Type = x.Key == protocolId ? Defines.SOT_SC_UPDATE_DATACK : Defines.SOT_SC_UPDATE_DATA })); #else foreach (var registeredProtocol in _dirtyPropsByProtocol) { if (registeredProtocol.Key == pFrom) { if (pFrom is BaseClusterProtocol) { continue; } registeredProtocol.Value.Add(new DirtyInfo { PropertyName = key, Type = Defines.SOT_SC_UPDATE_DATA_ACK }); } else { registeredProtocol.Value.Add(updateDirtyInfo); } } #endif return(Payload[key]); }
public void Measurement() { _cpuUses.AsParallel().ForAll( processCounterWithCpuUses => { processCounterWithCpuUses.Value.StorePerformanceCounter(); } ); }
/// <summary> /// Provides a predicate check from all <see cref="ConcurrentDictionary{TKey, TValue}"/> entries, /// but rather than create a new collection, it'll do a loop through exist entries in parallel. /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="collection"></param> /// <param name="predicate"></param> /// <exception cref="InvalidOperationException"></exception> /// <returns></returns> public static KeyValuePair <TKey, TValue>[] EntryWhereAsParallel <TKey, TValue>(this ConcurrentDictionary <TKey, TValue> collection, Func <KeyValuePair <TKey, TValue>, bool> predicate) { if (collection == null) { throw new InvalidOperationException("Collection must be not null."); } return(collection.AsParallel().Where(entry => predicate.Invoke(entry)).Select(entry => entry).ToArray()); }
/// <summary> /// Provides a predicate check from all <see cref="ConcurrentDictionary{TKey, TValue}.Values"/> entries, /// but rather than create a new collection, it'll do a loop through exist values in parallel. /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="collection"></param> /// <param name="predicate"></param> /// <exception cref="InvalidOperationException"></exception> /// <returns></returns> public static TValue[] ValueWhereAsParallel <TKey, TValue>(this ConcurrentDictionary <TKey, TValue> collection, Predicate <TValue> predicate) { if (collection == null) { throw new InvalidOperationException("Collection must be not null."); } return(collection.AsParallel().Where(entry => predicate.Invoke(entry.Value)).Select(entry => entry.Value).ToArray()); }
private void Loop(CancellationToken token) { new Thread(() => { while (!token.IsCancellationRequested) { try { var tick = queue.DequeueAsync(token).Result; logger.LogDebug("receive quote: {0}", tick); quotes.AsParallel().Where(q => q.Source == tick.Symbol).ForAll(q => { q.SetQuote(tick, logger); if (q.Change) { var quoteString = q.ToUniFeederStringFormat(); logger.LogDebug("receive ({0} {1} {2}) => ({3} {4} {5}) use translate: {6}", tick.Symbol, tick.Bid, tick.Ask, q.Symbol, q.LastBid, q.LastAsk, q.ToStringTranslates()); var quoteUniFeederFormat = quoteString.ToUniFeederByteArray(); clients.AsParallel().ForAll(c => { try { c.Value.Send(quoteUniFeederFormat); } catch (Exception e) { logger.LogError("client: {0} error send quote: {1}", c.Key, e.Message); Task.Run(() => RemoveClient(c.Key, 5)); } }); logger.LogDebug("quote sending {0} clients", clients.Count); } else { logger.LogDebug("quote not changed therefore it is not sent by the client"); } }); } catch (Exception e) { if (e is OperationCanceledException || e.InnerException is OperationCanceledException) { break; } else { logger.LogError("error in queue processing: {0}", e.Message); } } } }) { IsBackground = true }.Start(); }
private AngularChart GetLaptopSoldChart(List <SaleOrder> paidSaleOrders, NancyBlackDatabase db) { var chart = new AngularChart() { Title = "Sold Laptop", Labels = new List <string>(), EnumType = AngularChartType.Pie, IsLegend = true, Data = new List <dynamic>() }; var lookupLaptopId = new ConcurrentDictionary <int, int>(); // count sold laptop paidSaleOrders.AsParallel().ForAll((so => { // find laptop from item's url var laptop = so.ItemsDetail.Where(item => item.Url.StartsWith("/products/laptops")).FirstOrDefault(); // find laptop in another type (archive-laptops) if (laptop == null) { laptop = so.ItemsDetail.Where(item => item.Url.StartsWith("/products/archive-laptops")).FirstOrDefault(); } // count sold laptop when found one if (laptop != null) { var newQty = (int)laptop.Attributes.Qty; lookupLaptopId.AddOrUpdate(laptop.Id, newQty, (laptopId, existQty) => existQty + newQty); } })); var soldLaptopsData = new ConcurrentBag <dynamic>(); // group.Key = laptop's id and group.Value = laptop sold's count lookupLaptopId.AsParallel().ForAll((group => { var laptop = db.GetById <Product>(group.Key); soldLaptopsData.Add(new { Id = group.Key, Title = laptop.Title, Quantity = group.Value, Price = laptop.Price }); })); foreach (var laptop in soldLaptopsData.OrderBy(product => product.Price)) { chart.Labels.Add(laptop.Title); chart.Data.Add(laptop.Quantity); } return(chart); }
public static void Shutdown() { _tasks.AsParallel().ForAll(t => { t.Value._waitHandle.Set(); t.Value._abortHandle.Set(); }); Task.WaitAll(_tasks.Select(t => t.Value._task).ToArray(), TimeSpan.FromSeconds(20)); _tasks.Clear(); }
static string MostFrequentTriplet(string str, CancellationToken ct) { if (str == null || ct == null) { throw new ArgumentNullException(); } //Разделение на слова var words = str.Split(',').AsParallel().WithCancellation(ct) .Select(x => x.Trim()).Where(x => x != String.Empty); if (words.Count() == 0) { throw new ArgumentException(nameof(str)); } //Подсчёт кол-ва повторений триплетов var counters = new ConcurrentDictionary <string, int>(); var options = new ParallelOptions { CancellationToken = ct }; Parallel.ForEach(words, options, word => { Parallel.For(0, word.Length - 2, options, (j, s) => { var key = word.Substring(j, 3); counters.AddOrUpdate(key, 1, (k, i) => i + 1); }); }); //Максимальное число совпадений var max = counters.AsParallel() .WithCancellation(ct).Max(x => x.Value); //Все триплеты с max числом вхождений var res = counters.AsParallel().WithCancellation(ct) .Where(x => x.Value == max).Select(x => x.Key); //Форматирование результата return(String.Format("{0}\t{1}", String.Join(", ", res), max)); }
/// <summary> /// Starts battle /// </summary> public void StartFight() { if (fighters.Count < 1) { throw new InvalidOperationException("There should be at least couple of fighters to start a battle!"); } // without setting a degree of parallelism, it would only use the same number as number of cores fighters.AsParallel(). WithDegreeOfParallelism(fighters.Count). ForAll(f => f.Value.StartShootingAt(fighters)); // print winner after fight finished ObserveBattlefield(fighters.First().Value, "is a winner!"); }
public async Task SendVirus(SocketMessage message, string[] args) { if (args.Length < 2) { await message.Channel.SendMessageAsync("You must specify a virus name"); return; } args = args.Skip(1).Take(args.Length - 1).ToArray(); string name = string.Join(" ", args); bool exists = this.compendium.TryGetValue(name.ToLower(), out Virus foundVirus); if (exists) { string toSend = string.Format("```{0} - CR {1}\n{2}```", foundVirus.Name, foundVirus.CR, foundVirus.Description); await message.Channel.SendMessageAsync(toSend); return; } var virusList = (from kvp in compendium.AsParallel(). WithMergeOptions(ParallelMergeOptions.FullyBuffered) where kvp.Key.Contains(name.ToLower()) select kvp.Value.Name).OrderBy(Virus => Virus).ToArray(); switch (virusList.Length) { case 0: { var res = (from kvp in compendium.AsParallel().WithMergeOptions(ParallelMergeOptions.FullyBuffered) select Tuple.Create(kvp.Value.Name, StringSearch.GetDamerauLevenshteinDistance(name.ToLower(), kvp.Value.Name.ToLower()))) .OrderBy(selector => selector.Item2).Take(5); var toret = (from tup in res select tup.Item1); StringBuilder builder = new StringBuilder(); builder.Append("Did you mean: "); builder.Append(string.Join(", ", toret)); await message.Channel.SendMessageAsync(builder.ToString()); return; } case 1: { this.compendium.TryGetValue(virusList[1].ToLower(), out foundVirus); string toSend = string.Format("```{0} - CR {1}\n{2}```", foundVirus.Name, foundVirus.CR, foundVirus.Description); await message.Channel.SendMessageAsync(toSend); return; } default: await Library.SendStringArrayAsMessage(message, virusList); return; } }
/// <summary> /// 关闭已连接 socket 集合 /// </summary> /// <returns></returns> protected virtual async Task CloseConnectList() { await Task.Run(() => { if (this.connectedEntityList != null) { connectedEntityList.AsParallel().ForAll(f => { FreeUserToken(RemoveUserToken(f.Value)); }); this.connectedEntityList.Clear(); } }); }
public KeyValuePair <string, int>[] GetTop10Users(string logPath) { var usersStats = new ConcurrentDictionary <string, int>(); File.ReadLines(logPath).AsParallel() .Select(IpInfo.Parse) .ForAll(ipInfo => { usersStats.AddOrUpdate(ipInfo.Ip, key => ipInfo.CallDuration, (key, oldDuration) => oldDuration + ipInfo.CallDuration); }); return(usersStats.AsParallel() .OrderByDescending(keyValuePair => keyValuePair.Value).Take(10).ToArray()); }
private static void DoGetsupplierName(ConcurrentDictionary <Index, IEnumerable <OrderDetail> > dic) { ConcurrentDictionary <string, string> supplierNamesP = new ConcurrentDictionary <string, string>(); ConcurrentDictionary <string, string> supplierTypeP = new ConcurrentDictionary <string, string>(); dic.AsParallel().ForAll(x => { foreach (var y in x.Value) { supplierNamesP.TryAdd(y.SuplierName, null); supplierTypeP.TryAdd(y.SuplierType, null); } }); supplierNames = new HashSet <string>(supplierNamesP.Keys); supplierTypes = new HashSet <string>(supplierTypeP.Keys); }
public IList <SearchResult> Search(float[] feat, int topK) { return(m_feats .AsParallel() .Select(item => { var dis = Distance.Euclidean(feat, item.Value); return new SearchResult { Index = item.Key, Dis = dis }; }) .OrderBy(item => item.Dis) .Take(topK) .ToList()); }
private static void BufferCollectLoop() { States.AsParallel().ForAll(x => { if (!(x.Value.Thread?.IsAlive ?? false)) { if (States.TryRemove(x.Key, out var deadState)) { deadState.Asm?.Dispose(); } else { Console.Error.WriteLine("WARNING: ConcurrentDictionary<int, HookState> Hook.States --> Call to Hook.States.TryRemove(...) failed."); } } }); if (BufferCollectLoopStopTrigger.WaitOne(1000)) { BufferCollectLoopStopTriggerCallback.Set(); } }
/// <summary> /// slowest /// </summary> /// <param name="s"></param> /// <returns></returns> static string parallelUpperOrdered(string s) { System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); char [] newstring = new char [s.Length]; var oldstring = s.ToCharArray(); ConcurrentDictionary <int, char> indexedList = new ConcurrentDictionary <int, char>(); for (int i = 0; i < s.Length; i++) { indexedList.TryAdd(i, oldstring[i]); } indexedList .AsParallel() .ForAll(kvp => newstring[kvp.Key] = Convert.ToChar(kvp.Value.ToString().ToUpper())); timer.Stop(); Console.WriteLine(timer.ElapsedMilliseconds); return(new string(newstring)); }
public static Task Start(int limitToMonitor, int limitThreshold) { _limitToMonitor = limitToMonitor; _limitThreshold = limitThreshold; Task.Run(async() => { while (true) { foreach (var device in DeviceMeasurementToProcess.AsParallel()) { // todo : this is not 100% safe if (!ProcessedDeviceMeasurement.ContainsKey(device.Key)) { ProcessedDeviceMeasurement.TryAdd(device.Key, new List <DeviceMeasurement>()); } if (ProcessedDeviceMeasurement[device.Key].Count > _limitThreshold) { Console.WriteLine($"device {device.Key} has reached the threshold : {_limitToMonitor}"); } foreach (var measurement in device.Value.ToList()) { if (measurement.Temperature >= _limitToMonitor) { ProcessedDeviceMeasurement[device.Key].Add(measurement); } device.Value.Remove(measurement); } } await Task.Delay(1000); } }); return(Task.CompletedTask); }
private async Task<ConcurrentDictionary<Document, ConcurrentQueue<ValueTuple<ISymbol, IReferenceFinder>>>> CreateDocumentMapAsync( ConcurrentDictionary<Project, ConcurrentQueue<ValueTuple<ISymbol, IReferenceFinder>>> projectMap) { using (Logger.LogBlock(FeatureId.FindReference, FunctionId.FindReference_CreateDocumentMapAsync, this.cancellationToken)) { Func<Document, ConcurrentQueue<ValueTuple<ISymbol, IReferenceFinder>>> createQueue = d => new ConcurrentQueue<ValueTuple<ISymbol, IReferenceFinder>>(); var documentMap = new ConcurrentDictionary<Document, ConcurrentQueue<ValueTuple<ISymbol, IReferenceFinder>>>(); #if PARALLEL Roslyn.Utilities.TaskExtensions.RethrowIncorrectAggregateExceptions(cancellationToken, () => { projectMap.AsParallel().WithCancellation(cancellationToken).ForAll(kvp => { var project = kvp.Key; var projectQueue = kvp.Value; projectQueue.AsParallel().WithCancellation(cancellationToken).ForAll(symbolAndFinder => { var symbol = symbolAndFinder.Item1; var finder = symbolAndFinder.Item2; var documents = finder.DetermineDocumentsToSearch(symbol, project, cancellationToken) ?? SpecializedCollections.EmptyEnumerable<Document>(); foreach (var document in documents.Distinct().WhereNotNull()) { if (includeDocument(document)) { documentMap.GetOrAdd(document, createQueue).Enqueue(symbolAndFinder); } } }); }); }); #else foreach (var kvp in projectMap) { var project = kvp.Key; var projectQueue = kvp.Value; foreach (var symbolAndFinder in projectQueue) { this.cancellationToken.ThrowIfCancellationRequested(); var symbol = symbolAndFinder.Item1; var finder = symbolAndFinder.Item2; var documents = await finder.DetermineDocumentsToSearchAsync(symbol, project, this.documents, cancellationToken).ConfigureAwait(false) ?? SpecializedCollections.EmptyEnumerable<Document>(); foreach (var document in documents.Distinct().WhereNotNull()) { if (this.documents == null || this.documents.Contains(document)) { documentMap.GetOrAdd(document, createQueue).Enqueue(symbolAndFinder); } } } } #endif Contract.ThrowIfTrue(documentMap.Any(kvp => kvp.Value.Count != kvp.Value.ToSet().Count)); return documentMap; } }