Bug#706376: [Pkg-octave-devel] Bug#706376: Bug#706376: Bug#706376: Bug#706376: Bug#706376: octave: sparse matrix n*2^16

2013-06-19 Thread Jordi Gutiérrez Hermoso
On 19 June 2013 14:06, David Bateman da...@bateman.eu wrote:
 On 06/16/2013 03:59 AM, Jordi Gutiérrez Hermoso wrote:
 I'm saying the real problem is that we assume linear indexing works
 for all matrix types, including sparse matrices. I claim that this is
 the real problem.

 Who is assuming linear indexing works for all matrix types ? Where
 exactly is that stated ?

We are assuming it in our code. In numel for one. And in places like
whatever processes A(idx) for a logical index idx. We're not making
special cases in these places, if (sparse_type)
dont_linearly_index();

Each of these places that assume that linear indexing works needs to
be patched to check for sparse types.

 We can patch around this problem by avoiding linear
 indexing,

 The bug report was in trace that called isempty on a sparse matrix.
 Neither function needs numel or linear indexing. We aren't patching
 around anything, we are fixing a bug

We are fixing one symptom of a larger bug, a bug that is present in
many different locations.

  but this is just treating the symptoms, not the disease.

 So you advocate everyone moving to 64-bit indexing ?

That would delay the problem for a nontrivial amount of time. It would
be nice, but it wouldn't fix the problem.

There are other things we could do.

(1) Avoid linear indexing for sparse matrices as much as possible,
i.e. check everywhere we can think of for sparse matrix types. You've
mentioned a few more places where this should be checked.

(2) Warn when creating sparse matrices with large indices that some
operations may not work, or clearly error out when those operations
are attempted.

(3) Abstract octave_idx_type so that it doesn't actually use 32-bit
ints for sparse matrices.

 While I don't deny that we can make some progress masking the
 symptoms, the disease itself should also be treated somehow.

 There is no disease, and unless you want to artificially limit the
 size of sparse matrices that can be treated such that numel is less
 than 2^31 for 32 bit indexing and 2^63 for 64 bit indexing. Why do
 this which makes sparse matrices much less useful, so there is no
 solution for what you call a disease

Well, numel needs if(sparse_type) {weep();} or whatever.

 So essentially you're saying that sparse matrices with
 32-bit indexing and numel larger than 2^31 are useless!!
 I'm saying that they will fail in other unexpected ways,

 Isn't that the definition of a bug.

Yes, the real bug: that we have a tacit assumption in our code that
linear indexing works.

  and we shouln't mask symptoms.

 We never tried to. Look at the code in dim-vector.h

 quote
   // The following function will throw a std::bad_alloc ()
   // exception if the requested size is larger than can be indexed by
   // octave_idx_type. This may be smaller than the actual amount of
   // memory that can be safely allocated on a system.  However, if we
   // don't fail here, we can end up with a mysterious crash inside a
   // function that is iterating over an array using octave_idx_type
   // indices.

   octave_idx_type safe_numel (void) const;
 /quote

 The numel method of SparseT calls this method that is supposed to
 throw an error. However as the builtin Fnumel is calling args(1).numel()
 which is calling dims().numel() the sparse safe version of numel isn't
 being called. The solution is to add a numel method to
 octave_base_sparse that calls dims().safe_numel() instead. So this is a
 bug as well.

Yep, I suppose that's my dont_linearly_index() function suggested
above.

 As for linear indexing, if you look in idx-vector.cc you'll see that in
 the convert_index functions an error is returned like

 octave_idx_type idx = static_castoctave_idx_type(d)
 bool err = static_castdouble (idx) != d;

 So as expected

 s = speye (2^17);
 s (2^32)

 throws an error

 error: subscript indices must be either positive integers or logicals

 You might think this is a little cryptic but I wouldn't call it masking
 a symptom. I propose modifying this error to read

 error: subscript indices must be either positive integers less than 2^31
 or logicals

 for 32 bit indexing and

 error: subscript indices must be either positive integers less than 2^63
 or logicals

 for 64 bit indexing.

