<?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 Hi Sergey, in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183506#M29451</link>
    <description>&lt;P&gt;Hi Sergey,&lt;/P&gt;

&lt;P&gt;It seems your environment is more complex than the example in MKL manual.&lt;/P&gt;

&lt;P&gt;As the manual mentioned.&amp;nbsp; In a threaded application, avoid calling &lt;SPAN class="option"&gt;mkl_free_buffers&lt;/SPAN&gt; from each thread because the function has a global effect. Call &lt;A href="https://software.intel.com/node/c63d3016-941a-4c6d-a483-4454462644fd"&gt;&lt;SPAN class="option"&gt;&lt;U&gt;&lt;FONT color="#0066cc"&gt;mkl_thread_free_buffers&lt;/FONT&gt;&lt;/U&gt;&lt;/SPAN&gt;&lt;/A&gt; instead.&lt;/P&gt;

&lt;P&gt;So basically you should call mkl_thread_free_buffers instead of mk_free_buffers in your multithreaded C# environment. &amp;nbsp;&lt;/P&gt;

&lt;P&gt;Further discussion about multithread and memory leak, if possible, we may recommend to use sequential MKL in your multithread environment and disable the fast memory&amp;nbsp; as&amp;nbsp; mkl developer gudie:&lt;/P&gt;

&lt;P&gt;&lt;A href="https://software.intel.com/en-us/mkl-windows-developer-guide-avoiding-memory-leaks-in-intel-mkl"&gt;https://software.intel.com/en-us/mkl-windows-developer-guide-avoiding-memory-leaks-in-intel-mkl&lt;/A&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;When running, Intel MKL allocates and deallocates internal buffers to facilitate better performance. However, in some cases this behavior may result in memory leaks.&lt;/P&gt;

&lt;P&gt;To avoid memory leaks, you can do either of the following:&lt;/P&gt;

&lt;P&gt;Best Regards,&lt;/P&gt;

&lt;P&gt;Ying&lt;/P&gt;

&lt;UL id="GUID-524A0454-48CA-4B11-BAFA-F6C20FC8563F"&gt;
	&lt;LI&gt;
		&lt;P&gt;Set the &lt;SAMP class="codeph"&gt;&lt;FONT face="Courier New"&gt;MKL_DISABLE_FAST_MM&lt;/FONT&gt;&lt;/SAMP&gt; environment variable to 1 or call the &lt;SAMP class="codeph"&gt;&lt;FONT face="Courier New"&gt;mkl_disable_fast_mm()&lt;/FONT&gt;&lt;/SAMP&gt; function.&lt;/P&gt;

		&lt;P&gt;Be aware that this change may negatively impact performance of some Intel MKL functions, especially for small problem sizes.&lt;/P&gt;
	&lt;/LI&gt;
	&lt;LI&gt;
		&lt;P&gt;Call the &lt;SAMP class="codeph" id="GUID-D60B96AE-E2FE-4605-BEE8-A191650646FF"&gt;&lt;FONT face="Courier New"&gt;mkl_free_buffers()&lt;/FONT&gt;&lt;/SAMP&gt; function or the &lt;SAMP class="codeph"&gt;&lt;FONT face="Courier New"&gt;mkl_thread_free_buffers()&lt;/FONT&gt;&lt;/SAMP&gt; function in the current thread&lt;/P&gt;
	&lt;/LI&gt;
	&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Fri, 19 Jan 2018 01:53:16 GMT</pubDate>
    <dc:creator>Ying_H_Intel</dc:creator>
    <dc:date>2018-01-19T01:53:16Z</dc:date>
    <item>
      <title>mkl_free_buffers() and multiple threads</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183505#M29450</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;

&lt;P&gt;I have a question on behaviour of mkl_free_buffers() in multithreading setting. Is it OK if you have one thread which performs some activity with Intel MKL, and some other thread calls mkl_free_buffers() at the same time?&lt;/P&gt;

