<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How do we know that the code is thread safe in sample application? in Analyzers</title>
    <link>https://community.intel.com/t5/Analyzers/How-do-we-know-that-the-code-is-thread-safe-in-sample/m-p/987239#M10552</link>
    <description>&lt;P&gt;I am following &lt;A href="http://software.intel.com/en-us/node/471888"&gt;this tutorial&lt;/A&gt;&amp;nbsp;&amp;nbsp;to understand how to remove locks , using VTUNE &amp;nbsp;&lt;BR /&gt;
	This page says the following after collecting Vtune results: &amp;nbsp;&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;&lt;BR /&gt;
		&lt;STRONG&gt;**Identify the Hottest Code Lines**&lt;/STRONG&gt;&lt;BR /&gt;
		Click the &amp;nbsp;hotspot navigation button&lt;BR /&gt;
		&amp;nbsp;to go to the code line that took the most Wait time. VTune Amplifier&lt;BR /&gt;
		&amp;nbsp;highlights line 170 entering the critical section rgb_critical_section&lt;BR /&gt;
		&amp;nbsp;in the draw_task function. The draw_task function was waiting for&lt;BR /&gt;
		&amp;nbsp;almost 27 seconds while this code line was executing and most of the&lt;BR /&gt;
		&amp;nbsp;time the processor was underutilized. During this time, the critical&lt;BR /&gt;
		&amp;nbsp;section was contended 438 times.&lt;BR /&gt;
		&amp;nbsp;&lt;BR /&gt;
		&amp;nbsp;The rgb_critical section is the place where the application is&lt;BR /&gt;
		&amp;nbsp;serializing. Each thread has to wait for the critical section to be&lt;BR /&gt;
		&amp;nbsp;available before it can proceed. Only one thread can be in the&lt;BR /&gt;
		&amp;nbsp;critical section at a time. You need to optimize the code to make it&lt;BR /&gt;
		&amp;nbsp;more concurrent.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I was able to follow this tutorial until I reach the next section: &lt;A href="http://software.intel.com/en-us/node/471888"&gt;Remove the Lock&lt;/A&gt;&amp;nbsp;&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;&lt;BR /&gt;
		**Remove the Lock**&lt;/P&gt;

	&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
		&amp;gt; The rgb_critical_section was introduced to protect calculation from&lt;BR /&gt;
		&amp;gt; multithreaded access. The brief analysis shows that the code is thread&lt;BR /&gt;
		&amp;gt; safe and the critical section is not really needed.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;&lt;BR /&gt;
	My question is how do we know the code is thread safe?&amp;nbsp;&lt;/P&gt;

&lt;P&gt;As suggested I commented those line (EnterCritical... and LeaveCritical...), and saw huge performance gain, but I did not &amp;nbsp;get why this critical section is not required? Which analysis tells us this ? &amp;nbsp;&lt;/P&gt;

