Manage many repetitive CPU intensive tasks running in parallel?

I need to constantly perform 20 repetitive calculations with intensive computation as quickly as possible. Thus, there are 20 tasks that contain cyclic methods in:

while(!token.IsCancellationRequested) 

to repeat them as quickly as possible. All calculations are performed simultaneously. Unfortunately, this makes the program immune, so he added:

await Task.Delay(15); 

At this point, the program does not freeze, but adding Delay is the wrong approach, and it unreasonably slows down the computation speed. This is a WPF program without MVVM. What approach would you suggest keeping all 20 tasks at the same time? Each of them will be constantly repeated as soon as it ends. I would like to keep the load on the processor (all cores) with maximum values ​​(or close) to ensure maximum efficiency.

EDIT : There are 20 controls in which the user configures some settings. Calculations are performed in:

private async Task Calculate()
{
   Task task001 = null;
   task001 = Task.Run(async () => 
   {
      while (!CTSFor_task001.IsCancellationRequested)
      {
          await Task.Delay(15);
          await CPUIntensiveMethod();
      }
   }, CTSFor_task001.Token);
}

Each control is independent. Computations are 100% CPU related, without I / O. (All values ​​are taken from variables). During calculations, the values ​​of some user interface elements change:

 this.Dispatcher.BeginInvoke(new Action(() =>
     {
          this.lbl_001.Content = "someString";
     }));
+4
3

. , ( - ). , - .

(.. op 1 op 2, op 3, op 4...). , , " ", .. , , ( await async, , yay!).

. CPU, -. , - I/O, .

Parallelism - , . , , . , - .

() . , 99% , , .

parallelism (working) .

. - , , , , , , CPU ( ) - , , , ; , . , i ++, , 3 100 . 3 99% .

, CPU. parallelism, . , UI I/O, .

, async Calculate , , . I/O.

, Calculate, . , , . , Task.Run , .

? . , :

, , , . Task. , - .., . , .

, , , . await - :

private btn_Click(object sender, EventArgs e)
{
  var result = await Task.Run(Calculate);

  // Do some (little) work with the result once we get it
  tbxResult.Text = result;
}

async .

, , .

+5

, , - . Luaan, async await , Task.Run ; - .

. 20 BufferBlock, TPL DataFlow library.

, :

  • : Timer.
  • : Control.BeginInvoke
+1

@Luaan, async/await, , parallelism.

, - , CPUIntensiveMethod . await Calculate ( ) , while.

private async Task Calculate()
{
   while (!CTSFor_task001.IsCancellationRequested)
   {
       await Task.Run(CPUIntensiveMethod);
   }   
}
0
source

Source: https://habr.com/ru/post/1527862/


All Articles