&lt;P&gt;The reason I ask is that in some cases (heavily multithreaded C# TPL code calling MKL functions) NET framework spawns A LOT of temporary threads, which (when combined with fast MM feature of MKL) results in constantly leaking memory. So, I want to patch it by periodically calling mkl_free_buffers() from "garbage collector" thread. Say, calling it every 10 seconds.&lt;/P&gt;

&lt;P&gt;MKL forum gives contradicting answers (some posters from Intel say that this function is thread-safe, and some users report errors associated with this function). And MKL manual ( &lt;A href="https://software.intel.com/en-us/mkl-developer-reference-c-mkl-free-buffers" target="_blank"&gt;https://software.intel.com/en-us/mkl-developer-reference-c-mkl-free-buffers&lt;/A&gt; ) says in its "Usage of mkl_free_buffers with FFT Functions" section that calling mkl_free_buffers() in the middle of working code will result in a failure.&lt;/P&gt;

&lt;P&gt;Can you finally clarify this question? :)&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2018 10:31:32 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183505#M29450</guid>
      <dc:creator>bochkanov__sergey</dc:creator>
      <dc:date>2018-01-17T10:31:32Z</dc:date>
    </item>
    <item>
      <title>Hi Sergey,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183506#M29451</link>
      <description>&lt;P&gt;Hi Sergey,&lt;/P&gt;

&lt;P&gt;It seems your environment is more complex than the example in MKL manual.&lt;/P&gt;

&lt;P&gt;As the manual mentioned.&amp;nbsp; In a threaded application, avoid calling &lt;SPAN class="option"&gt;mkl_free_buffers&lt;/SPAN&gt; from each thread because the function has a global effect. Call &lt;A href="https://software.intel.com/node/c63d3016-941a-4c6d-a483-4454462644fd"&gt;&lt;SPAN class="option"&gt;&lt;U&gt;&lt;FONT color="#0066cc"&gt;mkl_thread_free_buffers&lt;/FONT&gt;&lt;/U&gt;&lt;/SPAN&gt;&lt;/A&gt; instead.&lt;/P&gt;

&lt;P&gt;So basically you should call mkl_thread_free_buffers instead of mk_free_buffers in your multithreaded C# environment. &amp;nbsp;&lt;/P&gt;

&lt;P&gt;Further discussion about multithread and memory leak, if possible, we may recommend to use sequential MKL in your multithread environment and disable the fast memory&amp;nbsp; as&amp;nbsp; mkl developer gudie:&lt;/P&gt;

&lt;P&gt;&lt;A href="https://software.intel.com/en-us/mkl-windows-developer-guide-avoiding-memory-leaks-in-intel-mkl"&gt;https://software.intel.com/en-us/mkl-windows-developer-guide-avoiding-memory-leaks-in-intel-mkl&lt;/A&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;When running, Intel MKL allocates and deallocates internal buffers to facilitate better performance. However, in some cases this behavior may result in memory leaks.&lt;/P&gt;

&lt;P&gt;To avoid memory leaks, you can do either of the following:&lt;/P&gt;

&lt;P&gt;Best Regards,&lt;/P&gt;

&lt;P&gt;Ying&lt;/P&gt;

&lt;UL id="GUID-524A0454-48CA-4B11-BAFA-F6C20FC8563F"&gt;
	&lt;LI&gt;
		&lt;P&gt;Set the &lt;SAMP class="codeph"&gt;&lt;FONT face="Courier New"&gt;MKL_DISABLE_FAST_MM&lt;/FONT&gt;&lt;/SAMP&gt; environment variable to 1 or call the &lt;SAMP class="codeph"&gt;&lt;FONT face="Courier New"&gt;mkl_disable_fast_mm()&lt;/FONT&gt;&lt;/SAMP&gt; function.&lt;/P&gt;

		&lt;P&gt;Be aware that this change may negatively impact performance of some Intel MKL functions, especially for small problem sizes.&lt;/P&gt;
	&lt;/LI&gt;
	&lt;LI&gt;
		&lt;P&gt;Call the &lt;SAMP class="codeph" id="GUID-D60B96AE-E2FE-4605-BEE8-A191650646FF"&gt;&lt;FONT face="Courier New"&gt;mkl_free_buffers()&lt;/FONT&gt;&lt;/SAMP&gt; function or the &lt;SAMP class="codeph"&gt;&lt;FONT face="Courier New"&gt;mkl_thread_free_buffers()&lt;/FONT&gt;&lt;/SAMP&gt; function in the current thread&lt;/P&gt;
	&lt;/LI&gt;
	&lt;LI&gt;&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 19 Jan 2018 01:53:16 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183506#M29451</guid>
      <dc:creator>Ying_H_Intel</dc:creator>
      <dc:date>2018-01-19T01:53:16Z</dc:date>
    </item>
    <item>
      <title>Hi, Ying!</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183507#M29452</link>
      <description>&lt;P&gt;Hi, Ying!&lt;/P&gt;

&lt;P&gt;1. My actual question was "will MKL break if I call mkl_free_buffers() while it works?" Would you like to clarify this point?&lt;/P&gt;

&lt;P&gt;Your reply basically states "do not call mkl_free_buffers() in multithreaded programs", but you do not tell me why. Is it performance - doing so will just degrade performance, without breaking down the program? Is it stability - doing so will destroy some structures currently in use?&lt;/P&gt;

&lt;P&gt;So, I am asking - why? :)&lt;/P&gt;