I think you'll still need to do something about a more realistic
situation of linear indexing with a logical matrix, which also ends up
translating into linear indexing thanks to our underlying bug: the
assumption that linear indexing works. In this case, there shouldn't
be an error at all, like Ed suggested, since we have enough
information in a logical matrix to avoid linear indexing.

 See the attached changeset

It looks good. Can you push it? Also, note that we have an actual
Octave bug for it:

https://savannah.gnu.org/bugs/?38936

Contrary to apperances, I don't mean to be unhelpful, so please let me
know if you can't push the fix yourself.

- Jordi G. H.


--
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org

Bug#706376: [Pkg-octave-devel] Bug#706376: Bug#706376: Bug#706376: Bug#706376: Bug#706376: octave: sparse matrix n*2^16

2013-06-19 Thread David Bateman
On 06/19/2013 09:53 PM, Jordi Gutiérrez Hermoso wrote:
 I think you'll still need to do something about a more realistic
 situation of linear indexing with a logical matrix, which also ends up
 translating into linear indexing thanks to our underlying bug: the
 assumption that linear indexing works. In this case, there shouldn't
 be an error at all, like Ed suggested, since we have enough
 information in a logical matrix to avoid linear indexing.

Huh ? How can a logical matrix linear index overflow ? The matrix is
limited to 2^31-1 elements in any case and so can never overflow! I
presume you means sparse logical matrix linear indexing. This is one
of the FIXMEs the the sparse code that has existed for a long while and
is documented in ov-bool-sparse.h

quote
  // FIXME Adapt idx_vector to allow sparse logical indexing!!
  idx_vector index_vector (void) const
{ return idx_vector (bool_array_value ()); }
/quote

and in the projects page (as copied from the old PROJECTS file)

quote
Sparse logical indexing in idx_vector class so that something like
'a=sprandn(1e6,1e6,1e-6); a(a1) = 0' won't cause a memory overflow.
/quote

At the time I wrote the above line, it seemed a major effort to do this
correct. It seems that Yaroslav added some code to treat sparse logical
index vectors but didn't connect it to the idx_vector method of
octave_sparse_bool_matrix. It should be used as it'll improve the speed
of the creation of idx_vector, but it doesn't help as the underlying
data type of idx_vector in this case is Arrayoctave_idx_type and so a
single index (even if its a sparse logical matrix) can result in an
overflow. This will take major reworking of the idx_vector class and the
uses of single index vectors in SparseT. In any case this throws and
error at this point and so won't silently fail

 It looks good. Can you push it? 

I'd like to add some tests first and see if any other bugs have turned
up after this change. For example the changes use made to sprand and
sprandn 2 years ago to call randperm also overflows. At the moment I'm
getting 791 failed tests with make check is that normal ?

David


-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org



Bug#706376: [Pkg-octave-devel] Bug#706376: Bug#706376: Bug#706376: Bug#706376: Bug#706376: octave: sparse matrix n*2^16

2013-06-19 Thread David Bateman
On 06/20/2013 01:10 AM, David Bateman wrote:
 I'd like to add some tests first and see if any other bugs have turned
 up after this change. For example the changes use made to sprand and
 sprandn 2 years ago to call randperm also overflows. At the moment I'm
 getting 791 failed tests with make check is that normal ? David 

Ok, it seems Jaroslav's code for idx_vector(Sparsebool hasn't been
used much in the last 5 years as it was completely wrong and when I
started using it, it caused 791 failures in make check. I've fixed his
code as it makes sense to use it and pushed my changeset at

http://hg.savannah.gnu.org/hgweb/octave/rev/8fce0ed4894a

David


-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of unsubscribe. Trouble? Contact listmas...@lists.debian.org