<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Multi-threaded Mex	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/multi-threaded-mex/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/multi-threaded-mex?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=multi-threaded-mex</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Thu, 02 May 2024 08:00:00 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>
		By: Antonio		</title>
		<link>https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-524151</link>

		<dc:creator><![CDATA[Antonio]]></dc:creator>
		<pubDate>Thu, 02 May 2024 08:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7771#comment-524151</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-524150&quot;&gt;Anon&lt;/a&gt;.

Note Intel&#039;s MKL implementations of BLAS/LAPACK are already multi-threaded, so it is likely that you won&#039;t achieve a vast improvement (the overall performance could even be harmed) in doing this, unless you ensure BLAS calls use single threading.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-524150">Anon</a>.</p>
<p>Note Intel&#8217;s MKL implementations of BLAS/LAPACK are already multi-threaded, so it is likely that you won&#8217;t achieve a vast improvement (the overall performance could even be harmed) in doing this, unless you ensure BLAS calls use single threading.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Anon		</title>
		<link>https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-524150</link>

		<dc:creator><![CDATA[Anon]]></dc:creator>
		<pubDate>Thu, 07 Sep 2023 16:43:00 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7771#comment-524150</guid>

					<description><![CDATA[The &lt;i&gt;mexCallMATLAB&lt;/i&gt; function raises interesting possibilities for parallelization. 

I spent a few hours (days...) writing a mexFunction that mimics the functionality of MATLAB&#039;s &lt;i&gt;pagefun&lt;/i&gt; but on the CPU. I put the call to &lt;i&gt;mexCallMATLAB&lt;/i&gt; inside a &lt;code&gt;#pragma omp parallel&lt;/code&gt; for loop, so essentially it calls the builtin functions (e.g. eig, svd, inv, etc.) rather than recreating all the LAPACK interfacing in C++. It works on a single thread but crashes if num_threads &#062; 1. Any ideas what cardinal rule this is breaking?]]></description>
			<content:encoded><![CDATA[<p>The <i>mexCallMATLAB</i> function raises interesting possibilities for parallelization. </p>
<p>I spent a few hours (days&#8230;) writing a mexFunction that mimics the functionality of MATLAB&#8217;s <i>pagefun</i> but on the CPU. I put the call to <i>mexCallMATLAB</i> inside a <code>#pragma omp parallel</code> for loop, so essentially it calls the builtin functions (e.g. eig, svd, inv, etc.) rather than recreating all the LAPACK interfacing in C++. It works on a single thread but crashes if num_threads &gt; 1. Any ideas what cardinal rule this is breaking?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Hossein		</title>
		<link>https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-475893</link>

		<dc:creator><![CDATA[Hossein]]></dc:creator>
		<pubDate>Mon, 22 Apr 2019 01:06:47 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7771#comment-475893</guid>

					<description><![CDATA[Hi,

I changed the size of matrices from 4 to 10000, i.e. matrix1=rand(10000) and matrix2=rand(10000); 
Then, I compared the speed in terms of the number of the threads. 
For Nthreads=1 and Nthreads=4 the runtime is almost the same!!! I do not know why?!?!]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>I changed the size of matrices from 4 to 10000, i.e. matrix1=rand(10000) and matrix2=rand(10000);<br />
Then, I compared the speed in terms of the number of the threads.<br />
For Nthreads=1 and Nthreads=4 the runtime is almost the same!!! I do not know why?!?!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Peter Cook		</title>
		<link>https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-434511</link>

		<dc:creator><![CDATA[Peter Cook]]></dc:creator>
		<pubDate>Mon, 06 Aug 2018 20:48:38 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7771#comment-434511</guid>

					<description><![CDATA[Yair,

One multithreaded mex file I can&#039;t say enough good things about is &lt;a href=&quot;https://github.com/gharveymn/getmatvar&quot; rel=&quot;nofollow&quot;&gt;getmatvar&lt;/a&gt; which, in addition to being faster than MATLAB builtins (i.e. matfile objects), allows you to read part of a struct without loading an entire mat file!

Also worth checking out is &lt;a href=&quot;https://www.mathworks.com/matlabcentral/fileexchange/38034-mexthread&quot; rel=&quot;nofollow&quot;&gt;mexthread&lt;/a&gt; if you have a compiler with std::thread

I&#039;ve seen a couple mex files pop up which use the BOOST library as well, but I can&#039;t think of one at this moment. Maybe another reader can link to an example.]]></description>
			<content:encoded><![CDATA[<p>Yair,</p>
<p>One multithreaded mex file I can&#8217;t say enough good things about is <a href="https://github.com/gharveymn/getmatvar" rel="nofollow">getmatvar</a> which, in addition to being faster than MATLAB builtins (i.e. matfile objects), allows you to read part of a struct without loading an entire mat file!</p>
<p>Also worth checking out is <a href="https://www.mathworks.com/matlabcentral/fileexchange/38034-mexthread" rel="nofollow">mexthread</a> if you have a compiler with std::thread</p>
<p>I&#8217;ve seen a couple mex files pop up which use the BOOST library as well, but I can&#8217;t think of one at this moment. Maybe another reader can link to an example.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-433288</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 24 Jul 2018 17:09:12 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7771#comment-433288</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-433279&quot;&gt;Andy Stamps&lt;/a&gt;.

@Andy - In general I believe that it is better to use MEX&#039;s &lt;i&gt;mxMalloc/mxCalloc/mxRealloc/mxFree&lt;/i&gt; than their ANSI-C counterparts. This will ensure that all the MEX function&#039;s memory is properly and automatically managed by Matlab&#039;s memory manager, reducing the likelihood of segmentation violations and memory leaks. I admit that I did not follow my own advice in the code presented here. It doesn&#039;t make much difference if you allocate and free the memory properly, but using the mx* variants is a bit safer in general.

Note that using &lt;i&gt;mxFree&lt;/i&gt; to deallocate memory that was originally allocated using &lt;i&gt;mxCreate*&lt;/i&gt;, or conversely using &lt;i&gt;mxDestroyArray&lt;/i&gt; to deallocate memory that was originally allocated using &lt;i&gt;mx*alloc&lt;/i&gt;, leads to a segmentation fault - &lt;i&gt;mx*alloc&lt;/i&gt; should always and only be used with a corresponding &lt;i&gt;mxFree&lt;/i&gt;, and similarly &lt;i&gt;mxCreate*&lt;/i&gt; should always and only be used with a corresponding &lt;i&gt;mxDestroyArray&lt;/i&gt;.

Another error occurs when trying to &lt;i&gt;mxDestroyArray&lt;/i&gt; or &lt;i&gt;mxFree&lt;/i&gt; within a C++ destructor, since at that point the memory manager has already freed the data.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-433279">Andy Stamps</a>.</p>
<p>@Andy &#8211; In general I believe that it is better to use MEX&#8217;s <i>mxMalloc/mxCalloc/mxRealloc/mxFree</i> than their ANSI-C counterparts. This will ensure that all the MEX function&#8217;s memory is properly and automatically managed by Matlab&#8217;s memory manager, reducing the likelihood of segmentation violations and memory leaks. I admit that I did not follow my own advice in the code presented here. It doesn&#8217;t make much difference if you allocate and free the memory properly, but using the mx* variants is a bit safer in general.</p>
<p>Note that using <i>mxFree</i> to deallocate memory that was originally allocated using <i>mxCreate*</i>, or conversely using <i>mxDestroyArray</i> to deallocate memory that was originally allocated using <i>mx*alloc</i>, leads to a segmentation fault &#8211; <i>mx*alloc</i> should always and only be used with a corresponding <i>mxFree</i>, and similarly <i>mxCreate*</i> should always and only be used with a corresponding <i>mxDestroyArray</i>.</p>
<p>Another error occurs when trying to <i>mxDestroyArray</i> or <i>mxFree</i> within a C++ destructor, since at that point the memory manager has already freed the data.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andy Stamps		</title>
		<link>https://undocumentedmatlab.com/articles/multi-threaded-mex#comment-433279</link>

		<dc:creator><![CDATA[Andy Stamps]]></dc:creator>
		<pubDate>Tue, 24 Jul 2018 14:47:15 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7771#comment-433279</guid>

					<description><![CDATA[Yair, Can you comment on what the distinction would be for using the ANSI C memory allocation routines (malloc/calloc/realloc/free) versus the MATLAB-specific variants (mxMalloc/mxCalloc/mxRealloc/mxFree)?  The ANSI versions are used in your example above, and so I&#039;m wondering if there was a specific rationale or if it comes down to personal preference.]]></description>
			<content:encoded><![CDATA[<p>Yair, Can you comment on what the distinction would be for using the ANSI C memory allocation routines (malloc/calloc/realloc/free) versus the MATLAB-specific variants (mxMalloc/mxCalloc/mxRealloc/mxFree)?  The ANSI versions are used in your example above, and so I&#8217;m wondering if there was a specific rationale or if it comes down to personal preference.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