&lt;P&gt;2. The problem with "avoid calling mkl_free_buffers(), use mkl_thread_free_buffers()" is that it is not an option here.&lt;/P&gt;

&lt;P&gt;MKL is used as linear algebra backend for another numerical library, which, in turn, is used by larger application. Due to performance considerations we can not call mkl_thread_free_buffers() every time we exit from our library. And calling thread_free_buffers when thread dies is also impossible: because larger application is written by other people, we do not know which call to our library is the last call for this particular thread.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2018 07:50:20 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183507#M29452</guid>
      <dc:creator>bochkanov__sergey</dc:creator>
      <dc:date>2018-01-19T07:50:20Z</dc:date>
    </item>
    <item>
      <title>Hi Sergey,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183508#M29453</link>
      <description>&lt;P&gt;Hi Sergey,&lt;/P&gt;

&lt;P&gt;The program shouldn't break if you can mkl_free_buffers() when it works.&amp;nbsp; Because the function&amp;nbsp;frees only "unused" buffers for all threads&amp;nbsp;.&lt;/P&gt;

&lt;P&gt;Yes, it will degrade performance&amp;nbsp;&lt;SPAN class="fontstyle0"&gt;&lt;FONT face="Verdana" size="2"&gt;due to the cost of reallocation of buffers for subsequent calls to Intel MKL functions&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;I&amp;nbsp;actually want to clarify &lt;FONT color="#1f497d" face="Calibri"&gt;what does leaking memory means. Is there’s a real memory leak?&lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;As you see, &amp;nbsp;&lt;SPAN class="fontstyle0"&gt;&lt;FONT face="Verdana" size="2"&gt;When running, Intel MKL allocates and deallocates internal buffers to facilitate better performance. However,&lt;BR /&gt;
	in some cases this behavior may result in memory leaks.&amp;nbsp; But once execution exit, the memory should be all freed and no memory leak.&amp;nbsp; So when you mentioned the leak memory here, do you mean at the end of execution there are some memory not&amp;nbsp;freed or caused by the mechanism of "&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face="Verdana" size="2"&gt;Intel MKL allocates and deallocates internal buffers" ?&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;&lt;FONT face="Verdana" size="2"&gt;If&amp;nbsp;it is the former,&amp;nbsp;let's investigate what &amp;nbsp;is real problem. &lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;&lt;FONT face="Verdana" size="2"&gt;if&amp;nbsp; the latter, &lt;/FONT&gt;&lt;FONT size="2"&gt;&lt;SPAN class="fontstyle0"&gt;&lt;FONT face="Verdana"&gt;you can do either of the following:&lt;BR /&gt;
	1. Set the &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="fontstyle2"&gt;MKL_DISABLE_FAST_MM &lt;/SPAN&gt;&lt;SPAN class="fontstyle0"&gt;&lt;FONT face="Verdana"&gt;environment variable to 1 or call the &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="fontstyle2"&gt;mkl_disable_fast_mm() &lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT size="2"&gt;&lt;SPAN class="fontstyle0"&gt;&lt;FONT face="Verdana"&gt;function.&lt;BR /&gt;
	Be aware that this change may negatively impact performance of some Intel MKL functions, especially for&lt;BR /&gt;
	small problem sizes.&lt;BR /&gt;
	2. &amp;nbsp;Call the &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="fontstyle2"&gt;mkl_free_buffers() &lt;/SPAN&gt;&lt;SPAN class="fontstyle0"&gt;&lt;FONT face="Verdana"&gt;function or the &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN class="fontstyle2"&gt;mkl_thread_free_buffers() &lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN class="fontstyle0"&gt;&lt;FONT face="Verdana" size="2"&gt;function in the current&lt;BR /&gt;
	thread.&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR style="text-transform: none; line-height: normal; text-indent: 0px; letter-spacing: normal; font-style: normal; font-variant: normal; font-weight: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" /&gt;
	I may&amp;nbsp;suggest to use disable fast mm, which should work too for your case and it will be better than constantly disrupt execution with cleaning buffer. You may select one depend on your test.&lt;/P&gt;

&lt;P&gt;Best Regards&lt;/P&gt;

&lt;P&gt;Ying&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2018 08:58:43 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183508#M29453</guid>
      <dc:creator>Ying_H_Intel</dc:creator>
      <dc:date>2018-01-26T08:58:43Z</dc:date>
    </item>
    <item>
      <title>1. Thank you for answer!</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183509#M29454</link>
      <description>&lt;P&gt;1. Thank you for answer!&lt;/P&gt;

