{"id":401,"date":"2015-03-26T05:43:21","date_gmt":"2015-03-25T19:43:21","guid":{"rendered":"http:\/\/www.nickdu.com\/?p=401"},"modified":"2015-03-26T05:43:21","modified_gmt":"2015-03-25T19:43:21","slug":"feel-the-power-of-parallel-computing-openmp","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=401","title":{"rendered":"Feel the power of parallel computing (OpenMP)"},"content":{"rendered":"<p>These two weeks, I am working on our product UI side to improve the performance of animation rendering. Previously, there is only one single thread to decode the animation line by line, and it takes around 50ms for the whole frame.<\/p>\n<p><a href=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2015\/03\/org.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" size-medium wp-image-402 aligncenter\" src=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2015\/03\/org-300x213.jpg\" alt=\"org\" width=\"300\" height=\"213\" \/><\/a><\/p>\n<p>Now, I change the way of rendering, and let all lines parallel decode to fully take advantage of modern multi-core CPU.<\/p>\n<p><a href=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2015\/03\/new.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" size-medium wp-image-403 aligncenter\" src=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2015\/03\/new-300x216.jpg\" alt=\"new\" width=\"300\" height=\"216\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Visual Studio natively supports <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/fw509c3b.aspx\" target=\"_blank\">OpenMP<\/a>, it gives me a easy way to access this powerful tool.<\/p>\n<h3 class=\"procedureSubHeading\">To set this compiler option in the Visual Studio development environment<\/h3>\n<div class=\"subSection\">\n<ol>\n<li>Open the project&#8217;s <span class=\"label\">Property Pages<\/span> dialog box. For details, see <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/e79xc5h1.aspx\" target=\"_blank\">How to: Open Project Property Pages<\/a>.<\/li>\n<li>Expand the <span class=\"label\">Configuration Properties<\/span> node.<\/li>\n<li>Expand the <span class=\"label\">C\/C++<\/span> node.<\/li>\n<li>Select the <span class=\"label\">Language<\/span> property page.<\/li>\n<li>Modify the <span class=\"label\">OpenMP Support<\/span> property.<\/li>\n<\/ol>\n<\/div>\n<p>After some simple code update, surprisingly, I found that my frame decoding performance boosts 950% (almost 10 times faster), from 8 FPS to 76 FPS!<\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s do simple test with the following code:<\/p>\n<pre lang=\"c\">#define TEST_LENGTH 0x3fffffff\n\ndouble mptest()\n{\n    LARGE_INTEGER  large_interger;\n    double dff;\n    __int64  c1, c2;\n    QueryPerformanceFrequency(&amp;large_interger);\n    dff = large_interger.QuadPart;\n    \/\/\n    unsigned char *test = new unsigned char[TEST_LENGTH];\n    QueryPerformanceCounter(&amp;large_interger);\n    c1 = large_interger.QuadPart;\n    #pragma omp parallel for\n    for (int i = 0; i&lt;TEST_LENGTH; i++)\n    {\n        test[i] = rand();\n    }\n    QueryPerformanceCounter(&amp;large_interger);\n    c2 = large_interger.QuadPart;\n    delete test;\n    return (c2 - c1) * 1000.0f \/ dff;\n}\n\ndouble test()\n{\n    LARGE_INTEGER  large_interger;\n    double dff;\n    __int64  c1, c2;\n    QueryPerformanceFrequency(&amp;large_interger);\n    dff = large_interger.QuadPart;\n    \/\/\n    unsigned char *test = new unsigned char[TEST_LENGTH];\n    QueryPerformanceCounter(&amp;large_interger);\n    c1 = large_interger.QuadPart;\n    for (int i = 0; i&lt;TEST_LENGTH; i++)\n    {\n        test[i] = rand();\n    }\n    QueryPerformanceCounter(&amp;large_interger);\n    c2 = large_interger.QuadPart;\n    delete test;\n    return (c2 - c1) * 1000.0f \/ dff;\n}\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n    printf(\"Random generation cost with MP %lfmsn\", mptest());\n    printf(\"Random generation cost without MP %lfmsn\", test());\n    _getch();\n    return 0;\n}\n<\/pre>\n<p>Look at the huge difference!<\/p>\n<p><a href=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2015\/03\/result.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-408\" src=\"http:\/\/www.nickdu.com\/wp-content\/uploads\/2015\/03\/result-300x152.jpg\" alt=\"result\" width=\"300\" height=\"152\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>These two weeks, I am working on our product UI side to improve the performance of animation rendering. Previously, there is only one single thread to decode the animation line by line, and it takes around 50ms for the whole frame. Now, I change the way of rendering, and let all lines parallel decode to &hellip; <a href=\"https:\/\/nickdu.com\/?p=401\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Feel the power of parallel computing (OpenMP)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-cc","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=401"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/401\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}