Misunderstanding of DotNet parallel computing (2)

Source: Internet
Author: User
Tags mscorlib

Misunderstanding of using html "target = _ blank> DotNet parallel computing (1)

● Misunderstanding 3. parallel computing is a matter of Operation

Indeed, DotNet will decide whether to use a parallel library to Process Code at runtime, but as early as you compile the code, the compiler has already prepared for this moment, in other words:

1. The structure of IL is different from that of the normal method for processing code using the parallel library.

2. Even if you choose to use parallel computing, and you do have a multi-core parallel worker (thread) CPU, your code is not necessarily parallel at runtime.

After TPL is used, CLR may break down tasks. One of the rationale is supported by IL. IL separates parallel task code to facilitate parallel operations in future, this can be seen from the following example. The core C # Code in the following two examples is Tostring () and Sleep (). Code A uses For to include Sleep, and Code B uses Parallel. for processing:

Code Part:

IL:

IL_000e: callvirt instance void [System]System.Diagnostics.Stopwatch::Start() 

IL_0013: nop

IL_0014: ldc.i4.0

IL_0015: stloc.2

IL_0016: br.s IL_0031

IL_0018: nop

IL_0019: ldloca.s i

IL_001b: call instance string [mscorlib]System.Int32::ToString()

IL_0020: stloc.0

IL_0021: ldc.i4 0xc8

IL_0026: call void [mscorlib]System.Threading.Thread::Sleep(int32)

IL_002b: nop

IL_002c: nop

IL_002d: ldloc.2

IL_002e: ldc.i4.1

IL_002f: add

IL_0030: stloc.2

IL_0031: ldloc.2

IL_0032: ldc.i4.s 10

IL_0034: clt

IL_0036: stloc.3

IL_0037: ldloc.3

IL_0038: brtrue.s IL_0018

IL_003a: ldloc.1

IL_003b: callvirt instance void [System]System.Diagnostics.Stopwatch::Stop()

We noticed that the Sleep of Code Part A directly appears in the Load method.

Let's take a look at the Parallel method:

Code Part B:

Form1_Load:

 

IL_0019: callvirt instanceVoid[System] System. Diagnostics. Stopwatch: Start ()

IL_001e: nop

IL_001f: ldc. i4.0

IL_0020: ldc. i4.s10

IL_0022: ldloc.1

IL_0023: ldftn instanceVoidWindowsFormsApplication4.Form1/<> C _ DisplayClass1::<Form1_Load>B _ 0(Int32)

IL_0029: newobj instanceVoid Class[Mscorlib] System. action'1<Int32>::. Ctor (Object,

NativeInt)

IL_002e: call valuetype [mscorlib] System. Threading. Tasks. ParallelLoopResult[Mscorlib] System. Threading. Tasks. Parallel: For (int32,

Int32,

Class[Mscorlib] System. action'1<Int32>)

IL_0033: pop

IL_0034: ldloc.0

IL_0035: callvirt instanceVoid[System] System. Diagnostics. Stopwatch: Stop ()

Pay attention to the red font. Sleep is no longer in the Load method, but is replaced by a "B _ 0". Parallel code is separated from the host code,The following is the IL of B _ 0:

. MethodPublicHidebysig instanceVoid <Form1_Load> B _ 0(Int32 I) cel managed

{

//Code size 26 (0x1a)

. Maxstack8

IL_0000: nop

IL_0001: ldarg.0

IL_0002: ldarga. s I

IL_0004: call instanceString[Mscorlib] System. Int32: ToString ()

IL_0009: stfldStringWindowsFormsApplication4.Form1/<> C _ DisplayClass1:

IL_000e: ldc. i40xc8

IL_0013: callVoid[Mscorlib] System. Threading. Thread: Sleep (int32)

IL_0018: nop

IL_0019: ret

}//End of method <> c _ DisplayClass1: <Form1_Load> B _

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.