&lt;P&gt;By the way, it would be good to tweak manual entry on disable_fast_mm(), because it states (in its FFT example) that calling this function will break MKL.&lt;/P&gt;

&lt;P&gt;2. On the "leak". I can not be 100% sure here, but...&lt;/P&gt;

&lt;P&gt;2A. It looks like that it is not a leak in a strict meaning of this word. Intel MKL correctly keeps track of allocated/deallocated/cached memory (at least, I have no evidence for the opposite).&lt;/P&gt;

&lt;P&gt;2B. This fast memory management feature seems to be unsuited for an environment which constantly creates and destroys new threads. Microsoft .NET Task Parallel Library (and&amp;nbsp;entire .NET framework in general) is an example of such environment.&lt;/P&gt;

&lt;P&gt;Although programmer may think that it has limited number of managed worker threads, internally TPL may create/destroy many native threads and constantly migrate execution context from one native thread to another one. Same managed thread may be represented by different underlying native threads in a different moments of its lifetime.&lt;/P&gt;

&lt;P&gt;2C. Because buffers allocated for now-gone threads will survive until the end of the application, we may have situation when MKL keeps allocating new and new per-thread buffers, with old ones being unused (they were used once or twice for a native thread which is now gone).&lt;/P&gt;

&lt;P&gt;Using thread_free_buffers() function is not an option, because this function should be called before thread terminates... but we do not know when it will terminate! Microsoft NET hides such implementation details from us.&lt;/P&gt;

&lt;P&gt;2D. Finally, the problem is greatly amplified by the fact that MKL may allocate as much as 5MB of memory for a simple 64x64 * 64x64 GEMM product (I traced its malloc() calls). So, memory "leaks" really fast. 50GB of memory may be exhausted in less than hour.&lt;/P&gt;

&lt;P&gt;And some applications are intended to be used for a month, or two, or even "eternally"... so even a minor "leak" will become a huge problem.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jan 2018 12:09:20 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183509#M29454</guid>
      <dc:creator>bochkanov__sergey</dc:creator>
      <dc:date>2018-01-26T12:09:20Z</dc:date>
    </item>
    <item>
      <title>Hi Sergey, </title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183510#M29455</link>
      <description>&lt;P&gt;Hi Sergey,&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Thank you for the details about the "leak".&amp;nbsp; I will bring them into our developer team.&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Regarding another way:&amp;nbsp; &amp;nbsp;&lt;SPAN style="font-size: 12px;"&gt;call the mkl_disable_fast_mm() function,&amp;nbsp; Have you tried it? (it may influence the performance, but may not so much )&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;Best Regards,&lt;/P&gt;

&lt;P&gt;Ying&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 06:34:27 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183510#M29455</guid>
      <dc:creator>Ying_H_Intel</dc:creator>
      <dc:date>2018-02-02T06:34:27Z</dc:date>
    </item>
    <item>
      <title>Hi, Ying!</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183511#M29456</link>
      <description>&lt;P&gt;Hi, Ying!&lt;/P&gt;

&lt;P&gt;Thank you for you reply!&lt;/P&gt;

&lt;P&gt;1. Yes, I tried mkl_disable_fast_mm(). My use case involves repeated multiplication of small matrices, roughly 64x64, and it turns out that for such small sizes FastMM-free mode is as bad as malloc() implementation being used. And single threaded performance and multicore scaling are two separate questions.&lt;/P&gt;

