Re: RE: Filling sparse matrix by A(i, i)=... .Fast for small matrices, slow for large.Is there a better way to fill the matrix?
Janusz opechowski <[email protected]> Wed, 27 Apr 2005 16:37:59 +0100 (BST)
| Newsgroups | gmane.comp.lib.mtl.devel |
|---|---|
| Message-ID | <[email protected]> |
Well..
Try to allocate/deallocate memory rarely.
How about that :
Try to use std::vector<> with sparse mtl matrix
and allocate memory not every entry you made
but say every 1000 entries :
--------------------------
std::vector<..> a;
int mem = /* 10^5 is also good */ 1000 ;
a.reserve( mem );
// put matrix entries, some loop :
{
a.push_back(some_entry)
if( a.size()>= a.capacity() )
{
mem+=1000;
a.reserve(mem);
}
}
// end loop
--------------------
and finally build up a sparse matrix :)
JO
--- Guilherme Parreira <[email protected]> wrote:
>
> Thanks, JO.
>
> It happens that I don't know a priori how many
> non-zero elements the matrix
> will contain.
>
> Regards,
> ==============================
> Eng. Guilherme F. Parreira
> Electrical Engineering Graduate Student
> GOPAC - UFMG - Brazil
> ==============================
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On
> Behalf Of Janusz opechowski
> Sent: terça-feira, 26 de abril de 2005 09:19
> To: General Matrix Template Library (MTL) list
> Subject: Re: MTL: RE: Filling sparse matrix by
> A(i,i)=... .Fast for small
> matrices,slow for large.Is there a better way to
> fill the matrix?
>
> Hi there,
> try using previously allocated memory.
>
> Everytime you add some value probably memory is
> allocate one more time
> ( .. how about std::vector<T>.reserve() mechanism
> dear sirs )
>
>
> Beautifull exampe of it is in MTL's
> online example file.
>
> Go there :
>
>
http://www.osl.iu.edu/research/mtl/show_src.php3?file=./examples/sparse_matr
> ix.cc
>
> Cheers.
> JO
>
>
> --- Guilherme Parreira <[email protected]>
> wrote:
>
> > The MTL documentation says to use iterators in
> order
> > to speed up computation
> > in matrices, but I don't know how.
> >
> > Regards,
> > ==============================
> > Eng. Guilherme F. Parreira
> > Electrical Engineering Graduate Student
> > GOPAC - UFMG - Brazil
> > ==============================
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]] On
> > Behalf Of Tony
> > Sent: domingo, 24 de abril de 2005 10:23
> > To: [email protected]
> > Subject: MTL: Filling sparse matrix by A(i, i)=...
> > .Fast for small
> > matrices,slow for large.Is there a better way to
> > fill the matrix?
> >
> > Hi
> >
> > I have a sparse matrix
> >
> > #define N 10000
> >
> > typedef matrix< Type,
> > rectangle<>,
> > compressed<>,
> > row_major >::type Matrix;
> >
> > Matrix A(N,N);
> >
> > and I am filling it up something like
> >
> > for(i=0; i < N ; i++){
> > A(i,i) = ....
> > lots of other stuff in here setting up
> other
> > elements of A
> >
> > }
> >
> > In my case I have quite a complex set of if
> > statements in the loop
> > setting up the A matrix and I have a maximum of 12
> > non zero entries on
> > each row.
> >
> > The code works fine for small matrix sizes, say N
> > less than 50,000.
> > But when I set N larger than this the code runs
> very
> > slowly. In my
> > case I would like to run the code with N=1500000.
> > But I need to leave
> > the computer on for several days for A to be
> > constructed.
> >
> > Question:
> > Can I fill the elements of A in a faster way? I
> am
> > wondering if if
> > would be faster adding a whole row at a time to A,
> > but I don't know
> > how to do this, if this is a possibility can
> someone
> > please show me
> > how to do this and/or any other ways I can speed
> up
> > filling up the
> > elements of A
> >
> > Thank you for your time
> >
> > Tony
> >
> > _______________________________________________
> > This list is archived at
> > http://www.osl.iu.edu/MailArchives/mtl-devel/
> >
> >
> >
> > _______________________________________________
> > This list is archived at
> > http://www.osl.iu.edu/MailArchives/mtl-devel/
> >
>
>
________________________________________________________________________
> Yahoo! Messenger - Communicate instantly..."Ping"
> your friends today! Download Messenger Now
> http://uk.messenger.yahoo.com/download/index.html
> _______________________________________________
> This list is archived at
> http://www.osl.iu.edu/MailArchives/mtl-devel/
>
>
>
>
>
> _______________________________________________
> This list is archived at
> http://www.osl.iu.edu/MailArchives/mtl-devel/
>
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/