CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Metaprogramming Nested Loops?

    What do you think is the best way to generate nested for loops using template metaprogramming? I'm making a library that performs basic vector and matrix algebra (I know about blitz++, but it does not have everything I want fore example N-Dimensional Cross Products, RREF and Determinants.). Currently, I need to compute the product of two matrices. I have looked at the blitz library implementation, but I couldn't figure out how all the elements were accessed using his algorithm. Perhaps one of you have also looked into this and know how it was done? Otherwise, I will just use a nested loop. There is nothing wrong with that right? Thanks.

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Metaprogramming Nested Loops?

    Quote Originally Posted by binarybob0001
    Currently, I need to compute the product of two matrices. I have looked at the blitz library implementation, but I couldn't figure out how all the elements were accessed using his algorithm.
    Why would you want to reinvent the wheel?
    Adding functions doesn't mean that you have to rewrite existing functions!

    Blitz++ probably uses the fastest known algorithm, perhaps with Divide To Conquer (I know that there are such "fast multiplication" algorithms on matrices).

    Quote Originally Posted by binarybob0001
    Otherwise, I will just use a nested loop. There is nothing wrong with that right?
    It is wrong, because :
    1) It'll be slow.
    2) You may have bugs.
    3) OOP (or even procedural programming) is designed to avoid rewriting an existing function.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #3
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: Metaprogramming Nested Loops?

    Are you saying it would be better for me to extend the blitz++ library. It cannot handle some things that it should be able to handle. Last time I use it, it produced odd results with things like (I no longer have the sample program unfortunately, but I will recreate if necessary).
    n = n*v; //n and v are vectors.
    The library is fast, but it is frustrating. Here's his algorithm.

    Code:
    // Template metaprogram for matrix-matrix multiplication
    template<int N_rows1, int N_columns, int N_columns2, int N_rowStride1,
        int N_colStride1, int N_rowStride2, int N_colStride2, int K>
    class _bz_meta_matrixMatrixProduct {
    public:
        enum { go = (K != N_columns - 1) };
    
        template<class T_numtype1, class T_numtype2>
        static inline BZ_PROMOTE(T_numtype1, T_numtype2)
        f(const T_numtype1* matrix1, const T_numtype2* matrix2, int i, int j)
        {
            return matrix1[i * N_rowStride1 + K * N_colStride1]
                * matrix2[K * N_rowStride2 + j * N_colStride2]
                + _bz_meta_matrixMatrixProduct<N_rows1 * go, N_columns * go,
                    N_columns2 * go, N_rowStride1 * go, N_colStride1 * go,
                    N_rowStride2 * go, N_colStride2 * go, (K+1) * go>
                  ::f(matrix1, matrix2, i, j);
        }
    };
    If it is a different algorithm, it does not look like it on the surface. There are more templates declared in blitz/meta/matmat.h, but I think this is the most important one. I do not see this as reinventing the wheel since I would have to modify his library quite a lot to get what I want done. Like any program or library, of course, I'm going to make bugs. I will have to exterminate them with extreme hostility.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Metaprogramming Nested Loops?

    Quote Originally Posted by binarybob0001
    Are you saying it would be better for me to extend the blitz++ library. It cannot handle some things that it should be able to handle. Last time I use it, it produced odd results with things like (I no longer have the sample program unfortunately, but I will recreate if necessary).
    I do not see this as reinventing the wheel since I would have to modify his library quite a lot to get what I want done.
    Honestly, I highly doubt a library as extensive as Blitz++ can't do matrix multiplication. Maybe you're doing something wrong?

    Having said this, have you filed a bug report to the Blitz++ programming group? Any recreated examples should have been presented to the authors of the Blitz++ library. There is little we can do with it here.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: Metaprogramming Nested Loops?

    OK, I will file a bug report. It's not the multiplication that is messed up. It is multiplying and assigning. Not with the *=, but with the example above (I hope I am remembering this right. If I play around, I'll stumble upon the same error again.). Any ways, I need a library that can take determinants and put matrices in RREF. If I modify blitz to do this, I would expect to incorperate it into the library for all to use, but I'm not that confident. That's the purpose for my library (Also, I still consider myself a newbie at C++ especially object oriented programming. I still have not taken any college classes in programming. I need a mentor : - () Anyways, is there a good method for unrolling a nested loop with templates. I can think of several ways, I just do not know which one is the best. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured