Parallel programming preliminary, Parallel programming
ParallelParallel Programming allows us to useUltimateCPU usage.Parallel Programming is different from multi-threaded programming.Multi-threaded programming, no matter how to enable the thread, is also inOn the same CPUSwitch the time slice. Parallel programming is moreCPU core at the same timeWork. It is wise to choose parallel time-consuming CPU computing operations. Generally, each CPU core representsHardware thread,Hyper-threadingTechnology enables a cpu core to have two hardware threads. As the name suggests, software threads are enabled in programs.
The following shows an example of the most basic parallel programming, which is sufficient to reflect the benefits of multi-core parallel operation. Of course, Microsoft. NET is encapsulated for us, and we don't have to pay too much attention to the underlying operations, so let's take a look at the running results.
Static void Main (string [] args) {# region experiment 1 var watch = Stopwatch. startNew (); watch. start (); FirstOption (); SecondOption (); Console. writeLine ("Serial programming time: {0}", watch. elapsedMilliseconds); watch. restart (); Parallel. invoke (FirstOption, SecondOption); watch. stop (); Console. writeLine ("concurrent programming time: {0}", watch. elapsedMilliseconds); Console. read ();} static void FirstOption () {// pretend that the CPU is time-consuming to operate the Console. writeLine ("I started at {0}", DateTime. now); Thread. sleep (1, 3000); Console. writeLine ("I am Task 1, it takes 3 s");} static void SecondOption () {// pretend that the CPU time is used to operate the Console. writeLine ("I started at {0}", DateTime. now); Thread. sleep (1, 5000); Console. writeLine ("I am Task 2, it takes 5 s ");}
After reading the results, I believe you don't need to say it. You also understand that parallel operations are performed by multiple cores at the same time.
You may ask what to do if I want to execute a dozen similar options in the example, it depends on the number of your cores. If the number of cores is insufficient, not all operations are performed in the same second.
The code won't be available. It means 11 options. We can see that my configuration is very good. Sometimes, only two options are enabled in the same second, because the other option takes a long time, in occupied status.
The following describes the For loop of Parallel. And limit the number of hardware threads that can be used.
The Code is as follows:
Var bag = new ConcurrentBag <int> (); GC. collect (); watch. start (); ParallelOptions options = new ParallelOptions (); // set the number of hardware threads options. maxDegreeOfParallelism = 1; // options. maxDegreeOfParallelism = 2;
// Options. MaxDegreeOfParallelism = 3;
// Options. maxDegreeOfParallelism = 4; Parallel. for (0, 20000000, options, I => {bag. add (I) ;}); // for (int I = 0; I <20000000; I ++) // {// bag. add (I); //} watch. stop (); Console. writeLine ("Parallel Computing: Set: {0}, time used: {1}", bag. count, watch. elapsedMilliseconds); Console. readKey ();
I used 1, 2, 3 hardware threads and 4 hardware threads for comparison. The effect is obvious!
The For loop of Parallel is actually the same as our general serial programming functions, but in Parallel programming, the. NET underlying layer helps us to use CPU to a large extent. If you do not limit the number of hardware threads, resources are usually used as much as possible.
I also tested the serial code.
In addition, due to the environmental impact, the advantage of parallelism is not absolute.