Parallel programming preliminary, Parallel programming

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.