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$>
References: <ibmvr7$9no$> <ibnee3$mer$> <ibni0g$cud$> <ibo39m$pgo$> <iv9uu7$rqk$> <ivah59$au6$> <kkmqa8$kv8$> <kkmu5v$44o$> <kl3agj$7pu$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1366643289 10044 (22 Apr 2013 15:08:09 GMT)
NNTP-Posting-Date: Mon, 22 Apr 2013 15:08:09 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: comp.soft-sys.matlab:794067

"Pierre " <> wrote in message <kl3agj$7pu$>...
> 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