I want to make my code quickly. I have got 8 cores. And I want use all of them. How can I do this in optimal way? I think than I can do this in 2 ways:
1. Make short functions. And parallel them. But I think that there will be many time for give management transmission from one core to another. Am I right? I think that make very short operations like ?a = 2 + 2? is time, which will be expend for management transmission from one core to another will be more than speed increment by use parallel operations. Am I right?
2. Make big functions. I think if every function have long operation, parallel effect will be better.
But I made experiments and in fact first way is more quickly that second. Why the first way is more quickly and how can I do this code more quick by using parallel operations?
Results:[FONT="]1 iteration:
1 threads, time: 2,5947303
2 threads, time: 1,5046816
3 threads, time: 1,2435103
4 threads, time: 1,1743574
5 threads, time: 1,8177255
6 threads, time: 1,8564871
7 threads, time: 1,7038264
8 threads, time: 1,7404472
Iteration block:
1 threads, time: 1,2824387
2 threads, time: 1,2592897
3 threads, time: 1,3303499
4 threads, time: 1,3710368
5 threads, time: 1,4195757
6 threads, time: 1,4460356
7 threads, time: 1,5213963
8 threads, time: 1,6072681[/FONT]
1. Make short functions. And parallel them. But I think that there will be many time for give management transmission from one core to another. Am I right? I think that make very short operations like ?a = 2 + 2? is time, which will be expend for management transmission from one core to another will be more than speed increment by use parallel operations. Am I right?
2. Make big functions. I think if every function have long operation, parallel effect will be better.
But I made experiments and in fact first way is more quickly that second. Why the first way is more quickly and how can I do this code more quick by using parallel operations?
C#:
[B]using[/B] System;
[B]using[/B] System.Collections.Generic;
[B]using[/B] System.ComponentModel;
[B]using[/B] System.Data;
[B]using[/B] System.Drawing;
[B]using[/B] System.Linq;
[B]using[/B] System.Text;
[B]using[/B] System.Threading.Tasks;
[B]using[/B] System.Windows.Forms;
[B]using[/B] System.Diagnostics;
[B]using[/B] System.Threading;
[B]namespace[/B] WindowsFormsApplication2
{
[B]public[/B] [B]partial[/B] [B]class[/B] Form1 : Form
{
[B]public[/B] Form1()
{
InitializeComponent();
}
[B]double[/B] [] _dArray = new [B]double[/B][10000000];
[B]double[/B][] _dArray2 = new [B]double[/B][10000000];
[B]int[/B] _iThreads = 8;
[B]int[/B] _iSizeBlock;
[B]private[/B] [B]void[/B] button1_Click([B]object[/B] sender, EventArgs e)
{
_iSizeBlock = _dArray.Length / _iThreads;[I]//size of block[/I]
[I]//[/I][I]random[/I]
Random r = new Random();
[B]for[/B] ([B]int[/B] i = 0; i < _dArray.Length; i++)
{
_dArray[i] = r.NextDouble();
_dArray2[i] = _dArray[i];
}
richTextBox1.Text = "1 interation:[B]\r\n[/B]";
[B]for[/B] ([B]int[/B] i = 1; i <= 8; i++)
{
ParallelOptions options = new ParallelOptions
{
MaxDegreeOfParallelism = i
};
Stopwatch st1 = new Stopwatch();
st1.Start();
Parallel.[B]For[/B](0, _dArray.Length, options, parallelOne);
st1.Stop();
richTextBox1.Text += i.ToString() + " threads, time: " + st1.Elapsed.TotalSeconds.ToString() + "[B]\r\n[/B]";
}
richTextBox1.Text += "Iterations block:[B]\r\n[/B]";
[B]for[/B] ([B]int[/B] i = 1; i <= 8; i++)
{
ParallelOptions options = new ParallelOptions
{
MaxDegreeOfParallelism = i
};
Stopwatch st1 = new Stopwatch();
st1.Start();
Parallel.[B]For[/B](0, i, options, ParallelBlock);
st1.Stop();
richTextBox1.Text += i.ToString() + " threads, time: " + st1.Elapsed.TotalSeconds.ToString() + "[B]\r\n[/B]";
}
}
[B]private[/B] [B]void[/B] ParallelBlock([B]int[/B] iIndex)
{
[B]int[/B] iStart = iIndex * _iSizeBlock;
[B]int[/B] iEnd = iStart + _iSizeBlock;
[I]//iIndex ? number Of Block[/I]
[B]for[/B] ([B]int[/B] i = iStart; i < iEnd; i++)
{
_dArray[i] = Someoperations(_dArray[i]);
}
}
[B]private[/B] [B]void[/B] parallelOne([B]int[/B] iIndex)
{
_dArray[iIndex] = Someoperations(_dArray[iIndex]);
}
[B]private[/B] [B]double[/B] Someoperations([B]double[/B] dInput)
{
[B]double[/B] Result = Math.Sin(dInput) * Math.Log(dInput + 10);
Result = Math.Pow(Result, 10);
Result += Math.Abs(Math.Cos(Result));
Result += Math.Sqrt(Result);
Result = Math.Pow(Result, 2);
[B]return[/B] Result;
}
}
Results:[FONT="]1 iteration:
1 threads, time: 2,5947303
2 threads, time: 1,5046816
3 threads, time: 1,2435103
4 threads, time: 1,1743574
5 threads, time: 1,8177255
6 threads, time: 1,8564871
7 threads, time: 1,7038264
8 threads, time: 1,7404472
Iteration block:
1 threads, time: 1,2824387
2 threads, time: 1,2592897
3 threads, time: 1,3303499
4 threads, time: 1,3710368
5 threads, time: 1,4195757
6 threads, time: 1,4460356
7 threads, time: 1,5213963
8 threads, time: 1,6072681[/FONT]