plinq

How to convert Parallel.For to PLINQ

只谈情不闲聊 提交于 2021-02-10 14:36:54
问题 Parallel.For(0, Height, new ParallelOptions { MaxDegreeOfParallelism = 6 }, y => { int currentLine = y * BMPData.Stride; for (int x = 0; x < Width; x = x + BPX) { var b = pixels[currentLine + x]; var g = pixels[currentLine + x + 1]; var r = pixels[currentLine + x + 2]; int avg = (r + g + b) / 3; pixels[currentLine + x] = (byte)avg; pixels[currentLine + x + 1] = (byte)avg; pixels[currentLine + x + 2] = (byte)avg; } }); This is basically a parallel code where it turns bitmap data pixels into

Impact of using AsParallel() and AsSequential() in the same query? C#

半城伤御伤魂 提交于 2021-01-28 11:23:00
问题 I was going through PLINQ in one of the books and it said: If you have a complex query that can benefit from parallel processing but also has some parts that should be done sequentially, you can use the AsSequential to stop your query from being processed in parallel. For Example: var parallelResult = numbers.AsParallel().AsOrdered() .Where(i => i % 2 == 0).AsSequential(); I want to understand why is it allowed and what is the impact on the result? Is it running parallel? Is it running

Does LINQ AsParallel() preserve thread safety?

与世无争的帅哥 提交于 2020-06-28 03:15:28
问题 I'm having some doublts about concurrency using LINQ AsParallel() . Suppose I have the following code: int counter = 0; someList.AsParallel().ForEach(item => { doStuff(); counter++; }); I haven't found much online... Is it safe to do something like this? Is there a better way of doing this? Should I do some locking action for counter ? Thanks in advance 回答1: Is it safe to do something like this? ( counter++ ) No. There is no thread-safety to begin with, just code that is single-threaded. When

Replace Task.WhenAll with PLinq

混江龙づ霸主 提交于 2020-01-25 06:39:08
问题 I'm having a method which calls a WCF service multiple times in parallel. To prevent an overload on the target system, I want to use PLinq's ability to limit the number of parallel executions. Now I wonder how I could rewrite my method in an efficient way. Here's my current implementation: private async Task RunFullImport(IProgress<float> progress) { var dataEntryCache = new ConcurrentHashSet<int>(); using var client = new ESBClient(); // WCF // Progress counters helpers float totalSteps = 1f

Combining PLINQ with Async method

邮差的信 提交于 2020-01-04 06:27:13
问题 I'm trying to combine my PLINQ statement like this: Enumerable.Range(0, _sortedList.Count()).AsParallel().WithDegreeOfParallelism(10) .Select(i => GetTransactionDetails(_sortedList[i].TransactionID)) .ToList(); With an async method like this: private async void GetTransactionDetails(string _trID) { await Task.Run(() => { }); } So that I can simply add an await operator in here: Enumerable.Range(0, _sortedList.Count()).AsParallel().WithDegreeOfParallelism(10) .Select(i => await

PLINQ on ConcurrentQueue isn't multithreading

北城以北 提交于 2020-01-02 17:50:24
问题 I have the following PLINQ statement in a C# program: foreach (ArrestRecord arrest in from row in arrestQueue.AsParallel() select row) { Geocoder geocodeThis = new Geocoder(arrest); writeQueue.Enqueue(geocodeThis.Geocode()); Console.Out.WriteLine("Enqueued " + ++k); } Both arrestQueue and writeQueue are ConcurrentQueues. Nothing is running in parallel: While running, total CPU usage is about 30%, and this is with everything else running, too. I have 8 cores (Hyper-Threading on a Core i7 720QM

Can we add new elements to a list using Parallel.ForEach()?

泄露秘密 提交于 2020-01-02 05:32:15
问题 My code does very simple stuff list already has elements. I have approximately 25000 elements (and I'm expecting to have more) in the list and each element is small (DateTime). List<DateTime> newList = new List<DateTime>(); Parallel.ForEach(list, l => newlist.Add(new DateTime(l.Ticks + 5000))); i.e, based on each element, I'm creating new elements and adding them to a different list. But, this doesn't seem to be a good programming approach. I hit this exceptions some times, but not everytime.

Does Parallel.ForEach require AsParallel()

那年仲夏 提交于 2020-01-01 08:02:57
问题 ParallelEnumerable has a static member AsParallel . If I have an IEnumerable<T> and want to use Parallel.ForEach does that imply that I should always be using AsParallel ? e.g. Are both of these correct (everything else being equal)? without AsParallel : List<string> list = new List<string>(); Parallel.ForEach<string>(GetFileList().Where(file => reader.Match(file)), f => list.Add(f)); or with AsParallel ? List<string> list = new List<string>(); Parallel.ForEach<string>(GetFileList().Where

Does Parallel.ForEach require AsParallel()

百般思念 提交于 2020-01-01 08:02:14
问题 ParallelEnumerable has a static member AsParallel . If I have an IEnumerable<T> and want to use Parallel.ForEach does that imply that I should always be using AsParallel ? e.g. Are both of these correct (everything else being equal)? without AsParallel : List<string> list = new List<string>(); Parallel.ForEach<string>(GetFileList().Where(file => reader.Match(file)), f => list.Add(f)); or with AsParallel ? List<string> list = new List<string>(); Parallel.ForEach<string>(GetFileList().Where