Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Inplace array (mex)
Date: Sat, 27 Jun 2009 21:13:01 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 41
Message-ID: <h2620t$162$1@fred.mathworks.com>
References: <h2540t$73d$1@fred.mathworks.com> <h25val$cna$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1246137181 1218 172.30.248.38 (27 Jun 2009 21:13:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 27 Jun 2009 21:13:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:551129


"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <h25val$cna$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h2540t$73d$1@fred.mathworks.com>...
> > I want to create an inplace column vector using MEX. The idea is to create a column array that has its data pointer points on the the data block of an existing (input) matrix. Everything going well until I set the data pointer. Then it crashes 
> > 
> > a=rand(1,7)
> > 
> > a =
> > 
> >     0.8147    0.9058    0.1270    0.9134    0.6324    0.0975    0.2785
> > 
> > >> b=inplacecolumn(a,3)
> > 
> > Am I allowed to do such thing? If not what cause the crash?
> 
> No. You cannot do this. The problem is what happens when variables get cleared. If "a" gets cleared, the MATLAB memory manager knows nothing about this shared memory with "b".  So the data memory for "a" is freed, and suddenly the "b" data pointer is pointing to invalid memory. Of course any access of this memory downstream could cause a crash.  And what happens when you clear "b", either before or after "a" is cleared? The MATLAB memory manager does not have any memory block address for allocations that match the "b" data pointer, so when it tries to check for sharing or tries to free this memory the results will be undefined, again potentially resulting in a crash.
> 

Oh James, I don't worry about those clearing things, I can just right my own inplace CLEAR to undo the b, in the reverse sequences from how they are created (such like your uninplacecolumn).

My problem is I cannot even create such in-place pointer in a newer Matlab version (can't tell which one). It works all right in older Matlab version, e.g., 2006B. Actually I was surprised by the example given in your post, and that's why I come back and run INPLACECOLUMN under 2006B and it works. So now this is the scope. What is going on with the newer Matlabn version to prevent me doing such thing?

I feel I must explain why I want to do such thing. The reason is I want to avoid unnecessary data copy when call a function using a column of a matrix:

a = rand(1000);
for k=1:size(A,2)
    myfun(a(:,k)); 
end

This calling will make a copy the data a(:,k) into a new vector. So I want to avoid the copy and do

a = rand(1000);
for k=1:size(A,2)
    ak = inplacecolumn(a,k);
    myfun(ak);
    uninplacecolumn(ak);
    clear ak; 
end

But it seems this will not work with recent Matlab version. Now if someone knows something about the reason, I would love to hear!!!!

Bruno