Thread Subject: Return empty sparse from a mex file

Subject: Return empty sparse from a mex file

From: Arne Vagren

Date: 26 Oct, 2009 10:02:02

Message: 1 of 9

Hello!

I'm trying to assemble and return a sparse matrix in a mex-function. At the end of the function, I try to correct for the actual number of non-zero entries in the following way (where Btu = mxGetPr(plhs[0]);):

"nbytes = actual_number_of_nonzeros*sizeof(*Btu);
 newptr = mxRealloc(Btu,nbytes);
 mxSetPr(plhs[0], newptr);
        
 nbytes = actual_number_of_nonzeros*sizeof(*ir);
 newptr = mxRealloc(ir, nbytes);
 mxSetIr(plhs[0], newptr);"

To test my function I have tried , the, excellent, spok-function, see http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok, which gives an ok except when the output is empty, i.e. nnz = 0. spok then returns the error message "double, but with no values present". The resulting matrix still seem to work fine but it's a little disturbing to have an error which I don't understand.

My question is thus: What should my function return if actual_number_of_nonzeros = 0?

Best regards
Arne

Subject: Return empty sparse from a mex file

From: Bruno Luong

Date: 26 Oct, 2009 10:49:01

Message: 2 of 9

"Arne Vagren" <arne.vagren@gmail.com> wrote in message <hc3s2q$fbd$1@fred.mathworks.com>...
> Hello!
>
> I'm trying to assemble and return a sparse matrix in a mex-function. At the end of the function, I try to correct for the actual number of non-zero entries in the following way (where Btu = mxGetPr(plhs[0]);):
>
> "nbytes = actual_number_of_nonzeros*sizeof(*Btu);
> newptr = mxRealloc(Btu,nbytes);
> mxSetPr(plhs[0], newptr);
>
> nbytes = actual_number_of_nonzeros*sizeof(*ir);
> newptr = mxRealloc(ir, nbytes);
> mxSetIr(plhs[0], newptr);"
>

There is a possibly problem in Jc array, which you didn't provide information.

Bruno

Subject: Return empty sparse from a mex file

From: Arne Vagren

Date: 26 Oct, 2009 11:19:02

Message: 3 of 9

Thanks for your reply. Actually, in the case nnz = 0, I haven't touched the jc array, it's
simply an array of zeros of size [number of columns + 1]

Subject: Return empty sparse from a mex file

From: Bruno Luong

Date: 26 Oct, 2009 11:45:18

Message: 4 of 9

"Arne Vagren" <arne.vagren@gmail.com> wrote in message <hc40j6$o3d$1@fred.mathworks.com>...
> Thanks for your reply. Actually, in the case nnz = 0, I haven't touched the jc array, it's
> simply an array of zeros of size [number of columns + 1]

Have you changed nzmax to zero accordingly (by using mxSetNzmax)?

Bruno

Subject: Return empty sparse from a mex file

From: Bruno Luong

Date: 26 Oct, 2009 11:50:18

Message: 5 of 9

Actually I just check, it seems nzmax is never set to 0 by Matlab

>> s=sparse([],[],[],1,1,0)
>> nzmax(s)

ans =

     1

So I would Realloc Pr and Ir with *1* element, set nzmax to 1, and fill Jc with zeros (as you did).

Bruno

Subject: Return empty sparse from a mex file

From: Arne Vagren

Date: 26 Oct, 2009 12:18:02

Message: 6 of 9

Ok, I added the line

    if (actual_number_of_nonzeros==0)
        actual_number_of_nonzeros = 1;

before reallocating Pr and Ir. It works fine now, although it seem a little odd to me. Thanks again for your comments!
/Arne

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hc42dq$ke8$1@fred.mathworks.com>...
> Actually I just check, it seems nzmax is never set to 0 by Matlab
>
> >> s=sparse([],[],[],1,1,0)
> >> nzmax(s)
>
> ans =
>
> 1
>
> So I would Realloc Pr and Ir with *1* element, set nzmax to 1, and fill Jc with zeros (as you did).
>
> Bruno

Subject: Return empty sparse from a mex file

From: Tim Davis

Date: 30 Oct, 2009 16:11:03

Message: 7 of 9

"Arne Vagren" <arne.vagren@gmail.com> wrote in message <hc3s2q$fbd$1@fred.mathworks.com>...
> Hello!
>
> I'm trying to assemble and return a sparse matrix in a mex-function. At the end of the function, I try to correct for the actual number of non-zero entries in the following way (where Btu = mxGetPr(plhs[0]);):
>
> "nbytes = actual_number_of_nonzeros*sizeof(*Btu);
> newptr = mxRealloc(Btu,nbytes);
> mxSetPr(plhs[0], newptr);
>
> nbytes = actual_number_of_nonzeros*sizeof(*ir);
> newptr = mxRealloc(ir, nbytes);
> mxSetIr(plhs[0], newptr);"
>
> To test my function I have tried , the, excellent, spok-function, see http://www.mathworks.com/matlabcentral/fileexchange/20186-spok-checks-if-a-matlab-sparse-matrix-is-ok, which gives an ok except when the output is empty, i.e. nnz = 0. spok then returns the error message "double, but with no values present". The resulting matrix still seem to work fine but it's a little disturbing to have an error which I don't understand.
>
> My question is thus: What should my function return if actual_number_of_nonzeros = 0?
>
> Best regards
> Arne

You are right to be troubled; spok is correctly telling you that you have constructed an invalid matrix. That matrix, with a NULL pointer for mxGetPr, will break MATLAB somewhere. Not everywhere, so it may seem fine, but it is not valid.

nnzmax(A) must never be zero, even if nnz(A) is zero. nnzmax(A)>=1 must always hold.

Subject: Return empty sparse from a mex file

From: James Tursa

Date: 30 Oct, 2009 17:46:03

Message: 8 of 9

"Tim Davis" <davis@cise.ufl.edu> wrote in message <hcf36n$83n$1@fred.mathworks.com>...
>
> You are right to be troubled; spok is correctly telling you that you have constructed an invalid matrix. That matrix, with a NULL pointer for mxGetPr, will break MATLAB somewhere. Not everywhere, so it may seem fine, but it is not valid.
>
> nnzmax(A) must never be zero, even if nnz(A) is zero. nnzmax(A)>=1 must always hold.

FYI added info just to complete this thread. There is a difference between full matrices and sparse matrices on this point. If you examine the pr of a full empty matrix, it can be NULL (0), whereas for a sparse matrix it always points to a valid memory location.

James Tursa

Subject: Return empty sparse from a mex file

From: Arne Vagren

Date: 3 Nov, 2009 19:19:20

Message: 9 of 9

Thanks! I guess that's a rather subtle error that could have caused me quite a bit of frustration...

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
sparse Arne Vagren 26 Oct, 2009 06:04:03
mex Arne Vagren 26 Oct, 2009 06:04:03
spok Arne Vagren 26 Oct, 2009 06:04:03
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com