Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Pass by reference in MexFunction
Date: Mon, 22 Apr 2013 15:08:09 +0000 (UTC)
Organization: Boeing Co
Lines: 17
Message-ID: <kl3jop$9ps$1@newscl01ah.mathworks.com>
References: <ibmvr7$9no$1@fred.mathworks.com> <ibnee3$mer$1@fred.mathworks.com> <ibni0g$cud$1@fred.mathworks.com> <ibo39m$pgo$1@fred.mathworks.com> <iv9uu7$rqk$1@newscl01ah.mathworks.com> <ivah59$au6$1@newscl01ah.mathworks.com> <kkmqa8$kv8$1@newscl01ah.mathworks.com> <kkmu5v$44o$1@newscl01ah.mathworks.com> <kl3agj$7pu$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-04-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1366643289 10044 172.30.248.35 (22 Apr 2013 15:08:09 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 22 Apr 2013 15:08:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:794067

"Pierre " <ppsscchhrrooeeddeerr@vitechnology.com> wrote in message <kl3agj$7pu$1@newscl01ah.mathworks.com>...
> 
> I'd like to point out, that when I used in-place modification of an array some time ago, it did produce erronous results, although, based solely on the MATLAB script, there was no obvious reason for shared memory issues. The situation was like this:
>   function cost = costFunction(...)
>   M = initialize_memory_space_for_huge_matrix();
>   for i=1:n
>       computeValueForMInPlace(M, inputData{i});
>       cost(i) = computeScoreBasedOnM(M);
>   end
> 
> costFunction was subject to optimization by lsqnonlin. I did that because memory allocation represented a serious problem and caused tremendous execution times, if memory for M was allocated inside the mex at each iteration. Now, for some reason, executing this code in debug mode worked just fine, but running it normally, caused the results to get messed up all over the place.
> 
> I suppose, although it's pure speculation, that either MATLAB somehow interlaced execution of the iterations (even though we don't have the parallel processing toolbox), based on the assumption, that M would remain constant during the entire loop as it's never being used on lhs inside the loop. Or that perhaps lsqnonlin has some parallel evaluation mechanism of costFunction which caused things to get messed up... but I cannot imagine how latter would create hidden shared-memory issues in this case?!

I would be interested in seeing a minimal version of the code that produces this problem. From your scant description it is hard to guess what the underlying problem was. (e.g., maybe a temporary variable that was cleared and you didn't realize it). 

james Tursa