<?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 Using a static variable in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034520#M20347</link>
    <description>&lt;P&gt;Using a static variable inside a "block" is a 'classic' way to cause a write data race. Unlike static variables at global scope which are initialized by _crtstartup ( or similar) the value of the static in a block is initialized during execution when the block is entered for the first time. Clearly this can cause a data race.&lt;/P&gt;

&lt;P&gt;thread 1 enters the block , and begins the static initialization&lt;/P&gt;

&lt;P&gt;thread 2 enters the block , maybe skips the initialization ( as thread 1 has set some flag) and calls zgemm_direct but the value of the static has not actually been set by thread 1 yet so will be arbitrary.&lt;/P&gt;

&lt;P&gt;I am assuming the variable is static for some reason?&lt;/P&gt;

&lt;P&gt;Obviously I could do something dirty like force a call to the matrix routines from the main thread at startup so this static gets initialized.&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Aug 2014 22:15:11 GMT</pubDate>
    <dc:creator>AndrewC</dc:creator>
    <dc:date>2014-08-28T22:15:11Z</dc:date>
    <item>
      <title>Thread safety of MKL_DIRECT_CALL</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034518#M20345</link>
      <description>&lt;P&gt;The MKL_DIRECT_CALL macros expand to something like&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;if(.....){

.......

}else{

static MKL_INT mkl_direct_call_flag = 0

zgemm_direct((transa), (transb), (m), (n), (k), (alpha), (a), (lda), (b), (ldb), (beta), (c), (ldc), &amp;amp;mkl_direct_call_flag);

}&lt;/PRE&gt;

&lt;P&gt;The&amp;nbsp; static variable is initialized the FIRST time the block is entered, so its not thread-safe.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Aug 2014 16:20:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034518#M20345</guid>
      <dc:creator>AndrewC</dc:creator>
      <dc:date>2014-08-28T16:20:00Z</dc:date>
    </item>
    <item>
      <title>Thank you for trying the new</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034519#M20346</link>
      <description>&lt;P&gt;Thank you for trying the new MKL_DIRECT_CALL feature! The static variable is always set to the same value within its scope (the value is determined at the compile time). Therefore, each thread should see the same value for mkl_direct_call_flag. However, we will look into this and evaluate alternatives to static MKL_INT. Perhaps we can just declare it as just an MKL_INT or as a const.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Aug 2014 20:44:32 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034519#M20346</guid>
      <dc:creator>Murat_G_Intel</dc:creator>
      <dc:date>2014-08-28T20:44:32Z</dc:date>
    </item>
    <item>
      <title>Using a static variable</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034520#M20347</link>
      <description>&lt;P&gt;Using a static variable inside a "block" is a 'classic' way to cause a write data race. Unlike static variables at global scope which are initialized by _crtstartup ( or similar) the value of the static in a block is initialized during execution when the block is entered for the first time. Clearly this can cause a data race.&lt;/P&gt;

&lt;P&gt;thread 1 enters the block , and begins the static initialization&lt;/P&gt;

&lt;P&gt;thread 2 enters the block , maybe skips the initialization ( as thread 1 has set some flag) and calls zgemm_direct but the value of the static has not actually been set by thread 1 yet so will be arbitrary.&lt;/P&gt;

&lt;P&gt;I am assuming the variable is static for some reason?&lt;/P&gt;

&lt;P&gt;Obviously I could do something dirty like force a call to the matrix routines from the main thread at startup so this static gets initialized.&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Aug 2014 22:15:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034520#M20347</guid>
      <dc:creator>AndrewC</dc:creator>
      <dc:date>2014-08-28T22:15:11Z</dc:date>
    </item>
    <item>
      <title>The variable was declared as</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034521#M20348</link>
      <description>&lt;P&gt;The variable was declared as static because its value was expected to remain fixed for a compilation unit.&amp;nbsp;I think we overlooked the thread safety issue. As you mentioned, we should probably change this declaration to regular int. Thank you very much again for the followup and letting us know the issue.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Aug 2014 18:23:37 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Thread-safety-of-MKL-DIRECT-CALL/m-p/1034521#M20348</guid>
      <dc:creator>Murat_G_Intel</dc:creator>
      <dc:date>2014-08-29T18:23:37Z</dc:date>
    </item>
  </channel>
</rss>