&lt;P&gt;&lt;BR /&gt;
	The relevant code is here in analyze_locks.cpp:&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp; &amp;nbsp; public:&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;void operator () (const tbb::blocked_range &amp;lt;int&amp;gt; &amp;amp;r) const {&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;unsigned int serial = 1;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;unsigned int mboxsize = sizeof(unsigned int)*(max_objectid() + 20);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;unsigned int * local_mbox = (unsigned int *) alloca(mboxsize);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;memset(local_mbox,0,mboxsize);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;for (int y=r.begin(); y!=r.end(); ++y) {&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;drawing_area drawing(startx, totaly-y, stopx-startx, 1);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Enter Critical Section to protect pixel calculation from multithreaded access (Needed?)&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;//&amp;nbsp;&amp;nbsp; &amp;nbsp;EnterCriticalSection(&amp;amp;rgb_critical_section);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for (int x = startx; x &amp;lt; stopx; x++) {&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; color_t c = render_one_pixel (x, y, local_mbox, serial, startx, stopx, starty, stopy);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; drawing.put_pixel(c);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Exit from the critical section&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;//&amp;nbsp;&amp;nbsp; &amp;nbsp;LeaveCriticalSection(&amp;amp;rgb_critical_section);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if(!video-&amp;gt;next_frame()) tbb::task::self().cancel_group_execution();&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;draw_task () {}&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; };&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Nov 2013 05:22:48 GMT</pubDate>
    <dc:creator>gpu_g_</dc:creator>
    <dc:date>2013-11-20T05:22:48Z</dc:date>
    <item>
      <title>How do we know that the code is thread safe in sample application?</title>
      <link>https://community.intel.com/t5/Analyzers/How-do-we-know-that-the-code-is-thread-safe-in-sample/m-p/987239#M10552</link>
      <description>&lt;P&gt;I am following &lt;A href="http://software.intel.com/en-us/node/471888"&gt;this tutorial&lt;/A&gt;&amp;nbsp;&amp;nbsp;to understand how to remove locks , using VTUNE &amp;nbsp;&lt;BR /&gt;
	This page says the following after collecting Vtune results: &amp;nbsp;&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;&lt;BR /&gt;
		&lt;STRONG&gt;**Identify the Hottest Code Lines**&lt;/STRONG&gt;&lt;BR /&gt;
		Click the &amp;nbsp;hotspot navigation button&lt;BR /&gt;
		&amp;nbsp;to go to the code line that took the most Wait time. VTune Amplifier&lt;BR /&gt;
		&amp;nbsp;highlights line 170 entering the critical section rgb_critical_section&lt;BR /&gt;
		&amp;nbsp;in the draw_task function. The draw_task function was waiting for&lt;BR /&gt;
		&amp;nbsp;almost 27 seconds while this code line was executing and most of the&lt;BR /&gt;
		&amp;nbsp;time the processor was underutilized. During this time, the critical&lt;BR /&gt;
		&amp;nbsp;section was contended 438 times.&lt;BR /&gt;
		&amp;nbsp;&lt;BR /&gt;
		&amp;nbsp;The rgb_critical section is the place where the application is&lt;BR /&gt;
		&amp;nbsp;serializing. Each thread has to wait for the critical section to be&lt;BR /&gt;
		&amp;nbsp;available before it can proceed. Only one thread can be in the&lt;BR /&gt;
		&amp;nbsp;critical section at a time. You need to optimize the code to make it&lt;BR /&gt;
		&amp;nbsp;more concurrent.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I was able to follow this tutorial until I reach the next section: &lt;A href="http://software.intel.com/en-us/node/471888"&gt;Remove the Lock&lt;/A&gt;&amp;nbsp;&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
	&lt;P&gt;&lt;BR /&gt;
		**Remove the Lock**&lt;/P&gt;

	&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
		&amp;gt; The rgb_critical_section was introduced to protect calculation from&lt;BR /&gt;
		&amp;gt; multithreaded access. The brief analysis shows that the code is thread&lt;BR /&gt;
		&amp;gt; safe and the critical section is not really needed.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;&lt;BR /&gt;
	My question is how do we know the code is thread safe?&amp;nbsp;&lt;/P&gt;

&lt;P&gt;As suggested I commented those line (EnterCritical... and LeaveCritical...), and saw huge performance gain, but I did not &amp;nbsp;get why this critical section is not required? Which analysis tells us this ? &amp;nbsp;&lt;/P&gt;

&lt;P&gt;&lt;BR /&gt;
	The relevant code is here in analyze_locks.cpp:&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp; &amp;nbsp; public:&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;void operator () (const tbb::blocked_range &amp;lt;int&amp;gt; &amp;amp;r) const {&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;unsigned int serial = 1;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;unsigned int mboxsize = sizeof(unsigned int)*(max_objectid() + 20);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;unsigned int * local_mbox = (unsigned int *) alloca(mboxsize);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;memset(local_mbox,0,mboxsize);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;for (int y=r.begin(); y!=r.end(); ++y) {&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;drawing_area drawing(startx, totaly-y, stopx-startx, 1);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Enter Critical Section to protect pixel calculation from multithreaded access (Needed?)&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;//&amp;nbsp;&amp;nbsp; &amp;nbsp;EnterCriticalSection(&amp;amp;rgb_critical_section);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for (int x = startx; x &amp;lt; stopx; x++) {&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; color_t c = render_one_pixel (x, y, local_mbox, serial, startx, stopx, starty, stopy);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; drawing.put_pixel(c);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;// Exit from the critical section&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;//&amp;nbsp;&amp;nbsp; &amp;nbsp;LeaveCriticalSection(&amp;amp;rgb_critical_section);&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if(!video-&amp;gt;next_frame()) tbb::task::self().cancel_group_execution();&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;draw_task () {}&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;BR /&gt;
	&amp;nbsp; &amp;nbsp; };&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2013 05:22:48 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/How-do-we-know-that-the-code-is-thread-safe-in-sample/m-p/987239#M10552</guid>
      <dc:creator>gpu_g_</dc:creator>
      <dc:date>2013-11-20T05:22:48Z</dc:date>
    </item>
    <item>
      <title>There is no conflict in</title>
      <link>https://community.intel.com/t5/Analyzers/How-do-we-know-that-the-code-is-thread-safe-in-sample/m-p/987240#M10553</link>
      <description>&lt;P&gt;There is no&amp;nbsp;conflict in parallelsim,&amp;nbsp;because all&amp;nbsp;pixels' position (x,y) are&amp;nbsp;different.&lt;/P&gt;

&lt;P&gt;Critcal Section here is unnecessary, you can use another tool Inspector XE to do thread check.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2013 14:36:26 GMT</pubDate>
      <guid>https://community.intel.com/t5/Analyzers/How-do-we-know-that-the-code-is-thread-safe-in-sample/m-p/987240#M10553</guid>
      <dc:creator>Peter_W_Intel</dc:creator>
      <dc:date>2013-11-20T14:36:26Z</dc:date>
    </item>
  </channel>
</rss>

