Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2006-07-05 09:22:21


On Wednesday 05 July 2006 14:37, Nico Galoppo wrote:
> That's what I thought, but I found that I had to first call resize()
> explicitly to avoid crashes when assign()'ing from tmp.

You have to call resize because assign() assumes that both matrices have the
same size.

> > // untested
> > for (size_type i = 0; i < sizeM; ++ i) { // create size1 vector elements
> > vector_data_value_type & ref =
> > data_.insert_element (i, vector_data_value_type ()) .resize (sizem,
> > false); ref.reserve(non_zeros / sizeM);
> > }
>
> I get the following error, it seems that my version of insert_element
> doesn't return anything. Is there a macro that needs to be defined in order
> for this to work?

Ok. After thinking about that idea I found a better solution:

// this should preserve the data of the container, but I would not
// rely on that ...
template <class CONTAINER>
class op_reserve
{
private:
  size_t size;
public:
  op_reserve() : size(0) {}
  op_reserve(const size_t n) : size(n) {}
  void operator()(CONTAINER & c) const
  {
    c.reserve(size);
  }
};

template<class CONTAINER>
op_reserve<CONTAINER>
do_reserve(const size_t n, const CONTAINER& /* c */)
{
  return op_reserve<CONTAINER>(n);
}

[...]

generalized_vector_of_vector< double,
  row_major, vector<compressed_vector<double> > > A(size,size);
std::for_each( A.data().begin(), A.data().end(), do_reserve(nnz_per_row,
A.data()[0]) );

> I see, thanks for the code. For this, I'll have find out the sparsity
> pattern of my matrix first, correct? I guess I could infer it by analyzing
> the temporary gvov. Or is iterating over the gvov in an ordered fashion
> sufficient?

The default assignment should be very fast. Make sure you have defined NDEBUG

compressed_matrix<double, row_major> B(size,size,A.nnz());
B.assign(A);

I attach another one of my timing examples.

mfg
Gunter

-------------
$ g++-4.1 -I $HOME/include/ -D NDEBUG -O2 sparse_fill3.cpp -o sparse_fill3
$ ./sparse_fill3 10000
size 10000
sum(column(A,0)) 48
test_matrix_fill_insert coordinate_matrix: 3.39
assign from coordinate_matrix: 0.1
sum(column(A,0)) 48
test_matrix_fill_insert vector_of_coordinate_vector: 1.27
assign from vector_of_coordinate_vector: 0.85
sum(column(A,0)) 48
test_matrix_fill_plus_assign vector_of_compressed_vector: 4.83
assign from vector_of_compressed_vector: 0.48

$ g++-4.1 -I $HOME/include/ -D NDEBUG -O2 sparse_fill3.cpp -o sparse_fill3 -D
RESERVE
$ ./sparse_fill3 10000
size 10000
sum(column(A,0)) 48
test_matrix_fill_insert coordinate_matrix: 3.53
assign from coordinate_matrix: 0.09
sum(column(A,0)) 48
test_matrix_fill_insert vector_of_coordinate_vector: 1.25
assign from vector_of_coordinate_vector: 0.82
sum(column(A,0)) 48
test_matrix_fill_plus_assign vector_of_compressed_vector: 4.83
assign from vector_of_compressed_vector: 0.48

the effect of reserve() is minimal.