{"id":201,"date":"2010-03-27T04:20:48","date_gmt":"2010-03-27T11:20:48","guid":{"rendered":"http:\/\/domemtech.com\/?p=201"},"modified":"2010-05-25T11:41:50","modified_gmt":"2010-05-25T18:41:50","slug":"parallel-programming-with-net","status":"publish","type":"post","link":"http:\/\/165.227.223.229\/index.php\/2010\/03\/27\/parallel-programming-with-net\/","title":{"rendered":"Parallel programming with .NET"},"content":{"rendered":"<p>Visual Studio 2010 has some interesting new features, one of which is parallel programming. This simple example, written in C# and F#, tests the difference between serial and parallel computations of Fibonacci numbers. The example uses the System.Threading.Tasks.Parallel.For method. Indeed, there is about a 4-fold improvement on my quad-core multiprocessor for long computations but not for short computations. There is a trade-off between the size of the computation versus the overhead to create, schedule, and synchronize threads.<br \/>\n\t<!--more--><\/p>\n<p>C#:<\/p>\n<p><script type=\"text\/javascript\" src=\"syntaxhighlighter\/scripts\/shCore.js\"><\/script><script type=\"text\/javascript\" src=\"syntaxhighlighter\/scripts\/shBrushCSharp.js\"><\/script><script type=\"text\/javascript\" src=\"syntaxhighlighter\/scripts\/shBrushFSharp.js\"><\/script><script type=\"text\/javascript\" src=\"syntaxhighlighter\/scripts\/shBrushPlain.js\"><\/script><link href=\"syntaxhighlighter\/styles\/shCore.css\" rel=\"stylesheet\" type=\"text\/css\" \/>\n<link href=\"syntaxhighlighter\/styles\/shThemeEclipse.css\" rel=\"Stylesheet\" type=\"text\/css\" \/>\n<pre class=\"brush: csharp; first-line: 1\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Diagnostics;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace ConsoleApplication1\r\n{\r\n    \r\n    class Program\r\n    {\r\n\r\n        Int32 fib(Int32 x)\r\n        {\r\n            if (x &lt;= 2)\r\n                return 1;\r\n            else\r\n                return fib(x - 1) + fib(x - 2);\r\n        }\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            DateTime current;\r\n            Program p = new Program();\r\n            Stopwatch sw = new Stopwatch();\r\n            double s = 0;\r\n            sw.Reset();\r\n            current = DateTime.Now;\r\n            Console.WriteLine(current);\r\n            sw.Start();\r\n            for (int i = 0; i &lt; 40; ++i)\r\n                p.fib(i);\r\n            sw.Stop();\r\n            s = sw.Elapsed.TotalMilliseconds;\r\n            System.Console.WriteLine(&quot;Serial = &quot; + s);\r\n            current = DateTime.Now;\r\n            Console.WriteLine(current);\r\n\r\n            double l = 0;\r\n            sw.Reset();\r\n            sw.Start();\r\n            Parallel.For(0, 40, j =&gt;\r\n                {\r\n                    p.fib(j);\r\n                });\r\n            sw.Stop();\r\n            l = sw.Elapsed.TotalMilliseconds;\r\n            System.Console.WriteLine(&quot;Parallel = &quot; + l);\r\n            current = DateTime.Now;\r\n            Console.WriteLine(current);\r\n\r\n            sw.Reset();\r\n            sw.Start();\r\n            for (int i = 0; i &lt; 10; ++i)\r\n                p.fib(40);\r\n            sw.Stop();\r\n            s = sw.Elapsed.TotalMilliseconds;\r\n            System.Console.WriteLine(&quot;Serial2 = &quot; + s);\r\n            current = DateTime.Now;\r\n            Console.WriteLine(current);\r\n\r\n            sw.Reset();\r\n            sw.Start();\r\n            Parallel.For(0, 10, j =&gt;\r\n            {\r\n                p.fib(40);\r\n            });\r\n            sw.Stop();\r\n            l = sw.Elapsed.TotalMilliseconds;\r\n            System.Console.WriteLine(&quot;Parallel = &quot; + l);\r\n            current = DateTime.Now;\r\n            Console.WriteLine(current);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: text; first-line: 1\">\r\n3\/27\/2010 6:54:22 AM\r\nSerial = 3243.8431\r\n3\/27\/2010 6:54:25 AM\r\nParallel = 3094.8497\r\n3\/27\/2010 6:54:28 AM\r\nSerial2 = 20070.4749\r\n3\/27\/2010 6:54:48 AM\r\nParallel = 6129.799\r\n3\/27\/2010 6:54:54 AM\r\n<\/pre>\n<p>F#:<\/p>\n<pre class=\"brush: fsharp; first-line: 1\">\r\n\/\/ Learn more about F# at http:\/\/fsharp.net\r\n\r\nlet rec fib x = if x &lt;= 2 then 1 else fib(x-1) + fib(x-2)\r\n\r\nlet sw = System.Diagnostics.Stopwatch();\r\nsw.Reset();\r\nlet c0 = System.DateTime.Now\r\nSystem.Console.WriteLine(c0);\r\nsw.Start();\r\nfor x in 0..40 do\r\n    fib(x);\r\n    done\r\nsw.Stop();\r\nlet s = sw.ElapsedMilliseconds;\r\nSystem.Console.WriteLine(s);\r\nlet c1 = System.DateTime.Now\r\nSystem.Console.WriteLine(c1);\r\n\r\nsw.Reset();\r\nsw.Start();\r\nAsync.Parallel [ for x in 0..40 -> async { return fib(x) } ]  |> Async.RunSynchronously\r\nsw.Stop();\r\nlet p = sw.ElapsedMilliseconds;\r\nSystem.Console.WriteLine(p);\r\nlet c2 = System.DateTime.Now\r\nSystem.Console.WriteLine(c2);\r\n\r\nsw.Reset();\r\nsw.Start();\r\nfor x in 0..10 do\r\n    fib(40);\r\n    done\r\nsw.Stop();\r\nlet s2 = sw.ElapsedMilliseconds;\r\nSystem.Console.WriteLine(s2);\r\nlet c3 = System.DateTime.Now\r\nSystem.Console.WriteLine(c3);\r\n\r\nsw.Reset();\r\nsw.Start();\r\nAsync.Parallel [ for x in 0..10 -> async { return fib(40) } ]  |> Async.RunSynchronously\r\nsw.Stop();\r\nlet p2 = sw.ElapsedMilliseconds;\r\nSystem.Console.WriteLine(p2);\r\nlet c4 = System.DateTime.Now\r\nSystem.Console.WriteLine(c4);\r\n\r\nlet y = 1\r\n\r\n\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: text; first-line: 1\">\r\n3\/27\/2010 7:01:39 AM\r\n5450\r\n3\/27\/2010 7:01:44 AM\r\n2351\r\n3\/27\/2010 7:01:47 AM\r\n22893\r\n3\/27\/2010 7:02:10 AM\r\n6663\r\n3\/27\/2010 7:02:16 AM\r\n<\/code><\/pre>\n<p><script type=\"text\/javascript\">\n     SyntaxHighlighter.all()\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 2010 has some interesting new features, one of which is parallel programming.  This simple test, written in C# and F#, tests the difference between serial and parallel computations of Fibonacci numbers.  Indeed, there is a 4-fold improvement for certain computations&#8211;but not every computation&#8211;on my quad-core multiprocessor.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/201"}],"collection":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":0,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"wp:attachment":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}