toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Toon-members] Dynamic Matrix allocation in an iteration loop?


From: E. Rosten
Subject: Re: [Toon-members] Dynamic Matrix allocation in an iteration loop?
Date: Tue, 23 Mar 2010 17:48:00 +0000 (GMT)
User-agent: Alpine 2.00 (LSU 1167 2008-08-23)

On Tue, 23 Mar 2010, Gerhard Reitmayr wrote:

Hi,

if you are worried about the overhead of memory allocation, then you could allocate a large matrix and then use for example a dynamical slice inside the loop. or make a reference matrix which you point at some preallocated array of data.

I think that is the best method.

small dynamic matrices might be allocated on
the stack anyway, so not real overhead compared to a static one...

using a resizable will not change as it will also allocate on resize (I think ?)

As yet, there is no resizable matrix. The current plan is to allow row-resizable and column-resizable matrix, where you can either resize the rows OR resize the columns but not both. The reason for this is that it matches a relatively common workflow (eg building up a Jacobian) and it is efficient to impement with a std::vector.

An implementation which allows efficient resizing in either rows or columns while keeping a copy of the data after a resize is possible, but tricky. If one does not wish to keep a copy of the data, then it is somewhat easier to implement.

-Ed

I would ask myself first, if the allocation is really a bottle neck. Always measure first, before attempting optimization! it might not really make a difference in your program. So profile your function, program etc. first and see if this allocation takes an inappropriate amount of time.

cheers,
 Gerhard

On 22 Mar 2010, at 18:25, ETH wrote:

Hello TooN Members

I have a simple question:
I have an iteration loop in which my matrix e.g A is always size dependend, for 
example in one iteration its  9 x 3 in another its 9x16 and so on.
The problem facing TooN is that I always make new dynamical Matrices for this 
purpose but this is probably stupid because I always allocate  new memory in 
each iteration?

Is there an efficient way to reduce allocation here?   By a Matrix <...,..., 
Reference>?
Or should I use Resizable Matrices?

Can someone help?

Thanks a lot!!




Am 20.03.2010 um 17:01 schrieb address@hidden:

Send Toon-members mailing list submissions to
        address@hidden

To subscribe or unsubscribe via the World Wide Web, visit
        http://lists.nongnu.org/mailman/listinfo/toon-members
or, via email, send a message with subject or body 'help' to
        address@hidden

You can reach the person managing the list at
        address@hidden

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Toon-members digest..."


Today's Topics:

 1.  NEW(with code) : Implementation of BaseClass with  virtual
    template functions with TOON (Gabriel N?tzi)


----------------------------------------------------------------------

Message: 1
Date: Fri, 19 Mar 2010 17:46:08 +0100
From: Gabriel N?tzi <address@hidden>
Subject: [Toon-members] NEW(with code) : Implementation of BaseClass
        with    virtual template functions with TOON
To: address@hidden
Message-ID: <address@hidden>
Content-Type: text/plain; charset="us-ascii"

(Here again because source code was missing)

Hello all,

I have a basic question where I am completely stuck with TooN:
I dont know how to realize this:

I have a DynamicSystem Class which describes a Dynamical System with template 
parameter <int NDOF>
I would like to have a Class Solver which has a reference to an instance of 
DynamicSystem.
The solver should be able to call several virtual functions from DynamicSystem. 
(subClass could be a Ball or a Car or what ever :-))
The main problem is that this main functions take Vectors/Matrices as Input and 
give Vector and Matrices as Output back (depending on NDOF)
but when I want to properly define such a function in TOON i need templates (to 
be able to input also slices and so on....)

like

template<typename BaseM, typename BaseV>
Matrix<3,3,double,BaseM> get_M (double t, Vector<3,double, BaseV > x   ){
                // some code
        }

But the problem is that in C++ we can not define templates as virtual 
functions......
(SEE ATTACHED SIMPLE CODE!!)

HOW DO I SOLVE this Problem with TooN?
How should I structure my Classes? Would be very helpful if someone can quickly 
append the changes in the attached source code (ITS SIMPLE :-))!

Thank you very much for your HELP!!


=========================================CODE======================================================



#include <iostream>
#include <TooN/TooN.h>

using namespace std;
using namespace TooN;

template<int NDOF>
class DynamicSystem {

public:
        DynamicSystem(){
        };
        ~DynamicSystem(){
        };

        virtual Matrix<NDOF,NDOF> get_M(double t, Vector<NDOF> x)=0;

private:
        int ndof;

};

class WoodPecker : public DynamicSystem<3>{
        public:
        WoodPecker(){
        };
        ~WoodPecker(){
        };

        Matrix<3,3> get_M(double t, Vector<3> x){
                Matrix<3,3> A =Identity;
                return A;
        }
};

template<int NDOF>
class Solver{
public:
        Solver(DynamicSystem<NDOF> & myref): ref(myref) {

        };
        ~Solver(){
        };
        DynamicSystem<NDOF> & ref;

        void dosome(){
                Vector<> a(NDOF);
                cout << ref.get_M(0,a) <<endl; //works
                // cout << ref.get_M(0,a.slice<>(1,NDOF)) <<endl; // does not 
work..... (shit) :-)
        }
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://lists.gnu.org/pipermail/toon-members/attachments/20100319/5dfd8339/attachment.html

------------------------------

_______________________________________________
Toon-members mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/toon-members


End of Toon-members Digest, Vol 43, Issue 2
*******************************************



_______________________________________________
Toon-members mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/toon-members

--
Gerhard Reitmayr
Institute for Computer Graphics and Vision
http://www.icg.tugraz.at/Members/gerhard
tel: ++43 316 873 5082





_______________________________________________
Toon-members mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/toon-members


--
(You can't go wrong with psycho-rats.)(http://mi.eng.cam.ac.uk/~er258)

/d{def}def/f{/Times s selectfont}d/s{11}d/r{roll}d f 2/m{moveto}d -1
r 230 350 m 0 1 179{ 1 index show 88 rotate 4 mul 0 rmoveto}for/s 12
    d f pop 235 420 translate 0 0 moveto 1 2 scale show showpage




reply via email to

[Prev in Thread] Current Thread [Next in Thread]