C#, What Is Next?

840 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
840
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • C#, What Is Next?

    1. 1. C#, What’s next? Pieter Joost van de Sande [email_address]
    2. 2. Agenda <ul><li>Multi core future and solution </li></ul><ul><li>Discussion </li></ul><ul><li>Specification </li></ul><ul><li>Discussion </li></ul>
    3. 3. Multi core future
    4. 4. Example Code to Parallelize <ul><li>void MultiplyMatrices( int size, double [,] m1, double [,] m2, double [,] result) </li></ul><ul><li>{ </li></ul><ul><li>for ( int i = 0; i < size; i++) { </li></ul><ul><li>for ( int j = 0; j < size; j++) { </li></ul><ul><li>result[i, j] = 0; </li></ul><ul><li>for ( int k = 0; k < size; k++) { </li></ul><ul><li>result[i, j] += m1[i, k] * m2[k, j]; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    5. 5. int N = size; int P = 2 * Environment .ProcessorCount; int Chunk = N / P; // size of a work chunk ManualResetEvent signal = new ManualResetEvent ( false ); int counter = P; // use a counter to reduce kernel transitions for ( int c = 0; c < P; c++) { // for each chunk ThreadPool .QueueUserWorkItem(o => { int lc = ( int )o; for ( int i = lc * Chunk; // process one chunk i < (lc + 1 == P ? N : (lc + 1) * Chunk); // respect upper bound i++) { // original loop body for ( int j = 0; j < size; j++) { result[i, j] = 0; for ( int k = 0; k < size; k++) { result[i, j] += m1[i, k] * m2[k, j]; } } } if ( Interlocked .Decrement( ref counter) == 0) { // efficient interlocked ops signal.Set(); // and kernel transition only when done } }, c); } signal.WaitOne(); Error Prone High Overhead Tricks Knowledge of Synchronization Primitives Heavy Synchronization Lack of Thread Reuse
    6. 6. Matrix multiplication <ul><li>void MultiplyMatrices( int size, double [,] m1, double [,] m2, double [,] result) </li></ul><ul><li>{ </li></ul><ul><li>for ( int i = 0; i < size; i++) { </li></ul><ul><li>for ( int j = 0; j < size; j++) { </li></ul><ul><li>result[i, j] = 0; </li></ul><ul><li>for ( int k = 0; k < size; k++) { </li></ul><ul><li>result[i, j] += m1[i, k] * m2[k, j]; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    7. 7. Matrix multiplication <ul><li>void MultiplyMatrices( int size, double [,] m1, double [,] m2, double [,] result) </li></ul><ul><li>{ </li></ul><ul><li>Parallel .For(0, size, i => { </li></ul><ul><li>for ( int j = 0; j < size; j++) { </li></ul><ul><li>result[i, j] = 0; </li></ul><ul><li>for ( int k = 0; k < size; k++) { </li></ul><ul><li>result[i, j] += m1[i, k] * m2[k, j]; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul><ul><li>} </li></ul>
    8. 8. Task class <ul><li>The lightweight unit of work </li></ul><ul><li>May run concurrently if it’s “worth it” </li></ul><ul><li>Supports </li></ul><ul><ul><li>parent/child relations </li></ul></ul><ul><ul><li>explicit grouping </li></ul></ul><ul><ul><li>cancelation </li></ul></ul><ul><ul><li>waiting </li></ul></ul>
    9. 9. TaskManager <ul><li>Represents a scheduler group </li></ul><ul><ul><li>think of it as a fancy thread-pool </li></ul></ul><ul><ul><li>one implicit to each AppDomain </li></ul></ul><ul><ul><li>can create explicitly for isolation </li></ul></ul><ul><ul><li>configurable </li></ul></ul>
    10. 10. Smart work stealing Worker Thread 1 Worker Thread 2 Program Thread Task 1 Task 2 Task 3 Task 5 Task 4
    11. 11. The status of the extensions <ul><li>This IS the next step for Microsoft .NET </li></ul><ul><li>Don’t use it in production! </li></ul><ul><li>There is a Community Technical Preview available </li></ul><ul><li>Also the toolset will change </li></ul>
    12. 12. Summary <ul><li>We’ll have 80 cores within 10 years </li></ul><ul><li>Microsoft is targeting the parallel problem with Parallel Extensions </li></ul><ul><li>The TaskManager class knows the hardware </li></ul><ul><li>It is smarter then you </li></ul>
    13. 13. Discussion <ul><li>Do we want accept/develop multi core? </li></ul><ul><li>Is this a final solution or just a small step? </li></ul><ul><li>Do we want this as a framework? </li></ul><ul><li>Do we want to go a step further and be unaware of parallelization? </li></ul>
    14. 14. Specification <ul><li>In C# we specify the following: </li></ul><ul><ul><li>Types </li></ul></ul><ul><ul><li>Names </li></ul></ul><ul><ul><li>Visibility </li></ul></ul><ul><ul><li>Interfaces </li></ul></ul><ul><ul><li>Signatures </li></ul></ul><ul><li>How about the rest? </li></ul>
    15. 15. Specification nowadays <ul><li>public class Boo </li></ul><ul><li>{ </li></ul><ul><li>public Int32 Add(Int32 x, Int32 y) </li></ul><ul><li>{ </li></ul><ul><li>return x + y; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Type name Type Method name Visibility Return type Arguments
    16. 16. Pre conditions, nowadays <ul><li>/// <summary> Divides <i>x</i> by <i>y</i>. </summary> </li></ul><ul><li>/// <param name=&quot;x&quot;>The value to divide.</param> </li></ul><ul><li>/// <param name=&quot;y&quot;>The value to divide by.</param>' </li></ul><ul><li>/// <exception cref=&quot;ArgumentOutOfRangeException&quot;> </li></ul><ul><li>/// Occurs if <i>y</i> is zero. </exception> </li></ul><ul><li>/// <returns>The result of dividing </li></ul><ul><li>/// <i>x</i> by <i>y</i>.</returns> </li></ul><ul><li>public static int IntegerDivide(int x, int y) </li></ul><ul><li>{ </li></ul><ul><li>if (y == 0) </li></ul><ul><li>throw new ArgumentOutOfRangeException(&quot;y&quot;); </li></ul><ul><li>return x / y; </li></ul><ul><li>} </li></ul>Specification of the post condition Actual post condition
    17. 17. Pre conditions, Spec# <ul><li>public class A </li></ul><ul><li>{ </li></ul><ul><li>public static int IntegerDivide(int x, int y) </li></ul><ul><li>requires y != 0; </li></ul><ul><li>{ </li></ul><ul><li>return x / y; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    18. 18. Pre conditions, Spec# <ul><li>public class MyLibrary </li></ul><ul><li>{ </li></ul><ul><li>public static void Clear(int[]! xs) </li></ul><ul><li>{ </li></ul><ul><li>for (int i = 0; i < xs.Length; i++) // OK </li></ul><ul><li>{ </li></ul><ul><li>xs[i] = 0; // OK </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    19. 19. Pre conditions, Spec# <ul><li>public class MyLibrary </li></ul><ul><li>{ </li></ul><ul><li>public static void Clear(int[] /*^!^*/ xs) </li></ul><ul><li>{ </li></ul><ul><li>for (int i = 0; i < xs.Length; i++) // OK </li></ul><ul><li>{ </li></ul><ul><li>xs[i] = 0; // OK </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    20. 20. Cannot prove that z != 0 <ul><li>public class Client </li></ul><ul><li>{ </li></ul><ul><li>public static void TryBad(int z) </li></ul><ul><li>{ </li></ul><ul><li>// error: cannot prove that z != 0 </li></ul><ul><li>A.IntegerDivide(27,z); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    21. 21. Prove that z != 0 <ul><li>public class Client </li></ul><ul><li>{ </li></ul><ul><li>public static void TryOK(int z) </li></ul><ul><li>requires z != 0; </li></ul><ul><li>{ </li></ul><ul><li>// no error: can prove that z != 0 </li></ul><ul><li>A.IntegerDivide(27,z); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    22. 22. Post conditions, Spec# <ul><li>public class A </li></ul><ul><li>{ </li></ul><ul><li>public static int AddTogether(int x, int y) </li></ul><ul><li>ensures result == x+y; </li></ul><ul><li>{ </li></ul><ul><li>return x + y; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    23. 23. Post conditions usage <ul><li>public class Client </li></ul><ul><li>{ </li></ul><ul><li>public static void Main() </li></ul><ul><li>{ </li></ul><ul><li>int x = A.AddTogether(4, 2); </li></ul><ul><li>// no error: can prove x != 0 </li></ul><ul><li>A.IntegerDivide(27, x); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    24. 24. The status of Spec# <ul><li>It is a Research project </li></ul><ul><li>Current release: 1.0.5030 </li></ul><ul><li>No documentation available </li></ul><ul><li>Visual Studio 2k5 & 2k8 support </li></ul><ul><li>Not part of the current strategies </li></ul>
    25. 25. Summary <ul><li>Spec# is a Microsoft Research project </li></ul><ul><li>We can specify more with Spec# </li></ul><ul><ul><li>What means that we can drop some documentation </li></ul></ul><ul><li>We can find more bugs at compile time </li></ul><ul><li>We can keep compatibility with /*^ ^*/ </li></ul>
    26. 26. Discussion <ul><li>Are we glad with more specification? </li></ul><ul><li>Do we want to learn this extra syntax? </li></ul><ul><li>Can you trust your specification? </li></ul><ul><li>Do we want this in our language or framework? </li></ul>
    27. 27. Vragen? Feedback? Seminar? Congres? Pieter Joost van de Sande [email_address] Blog: http://born2code.net Site: www.sogyo.nl

    ×