Preliminary parallel parallel programming

Source: Internet
Author: User

Parallel Parallel programming allows us to use the extreme CPU. parallel programming differs from multithreaded programming in that multithreaded programming, regardless of how threads are opened, also switches time slices on the same CPU . Parallel programming is a multi- CPU core that works at the same time . It is advisable to choose parallelism for time-consuming CPU compute operations. Typically, each CPU core represents a hardware thread , but Hyper-threading technology allows a CPU core to have two hardware threads. The software thread is the name that we open in the program.

The following is an example of the most basic parallel programming, also sufficient to embody the benefits of multi-core parallel operation, of course Microsoft. NET is packaged for us, we don't have to pay much attention to the underlying operations, so let's look at the results of the operation.

Static voidMain (string[] args) {            #regionExperiment 1varWatch =stopwatch.startnew (); Watch.            Start ();            Firstoption ();            Secondoption (); Console.WriteLine ("Serial Programming Time: {0}", watch.            Elapsedmilliseconds); Watch.            Restart ();            Parallel.Invoke (Firstoption, secondoption); Watch.            Stop (); Console.WriteLine ("Parallel Programming Time: {0}", watch.            Elapsedmilliseconds);        Console.read (); }        Static voidfirstoption () {//pretending that CPU time is taking actionConsole.WriteLine ("I started at time {0}", DateTime.Now); Thread.Sleep ( the); Console.WriteLine ("I'm mission one, time consuming 3s"); }        Static voidsecondoption () {//pretending that CPU time is taking actionConsole.WriteLine ("I started at time {0}", DateTime.Now); Thread.Sleep ( the); Console.WriteLine ("I am mission two, time consuming 5s"); }

After reading the results, I believe you do not need me to say, you also understand that parallel operation by multi-core operation.

Maybe you'll ask if I need to do more than 10 options like the one in the example, it depends on the number of cores you have, and if the number of cores is insufficient, not all operations are in the same second.

Code is not on, is to get 11 option, you can see my configuration is very vegetable, sometimes the same second open only two option, because the other option time is longer, in the occupied state.

The For loop for parallel is described below. and limit the number of hardware threads it can use.

The code is as follows:

 varBag =Newconcurrentbag<int>(); Gc.            Collect (); Watch.            Start (); ParallelOptions Options=Newparalleloptions (); //setting the number of hardware threadsOptions. 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 Computation: The collection has: {0}, Time used: {1}", bag. Count, watch. Elapsedmilliseconds); Console.readkey ();

I compared the number of hardware threads to 4 hardware threads respectively. The effect is obvious!

The For loop about parallel is actually the same as our normal serial programming function, except in parallel programming. NET bottom-up helps us to use the CPU very much. If you do not limit the number of hardware threads, you will typically use resources as much as possible.

I also tested the serial code.

In addition, the advantage of parallelism is not absolute because of environmental impact.

Preliminary parallel parallel programming

Related Article

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.