&lt;P&gt;Say, I never noticed any significant decrease at my 4-core Ubuntu desktop (GCC, glibc malloc), whether it was serial or multithreaded. But under Windows (MSVC, Microsoft CRT malloc) it started to scale badly with just 2 worker threads :(((((((((((&lt;/P&gt;

&lt;P&gt;2. One important notice on the "leak". I have to tell that it is really hard to reproduce. I talked with two independent companies which were hit by the "leak", but was unable to reproduce it at my Windows box no matter what.&lt;/P&gt;

&lt;P&gt;One of the companies used TSLab, .NET trading environment, 8-core system, moderate CPU load. "Leak" is present, memory leaks every second we use MKL functions. Another one performed a lot of high-load heavily threaded scientific calculations on 16-core 2-way hyperthreaded system, and some applications were hit by the "leak", some were not. It may depend on specific version of .NET framework being used, and specific usage of NET TPL features.&lt;/P&gt;

&lt;P&gt;So, I described it as much as I can in my previous post... but it is possible that you will be unable to reproduce it at your servers.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Feb 2018 09:38:45 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183511#M29456</guid>
      <dc:creator>bochkanov__sergey</dc:creator>
      <dc:date>2018-02-02T09:38:45Z</dc:date>
    </item>
    <item>
      <title>In case it is of any interest</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183512#M29457</link>
      <description>&lt;P&gt;In case it is of any interest, we have hit this exact issue. We diagnosed it (eventually) and found the solution, and then came across this discussion whilst attempting to verify&amp;nbsp;that&amp;nbsp;mkl_free_buffers() is safe in a multithreaded system (of course it transpires there is a better way, using thread specific calls.)&lt;/P&gt;&lt;P&gt;Our system uses TPL calling into a native dll, which accesses MKL, on Windows. The buffer allocations being per thread, and allocated by MKL, no problems appeared in any of our unit tests or console applications, even ones driven through the API using .Net - until we called in using the TPL.&lt;BR /&gt;We proved the issue by catching the underlying native thread ids (which are different from the ones shown in .Net) and seeing the allocations rising with each additional thread id.&lt;BR /&gt;&lt;BR /&gt;Calling&amp;nbsp;mkl_free_buffers() does prevent the increase in memory allocation. However, we're implementing a per-thread buffer free, at the point the thread is released.&lt;BR /&gt;&lt;BR /&gt;Sergey - thanks for your descriptions - they match precisely what we had determined we were seeing.&lt;BR /&gt;&lt;BR /&gt;Ying - thanks for your information.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Found using MKL 2018, Win32, Intel Parallel Studio XE Composer Edition 2017, Windows SDK 10.17763, Visual Studio 2017 (v141), .Net 4.7, Windows 10&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 13:39:44 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183512#M29457</guid>
      <dc:creator>Richard_G_5</dc:creator>
      <dc:date>2019-09-26T13:39:44Z</dc:date>
    </item>
    <item>
      <title>Hi @Richard G.,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183513#M29458</link>
      <description>&lt;P&gt;Edited, duplicate post&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2019 15:05:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183513#M29458</guid>
      <dc:creator>T__Petter</dc:creator>
      <dc:date>2019-12-05T15:05:00Z</dc:date>
    </item>
    <item>
      <title>Hi Richard G,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183514#M29459</link>
      <description>&lt;P&gt;Hi Richard G,&lt;/P&gt;&lt;P&gt;After a couple of weeks debugging I finally found the cause of my apps memory problems after reading this post. I have exactly the same combination of TPL and MKL. In my case, mkl_free_buffers() seems to solve the problem, but your solution of freeing memory when a native thread is released seems even better. I can not, however, see how I can&amp;nbsp;run code at the point the thread is released. Could you point me in the right direction?&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;Petter T&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2019 08:39:09 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183514#M29459</guid>
      <dc:creator>T__Petter</dc:creator>
      <dc:date>2019-12-06T08:39:09Z</dc:date>
    </item>
    <item>
      <title>Hi T, Petter,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183515#M29460</link>
      <description>&lt;P&gt;Hi T, Petter,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The solution we implemented, and which (so far as we can tell) works perfectly, is as follows:&lt;BR /&gt;&lt;BR /&gt;in the Dll Main function, implement functions to handle thread and process&amp;nbsp;detachment:&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="brush:cpp; class-name:dark;"&gt;    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
        HandleProcessAttachment();
        break;
    case DLL_PROCESS_DETACH:
        HandleProcessDetachment();   // This function for when the process exits
        break;
    case DLL_THREAD_DETACH:
        HandleThreadDetachment();    // This function for when a thread exits
        break;
    }&lt;/PRE&gt;

&lt;P&gt;In the function implementations, do the following (in addition to whatever else needs doing in them)&lt;/P&gt;

&lt;PRE class="brush:cpp; class-name:dark;"&gt;void HandleProcessDetachment() noexcept
{&amp;nbsp;

&amp;nbsp; &amp;nbsp;// ...

&amp;nbsp; &amp;nbsp;mkl_free_buffers();

}

void HandleThreadDetachment()
{
&amp;nbsp; &amp;nbsp; mkl_thread_free_buffers();
}&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This way, each time a thread is detached, the buffer allocated for it is cleared, and when the process is detached, all buffers are cleared.&lt;BR /&gt;We see no negative performance impacts, and the memory leak issues no longer manifest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Richard G&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 11:14:44 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/mkl-free-buffers-and-multiple-threads/m-p/1183515#M29460</guid>
      <dc:creator>Richard_G_5</dc:creator>
      <dc:date>2019-12-13T11:14:44Z</dc:date>
    </item>
  </channel>
</rss>

