parallel.foreach

Difference between ThreadPool.QueueUserWorkItem and Parallel.ForEach?

随声附和 提交于 2019-12-04 15:37:33
问题 What is the main difference between two of following approaches: ThreadPool.QueueUserWorkItem Clients objClient = new Clients(); List<Clients> objClientList = Clients.GetClientList(); foreach (var list in objClientList) { ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list); } System.Threading.Tasks.Parallel ForEach Clients objClient = new Clients(); List<Clients> objClientList = Clients.GetClientList(); Parallel.ForEach<Clients>(objClientList, list => { SendFilesToClient

How can I achieve maximum parallelism and utilize maximum CPU with Parallel.ForEach?

烂漫一生 提交于 2019-12-04 11:23:06
问题 There is a C# function A(arg1, arg2) which needs to be called lots of times. To do this fastest, I am using parallel programming. Take the example of the following code: long totalCalls = 2000000; int threads = Environment.ProcessorCount; ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = threads; Parallel.ForEach(Enumerable.Range(1, threads), options, range => { for (int i = 0; i < total / threads; i++) { // init arg1 and arg2 var value = A(arg1, agr2); // do

How can I maximize the performance of element-wise operation on an big array in C#

自古美人都是妖i 提交于 2019-12-04 08:18:32
The operation is to multiply every i-th element of a array (call it A) and i-th element of a matrix of the same size(B), and update the same i-th element of A with the value earned. In a arithmetic formula, A'[i] = A[i]*B[i] (0 < i < n(A)) What's the best way to optimize this operation in a multi-core environment? Here's my current code; var learningRate = 0.001f; var m = 20000; var n = 40000; var W = float[m*n]; var C = float[m*n]; //my current code ...[1] Parallel.ForEach(Enumerable.Range(0, m), i => { for (int j = 0; j <= n - 1; j++) { W[i*n+j] *= C[i*n+j]; } }); //This is somehow far

Parallel.Foreach starts to idle with Invoke [closed]

爱⌒轻易说出口 提交于 2019-12-04 07:33:01
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I have a problem with the Parallel.Foreach loop. It works as it should as long I dont Invoke a method to increase the Progressbar Value of the parent GUI program. Where KeinPapierVersand is a simple List<int> object and EinzelnachweisDruckDatum is a DateTime . Parallel.ForEach(KeinPapierVersand, partner => {

Thread safety of yield return with Parallel.ForEach()

一曲冷凌霜 提交于 2019-12-04 00:13:08
问题 Consider the following code sample, which creates an enumerable collection of integers and processes it in parallel: using System.Collections.Generic; using System.Threading.Tasks; public class Program { public static void Main() { Parallel.ForEach(CreateItems(100), item => ProcessItem(item)); } private static IEnumerable<int> CreateItems(int count) { for (int i = 0; i < count; i++) { yield return i; } } private static void ProcessItem(int item) { // Do something } } Is it guaranteed that the

Is it OK to do some async/await inside some .NET Parallel.ForEach() code?

不打扰是莪最后的温柔 提交于 2019-12-03 13:08:08
问题 Given the following code, is it OK to do async/await inside a Parallel.ForEach ? eg. Parallel.ForEach(names, name => { // Do some stuff... var foo = await GetStuffFrom3rdPartyAsync(name); // Do some more stuff, with the foo. }); or is there some gotcha's that I need to be made aware of? EDIT: No idea if this compiles, btw. Just Pseduo-code .. thinking out loud. 回答1: No, It doesn't make sense to combine async with Paralell.Foreach . Consider the following example: private void DoSomething() {

How can I assign a name to a task in TPL

送分小仙女□ 提交于 2019-12-03 09:50:19
I'm going to use lots of tasks running on my application. Each bunch of tasks is running for some reason. I would like to name these tasks so when I watch the Parallel Tasks window, I could recognize them easily. With another point of view, consider I'm using tasks at the framework level to populate a list. A developer that use my framework is also using tasks for her job. If she looks at the Parallel Tasks Window she will find some tasks having no idea about. I want to name tasks so she can distinguish the framework tasks from her tasks. It would be very convenient if there was such API: var

Difference between ThreadPool.QueueUserWorkItem and Parallel.ForEach?

雨燕双飞 提交于 2019-12-03 09:43:44
What is the main difference between two of following approaches: ThreadPool.QueueUserWorkItem Clients objClient = new Clients(); List<Clients> objClientList = Clients.GetClientList(); foreach (var list in objClientList) { ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list); } System.Threading.Tasks.Parallel ForEach Clients objClient = new Clients(); List<Clients> objClientList = Clients.GetClientList(); Parallel.ForEach<Clients>(objClientList, list => { SendFilesToClient(list); }); I am new to multi-threading and want to know what's going to happen in each case (in terms of

Is it OK to do some async/await inside some .NET Parallel.ForEach() code?

孤者浪人 提交于 2019-12-03 04:08:26
Given the following code, is it OK to do async/await inside a Parallel.ForEach ? eg. Parallel.ForEach(names, name => { // Do some stuff... var foo = await GetStuffFrom3rdPartyAsync(name); // Do some more stuff, with the foo. }); or is there some gotcha's that I need to be made aware of? EDIT: No idea if this compiles, btw. Just Pseduo-code .. thinking out loud. No, It doesn't make sense to combine async with Paralell.Foreach . Consider the following example: private void DoSomething() { var names = Enumerable.Range(0,10).Select(x=> "Somename" + x); Parallel.ForEach(names, async(name) => {

Parallel.Foreach starts to idle with Invoke [closed]

笑着哭i 提交于 2019-12-02 14:35:31
I have a problem with the Parallel.Foreach loop. It works as it should as long I dont Invoke a method to increase the Progressbar Value of the parent GUI program. Where KeinPapierVersand is a simple List<int> object and EinzelnachweisDruckDatum is a DateTime . Parallel.ForEach(KeinPapierVersand, partner => { generate_PCL_nachweis(partner, EinzelnachweisDruckDatum, true, false); generate_BGF_Report(partner, EinzelnachweisDruckDatum, false); //If the following line is uncommented, the loop starts to idle after about 200 // processed Items and will never reach the code after the loop.